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