From 58da8e7e9a2ab46008584dd3d1bf54949b3a3715 Mon Sep 17 00:00:00 2001 From: Sai Revanth <132150745+SaiRevanth25@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:47:06 +0530 Subject: [PATCH] [ENH] Half Cauchy Distribution (#371) Implements Half Cauchy Distribution, towards #22 --- docs/source/api_reference/distributions.rst | 1 + skpro/distributions/__init__.py | 2 + skpro/distributions/halfcauchy.py | 79 +++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 skpro/distributions/halfcauchy.py diff --git a/docs/source/api_reference/distributions.rst b/docs/source/api_reference/distributions.rst index 62548000b..bffcda485 100644 --- a/docs/source/api_reference/distributions.rst +++ b/docs/source/api_reference/distributions.rst @@ -41,6 +41,7 @@ Continuous support Exponential Fisk Gamma + HalfCauchy HalfNormal Laplace Logistic diff --git a/skpro/distributions/__init__.py b/skpro/distributions/__init__.py index 2f5e321ed..2f5a317b9 100644 --- a/skpro/distributions/__init__.py +++ b/skpro/distributions/__init__.py @@ -12,6 +12,7 @@ "Exponential", "Fisk", "Gamma", + "HalfCauchy", "HalfNormal", "IID", "Laplace", @@ -39,6 +40,7 @@ from skpro.distributions.exponential import Exponential from skpro.distributions.fisk import Fisk from skpro.distributions.gamma import Gamma +from skpro.distributions.halfcauchy import HalfCauchy from skpro.distributions.halfnormal import HalfNormal from skpro.distributions.laplace import Laplace from skpro.distributions.logistic import Logistic diff --git a/skpro/distributions/halfcauchy.py b/skpro/distributions/halfcauchy.py new file mode 100644 index 000000000..bcac0e927 --- /dev/null +++ b/skpro/distributions/halfcauchy.py @@ -0,0 +1,79 @@ +# copyright: skpro developers, BSD-3-Clause License (see LICENSE file) +"""Half-Cauchy probability distribution.""" + +__author__ = ["SaiRevanth25"] + +import pandas as pd +from scipy.stats import halfcauchy, rv_continuous + +from skpro.distributions.adapters.scipy import _ScipyAdapter + + +class HalfCauchy(_ScipyAdapter): + r"""Half-Cauchy distribution. + + This distribution is univariate, without correlation between dimensions + for the array-valued case. + + The half-Cauchy distribution is a continuous probability distribution that + is the positive half of the Cauchy distribution. It is commonly used in + Bayesian statistics, especially as a prior distribution for scale parameters + due to its heavy tails and non-negativity. + + The half-Cauchy distribution is parametrized by the scale parameter + :math:`\beta`, such that the pdf is + + .. math:: + + f(x) = \frac{2}{\pi \beta \left(1 + \left(\frac{x}{\beta}\right)^2\right)}, + x>0 otherwise 0 + + The scale parameter :math:`\beta` is represented by the parameter ``beta``. + + Parameters + ---------- + beta : float or array of float (1D or 2D), must be positive + scale parameter of the half-Cauchy distribution + index : pd.Index, optional, default = RangeIndex + columns : pd.Index, optional, default = RangeIndex + + Example + ------- + >>> from skpro.distributions.halfcauchy import HalfCauchy + + >>> hc = HalfCauchy(beta=1) + """ + + _tags = { + "capabilities:approx": ["pdfnorm"], + "capabilities:exact": ["mean", "var", "pdf", "log_pdf", "cdf", "ppf"], + "distr:measuretype": "continuous", + "distr:paramtype": "parametric", + "broadcast_init": "on", + } + + def __init__(self, beta, index=None, columns=None): + self.beta = beta + + super().__init__(index=index, columns=columns) + + def _get_scipy_object(self) -> rv_continuous: + return halfcauchy + + def _get_scipy_param(self): + beta = self._bc_params["beta"] + return [beta], {} + + @classmethod + def get_test_params(cls, parameter_set="default"): + """Return testing parameter settings for the estimator.""" + # array case examples + params1 = {"beta": [[1, 2], [3, 4]]} + params2 = { + "beta": 1, + "index": pd.Index([1, 2, 5]), + "columns": pd.Index(["a", "b"]), + } + # scalar case examples + params3 = {"beta": 2} + return [params1, params2, params3]