Function Repository Resource:

HighestDensityInterval (1.0.0) current version: 1.1.0 »

Source Notebook

Compute the shortest interval that contains a give amount of probability mass

Contributed by: Sjoerd Smit

ResourceFunction["HighestDensityInterval"][dist, p]

returns the shortest Interval for distribution dist that contains probability mass p.

ResourceFunction["HighestDensityInterval"][vec, p]

returns the shortest Interval that contains at least fraction p of the values in numerical vector vec.

Details

ResourceFunction["HighestDensityInterval"] only works for univariate distributions that can be numerically sampled.
The probability p has to be a number between 0 and 1.

Examples

Basic Examples (5) 

Find the shortest interval that contains 90% of the probability mass of a GammaDistribution:

In[1]:=
iv = ResourceFunction["HighestDensityInterval"][
  GammaDistribution[4, 2], 0.9]
Out[1]=

Verify that the interval contains 90% of the probability mass:

In[2]:=
Probability[Between[x, First[iv]], x \[Distributed] GammaDistribution[4, 2]]
Out[2]=

Check the length of the interval:

In[3]:=
RegionMeasure[iv]
Out[3]=

Show the probability contained in the interval:

In[4]:=
Show[
 Plot[
  PDF[GammaDistribution[4, 2], x], {x, 0, 20}
  ],
 Plot[
  PDF[GammaDistribution[4, 2], x], {x, iv[[1, 1]], iv[[1, 2]]},
  Filling -> Axis,
  PlotRange -> {0, Automatic}
  ]
 ]
Out[4]=

Compute the interval using samples from the distribution:

In[5]:=
ResourceFunction["HighestDensityInterval"][
 RandomVariate[GammaDistribution[4, 2], 10^6], 0.9]
Out[5]=

Applications (3) 

The HighestDensityInterval can be used to detect and remove outliers:

In[6]:=
data = {8, 0, 7, 10, 3, 8, 4, 1000, 7, 6, 10, 5};
iv = ResourceFunction["HighestDensityInterval"][data, 0.95]
Out[7]=

Remove the outliers:

In[8]:=
dataCleaned = Select[data, Between @@ iv]
Out[8]=

Compute the mean of the data without the outlier:

In[9]:=
Mean[dataCleaned] // N
Out[9]=

Publisher

Sjoerd Smit

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.1.0 – 23 September 2024
  • 1.0.0 – 20 September 2024

Related Resources

License Information