Skip to content
Open
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
2 changes: 1 addition & 1 deletion skpro/distributions/halfcauchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _get_scipy_object(self) -> rv_continuous:

def _get_scipy_param(self):
beta = self._bc_params["beta"]
return [beta], {}
return [], {"scale": beta}
Comment on lines 68 to +70

@classmethod
def get_test_params(cls, parameter_set="default"):
Expand Down
2 changes: 1 addition & 1 deletion skpro/distributions/halflogistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _get_scipy_object(self) -> rv_continuous:

def _get_scipy_param(self):
beta = self._bc_params["beta"]
return [beta], {}
return [], {"scale": beta}

@classmethod
def get_test_params(cls, parameter_set="default"):
Expand Down
2 changes: 1 addition & 1 deletion skpro/distributions/halfnormal.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _get_scipy_object(self) -> rv_continuous:

def _get_scipy_param(self):
sigma = self._bc_params["sigma"]
return [sigma], {}
return [], {"scale": sigma}

@classmethod
def get_test_params(cls, parameter_set="default"):
Expand Down
110 changes: 110 additions & 0 deletions skpro/distributions/tests/test_half_distributions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""Regression tests for half-distribution scipy parameter mappings."""
# copyright: skpro developers, BSD-3-Clause License (see LICENSE file)

import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_allclose
from scipy.stats import halflogistic, halfnorm

from skpro.distributions.halflogistic import HalfLogistic
from skpro.distributions.halfnormal import HalfNormal
from skpro.tests.test_switch import run_test_module_changed


@pytest.mark.skipif(
not run_test_module_changed("skpro.distributions"),
reason="run only if skpro.distributions has been changed",
)
def test_halfnormal_scalar_matches_scipy_scale():
"""HalfNormal scalar pdf/cdf/ppf should match scipy with scale=sigma."""
sigma = 2.5
dist = HalfNormal(sigma=sigma)

x = 1.7
p = 0.8

assert_allclose(dist.pdf(x), halfnorm.pdf(x, scale=sigma), rtol=1e-12)
assert_allclose(dist.cdf(x), halfnorm.cdf(x, scale=sigma), rtol=1e-12)
assert_allclose(dist.ppf(p), halfnorm.ppf(p, scale=sigma), rtol=1e-12)


@pytest.mark.skipif(
not run_test_module_changed("skpro.distributions"),
reason="run only if skpro.distributions has been changed",
)
def test_halflogistic_scalar_matches_scipy_scale():
"""HalfLogistic scalar pdf/cdf/ppf should match scipy with scale=beta."""
beta = 1.8
dist = HalfLogistic(beta=beta)

x = 1.3
p = 0.7

assert_allclose(dist.pdf(x), halflogistic.pdf(x, scale=beta), rtol=1e-12)
assert_allclose(dist.cdf(x), halflogistic.cdf(x, scale=beta), rtol=1e-12)
assert_allclose(dist.ppf(p), halflogistic.ppf(p, scale=beta), rtol=1e-12)


Comment on lines +19 to +48
@pytest.mark.skipif(
Comment on lines +19 to +49
not run_test_module_changed("skpro.distributions"),
reason="run only if skpro.distributions has been changed",
)
def test_halfnormal_broadcast_and_shape():
"""HalfNormal should preserve broadcasted shape/index/columns."""
sigma = [[1.0, 2.0], [3.0, 4.0]]
dist = HalfNormal(sigma=sigma)

x = pd.DataFrame([[0.5, 1.0], [2.0, 3.0]], index=dist.index, columns=dist.columns)
p = pd.DataFrame([[0.1, 0.2], [0.7, 0.9]], index=dist.index, columns=dist.columns)

pdf = dist.pdf(x)
cdf = dist.cdf(x)
ppf = dist.ppf(p)

assert pdf.shape == dist.shape
assert cdf.shape == dist.shape
assert ppf.shape == dist.shape
assert pdf.index.equals(dist.index)
assert cdf.index.equals(dist.index)
assert ppf.index.equals(dist.index)
assert pdf.columns.equals(dist.columns)
assert cdf.columns.equals(dist.columns)
assert ppf.columns.equals(dist.columns)

sigma_np = np.asarray(sigma)
assert_allclose(pdf.to_numpy(), halfnorm.pdf(x.to_numpy(), scale=sigma_np), rtol=1e-12)
assert_allclose(cdf.to_numpy(), halfnorm.cdf(x.to_numpy(), scale=sigma_np), rtol=1e-12)
assert_allclose(ppf.to_numpy(), halfnorm.ppf(p.to_numpy(), scale=sigma_np), rtol=1e-12)


@pytest.mark.skipif(
not run_test_module_changed("skpro.distributions"),
reason="run only if skpro.distributions has been changed",
)
def test_halflogistic_broadcast_and_shape():
"""HalfLogistic should preserve broadcasted shape/index/columns."""
beta = [[1.0, 2.0], [3.0, 4.0]]
dist = HalfLogistic(beta=beta)

x = pd.DataFrame([[0.5, 1.0], [2.0, 3.0]], index=dist.index, columns=dist.columns)
p = pd.DataFrame([[0.1, 0.2], [0.7, 0.9]], index=dist.index, columns=dist.columns)

pdf = dist.pdf(x)
cdf = dist.cdf(x)
ppf = dist.ppf(p)

assert pdf.shape == dist.shape
assert cdf.shape == dist.shape
assert ppf.shape == dist.shape
assert pdf.index.equals(dist.index)
assert cdf.index.equals(dist.index)
assert ppf.index.equals(dist.index)
assert pdf.columns.equals(dist.columns)
assert cdf.columns.equals(dist.columns)
assert ppf.columns.equals(dist.columns)

beta_np = np.asarray(beta)
assert_allclose(pdf.to_numpy(), halflogistic.pdf(x.to_numpy(), scale=beta_np), rtol=1e-12)
assert_allclose(cdf.to_numpy(), halflogistic.cdf(x.to_numpy(), scale=beta_np), rtol=1e-12)
assert_allclose(ppf.to_numpy(), halflogistic.ppf(p.to_numpy(), scale=beta_np), rtol=1e-12)
71 changes: 71 additions & 0 deletions skpro/distributions/tests/test_halfcauchy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Tests for HalfCauchy distribution."""
# copyright: skpro developers, BSD-3-Clause License (see LICENSE file)

import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_allclose
from scipy.stats import halfcauchy

from skpro.distributions.halfcauchy import HalfCauchy
from skpro.tests.test_switch import run_test_module_changed


@pytest.mark.skipif(
not run_test_module_changed("skpro.distributions"),
reason="run only if skpro.distributions has been changed",
)
def test_halfcauchy_scalar_matches_scipy():
"""HalfCauchy scalar pdf/cdf/ppf should match scipy implementation."""
beta = 2.5
dist = HalfCauchy(beta=beta)

x = 1.7
p = 0.8

assert_allclose(dist.pdf(x), halfcauchy.pdf(x, scale=beta), rtol=1e-12)
assert_allclose(dist.cdf(x), halfcauchy.cdf(x, scale=beta), rtol=1e-12)
assert_allclose(dist.ppf(p), halfcauchy.ppf(p, scale=beta), rtol=1e-12)

# Check support behavior (x < 0 outside support)
assert_allclose(dist.pdf(-1.0), 0.0, atol=1e-15)
assert_allclose(dist.cdf(-1.0), 0.0, atol=1e-15)


@pytest.mark.skipif(
not run_test_module_changed("skpro.distributions"),
reason="run only if skpro.distributions has been changed",
)
def test_halfcauchy_broadcast_and_shape():
"""HalfCauchy should broadcast and preserve shape/index/columns."""
beta = [[1.0, 2.0], [3.0, 4.0]]
dist = HalfCauchy(beta=beta)

x = pd.DataFrame([[0.5, 1.0], [2.0, 3.0]], index=dist.index, columns=dist.columns)
p = pd.DataFrame([[0.1, 0.2], [0.7, 0.9]], index=dist.index, columns=dist.columns)

pdf = dist.pdf(x)
cdf = dist.cdf(x)
ppf = dist.ppf(p)

assert isinstance(pdf, pd.DataFrame)
assert isinstance(cdf, pd.DataFrame)
assert isinstance(ppf, pd.DataFrame)

assert pdf.shape == dist.shape
assert cdf.shape == dist.shape
assert ppf.shape == dist.shape

assert pdf.index.equals(dist.index)
assert cdf.index.equals(dist.index)
assert ppf.index.equals(dist.index)

assert pdf.columns.equals(dist.columns)
assert cdf.columns.equals(dist.columns)
assert ppf.columns.equals(dist.columns)

beta_np = np.asarray(beta)

assert_allclose(pdf.to_numpy(), halfcauchy.pdf(x.to_numpy(), scale=beta_np), rtol=1e-12)
assert_allclose(cdf.to_numpy(), halfcauchy.cdf(x.to_numpy(), scale=beta_np), rtol=1e-12)
assert_allclose(ppf.to_numpy(), halfcauchy.ppf(p.to_numpy(), scale=beta_np), rtol=1e-12)
Loading