From e1fd73e095eac67b7806aab213f72127f94c3447 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Thu, 6 Oct 2022 18:30:25 +1100 Subject: [PATCH] Add test case for beta_ratio.pdf/cdf/mean --- .gitignore | 2 + tests/__init__.py | 0 tests/beta_ratio_vs_jsaffer.npy | Bin 0 -> 1728 bytes tests/test_beta_ratio.py | 80 ++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 tests/__init__.py create mode 100644 tests/beta_ratio_vs_jsaffer.npy create mode 100644 tests/test_beta_ratio.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d35cb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +*.pyc diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/beta_ratio_vs_jsaffer.npy b/tests/beta_ratio_vs_jsaffer.npy new file mode 100644 index 0000000000000000000000000000000000000000..71b4851e89bb8384ef83705a20aa83f35c57b1eb GIT binary patch literal 1728 zcmbW0`#Ti)9>-h7E=M*}?y|I5vO>xh&fB3la>?VMs1%u^tYOBI+pgSFO(VCIETtHG za*Y}pZ8@Y^L>e;Laang}zA-L?Y-i5%obwNy_b;#K{r=_ie4h97&UE#3ar6FcUBtQ= z{QwI6G);fMj=t5I@AQpy^v_Uf;WWRHlT=y&_~^)WpQV zNGDF`|CHRi|0>gXkWnv4M=+bl&U##l@?(eM|4e9rfg%6wi={RQ1U#u4SuQAF*xCi? z_M=JB){5CU1a6BUZ8?4vadsvAY_my}ZZO+w^I`_ZJSl#j@&|BtP=1;&nZ+KZMAl7p z5qQZ|6GE9s?vi)CXX*lYD#kS9_(jxR4sNJ*UP4T}uJ(dp3E{~TMOOmEXtsWpUtT4~ zUk8?-Z|oK0#)5|xyGM+W*;D6atHp3+PHNo=5#vqLTKoZdF`idh)A-CK6!iJKpIu%= zi|L>l-*piKJ*i&S84Hm1)?Ns0o`-!(x!GZ%2#&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TNjU8{7nFJlrSU!SW0t?oGExAPmZQ)eBWUCI|XA}6d zbjqxs5p1Zhs*-Ld82I|_@y%@n@|kTfqq_(?!+sIE^bp)PcsY{CBe=tLkdELJe5;wh zZET2Ob$YHqJVMazR8+$rCz2hxQ;ntwK31Rm!*@n<_p9)~c?k*nUG5Rp4+Jq*;{~O& z1VI%KPHz. + +import numpy as np + +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('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('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('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 + dist = yli.beta_ratio(3, 6, 12, 7) + + # Compute empirical mean + sample = dist.rvs(1000, random_state=31415) + + # Allow 0.01 tolerance + assert np.abs(dist.mean() - sample.mean()) < 0.01