Skip to content

Commit

Permalink
[ENH] Gamma Distribution (#355)
Browse files Browse the repository at this point in the history
Implements Gamma distribution. Towards #22
  • Loading branch information
ShreeshaM07 authored May 24, 2024
1 parent efa58eb commit 3d0d334
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/source/api_reference/distributions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Continuous support
ChiSquared
Exponential
Fisk
Gamma
Laplace
Logistic
Normal
Expand Down
2 changes: 2 additions & 0 deletions skpro/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"Empirical",
"Exponential",
"Fisk",
"Gamma",
"IID",
"Laplace",
"Logistic",
Expand All @@ -34,6 +35,7 @@
from skpro.distributions.empirical import Empirical
from skpro.distributions.exponential import Exponential
from skpro.distributions.fisk import Fisk
from skpro.distributions.gamma import Gamma
from skpro.distributions.laplace import Laplace
from skpro.distributions.logistic import Logistic
from skpro.distributions.lognormal import LogNormal
Expand Down
78 changes: 78 additions & 0 deletions skpro/distributions/gamma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# copyright: skpro developers, BSD-3-Clause License (see LICENSE file)
"""Exponential probability distribution."""

__author__ = ["ShreeshaM07"]

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

from skpro.distributions.adapters.scipy import _ScipyAdapter


class Gamma(_ScipyAdapter):
r"""Gamma Distribution.
The Gamma Distribution is parameterized by shape :math:`\alpha` and
rate :math:`\beta`, such that the pdf is
.. math:: f(x) = \frac{x^{\alpha-1}\exp\left(-\beta x\right) \beta^{\alpha}}{\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 rate parameter which is also
inverse of the scale parameter.
index : pd.Index, optional, default = RangeIndex
columns : pd.Index, optional, default = RangeIndex
Example
-------
>>> from skpro.distributions.gamma import Gamma
>>> d = Gamma(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 gamma

def _get_scipy_param(self):
alpha = self._bc_params["alpha"]
beta = self._bc_params["beta"]
scale = 1 / 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]

0 comments on commit 3d0d334

Please sign in to comment.