tests: avoid scipy dependency

The χ² in test_error_biased can be easily minimised analytically
This commit is contained in:
Michele Guerini Rocco 2024-10-09 18:09:57 +02:00 committed by rnhmjoj
parent 5c78af975a
commit e5e471725c
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

View File

@ -204,19 +204,14 @@ class GrayTest:
err = data['Λ_r'].var() / 10
self.assertGreater(err, 0, msg="Λ is exactly constant")
def χ2(k, var):
'''
Reduced χ² for the curve fit: Λ(s) = kvar(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_⊥']:
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):
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