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

[ENH] Added Inverse Gamma distribution #415

Merged
merged 6 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions skpro/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
__all__ = [
"Alpha",
"Beta",
"Binomial",
"ChiSquared",
"Delta",
"Empirical",
Expand All @@ -14,6 +15,7 @@
"Gamma",
"HalfNormal",
"IID",
"InverseGamma",
"Laplace",
"Logistic",
"LogNormal",
Expand All @@ -32,6 +34,7 @@

from skpro.distributions.alpha import Alpha
from skpro.distributions.beta import Beta
from skpro.distributions.binomial import Binomial
from skpro.distributions.chi_squared import ChiSquared
from skpro.distributions.compose import IID
from skpro.distributions.delta import Delta
Expand All @@ -40,6 +43,7 @@
from skpro.distributions.fisk import Fisk
from skpro.distributions.gamma import Gamma
from skpro.distributions.halfnormal import HalfNormal
from skpro.distributions.inversegamma import InverseGamma
from skpro.distributions.laplace import Laplace
from skpro.distributions.logistic import Logistic
from skpro.distributions.lognormal import LogNormal
Expand Down
73 changes: 73 additions & 0 deletions skpro/distributions/binomial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# copyright: skpro developers, BSD-3-Clause License (see LICENSE file)
"""Binomial probability distribution."""

__author__ = ["meraldoantonio"]

import pandas as pd
from scipy.stats import binom, rv_discrete

from skpro.distributions.adapters.scipy import _ScipyAdapter


class Binomial(_ScipyAdapter):
r"""Binomial distribution.

Most methods wrap ``scipy.stats.binom``.
The Binomial distribution is parameterized by the number of trials :math:`n`
and the probability of success :math:`p`,
such that the probability mass function (PMF) is given by:

.. math:: P(X = k) = \binom{n}{k} p^k (1-p)^{n-k}

Parameters
----------
n : int or array of int (1D or 2D), must be non-negative
p : float or array of float (1D or 2D), must be in [0, 1]
index : pd.Index, optional, default = RangeIndex
columns : pd.Index, optional, default = RangeIndex

Example
-------
>>> from skpro.distributions.binomial import Binomial

>>> d = Binomial(n=[[10, 10], [20, 30], [40, 50]], p=0.5)
"""

_tags = {
"capabilities:approx": ["pmf"],
"capabilities:exact": ["mean", "var", "pmf", "log_pmf", "cdf", "ppf"],
"distr:measuretype": "discrete",
"distr:paramtype": "parametric",
"broadcast_init": "on",
}

def __init__(self, n, p, index=None, columns=None):
self.n = n
self.p = p

super().__init__(index=index, columns=columns)

def _get_scipy_object(self) -> rv_discrete:
return binom

def _get_scipy_param(self):
n = self._bc_params["n"]
p = self._bc_params["p"]

return [], {"n": n, "p": p}

@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator."""
# array case examples
params1 = {"n": [[10, 10], [20, 30], [40, 50]], "p": 0.5}
params2 = {
"n": 10,
"p": 0.5,
"index": pd.Index([1, 2, 5]),
"columns": pd.Index(["a", "b"]),
}
# scalar case examples
params3 = {"n": 15, "p": 0.7}

return [params1, params2, params3]
79 changes: 79 additions & 0 deletions skpro/distributions/inversegamma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# copyright: skpro developers, BSD-3-Clause License (see LICENSE file)
"""Inverse Gamma probability distribution."""

__author__ = ["meraldoantonio"]

import pandas as pd
from scipy.stats import invgamma, rv_continuous

from skpro.distributions.adapters.scipy import _ScipyAdapter


class InverseGamma(_ScipyAdapter):
r"""Inverse Gamma Distribution.

Most methods wrap ``scipy.stats.invgamma``.

The Inverse Gamma Distribution is parameterized by shape :math:`\alpha` and
scale :math:`\beta`, such that the pdf is

.. math:: f(x) = \frac{\beta^{\alpha} x^{-\alpha-1} \exp\left(-\frac{\beta}{x}\right)}{\tau(\alpha)}

where :math:`\tau(\alpha)` is the Gamma function.
For all positive integers, :math:`\tau(\alpha) = (\alpha-1)!`.

Parameters
----------
alpha : float or array of float (1D or 2D)
It represents the shape parameter.
beta : float or array of float (1D or 2D)
It represents the scale parameter.
index : pd.Index, optional, default = RangeIndex
columns : pd.Index, optional, default = RangeIndex

Example
-------
>>> from skpro.distributions.invgamma import InverseGamma

>>> d = InverseGamma(beta=[[1, 1], [2, 3], [4, 5]], alpha=2)
""" # noqa: E501

_tags = {
"capabilities:approx": ["energy", "pdfnorm"],
"capabilities:exact": ["mean", "var", "pdf", "log_pdf", "cdf", "ppf"],
"distr:measuretype": "continuous",
"distr:paramtype": "parametric",
"broadcast_init": "on",
}

def __init__(self, alpha, beta, index=None, columns=None):
self.alpha = alpha
self.beta = beta

super().__init__(index=index, columns=columns)

def _get_scipy_object(self) -> rv_continuous:
return invgamma

def _get_scipy_param(self):
alpha = self._bc_params["alpha"]
beta = self._bc_params["beta"]
scale = beta

return [], {"a": alpha, "scale": scale}

@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator."""
# array case examples
params1 = {"alpha": [6, 2.5], "beta": [[1, 1], [2, 3], [4, 5]]}
params2 = {
"alpha": 2,
"beta": 3,
"index": pd.Index([1, 2, 5]),
"columns": pd.Index(["a", "b"]),
}
# scalar case examples
params3 = {"alpha": 1.5, "beta": 2.1}

return [params1, params2, params3]