Created
March 16, 2015 18:15
-
-
Save mtrbean/5655434f6f2720a2a01e to your computer and use it in GitHub Desktop.
Highest Density Interval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hdi(frozen_dist, alpha=0.05, **args): | |
""" Highest density interval for a unimodel distribution """ | |
# freeze distribution with given arguments | |
def interval_width(hdi_pt_l): | |
return frozen_dist.ppf(alpha + hdi_pt_l) - frozen_dist.ppf(hdi_pt_l) | |
# find hdi_pt_l that minimizes interval_width | |
hdi_pt_l = fmin(interval_width, 1-alpha, ftol=1e-8, disp=False)[0] | |
# return interval as array([low, high]) | |
return frozen_dist.ppf([hdi_pt_l, alpha + hdi_pt_l]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment