95 lines
3.1 KiB
Python
95 lines
3.1 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_cdf_vs_empirical():
|
||
|
"""Compare beta_ratio.cdf with empirical distribution"""
|
||
|
|
||
|
# Define the example beta distribution
|
||
|
beta1 = stats.beta(3, 6)
|
||
|
beta2 = stats.beta(12, 7)
|
||
|
dist = yli.beta_oddsratio.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 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2))
|
||
|
|
||
|
# Values to check
|
||
|
x = np.linspace(0, 2, 10)
|
||
|
y1 = dist.cdf(x)
|
||
|
y2 = [(sample < xx).sum()/sample.size for xx in x]
|
||
|
|
||
|
# Allow 0.01 tolerance
|
||
|
assert (np.abs(y1 - y2) < 0.01).all()
|
||
|
|
||
|
def test_beta_ratio_ppf_vs_empirical():
|
||
|
"""Compare beta_ratio.ppf with empirical distribution"""
|
||
|
|
||
|
# Define the example beta distribution
|
||
|
beta1 = stats.beta(3, 6)
|
||
|
beta2 = stats.beta(12, 7)
|
||
|
dist = yli.beta_oddsratio.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 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2))
|
||
|
|
||
|
# Values to check
|
||
|
x = np.linspace(0, 0.9, 10)
|
||
|
y1 = dist.ppf(x)
|
||
|
y2 = np.quantile(sample, x)
|
||
|
|
||
|
# Allow 0.01 tolerance
|
||
|
assert (np.abs(y1 - y2) < 0.01).all()
|
||
|
|
||
|
def test_beta_ratio_mean_vs_empirical():
|
||
|
"""Compare beta_ratio.mean (vs _munp) with empirical mean"""
|
||
|
|
||
|
# Define the example beta distribution
|
||
|
beta1 = stats.beta(3, 6)
|
||
|
beta2 = stats.beta(12, 7)
|
||
|
dist = yli.beta_oddsratio.from_scipy(beta1, beta2)
|
||
|
|
||
|
# Compute empirical mean
|
||
|
samples_p1 = beta1.rvs(10_000, random_state=31415)
|
||
|
samples_p2 = beta2.rvs(10_000, random_state=92653)
|
||
|
sample = (samples_p1 / (1 - samples_p1)) / (samples_p2 / (1 - 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 (vs _munp) with empirical variance"""
|
||
|
|
||
|
# Define the example beta distribution
|
||
|
beta1 = stats.beta(3, 6)
|
||
|
beta2 = stats.beta(12, 7)
|
||
|
dist = yli.beta_oddsratio.from_scipy(beta1, beta2)
|
||
|
|
||
|
# Compute empirical mean
|
||
|
samples_p1 = beta1.rvs(10_000, random_state=31415)
|
||
|
samples_p2 = beta2.rvs(10_000, random_state=92653)
|
||
|
sample = (samples_p1 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2))
|
||
|
|
||
|
# Allow 0.01 tolerance
|
||
|
assert np.abs(dist.var() - sample.var()) < 0.01
|