[docs]defhdi(x,ci=0.95,show=False,**kwargs):"""**Highest Density Interval (HDI)** Compute the Highest Density Interval (HDI) of a distribution. All points within this interval have a higher probability density than points outside the interval. The HDI can be used in the context of uncertainty characterisation of posterior distributions (in the Bayesian farmework) as Credible Interval (CI). Unlike equal-tailed intervals that typically exclude 2.5% from each tail of the distribution and always include the median, the HDI is not equal-tailed and therefore always includes the mode(s) of posterior distributions. Parameters ---------- x : Union[list, np.array, pd.Series] A vector of values. ci : float Value of probability of the (credible) interval - CI (between 0 and 1) to be estimated. Default to .95 (95%). show : bool If ``True``, the function will produce a figure. **kwargs : Line2D properties Other arguments to be passed to :func:`nk.density`. See Also -------- density Returns ---------- float(s) The HDI low and high limits. fig Distribution plot. Examples ---------- .. ipython:: python import numpy as np import neurokit2 as nk x = np.random.normal(loc=0, scale=1, size=100000) @savefig p_hdi1.png scale=100% ci_min, ci_high = nk.hdi(x, ci=0.95, show=True) @suppress plt.close() """x_sorted=np.sort(x)window_size=np.ceil(ci*len(x_sorted)).astype("int")ifwindow_size<2:raiseValueError("NeuroKit error: hdi(): `ci` is too small or x does not contain enough data points.")nCIs=len(x_sorted)-window_sizeciWidth=[0]*nCIsforiinnp.arange(0,nCIs):ciWidth[i]=x_sorted[i+window_size]-x_sorted[i]hdi_low=x_sorted[ciWidth.index(np.min(ciWidth))]hdi_high=x_sorted[ciWidth.index(np.min(ciWidth))+window_size]ifshowisTrue:_hdi_plot(x,hdi_low,hdi_high,**kwargs)returnhdi_low,hdi_high