Skip to content

Commit b12c8f4

Browse files
committed
test:First version of separation tests.
1 parent 924b15c commit b12c8f4

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/01-separation.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import unittest
2+
import numpy
3+
from Regressionizer import *
4+
5+
6+
def chebyshev_t_polynomials(n):
7+
if n == 0:
8+
return lambda x: 1
9+
elif n == 1:
10+
return lambda x: x
11+
else:
12+
T0 = lambda x: 1
13+
T1 = lambda x: x
14+
for i in range(2, n + 1):
15+
Tn = lambda x, T0=T0, T1=T1: 2 * x * T1(x) - T0(x)
16+
T0, T1 = T1, Tn
17+
return Tn
18+
19+
20+
chebyshev_polynomials = [chebyshev_t_polynomials(i) for i in range(8)]
21+
22+
23+
class MyTestCase(unittest.TestCase):
24+
x = numpy.linspace(0, 2, 400)
25+
y = numpy.sin(2 * numpy.pi * x) + numpy.random.normal(0, 0.4, x.shape)
26+
data = numpy.column_stack((x, y))
27+
funcs = [lambda x: 1, lambda x: x, lambda x: numpy.cos(x), lambda x: numpy.cos(3 * x), lambda x: numpy.cos(6 * x)]
28+
29+
def test_quantile_regression(self):
30+
probs = [0.2, 0.5, 0.8]
31+
32+
frac_dict = (
33+
Regressionizer(self.data)
34+
.quantile_regression(knots=10, probs=probs)
35+
.separate(cumulative=True, fractions=True)
36+
.take_value()
37+
)
38+
39+
for prob in probs:
40+
self.assertAlmostEqual(frac_dict[prob], prob, delta=0.025)
41+
42+
def test_quantile_regression_fit(self):
43+
probs = [0.2, 0.5, 0.8]
44+
45+
frac_dict = (
46+
Regressionizer(self.data)
47+
.quantile_regression_fit(funcs=chebyshev_polynomials, probs=probs)
48+
.separate(cumulative=True, fractions=True)
49+
.take_value()
50+
)
51+
52+
for prob in probs:
53+
self.assertAlmostEqual(frac_dict[prob], prob, delta=0.025)
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

0 commit comments

Comments
 (0)