scipy-yli/tests/test_beta_ratio.py

102 lines
3.4 KiB
Python

# scipy-yli: Helpful SciPy utilities and recipes
# Copyright © 2022 Lee Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import numpy as np
from scipy import stats
import yli
def test_beta_ratio_vs_jsaffer_pdf():
"""Compare beta_ratio.pdf with result from https://github.com/jsaffer/beta_quotient_distribution"""
# Define the example beta distribution
a1, b1, a2, b2 = 3, 6, 12, 7
# Compute PDF
x = np.linspace(0, 2, 100)
dist = yli.beta_ratio(a1, b1, a2, b2)
y = dist.pdf(x)
# Compare with expected values from jsaffer implementation
expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[0]
# Allow 1e-10 tolerance
diff = np.abs(y - expected)
assert (diff < 1e-10).all()
def test_beta_ratio_vs_jsaffer_cdf():
"""Compare beta_ratio.cdf with result from https://github.com/jsaffer/beta_quotient_distribution"""
# Define the example beta distribution
a1, b1, a2, b2 = 3, 6, 12, 7
# Compute PDF
x = np.linspace(0, 2, 100)
dist = yli.beta_ratio(a1, b1, a2, b2)
y = dist.cdf(x)
# Compare with expected values from jsaffer implementation
expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[1]
# Allow 1e-10 tolerance
diff = np.abs(y - expected)
assert (diff < 1e-10).all()
def _gen_beta_ratio_vs_jsaffer():
"""Generate beta_ratio_vs_jsaffer.npy for test_beta_ratio_vs_jsaffer_pdf/cdf"""
import beta_quotient_distribution
a1, b1, a2, b2 = 3, 6, 12, 7
x = np.linspace(0, 2, 100)
y1 = np.vectorize(lambda w: float(beta_quotient_distribution.pdf_bb_ratio(a1, a2, b1, b2, w)))(x)
y2 = np.vectorize(lambda w: float(beta_quotient_distribution.cdf_bb_ratio(a1, a2, b1, b2, w)))(x)
np.save('tests/beta_ratio_vs_jsaffer.npy', np.array([y1, y2]), allow_pickle=False)
def test_beta_ratio_mean_vs_empirical():
"""Compare beta_ratio.mean (via beta_ratio._munp) with empirical mean"""
# Define the example beta distribution
beta1 = stats.beta(3, 6)
beta2 = stats.beta(12, 7)
dist = yli.beta_ratio.from_scipy(beta1, beta2)
# Compute empirical distribution
samples_p1 = beta1.rvs(10_000, random_state=31415)
samples_p2 = beta2.rvs(10_000, random_state=92653)
sample = samples_p1 / samples_p2
# Allow 0.01 tolerance
assert np.abs(dist.mean() - sample.mean()) < 0.01
def test_beta_ratio_var_vs_empirical():
"""Compare beta_ratio.var (via beta_ratio._munp) with empirical variance"""
# Define the example beta distribution
beta1 = stats.beta(3, 6)
beta2 = stats.beta(12, 7)
dist = yli.beta_ratio.from_scipy(beta1, beta2)
# Compute empirical distribution
samples_p1 = beta1.rvs(10_000, random_state=31415)
samples_p2 = beta2.rvs(10_000, random_state=92653)
sample = samples_p1 / samples_p2
# Allow 0.01 tolerance
assert np.abs(dist.var() - sample.var()) < 0.01