-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_pymaxent_regression.py
More file actions
191 lines (155 loc) · 7.63 KB
/
Copy pathtest_pymaxent_regression.py
File metadata and controls
191 lines (155 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
"""
test_pymaxent_regression.py
---------------------------
Regression suite for PyMaxEnt. Loads gold standard lambda values from
gold_standards.npz (generated by generate_gold_standards.py) and checks
that reconstruct() still produces matching results.
Run with pytest:
pytest test_pymaxent_regression.py -v
Or directly:
python test_pymaxent_regression.py
"""
import warnings
import numpy as np
import pytest
from scipy.integrate import quad
from scipy.stats import lognorm
try:
from pymaxent import reconstruct, moments_c
except ImportError:
from src.pymaxent import reconstruct, moments_c
warnings.filterwarnings("ignore")
RTOL = 1e-5
ATOL = 1e-7
# ---------------------------------------------------------------------------
# Load gold standards
# ---------------------------------------------------------------------------
try:
_gs = np.load("gold_standards.npz")
except FileNotFoundError:
raise FileNotFoundError(
"gold_standards.npz not found. "
"Run generate_gold_standards.py first."
)
# ---------------------------------------------------------------------------
# Distribution definitions (mirror of examples.ipynb)
# ---------------------------------------------------------------------------
def gauss(x):
sigma, mu = 0.2, 1.0
A = 1.0 / (sigma * np.sqrt(2 * np.pi))
return A * np.exp(-(x - mu) ** 2 / (2 * sigma ** 2))
def gauss2(x):
s0, m0 = 1.0/14.0, 1.0/4.0; A0 = 1.0/(2.0*s0*np.sqrt(2*np.pi))
s1, m1 = 1.0/20.0, 2.0/4.0; A1 = 1.0/(2.0*s1*np.sqrt(2*np.pi))
return (A0*np.exp(-(x-m0)**2/(2*s0**2))
+ A1*np.exp(-(x-m1)**2/(2*s1**2)))
def gauss3(x):
s0, m0 = 1.0/14.0, 1.0/4.0; A0 = 1.0/(2.0*s0*np.sqrt(2*np.pi))
s1, m1 = 1.0/20.0, 2.0/4.0; A1 = 1.0/(2.0*s1*np.sqrt(2*np.pi))
s2, m2 = 1.0/20.0, 3.0/4.0; A2 = 1.0/(2.0*s1*np.sqrt(2*np.pi))
return (A0*np.exp(-(x-m0)**2/(2*s0**2))
+ A1*np.exp(-(x-m1)**2/(2*s1**2))
+ A2*np.exp(-(x-m2)**2/(2*s2**2)))
def beta_dist(x):
a, b = 3, 9
btm = quad(lambda X: X**(a-1)*(1-X)**(b-1), 0, 1)[0]
return x**(a-1) * (1-x)**(b-1) / btm
def beta2_dist(x):
a = b = 0.5
btm = quad(lambda X: X**(a-1)*(1-X)**(b-1), 0, 1)[0]
return x**(a-1) * (1-x)**(b-1) / btm
_ld = lognorm([0.25], loc=0.2)
def lognorm_pdf(x):
return np.squeeze(_ld.pdf(x))
# ---------------------------------------------------------------------------
# Discrete tests
# ---------------------------------------------------------------------------
class TestDiscrete:
def test_D1_uniform_single_moment(self):
"""D1 — Single moment (μ0=1) on a 6-sided die → uniform distribution."""
mu = np.array([1]); x = np.array([1,2,3,4,5,6])
sol, lambdas = reconstruct(mu=mu, rndvar=x)
np.testing.assert_allclose(lambdas, _gs["D1_lambdas"], rtol=RTOL, atol=ATOL)
np.testing.assert_allclose(sol, _gs["D1_sol"], rtol=RTOL, atol=ATOL)
def test_D2_unbiased_die(self):
"""D2 — Two moments (μ0=1, μ1=3.5) → uniform distribution for fair die."""
mu = [1, 3.5]; x = [1,2,3,4,5,6]
sol, lambdas = reconstruct(mu=mu, rndvar=x)
np.testing.assert_allclose(lambdas, _gs["D2_lambdas"], rtol=RTOL, atol=ATOL)
np.testing.assert_allclose(sol, _gs["D2_sol"], rtol=RTOL, atol=ATOL)
def test_D3_biased_die(self):
"""D3 — Biased die: true probs [1,1,1,2,3,4]/12, two moments supplied."""
pi = np.array([1,1,1,2,3,4]) / 12.0; x = [1,2,3,4,5,6]
mu = [1, np.sum(pi * np.array(x))]
sol, lambdas = reconstruct(mu=mu, rndvar=x)
np.testing.assert_allclose(lambdas, _gs["D3_lambdas"], rtol=RTOL, atol=ATOL)
np.testing.assert_allclose(sol, _gs["D3_sol"], rtol=RTOL, atol=ATOL)
# Sanity: reconstructed μ1 must recover the input moment
np.testing.assert_allclose(np.sum(np.array(sol) * np.array(x)), mu[1], rtol=1e-6)
# ---------------------------------------------------------------------------
# Continuous tests
# ---------------------------------------------------------------------------
class TestContinuous:
def test_C1_gaussian_3moments(self):
"""C1 — Unimodal Gaussian (μ=1, σ=0.2), 3 moments, bounds [0,2]."""
mu = moments_c(gauss, 3, bnds=[0,2])
_, lambdas = reconstruct(mu=mu, bnds=[0,2])
np.testing.assert_allclose(lambdas, _gs["C1_lambdas"], rtol=RTOL, atol=ATOL)
def test_C2_bimodal_gaussian_5moments(self):
"""C2 — Bimodal Gaussian, 5 moments, bounds [0,1]."""
mu = moments_c(gauss2, 5, bnds=[0,1])
_, lambdas = reconstruct(mu=mu, bnds=[0,1])
np.testing.assert_allclose(lambdas, _gs["C2_lambdas"], rtol=RTOL, atol=ATOL)
def test_C3_bimodal_gaussian_10moments(self):
"""C3 — Bimodal Gaussian, 10 moments, bounds [0,1]."""
mu = moments_c(gauss2, 10, bnds=[0,1])
_, lambdas = reconstruct(mu=mu, bnds=[0,1])
np.testing.assert_allclose(lambdas, _gs["C3_lambdas"], rtol=RTOL, atol=ATOL)
def test_C4_trimodal_gaussian_5moments(self):
"""C4 — Trimodal Gaussian, 5 moments, bounds [0,1]."""
mu = moments_c(gauss3, 5, bnds=[0,1])
_, lambdas = reconstruct(mu=mu, bnds=[0,1])
np.testing.assert_allclose(lambdas, _gs["C4_lambdas"], rtol=RTOL, atol=ATOL)
def test_C5_trimodal_gaussian_13moments(self):
"""C5 — Trimodal Gaussian, 13 moments, bounds [0,1]."""
mu = moments_c(gauss3, 13, bnds=[0,1])
_, lambdas = reconstruct(mu=mu, bnds=[0,1])
np.testing.assert_allclose(lambdas, _gs["C5_lambdas"], rtol=RTOL, atol=ATOL)
def test_C6a_beta_alpha3_beta9_3moments(self):
"""C6a — Beta (α=3, β=9), 3 moments, bounds [0,1]."""
mu = moments_c(beta_dist, 3, bnds=[0,1])
_, lambdas = reconstruct(mu=mu, bnds=[0,1])
np.testing.assert_allclose(lambdas, _gs["C6a_lambdas"], rtol=RTOL, atol=ATOL)
def test_C6b_beta_alpha3_beta9_5moments(self):
"""C6b — Beta (α=3, β=9), 5 moments, bounds [0,1]."""
mu = moments_c(beta_dist, 5, bnds=[0,1])
_, lambdas = reconstruct(mu=mu, bnds=[0,1])
np.testing.assert_allclose(lambdas, _gs["C6b_lambdas"], rtol=RTOL, atol=ATOL)
def test_C7a_beta_alpha05_beta05_3moments(self):
"""C7a — Beta (α=β=0.5, U-shaped), 3 moments, bounds [0,1]."""
mu = moments_c(beta2_dist, 3, bnds=[0,1])
_, lambdas = reconstruct(mu=mu, bnds=[0,1])
np.testing.assert_allclose(lambdas, _gs["C7a_lambdas"], rtol=RTOL, atol=ATOL)
def test_C7b_beta_alpha05_beta05_5moments(self):
"""C7b — Beta (α=β=0.5, U-shaped), 5 moments, bounds [0,1]."""
mu = moments_c(beta2_dist, 5, bnds=[0,1])
_, lambdas = reconstruct(mu=mu, bnds=[0,1])
np.testing.assert_allclose(lambdas, _gs["C7b_lambdas"], rtol=RTOL, atol=ATOL)
def test_C8a_lognormal_3moments(self):
"""C8a — Log-normal (σ=0.25, loc=0.2), 3 moments, bounds [0,5]."""
mu = moments_c(lognorm_pdf, 3, bnds=[0,5])
_, lambdas = reconstruct(mu=mu, bnds=[0,5])
np.testing.assert_allclose(lambdas, _gs["C8a_lambdas"], rtol=RTOL, atol=ATOL)
def test_C8b_lognormal_5moments(self):
"""C8b — Log-normal (σ=0.25, loc=0.2), 5 moments, bounds [0,5]."""
mu = moments_c(lognorm_pdf, 5, bnds=[0,5])
_, lambdas = reconstruct(mu=mu, bnds=[0,5])
np.testing.assert_allclose(lambdas, _gs["C8b_lambdas"], rtol=RTOL, atol=ATOL)
# ---------------------------------------------------------------------------
# Self-runner
# ---------------------------------------------------------------------------
if __name__ == "__main__":
import sys
result = pytest.main([__file__, "-v"])
sys.exit(result)