2022-10-06 18:30:25 +11:00
|
|
|
# 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/>.
|
|
|
|
|
2022-10-13 12:53:52 +11:00
|
|
|
from pytest import approx
|
|
|
|
|
2022-10-06 18:30:25 +11:00
|
|
|
import numpy as np
|
2022-10-07 16:52:51 +11:00
|
|
|
from scipy import stats
|
2022-10-06 18:30:25 +11:00
|
|
|
|
|
|
|
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
|
2022-12-02 21:43:05 +11:00
|
|
|
expected = np.load('tests/data/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[0]
|
2022-10-13 12:53:52 +11:00
|
|
|
assert y == approx(expected)
|
2022-10-06 18:30:25 +11:00
|
|
|
|
|
|
|
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
|
2022-12-02 21:43:05 +11:00
|
|
|
expected = np.load('tests/data/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[1]
|
2022-10-13 12:53:52 +11:00
|
|
|
assert y == approx(expected)
|
2022-10-06 18:30:25 +11:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-12-02 21:43:05 +11:00
|
|
|
np.save('tests/data/beta_ratio_vs_jsaffer.npy', np.array([y1, y2]), allow_pickle=False)
|
2022-10-06 18:30:25 +11:00
|
|
|
|
|
|
|
def test_beta_ratio_mean_vs_empirical():
|
|
|
|
"""Compare beta_ratio.mean (via beta_ratio._munp) with empirical mean"""
|
|
|
|
|
|
|
|
# Define the example beta distribution
|
2022-10-07 16:52:51 +11:00
|
|
|
beta1 = stats.beta(3, 6)
|
|
|
|
beta2 = stats.beta(12, 7)
|
|
|
|
dist = yli.beta_ratio.from_scipy(beta1, beta2)
|
2022-10-06 18:30:25 +11:00
|
|
|
|
2022-10-07 16:52:51 +11:00
|
|
|
# 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
|
2022-10-06 18:30:25 +11:00
|
|
|
|
|
|
|
# Allow 0.01 tolerance
|
2022-10-13 12:53:52 +11:00
|
|
|
assert dist.mean() == approx(sample.mean(), abs=0.01)
|
2022-10-07 16:52:51 +11:00
|
|
|
|
|
|
|
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
|
2022-10-13 12:53:52 +11:00
|
|
|
assert dist.var() == approx(sample.var(), abs=0.01)
|