Skip to content

Commit 126e519

Browse files
Harrison 3063/#36 - PMCL - Maps spx: Use reduced chi-squared
Divide by degrees of freedom (which is actually #points - #parameters) to get the reduced chi-squared
1 parent 80f4e5b commit 126e519

6 files changed

Lines changed: 37 additions & 36 deletions

File tree

imap_l3_processing/cdf/config/imap_hi_l3_variable_attrs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ ena_spectral_scalar_stat_uncert:
431431
ena_spectral_index_chisq:
432432
NAME: ena_spectral_index_chisq
433433
DATA_TYPE: CDF_REAL4
434-
CATDESC: ENA spectral index chi-squared
434+
CATDESC: ENA spectral index reduced chi-squared
435435
DEPEND_0: epoch
436436
DEPEND_1: energy
437437
DEPEND_2: longitude

imap_l3_processing/cdf/config/imap_lo_l3_variable_attrs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ ena_spectral_scalar_stat_uncert:
694694
ena_spectral_index_chisq:
695695
NAME: ena_spectral_index_chisq
696696
DATA_TYPE: CDF_REAL4
697-
CATDESC: ENA spectral index chi-squared
697+
CATDESC: ENA spectral index reduced chi-squared
698698
DEPEND_0: epoch
699699
DEPEND_1: energy
700700
DEPEND_2: longitude

imap_l3_processing/cdf/config/imap_ultra_l3_variable_attrs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ ena_spectral_scalar_stat_uncert:
431431
ena_spectral_index_chisq:
432432
NAME: ena_spectral_index_chisq
433433
DATA_TYPE: CDF_REAL4
434-
CATDESC: ENA spectral index chi-squared
434+
CATDESC: ENA spectral index reduced chi-squared
435435
DEPEND_0: epoch
436436
DEPEND_1: energy
437437
DEPEND_2: longitude

imap_l3_processing/maps/spectral_fit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def fit_arrays_to_power_law(fluxes: np.ndarray, uncertainties: np.ndarray, energ
167167
scalar_coefficients[i] = a
168168
gamma_errors[i] = gamma_error
169169
scalar_coefficient_errors[i] = a_error
170-
chisqs[i] = fit.fnorm
170+
chisqs[i] = fit.fnorm / fit.dof
171171
output_gammas[epoch, 0] = gammas.reshape(fluxes.shape[2:])
172172
output_gamma_errors[epoch, 0] = gamma_errors.reshape(fluxes.shape[2:])
173173
output_scalar_coefficients[epoch, 0] = scalar_coefficients.reshape(fluxes.shape[2:])

tests/maps/test_spectral_fit.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def test_finds_best_fit(self):
5555
np.testing.assert_array_almost_equal( result_chi_square ,np.zeros((1,1, *spacial_dimension_shape)))
5656

5757
def test_finds_chi_squared_for_fit(self):
58-
energies = np.array([1, 10, 100])
59-
flux_data = np.array([99, 10, 1])
60-
errors = np.array([1,1, 1e-3])
58+
energies = np.array([1, 10, 30, 100])
59+
flux_data = np.array([99, 10, 3, 1])
60+
errors = np.array([1,1, 0.1, 1e-3])
6161

6262
cases = [
6363
("rectangular", (1, 1)),
@@ -73,7 +73,8 @@ def test_finds_chi_squared_for_fit(self):
7373
fitted_flux = result_A * np.power(energies.reshape((-1,*spacial_dimension_shape)), -result_gamma)
7474
residual = fitted_flux - flux
7575
chisquared = np.sum(np.square(residual/uncertainty))
76-
np.testing.assert_array_almost_equal(result_chi_square, chisquared)
76+
reduced_chisquared = chisquared / (len(energies) - 2)
77+
np.testing.assert_array_almost_equal(result_chi_square, reduced_chisquared)
7778

7879
def test_spectral_fit_map(self):
7980
epoch = np.array([datetime.now()])
@@ -332,8 +333,8 @@ def test_finds_best_fit_with_ibex_data(self):
332333
[52.494673, 44.64706]]]]))
333334
np.testing.assert_array_almost_equal(scalar_coefficient_errors, np.array([[[[32.245348, 26.075043],
334335
[19.584518, 20.080801]]]]))
335-
np.testing.assert_array_almost_equal(chisq, np.array([[[[1.33863 , 1.174914],
336-
[1.034745, 1.426575]]]]))
336+
np.testing.assert_array_almost_equal(chisq, np.array([[[[0.44621 , 0.391638],
337+
[0.344915, 0.475525]]]]))
337338

338339
def test_finds_best_fit_with_zeros_in_flux_and_not_uncertainty(self):
339340
energies = np.geomspace(1, 1e10, 23)
@@ -354,7 +355,7 @@ def test_finds_best_fit_with_zeros_in_flux_and_not_uncertainty(self):
354355
np.array([1.472697]).reshape(1, 1, *spacial_dimension_shape))
355356
np.testing.assert_array_almost_equal(scalar_coefficients,
356357
np.array([1.251819]).reshape(1, 1, *spacial_dimension_shape))
357-
np.testing.assert_array_almost_equal(chisq, np.array([46.587911]).reshape(1, 1, *spacial_dimension_shape))
358+
np.testing.assert_array_almost_equal(chisq, np.array([2.218472]).reshape(1, 1, *spacial_dimension_shape))
358359

359360
def test_returns_nan_when_only_one_point_is_valid(self):
360361
energies = np.geomspace(1, 1e10, 5)

0 commit comments

Comments
 (0)