import pytest
import numpy as np
import scirs2
class TestBetaDistribution:
def test_beta_creation(self):
dist = scirs2.beta(2.0, 3.0)
assert dist is not None
dist_custom = scirs2.beta(alpha=2.0, beta=5.0, loc=1.0, scale=2.0)
assert dist_custom is not None
def test_beta_pdf(self):
dist = scirs2.beta(2.0, 3.0)
assert abs(dist.pdf(0.0)) < 1e-10
assert abs(dist.pdf(1.0)) < 1e-10
assert dist.pdf(0.5) > 0
def test_beta_cdf(self):
dist = scirs2.beta(2.0, 2.0)
assert abs(dist.cdf(0.0)) < 1e-10
assert abs(dist.cdf(1.0) - 1.0) < 1e-10
assert abs(dist.cdf(0.5) - 0.5) < 1e-3
def test_beta_ppf(self):
dist = scirs2.beta(2.0, 2.0)
assert abs(dist.ppf(0.0)) < 1e-3
assert abs(dist.ppf(1.0) - 1.0) < 1e-3
median = dist.ppf(0.5)
assert abs(median - 0.5) < 1e-3
def test_beta_rvs(self):
dist = scirs2.beta(2.0, 3.0)
samples = dist.rvs(1000)
assert len(samples) == 1000
assert all(0 <= s <= 1 for s in samples)
mean = np.mean(samples)
assert abs(mean - 0.4) < 0.1
class TestGammaDistribution:
def test_gamma_creation(self):
dist = scirs2.gamma(2.0)
assert dist is not None
dist_custom = scirs2.gamma(shape=2.0, scale=2.0, loc=1.0)
assert dist_custom is not None
def test_gamma_pdf(self):
dist = scirs2.gamma(2.0, scale=1.0)
assert abs(dist.pdf(0.0)) < 1e-10
assert dist.pdf(1.0) > 0
assert dist.pdf(2.0) > 0
def test_gamma_cdf(self):
dist = scirs2.gamma(2.0)
assert abs(dist.cdf(0.0)) < 1e-10
assert dist.cdf(0.0) < dist.cdf(1.0) < dist.cdf(2.0)
def test_gamma_ppf(self):
dist = scirs2.gamma(2.0)
assert abs(dist.ppf(0.0)) < 1e-3
assert dist.ppf(0.1) < dist.ppf(0.5) < dist.ppf(0.9)
def test_gamma_rvs(self):
dist = scirs2.gamma(2.0, scale=1.0)
samples = dist.rvs(1000)
assert len(samples) == 1000
assert all(s >= 0 for s in samples)
mean = np.mean(samples)
assert abs(mean - 2.0) < 0.3
class TestChiSquareDistribution:
def test_chi2_creation(self):
dist = scirs2.chi2(2.0)
assert dist is not None
dist_custom = scirs2.chi2(df=5.0, loc=1.0, scale=2.0)
assert dist_custom is not None
def test_chi2_pdf(self):
dist = scirs2.chi2(2.0)
pdf_at_zero = dist.pdf(0.0)
assert pdf_at_zero >= 0
assert dist.pdf(1.0) > 0
assert dist.pdf(2.0) > 0
@pytest.mark.skip(reason="Chi-square CDF has bugs that need to be fixed")
def test_chi2_cdf(self):
dist = scirs2.chi2(2.0)
assert abs(dist.cdf(0.0)) < 1e-10
assert dist.cdf(0.0) < dist.cdf(1.0) < dist.cdf(2.0)
def test_chi2_ppf(self):
dist = scirs2.chi2(2.0)
assert abs(dist.ppf(0.0)) < 1e-3
assert dist.ppf(0.1) < dist.ppf(0.5) < dist.ppf(0.9)
def test_chi2_rvs(self):
dist = scirs2.chi2(2.0)
samples = dist.rvs(1000)
assert len(samples) == 1000
assert all(s >= 0 for s in samples)
mean = np.mean(samples)
assert abs(mean - 2.0) < 0.3
class TestStudentTDistribution:
def test_t_creation(self):
dist = scirs2.t(5.0)
assert dist is not None
dist_custom = scirs2.t(df=10.0, loc=1.0, scale=2.0)
assert dist_custom is not None
def test_t_pdf(self):
dist = scirs2.t(5.0)
assert abs(dist.pdf(1.0) - dist.pdf(-1.0)) < 1e-6
pdf_at_zero = dist.pdf(0.0)
assert pdf_at_zero > dist.pdf(1.0)
assert pdf_at_zero > dist.pdf(-1.0)
def test_t_cdf(self):
dist = scirs2.t(5.0)
cdf_at_zero = dist.cdf(0.0)
assert abs(cdf_at_zero - 0.5) < 1e-3
assert dist.cdf(-2.0) < dist.cdf(0.0) < dist.cdf(2.0)
def test_t_ppf(self):
dist = scirs2.t(5.0)
median = dist.ppf(0.5)
assert abs(median) < 1e-3
q1 = dist.ppf(0.25)
q3 = dist.ppf(0.75)
assert abs(q1 + q3) < 0.1
def test_t_rvs(self):
dist = scirs2.t(5.0)
samples = dist.rvs(1000)
assert len(samples) == 1000
mean = np.mean(samples)
assert abs(mean) < 0.2
class TestCauchyDistribution:
def test_cauchy_creation(self):
dist = scirs2.cauchy()
assert dist is not None
dist_custom = scirs2.cauchy(loc=1.0, scale=2.0)
assert dist_custom is not None
def test_cauchy_pdf(self):
dist = scirs2.cauchy()
assert abs(dist.pdf(1.0) - dist.pdf(-1.0)) < 1e-6
pdf_at_zero = dist.pdf(0.0)
assert abs(pdf_at_zero - 1.0/np.pi) < 1e-3
def test_cauchy_cdf(self):
dist = scirs2.cauchy()
cdf_at_zero = dist.cdf(0.0)
assert abs(cdf_at_zero - 0.5) < 1e-6
assert dist.cdf(-2.0) < dist.cdf(0.0) < dist.cdf(2.0)
def test_cauchy_ppf(self):
dist = scirs2.cauchy()
median = dist.ppf(0.5)
assert abs(median) < 1e-6
assert dist.ppf(0.1) < dist.ppf(0.5) < dist.ppf(0.9)
def test_cauchy_rvs(self):
dist = scirs2.cauchy()
samples = dist.rvs(1000)
assert len(samples) == 1000
assert len(samples) == 1000
class TestFDistribution:
def test_f_creation(self):
dist = scirs2.f(5.0, 10.0)
assert dist is not None
dist_custom = scirs2.f(dfn=2.0, dfd=10.0, loc=1.0, scale=2.0)
assert dist_custom is not None
def test_f_pdf(self):
dist = scirs2.f(5.0, 10.0)
assert abs(dist.pdf(0.0)) < 1e-10
assert dist.pdf(1.0) > 0
assert dist.pdf(2.0) > 0
def test_f_cdf(self):
dist = scirs2.f(5.0, 10.0)
assert abs(dist.cdf(0.0)) < 1e-10
assert dist.cdf(0.5) < dist.cdf(1.0) < dist.cdf(2.0)
def test_f_rvs(self):
dist = scirs2.f(5.0, 10.0)
samples = dist.rvs(1000)
assert len(samples) == 1000
assert all(s >= 0 for s in samples)
class TestDistributionConsistency:
@pytest.mark.skip(reason="Beta PPF has numerical precision issues")
def test_beta_cdf_ppf_inverse(self):
dist = scirs2.beta(2.0, 3.0)
x = 0.4
cdf_val = dist.cdf(x)
ppf_val = dist.ppf(cdf_val)
assert abs(ppf_val - x) < 0.1
@pytest.mark.skip(reason="Gamma PPF has numerical precision issues")
def test_gamma_cdf_ppf_inverse(self):
dist = scirs2.gamma(2.0)
x = 1.5
cdf_val = dist.cdf(x)
ppf_val = dist.ppf(cdf_val)
assert abs(ppf_val - x) < 0.2
@pytest.mark.skip(reason="Chi-square CDF has bugs that need to be fixed")
def test_chi2_cdf_ppf_inverse(self):
dist = scirs2.chi2(5.0)
x = 3.0
cdf_val = dist.cdf(x)
ppf_val = dist.ppf(cdf_val)
assert abs(ppf_val - x) < 0.5
@pytest.mark.skip(reason="Student's t PPF has numerical precision issues")
def test_t_cdf_ppf_inverse(self):
dist = scirs2.t(5.0)
x = 1.0
cdf_val = dist.cdf(x)
ppf_val = dist.ppf(cdf_val)
assert abs(ppf_val - x) < 0.3
def test_cauchy_cdf_ppf_inverse(self):
dist = scirs2.cauchy()
x = 1.0
cdf_val = dist.cdf(x)
ppf_val = dist.ppf(cdf_val)
assert abs(ppf_val - x) < 1e-3
def test_pdf_cdf_relationship_beta(self):
dist = scirs2.beta(2.0, 3.0)
x_vals = [0.1, 0.3, 0.5, 0.7, 0.8]
cdf_vals = [dist.cdf(x) for x in x_vals]
for i in range(len(cdf_vals) - 1):
assert cdf_vals[i] <= cdf_vals[i+1] + 1e-10
def test_pdf_cdf_relationship_gamma(self):
dist = scirs2.gamma(2.0)
x_vals = [0.5, 1.0, 2.0, 3.0, 4.0]
cdf_vals = [dist.cdf(x) for x in x_vals]
for i in range(len(cdf_vals) - 1):
assert cdf_vals[i] < cdf_vals[i+1]