Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChiSquared distribution #101

Merged
merged 1 commit into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions preliz/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Beta,
BetaScaled,
Cauchy,
ChiSquared,
Exponential,
Gamma,
HalfCauchy,
Expand Down
65 changes: 65 additions & 0 deletions preliz/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,71 @@ def _fit_mle(self, sample, **kwargs):
self._update(alpha, beta)


class ChiSquared(Continuous):
r"""
Chi squared log-likelihood.

The pdf of this distribution is

.. math::

f(x \mid \nu) =
\frac{x^{(\nu-2)/2}e^{-x/2}}{2^{\nu/2}\Gamma(\nu/2)}

.. plot::
:context: close-figs

import arviz as az
from preliz import ChiSquared
az.style.use('arviz-white')
nus = [1., 3., 9.]
for nu in nus:
ax = ChiSquared(nu).plot_pdf(support=(0,20))
ax.set_ylim(0, 0.6)

======== ===============================
Support :math:`x \in [0, \infty)`
Mean :math:`\nu`
Variance :math:`2 \nu`
======== ===============================

Parameters
----------
nu : float
Degrees of freedom (nu > 0).
"""

def __init__(self, nu=None):
super().__init__()
self.nu = nu
self.name = "chisquared"
self.params = (self.nu,)
self.param_names = ("nu",)
self.params_support = ((eps, np.inf),)
self.dist = stats.chi2
self.support = (0, np.inf)
self._update_rv_frozen()

def _get_frozen(self):
frozen = None
if any(self.params):
frozen = self.dist(self.nu)
return frozen

def _update(self, nu):
self.nu = nu
self.params = (self.nu,)
self._update_rv_frozen()

def _fit_moments(self, mean, sigma=None): # pylint: disable=unused-argument
nu = mean
self._update(nu)

def _fit_mle(self, sample, **kwargs):
nu, _, _ = self.dist.fit(sample, **kwargs)
self._update(nu)


class Exponential(Continuous):
r"""
Exponential Distribution
Expand Down
3 changes: 3 additions & 0 deletions preliz/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from preliz.distributions import (
Beta,
Cauchy,
ChiSquared,
Gamma,
Exponential,
HalfCauchy,
Expand Down Expand Up @@ -33,6 +34,7 @@
[
(Normal, (0, 1)),
(Beta, (2, 5)),
(ChiSquared, (1,)),
(Gamma, (1, 0.5)),
(HalfNormal, (1,)),
(HalfStudent, (3, 1)),
Expand Down Expand Up @@ -80,6 +82,7 @@ def test_moments(distribution, params):
(Normal, (0, 1)),
(Beta, (2, 5)),
(Cauchy, (0, 1)),
(ChiSquared, (1,)),
(Gamma, (1, 0.5)),
(HalfCauchy, (1,)),
(HalfNormal, (1,)),
Expand Down
2 changes: 2 additions & 0 deletions preliz/tests/test_maxent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from preliz.distributions import (
Beta,
Cauchy,
ChiSquared,
Exponential,
Gamma,
Laplace,
Expand All @@ -34,6 +35,7 @@
[
(Beta, "beta", 0.2, 0.6, 0.9, None, (0, 1), (6.112, 9.101)),
(Cauchy, "cauchy", -1, 1, 0.6, None, (-np.inf, np.inf), (0, 0.726)),
(ChiSquared, "chisquared", 0, 4, 0.9, 1, (0, np.inf), (1.659)),
(Exponential, "exponential", 0, 4, 0.9, None, (0, np.inf), (0.575)),
(Gamma, "gamma", 0, 10, 0.7, None, (0, np.inf), (0.868, 0.103)),
(HalfCauchy, "halfcauchy", 0, 10, 0.7, None, (0, np.inf), (5.095)),
Expand Down
1 change: 1 addition & 0 deletions preliz/tests/test_mle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
(pz.Beta, (2, 5)),
(pz.BetaScaled, (2, 5, -1, 4)),
(pz.Cauchy, (0, 1)),
(pz.ChiSquared, (1,)),
(pz.Exponential, (5,)),
(pz.Gamma, (2, 5)),
(pz.HalfCauchy, (1,)),
Expand Down