1- from itertools import product
2-
31import numpy as np
42import pytest
3+ from numpy .testing import assert_allclose
54from scipy .integrate import cumulative_trapezoid
6- from scipy .stats import kstest
5+ from scipy .stats import (
6+ kstest ,
7+ laplace ,
8+ loglaplace ,
9+ lognorm ,
10+ loguniform ,
11+ norm ,
12+ uniform ,
13+ )
714
815from petab .v1 .distributions import *
916from petab .v2 .C import *
1017
1118
1219@pytest .mark .parametrize (
13- "distribution, transform" ,
14- list (
15- product (
16- [
17- Normal (2 , 1 ),
18- Normal (2 , 1 , log = True ),
19- Normal (2 , 1 , log = 10 ),
20- Uniform (2 , 4 ),
21- Uniform (- 2 , 4 , log = True ),
22- Uniform (2 , 4 , log = 10 ),
23- Laplace (1 , 2 ),
24- Laplace (1 , 0.5 , log = True ),
25- ],
26- [LIN , LOG , LOG10 ],
27- )
28- ),
20+ "distribution" ,
21+ [
22+ Normal (2 , 1 ),
23+ Normal (2 , 1 , log = True ),
24+ Normal (2 , 1 , log = 10 ),
25+ Uniform (2 , 4 ),
26+ Uniform (- 2 , 4 , log = True ),
27+ Uniform (2 , 4 , log = 10 ),
28+ Laplace (1 , 2 ),
29+ Laplace (1 , 0.5 , log = True ),
30+ ],
2931)
30- def test_sample_matches_pdf (distribution , transform ):
32+ def test_sample_matches_pdf (distribution ):
3133 """Test that the sample matches the PDF."""
3234 np .random .seed (1 )
3335 N_SAMPLES = 10_000
34- distribution .transform = transform
3536 sample = distribution .sample (N_SAMPLES )
3637
37- # pdf -> cdf
3838 def cdf (x ):
39+ # pdf -> cdf
3940 return cumulative_trapezoid (distribution .pdf (x ), x )
4041
4142 # Kolmogorov-Smirnov test to check if the sample is drawn from the CDF
@@ -49,3 +50,38 @@ def cdf(x):
4950 # plt.show()
5051
5152 assert p > 0.05 , (p , distribution )
53+
54+ # Test samples match scipy CDFs
55+ reference_pdf = None
56+ if isinstance (distribution , Normal ) and distribution .logbase is False :
57+ reference_pdf = norm .pdf (sample , distribution .loc , distribution .scale )
58+ elif isinstance (distribution , Uniform ) and distribution .logbase is False :
59+ reference_pdf = uniform .pdf (
60+ sample , distribution ._low , distribution ._high - distribution ._low
61+ )
62+ elif isinstance (distribution , Laplace ) and distribution .logbase is False :
63+ reference_pdf = laplace .pdf (
64+ sample , distribution .loc , distribution .scale
65+ )
66+ elif isinstance (distribution , Normal ) and distribution .logbase == np .exp (
67+ 1
68+ ):
69+ reference_pdf = lognorm .pdf (
70+ sample , scale = np .exp (distribution .loc ), s = distribution .scale
71+ )
72+ elif isinstance (distribution , Uniform ) and distribution .logbase == np .exp (
73+ 1
74+ ):
75+ reference_pdf = loguniform .pdf (
76+ sample , np .exp (distribution ._low ), np .exp (distribution ._high )
77+ )
78+ elif isinstance (distribution , Laplace ) and distribution .logbase == np .exp (
79+ 1
80+ ):
81+ reference_pdf = loglaplace .pdf (
82+ sample , c = 1 / distribution .scale , scale = np .exp (distribution .loc )
83+ )
84+ if reference_pdf is not None :
85+ assert_allclose (
86+ distribution .pdf (sample ), reference_pdf , rtol = 1e-10 , atol = 1e-14
87+ )
0 commit comments