tests: avoid scipy dependency
The χ² in test_error_biased can be easily minimised analytically
This commit is contained in:
parent
5c78af975a
commit
e5e471725c
@ -204,19 +204,14 @@ class GrayTest:
|
|||||||
err = data['Λ_r'].var() / 10
|
err = data['Λ_r'].var() / 10
|
||||||
self.assertGreater(err, 0, msg="Λ is exactly constant")
|
self.assertGreater(err, 0, msg="Λ is exactly constant")
|
||||||
|
|
||||||
def χ2(k, var):
|
|
||||||
'''
|
|
||||||
Reduced χ² for the curve fit: Λ(s) = k⋅var(s)
|
|
||||||
'''
|
|
||||||
res = (data['Λ_r'] - k*data[var]) / err
|
|
||||||
return np.sum(res**2) / (data.size - 1)
|
|
||||||
|
|
||||||
import scipy.optimize
|
|
||||||
|
|
||||||
for var in ['X', 'Y', 'N_⊥']:
|
for var in ['X', 'Y', 'N_⊥']:
|
||||||
k_best = scipy.optimize.minimize(χ2, x0=1, args=var).x[0]
|
# Minimise the χ²(k) = |(Λ_r - k⋅var) / err|² / (n - 1)
|
||||||
|
# The solution is simply: k = (Λ⋅var)/var⋅var
|
||||||
|
k = np.dot(data['Λ_r'], data[var]) / np.linalg.norm(data[var])**2
|
||||||
with self.subTest(var=var):
|
with self.subTest(var=var):
|
||||||
self.assertGreater(χ2(k_best, var), 1)
|
res = (data['Λ_r'] - k*data[var]) / err
|
||||||
|
χ2 = np.linalg.norm(res)**2 / (data.size - 1)
|
||||||
|
self.assertGreater(χ2, 1)
|
||||||
|
|
||||||
|
|
||||||
# Command line options
|
# Command line options
|
||||||
|
Loading…
Reference in New Issue
Block a user