def plotMAP(x, ax=None, error=0.01, frac=[0.65,0.95, 0.975], usehpd=True,
hist={'histtype':'step'}, vlines={}, fill={},
optbins={'method':'freedman'}, *args, **kwargs):
""" Plot the MAP of a given sample and add statistical info
If not specified, binning is assumed from the error value or using
mystats.optbins if available.
if mystats module is not available, hpd keyword has no effect
inputs:
x dataset
keywords
ax axe object to use during plotting
error error to consider on the estimations
frac fractions of sample to highlight (def 65%, 95%, 97.5%)
hpd if set, uses mystats.hpd to estimate the confidence intervals
hist keywords forwarded to hist command
optbins keywords forwarded to mystats.optbins command
vlines keywords forwarded to vlines command
fill keywords forwarded to fill command
"""
_x = np.ravel(x)
if ax is None:
ax = plt.gca()
if not ('bins' in hist):
bins = get_optbins(x, method=optbins['method'], ret='N')
n, b, p = ax.hist(_x, bins=bins, *args, **hist)
else:
n, b, p = ax.hist(_x, *args, **hist)
c = 0.5 * (b[:-1] + b[1:])
# dc = 0.5 * (b[:-1] - b[1:])
ind = n.argmax()
_ylim = ax.get_ylim()
if usehpd is True:
_hpd = hpd(_x, 1 - 0.01)
ax.vlines(_hpd, _ylim[0], _ylim[1], **vlines)
for k in frac:
nx = hpd(_x, 1. - k)
ax.fill_between(nx, _ylim[0], _ylim[1], alpha=0.4 / float(len(frac)), zorder=-1, **fill)
else:
ax.vlines(c[ind], _ylim[0], _ylim[1], **vlines)
cx = c[ n.argsort() ][::-1]
cn = n[ n.argsort() ][::-1].cumsum()
for k in frac:
sx = cx[np.where(cn <= cn[-1] * float(k))]
sx = [sx.min(), sx.max()]
ax.fill_between(sx, _ylim[0], _ylim[1], alpha=0.4 / float(len(frac)), zorder=-1, **fill)
theme(ax=ax)
ax.set_xlabel(r'Values')
ax.set_ylabel(r'Counts')
评论列表
文章目录