From 9de9f9c6e3028d55ef305614566777324f27bb59 Mon Sep 17 00:00:00 2001 From: Malik Akbar Hashemi Rafsanjani <77711133+malikrafsan@users.noreply.github.com> Date: Tue, 7 May 2024 15:52:27 +0700 Subject: [PATCH] [ENH] Beta Distribution (#298) Towards #22 This PR implements a Beta distribution based on the Scipy Adapter --- docs/source/api_reference/distributions.rst | 1 + skpro/distributions/__init__.py | 2 + skpro/distributions/beta.py | 73 +++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 skpro/distributions/beta.py diff --git a/docs/source/api_reference/distributions.rst b/docs/source/api_reference/distributions.rst index 382cdde5f..45fcfa5fe 100644 --- a/docs/source/api_reference/distributions.rst +++ b/docs/source/api_reference/distributions.rst @@ -35,6 +35,7 @@ Continuous support :toctree: auto_generated/ :template: class.rst + Beta ChiSquared Fisk Laplace diff --git a/skpro/distributions/__init__.py b/skpro/distributions/__init__.py index cb17a9198..338a80195 100644 --- a/skpro/distributions/__init__.py +++ b/skpro/distributions/__init__.py @@ -3,6 +3,7 @@ # adapted from sktime __all__ = [ + "Beta", "ChiSquared", "Delta", "Empirical", @@ -22,6 +23,7 @@ "Weibull", ] +from skpro.distributions.beta import Beta from skpro.distributions.chi_squared import ChiSquared from skpro.distributions.delta import Delta from skpro.distributions.empirical import Empirical diff --git a/skpro/distributions/beta.py b/skpro/distributions/beta.py new file mode 100644 index 000000000..284b0b1c8 --- /dev/null +++ b/skpro/distributions/beta.py @@ -0,0 +1,73 @@ +# copyright: skpro developers, BSD-3-Clause License (see LICENSE file) +"""Beta probability distribution.""" + +__author__ = ["malikrafsan"] + +import pandas as pd +from scipy.stats import beta, rv_continuous + +from skpro.distributions.adapters.scipy import _ScipyAdapter + + +class Beta(_ScipyAdapter): + r"""Beta distribution. + + The Beta distribution is parametrized by two shape parameters :math:`\alpha` + and :math:`\beta`, such that the probability density function (PDF) is given by: + + .. math:: f(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)} + + where :math:`B(\alpha, \beta)` is the beta function. The beta function + is a normalization constant to ensure that the total probability is 1. + + Parameters + ---------- + alpha : float or array of float (1D or 2D), must be positive + beta : float or array of float (1D or 2D), must be positive + index : pd.Index, optional, default = RangeIndex + columns : pd.Index, optional, default = RangeIndex + + Example + ------- + >>> from skpro.distributions.beta import Beta + + >>> d = Beta(beta=[[1, 1], [2, 3], [4, 5]], alpha=2) + """ + + _tags = { + "capabilities:approx": ["energy", "pdfnorm"], + "capabilities:exact": ["mean", "var", "pdf", "log_pdf", "cdf", "ppf"], + "distr:measuretype": "continuous", + "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 beta + + def _get_scipy_param(self): + alpha = self._bc_params["alpha"] + beta = self._bc_params["beta"] + + return [], {"a": alpha, "b": beta} + + @classmethod + def get_test_params(cls, parameter_set="default"): + """Return testing parameter settings for the estimator.""" + # array case examples + params1 = {"alpha": [[1, 1], [2, 3], [4, 5]], "beta": 3} + 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]