1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 from numpy import array, float64, zeros
25 from unittest import TestCase
26
27
28 from maths_fns.chi2 import *
29
30
32 """Unit tests for the maths_fns.chi2 relax module."""
33
35 """Create a number of objects for the calculation and testing of the chi-squared equations."""
36
37
38 self.data = array([1.0, 1.5, 2.0, 2.5, 3.0], float64)
39
40
41 self.back_calc = array([0.9, 1.45, 2.0, 2.55, 3.1], float64)
42
43
44 self.back_calc_grad = array([[ 0.1, 0.2, 0.3, 0.2, 0.1],
45 [-0.2, -0.1, 0.0, 0.1, 0.2]], float64)
46
47
48 self.back_calc_hess = array([[[0.01, 0.005, 0.0, 0.005, 0.01],
49 [0.05, 0.01, 0.0, 0.01, 0.05]],
50 [[0.001, 0.0005, 0.0, 0.0005, 0.001],
51 [0.005, 0.001, 0.0, 0.001, 0.005]]],
52 float64)
53
54
55 self.errors = array([0.1, 0.1, 0.1, 0.1, 0.1], float64)
56
57
59 """Delete all the data structures."""
60
61 del self.data
62 del self.back_calc
63 del self.back_calc_grad
64 del self.back_calc_hess
65 del self.errors
66
67
69 """Unit test for the value returned by the chi2 function.
70
71 The chi-squared value is 2.5 for the following data::
72
73 data = | 1.0 1.5 2.0 2.5 3.0 |,
74
75 back_calc = | 0.9 1.45 2.0 2.55 3.1 |,
76
77 errors = | 0.1 0.1 0.1 0.1 0.1 |.
78 """
79
80
81 val = chi2(self.data, self.back_calc, self.errors)
82
83
84 self.assertEqual(val, 2.5)
85
86
87 del val
88
89
91 """Unit test for the chi-squared gradient created by the dchi2 function.
92
93 The chi-squared gradient is [0, 10] for the following data::
94
95 data = | 1.0 1.5 2.0 2.5 3.0 |,
96
97 back_calc = | 0.9 1.45 2.0 2.55 3.1 |,
98
99 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 |
100 | -0.2 -0.1 0.0 0.1 0.2 |,
101
102 errors = | 0.1 0.1 0.1 0.1 0.1 |.
103 """
104
105
106 grad = zeros(2, float64)
107 dchi2(grad, 2, self.data, self.back_calc, self.back_calc_grad, self.errors)
108
109
110 self.assertAlmostEqual(grad[0], 0.0, places=13)
111 self.assertAlmostEqual(grad[1], 10.0, places=13)
112
113
114 del grad
115
116
118 """Unit test for the chi-squared gradient created by the dchi2_element function.
119
120 The chi-squared gradient is [0, 10] for the following data::
121
122 data = | 1.0 1.5 2.0 2.5 3.0 |,
123
124 back_calc = | 0.9 1.45 2.0 2.55 3.1 |,
125
126 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 |
127 | -0.2 -0.1 0.0 0.1 0.2 |,
128
129 errors = | 0.1 0.1 0.1 0.1 0.1 |.
130 """
131
132
133 grad0 = dchi2_element(self.data, self.back_calc, self.back_calc_grad[0], self.errors)
134 grad1 = dchi2_element(self.data, self.back_calc, self.back_calc_grad[1], self.errors)
135
136
137 self.assertAlmostEqual(grad0, 0.0, places=13)
138 self.assertAlmostEqual(grad1, 10.0, places=13)
139
140
141 del grad0, grad1
142
143
145 """Unit test for the chi-squared Hessian created by the d2chi2 function.
146
147 For the data::
148
149 data = | 1.0 1.5 2.0 2.5 3.0 |,
150
151 back_calc = | 0.9 1.45 2.0 2.55 3.1 |,
152
153 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 |
154 |-0.2 -0.1 0.0 0.1 0.2 |,
155
156 back_calc_hess[0] = | 0.01 0.005 0.0 0.005 0.01 |
157 | 0.05 0.01 0.0 0.01 0.05 |,
158
159 back_calc_hess[1] = | 0.001 0.0005 0.0 0.0005 0.001 |
160 | 0.005 0.001 0.0 0.001 0.005 |,
161
162 errors = | 0.1 0.1 0.1 0.1 0.1 |,
163
164 the chi-squared hessian is::
165
166 Hessian = | 38.0 0.0 |
167 | 0.0 20.0 |.
168 """
169
170
171 hess = zeros((2, 2), float64)
172 d2chi2(hess, 2, self.data, self.back_calc, self.back_calc_grad, self.back_calc_hess, self.errors)
173
174
175 self.assertAlmostEqual(hess[0, 0], 38.0, places=13)
176 self.assertAlmostEqual(hess[0, 1], 0.0, places=13)
177 self.assertAlmostEqual(hess[1, 0], 0.0, places=13)
178 self.assertAlmostEqual(hess[1, 1], 20.0, places=13)
179
180
181 del hess
182
183
185 """Unit test for the chi-squared Hessian created by the d2chi2_element function.
186
187 For the data::
188
189 data = | 1.0 1.5 2.0 2.5 3.0 |,
190
191 back_calc = | 0.9 1.45 2.0 2.55 3.1 |,
192
193 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 |
194 |-0.2 -0.1 0.0 0.1 0.2 |,
195
196 back_calc_hess[0] = | 0.01 0.005 0.0 0.005 0.01 |
197 | 0.05 0.01 0.0 0.01 0.05 |,
198
199 back_calc_hess[1] = | 0.001 0.0005 0.0 0.0005 0.001 |
200 | 0.005 0.001 0.0 0.001 0.005 |,
201
202 errors = | 0.1 0.1 0.1 0.1 0.1 |,
203
204 the chi-squared hessian is::
205
206 Hessian = | 38.0 0.0 |
207 | 0.0 20.0 |.
208 """
209
210
211 hess00 = d2chi2_element(self.data, self.back_calc, self.back_calc_grad[0], self.back_calc_grad[0], self.back_calc_hess[0, 0], self.errors)
212 hess01 = d2chi2_element(self.data, self.back_calc, self.back_calc_grad[0], self.back_calc_grad[1], self.back_calc_hess[0, 1], self.errors)
213 hess10 = d2chi2_element(self.data, self.back_calc, self.back_calc_grad[1], self.back_calc_grad[0], self.back_calc_hess[1, 0], self.errors)
214 hess11 = d2chi2_element(self.data, self.back_calc, self.back_calc_grad[1], self.back_calc_grad[1], self.back_calc_hess[1, 1], self.errors)
215
216
217 self.assertAlmostEqual(hess00, 38.0, places=13)
218 self.assertAlmostEqual(hess01, 0.0, places=13)
219 self.assertAlmostEqual(hess10, 0.0, places=13)
220 self.assertAlmostEqual(hess11, 20.0, places=13)
221
222
223 del hess00, hess01, hess10, hess11
224