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 Gumbel distribution #120

Merged
merged 2 commits into from
Dec 6, 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 @@ -8,6 +8,7 @@
ChiSquared,
Exponential,
Gamma,
Gumbel,
HalfCauchy,
HalfNormal,
HalfStudent,
Expand Down
75 changes: 75 additions & 0 deletions preliz/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,81 @@ def _fit_mle(self, sample, **kwargs):
self._update(alpha, 1 / beta)


class Gumbel(Continuous):
r"""
Gumbel distribution.

The pdf of this distribution is

.. math::

f(x \mid \mu, \beta) = \frac{1}{\beta}e^{-(z + e^{-z})}

where

.. math::

z = \frac{x - \mu}{\beta}

.. plot::
:context: close-figs

import arviz as az
from preliz import Gumbel
az.style.use('arviz-white')
mus = [0., 4., -1.]
betas = [1., 2., 4.]
for mu, beta in zip(mus, betas):
Gumbel(mu, beta).plot_pdf(support=(-10,20))

======== ==========================================
Support :math:`x \in \mathbb{R}`
Mean :math:`\mu + \beta\gamma`, where :math:`\gamma` is the Euler-Mascheroni constant
Variance :math:`\frac{\pi^2}{6} \beta^2`
======== ==========================================

Parameters
----------
mu : float
Location parameter.
beta : float
Scale parameter (beta > 0).
"""

def __init__(self, mu=None, beta=None):
super().__init__()
self.mu = mu
self.beta = beta
self.name = "gumbel"
self.params = (self.mu, self.beta)
self.param_names = ("mu", "beta")
self.params_support = ((-np.inf, np.inf), (eps, np.inf))
self.dist = stats.gumbel_r
self.support = (-np.inf, np.inf)
self._update_rv_frozen()

def _get_frozen(self):
frozen = None
if any(self.params):
frozen = self.dist(loc=self.mu, scale=self.beta)
return frozen

def _update(self, mu, beta):
self.mu = mu
self.beta = beta
self.params = (self.mu, self.beta)
self._update_rv_frozen()

def _fit_moments(self, mean, sigma):
beta = sigma / np.pi * 6**0.5
mu = mean - beta * np.euler_gamma
self._update(mu, beta)

def _fit_mle(self, sample, **kwargs):
mu, beta = self.dist.fit(sample, **kwargs)
self._update(mu, beta)


class HalfCauchy(Continuous):
r"""
HalfCauchy 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 @@ -7,6 +7,7 @@
Cauchy,
ChiSquared,
Gamma,
Gumbel,
Exponential,
HalfCauchy,
HalfNormal,
Expand Down Expand Up @@ -38,6 +39,7 @@
(Beta, (2, 5)),
(ChiSquared, (1,)),
(Gamma, (1, 0.5)),
(Gumbel, (1, 2)),
(HalfNormal, (1,)),
(HalfStudent, (3, 1)),
(HalfStudent, (1000000, 1)),
Expand Down Expand Up @@ -88,6 +90,7 @@ def test_moments(distribution, params):
(Cauchy, (0, 1)),
(ChiSquared, (1,)),
(Gamma, (1, 0.5)),
(Gumbel, (0, 1)),
(HalfCauchy, (1,)),
(HalfNormal, (1,)),
(HalfStudent, (3, 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 @@ -18,6 +18,7 @@
ChiSquared,
Exponential,
Gamma,
Gumbel,
Laplace,
Logistic,
LogNormal,
Expand All @@ -40,6 +41,7 @@
(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)),
(Gumbel, "gumbel", 0, 10, 0.9, None, (-np.inf, np.inf), (3.557, 2.598)),
(HalfCauchy, "halfcauchy", 0, 10, 0.7, None, (0, np.inf), (5.095)),
(HalfNormal, "halfnormal", 0, 10, 0.7, None, (0, np.inf), (9.648)),
(HalfStudent, "halfstudent", 0, 10, 0.7, 3, (0, np.inf), (8.001)),
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 @@ -13,6 +13,7 @@
(pz.ChiSquared, (1,)),
(pz.Exponential, (5,)),
(pz.Gamma, (2, 5)),
(pz.Gumbel, (0, 2)),
(pz.HalfCauchy, (1,)),
(pz.HalfNormal, (1,)),
(pz.HalfStudent, (3, 1)),
Expand Down