From 12a07079986d2a4f7a0c2c70c0aaa905231dfdd0 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Tue, 18 Oct 2022 17:57:19 +1100 Subject: [PATCH] Move ConfidenceInterval to yli.utils --- tests/test_hdi.py | 2 +- yli/distributions.py | 5 +---- yli/utils.py | 12 ++++++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/test_hdi.py b/tests/test_hdi.py index 4d2d841..cf4aa05 100644 --- a/tests/test_hdi.py +++ b/tests/test_hdi.py @@ -39,4 +39,4 @@ def test_hdi_beta_vs_r(): #1 bayes 12 250 13 239 0.0515873 0.02594348 0.0793006 0.05 expected = np.array([0.02594348, 0.0793006]) - assert hdi == approx(expected) + assert (hdi.lower, hdi.upper) == approx(expected) diff --git a/yli/distributions.py b/yli/distributions.py index 981aaa7..b33dd20 100644 --- a/yli/distributions.py +++ b/yli/distributions.py @@ -17,9 +17,8 @@ import numpy as np from scipy import integrate, optimize, stats -import collections - from .config import config +from .utils import ConfidenceInterval class betarat_gen(stats.rv_continuous): """ @@ -294,8 +293,6 @@ class transformed_gen(stats.rv_continuous): transformed_dist = transformed_gen(name='transformed') -ConfidenceInterval = collections.namedtuple('ConfidenceInterval', ['lower', 'upper']) - def hdi(distribution, level=None): """ Get the highest density interval for the distribution, e.g. for a Bayesian posterior, the highest posterior density interval (HPD/HDI) diff --git a/yli/utils.py b/yli/utils.py index aff3356..d8a1043 100644 --- a/yli/utils.py +++ b/yli/utils.py @@ -139,6 +139,18 @@ def fmt_p(p, *, html, tabular=False): # ------------------------------ # General result-related classes +class ConfidenceInterval: + """A confidence interval""" + + def __init__(self, lower, upper): + self.lower = lower + self.upper = upper + + def _repr_html_(self): + return self.summary() + + def summary(self): + return '{:.2f}–{:.2f}'.format(self.lower, self.upper) class Estimate: """A point estimate and surrounding confidence interval"""