This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_func.py
368 lines (279 loc) · 11.7 KB
/
test_func.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# -*- coding: utf-8 -*-
from math import exp, log
import numpy as np
from iminuit import describe
from numpy.testing import assert_allclose
from probfit import pdf
from probfit._libstat import _vector_apply, csum, integrate1d, wlogyx, xlogyx
from probfit.functor import construct_arg, fast_tuple_equal
from probfit.funcutil import merge_func_code
def f(x, y, z):
return x + y + z
def f2(x, z, a):
return x + z + a
def g(x, a, b):
return x + a + b
def h(x, c, d):
return x + c + d
def k_1(y, z):
return y + z
def k_2(i, j):
return i + j
# cpdef double doublegaussian(double x, double mean,
# double sigma_L, double sigma_R)
def test_doublegaussian():
assert describe(pdf.doublegaussian) == ["x", "mean", "sigma_L", "sigma_R"]
assert_allclose(pdf.doublegaussian(0.0, 0.0, 1.0, 2.0), 1.0)
assert_allclose(pdf.doublegaussian(-1.0, 0.0, 1.0, 2.0), 0.6065306597126334)
assert_allclose(pdf.doublegaussian(1.0, 0.0, 1.0, 2.0), 0.8824969025845955)
# cpdef double ugaussian(double x, double mean, double sigma)
def test_ugaussian():
assert describe(pdf.ugaussian) == ["x", "mean", "sigma"]
assert_allclose(pdf.ugaussian(0, 0, 1), 1.0)
assert_allclose(pdf.ugaussian(-1, 0, 1), 0.6065306597126334)
assert_allclose(pdf.ugaussian(1, 0, 1), 0.6065306597126334)
# cpdef double gaussian(double x, double mean, double sigma)
def test_gaussian():
assert describe(pdf.gaussian) == ["x", "mean", "sigma"]
assert_allclose(pdf.gaussian(0, 0, 1), 0.3989422804014327)
assert_allclose(pdf.gaussian(-1, 0, 1), 0.24197072451914337)
assert_allclose(pdf.gaussian(1, 0, 1), 0.24197072451914337)
# cpdef double crystalball(double x,double alpha,double n,double mean,double sigma)
def test_crystalball():
assert describe(pdf.crystalball) == ["x", "alpha", "n", "mean", "sigma"]
assert_allclose(pdf.crystalball(10, 1, 2, 10, 2), 1.0)
assert_allclose(pdf.crystalball(11, 1, 2, 10, 2), 0.8824969025845955)
assert_allclose(pdf.crystalball(12, 1, 2, 10, 2), 0.6065306597126334)
assert_allclose(pdf.crystalball(14, 1, 2, 10, 2), 0.1353352832366127)
assert_allclose(pdf.crystalball(6, 1, 2, 10, 2), 0.26956918209450376)
# cpdef double doubecrystalball(double x,double alpha,double alpha2, double n,double n2, double mean,double sigma)
def test_doublecrystalball():
assert describe(pdf.doublecrystalball) == [
"x",
"alpha",
"alpha2",
"n",
"n2",
"mean",
"sigma",
]
assert_allclose(pdf.doublecrystalball(10, 1, 1, 2, 2, 10, 2), 1.0)
assert_allclose(pdf.doublecrystalball(11, 1, 1, 2, 2, 10, 2), 0.8824969025845955)
assert_allclose(pdf.doublecrystalball(12, 1, 1, 2, 2, 10, 2), 0.6065306597126334)
assert_allclose(pdf.doublecrystalball(14, 1, 1, 2, 2, 10, 2), 0.26956918209450376)
assert_allclose(pdf.doublecrystalball(6, 1, 1, 2, 2, 10, 2), 0.26956918209450376)
assert_allclose(pdf.doublecrystalball(-10, 1, 5, 3, 4, 10, 2), 0.00947704155801)
assert_allclose(pdf.doublecrystalball(0, 1, 5, 3, 4, 10, 2), 0.047744395954055)
assert_allclose(pdf.doublecrystalball(11, 1, 5, 3, 4, 10, 2), 0.8824969025846)
assert_allclose(pdf.doublecrystalball(20, 1, 5, 3, 4, 10, 2), 0.0000037266531720786)
assert_allclose(
pdf.doublecrystalball(25, 1, 5, 3, 4, 10, 2), 0.00000001287132228271
)
# cpdef double argus(double x, double c, double chi, double p)
def test_argus():
assert describe(pdf.argus) == ["x", "c", "chi", "p"]
assert_allclose(pdf.argus(6.0, 10, 2, 3), 0.004373148605400128)
assert_allclose(pdf.argus(10.0, 10, 2, 3), 0.0)
assert_allclose(pdf.argus(8.0, 10, 2, 3), 0.0018167930603254737)
# cpdef double cruijff(double x, double m_0, double sigma_L, double sigma_R, double alpha_L, double alpha_R)
def test_cruijff():
assert describe(pdf.cruijff) == [
"x",
"m_0",
"sigma_L",
"sigma_R",
"alpha_L",
"alpha_R",
]
val = pdf.cruijff(0, 0, 1.0, 2.0, 1.0, 2.0)
assert_allclose(val, 1.0)
vl = pdf.cruijff(0, 1, 1.0, 1.0, 2.0, 2.0)
vr = pdf.cruijff(2, 1, 1.0, 1.0, 2.0, 2.0)
assert_allclose(vl, vr)
assert_allclose(vl, 0.7788007830714)
assert_allclose(vr, 0.7788007830714)
# cpdef double linear(double x, double m, double c)
def test_linear():
assert describe(pdf.linear) == ["x", "m", "c"]
assert_allclose(pdf.linear(1, 2, 3), 5)
assert hasattr(pdf.linear, "integrate")
integral = pdf.linear.integrate((0.0, 1.0), 1, 1, 1)
assert_allclose(integral, 1.5)
# cpdef double poly2(double x, double a, double b, double c)
def test_poly2():
assert describe(pdf.poly2) == ["x", "a", "b", "c"]
assert_allclose(pdf.poly2(2, 3, 4, 5), 25)
# cpdef double poly3(double x, double a, double b, double c, double d)
def test_poly3():
assert describe(pdf.poly3) == ["x", "a", "b", "c", "d"]
assert_allclose(pdf.poly3(2, 3, 4, 5, 6), 56.0)
def test_polynomial():
p = pdf.Polynomial(1)
assert describe(p) == ["x", "c_0", "c_1"]
assert_allclose(p(2, 2, 1), 4)
integral = p.integrate((0, 1), 1, 2, 1)
assert_allclose(integral, 2.5)
p = pdf.Polynomial(2)
assert describe(p) == ["x", "c_0", "c_1", "c_2"]
assert_allclose(p(2, 3, 4, 5), 31)
integral = p.integrate((2, 10), 10, 1, 2, 3)
analytical = 8 + 2 / 2.0 * (10 ** 2 - 2 ** 2) + 3 / 3.0 * (10 ** 3 - 2 ** 3)
assert_allclose(integral, analytical)
# cpdef double novosibirsk(double x, double width, double peak, double tail)
def test_novosibirsk():
assert describe(pdf.novosibirsk) == ["x", "width", "peak", "tail"]
assert_allclose(pdf.novosibirsk(3, 2, 3, 4), 1.1253517471925912e-07)
def test_rtv_breitwigner():
assert describe(pdf.rtv_breitwigner) == ["x", "m", "gamma"]
assert_allclose(pdf.rtv_breitwigner(1, 1, 1.0), 0.8194496535636714)
assert_allclose(pdf.rtv_breitwigner(1, 1, 2.0), 0.5595531041435416)
assert_allclose(pdf.rtv_breitwigner(1, 2, 3.0), 0.2585302502852219)
def test_cauchy():
assert describe(pdf.cauchy), ["x", "m", "gamma"]
assert_allclose(pdf.cauchy(1, 1, 1.0), 0.3183098861837907)
assert_allclose(pdf.cauchy(1, 1, 2.0), 0.15915494309189535)
assert_allclose(pdf.cauchy(1, 2, 4.0), 0.07489644380795074)
def test_johnsonSU():
assert describe(pdf.johnsonSU), ["x", "mean", "sigma", "nu", "tau"]
assert_allclose(pdf.johnsonSU(1.0, 1.0, 1.0, 1.0, 1.0), 0.5212726124342)
assert_allclose(pdf.johnsonSU(1.0, 2.0, 1.0, 1.0, 1.0), 0.1100533373219)
assert_allclose(pdf.johnsonSU(1.0, 2.0, 2.0, 1.0, 1.0), 0.4758433826682)
j = pdf.johnsonSU
assert hasattr(j, "integrate")
integral = j.integrate((-100, 100), 0, 1.0, 1.0, 1.0, 1.0)
assert_allclose(integral, 1.0)
integral = j.integrate((0, 2), 0, 1.0, 1.0, 1.0, 1.0)
assert_allclose(integral, 0.8837311663857358)
def test_exponential():
assert describe(pdf.exponential), ["x", "lambda"]
assert_allclose(pdf.exponential(0.0, 1.0), 1.0)
assert_allclose(pdf.exponential(0.0, 10.0), 10.0)
assert_allclose(pdf.exponential(1.0, 1.0), exp(-1))
assert_allclose(pdf.exponential(2.0, 1.0), exp(-2))
assert_allclose(pdf.exponential(1.0, 2.0), 2 * exp(-2))
assert_allclose(pdf.exponential(2.0, 2.0), 2 * exp(-4))
j = pdf.exponential
assert hasattr(j, "integrate")
integral = j.integrate((-100, 100), 0, 1.0)
assert_allclose(integral, 1.0)
integral = j.integrate((0, 1), 0, 1)
assert_allclose(integral, 1.0 - exp(-1))
integral = j.integrate((1, 2), 0, 1)
assert_allclose(integral, exp(-1) - exp(-2))
integral = j.integrate((0, 1), 0, 2)
assert_allclose(integral, 1.0 - exp(-2))
integral = j.integrate((1, 2), 0, 2)
assert_allclose(integral, exp(-2) - exp(-4))
def test_HistogramPdf():
be = np.array([0, 1, 3, 4], dtype=float)
hy = np.array([10, 30, 50], dtype=float)
norm = float((hy * np.diff(be)).sum())
f = pdf.HistogramPdf(hy, be)
assert_allclose(f(0.5), 10.0 / norm)
assert_allclose(f(1.2), 30.0 / norm)
assert_allclose(f(2.9), 30.0 / norm)
assert_allclose(f(3.6), 50.0 / norm)
assert hasattr(f, "integrate")
integral = f.integrate((0, 4))
assert_allclose(integral, 1.0)
integral = f.integrate((0.5, 3.4))
assert_allclose(integral, (10 * 0.5 + 30 * 2 + 50 * 0.4) / norm)
integral = f.integrate((1.2, 4.5))
assert_allclose(integral, (30 * 1.8 + 50 * 1) / norm)
def test__vector_apply():
def f(x, y):
return x * x + y
y = 10
a = np.array([1.0, 2.0, 3.0])
expected = [f(x, y) for x in a]
va = _vector_apply(f, a, tuple([y]))
assert_allclose(va, expected)
def test_integrate1d():
def f(x, y):
return x * x + y
def intf(x, y):
return x * x * x / 3.0 + y * x
bound = (-2.0, 1.0)
y = 3.0
integral = integrate1d(f, bound, 1000, tuple([y]))
analytic = intf(bound[1], y) - intf(bound[0], y)
assert_allclose(integral, analytic)
def test_integrate1d_analytic():
class temp:
def __call__(self, x, m, c):
return m * x ** 2 + c
def integrate(self, bound, nint, m, c):
a, b = bound
return b - a # (wrong on purpose)
bound = (0.0, 10.0)
f = temp()
integral = integrate1d(f, bound, 10, (2.0, 3.0))
assert_allclose(integral, bound[1] - bound[0])
def test_csum():
x = np.array([1, 2, 3], dtype=np.double)
s = csum(x)
assert_allclose(s, 6.0)
def test_xlogyx():
def bad(x, y):
return x * log(y / x)
assert_allclose(xlogyx(1.0, 1.0), bad(1.0, 1.0))
assert_allclose(xlogyx(1.0, 2.0), bad(1.0, 2.0))
assert_allclose(xlogyx(1.0, 3.0), bad(1.0, 3.0))
assert_allclose(xlogyx(0.0, 1.0), 0.0)
def test_wlogyx():
def bad(w, y, x):
return w * log(y / x)
assert_allclose(wlogyx(1.0, 1.0, 1.0), bad(1.0, 1.0, 1.0))
assert_allclose(wlogyx(1.0, 2.0, 3.0), bad(1.0, 2.0, 3.0))
assert_allclose(wlogyx(1e-50, 1e-20, 1.0), bad(1e-50, 1e-20, 1.0))
def test_construct_arg():
arg = (1, 2, 3, 4, 5, 6)
pos = np.array([0, 2, 4], dtype=np.int)
carg = construct_arg(arg, pos)
assert carg == (1, 3, 5)
def test_merge_func_code():
funccode, [pf, pg, ph] = merge_func_code(f, g, h)
assert funccode.co_varnames == ("x", "y", "z", "a", "b", "c", "d")
assert tuple(pf) == (0, 1, 2)
assert tuple(pg) == (0, 3, 4)
assert tuple(ph) == (0, 5, 6)
def test_merge_func_code_prefix():
funccode, [pf, pg, ph] = merge_func_code(
f, g, h, prefix=["f_", "g_", "h_"], skip_first=True
)
expected = "x", "f_y", "f_z", "g_a", "g_b", "h_c", "h_d"
assert funccode.co_varnames == expected
assert tuple(pf) == (0, 1, 2)
assert tuple(pg) == (0, 3, 4)
assert tuple(ph) == (0, 5, 6)
def test_merge_func_code_factor_list():
funccode, [pf, pg, pk_1, pk_2] = merge_func_code(
f, g, prefix=["f_", "g_"], skip_first=True, factor_list=[k_1, k_2]
)
expected = "x", "f_y", "f_z", "g_a", "g_b", "g_i", "g_j"
assert funccode.co_varnames == expected
assert tuple(pf) == (0, 1, 2)
assert tuple(pg) == (0, 3, 4)
assert tuple(pk_1) == (1, 2)
assert tuple(pk_2) == (5, 6)
def test_merge_func_code_skip_prefix():
funccode, _ = merge_func_code(
f, f2, prefix=["f_", "g_"], skip_first=True, skip_prefix=["z"]
)
assert funccode.co_varnames == ("x", "f_y", "z", "g_a")
def test_fast_tuple_equal():
a = (1.0, 2.0, 3.0)
b = (1.0, 2.0, 3.0)
assert fast_tuple_equal(a, b, 0) is True
a = (1.0, 4.0, 3.0)
b = (1.0, 2.0, 3.0)
assert fast_tuple_equal(a, b, 0) is False
a = (4.0, 3.0)
b = (1.0, 4.0, 3.0)
assert fast_tuple_equal(a, b, 1) is True
a = (4.0, 5.0)
b = (1.0, 4.0, 3.0)
assert fast_tuple_equal(a, b, 1) is False
a = tuple([])
b = tuple([])
assert fast_tuple_equal(a, b, 0) is True