diff --git a/mesmer/stats/_harmonic_model.py b/mesmer/stats/_harmonic_model.py index 14cc544c..39be2cb3 100644 --- a/mesmer/stats/_harmonic_model.py +++ b/mesmer/stats/_harmonic_model.py @@ -143,11 +143,12 @@ def residuals_from_fourier_series(coeffs, yearly_predictor, mon_target): residuals_from_fourier_series, first_guess, args=(yearly_predictor, monthly_target), + loss="linear", method="lm", ) coeffs = minimize_result.x - mse = np.mean(minimize_result.func**2) + mse = np.mean(minimize_result.fun**2) return coeffs, mse diff --git a/mesmer/stats/_power_transformer.py b/mesmer/stats/_power_transformer.py index f0ef73c9..e21b24b2 100644 --- a/mesmer/stats/_power_transformer.py +++ b/mesmer/stats/_power_transformer.py @@ -177,10 +177,7 @@ def _neg_log_likelihood(coeffs): first_guess = np.array([1, 0]) xi_0, xi_1 = minimize( - _neg_log_likelihood, - x0=first_guess, - bounds=bounds, - method="Nelder-Mead", + _neg_log_likelihood, x0=first_guess, bounds=bounds, method="L-BFGS-B", jac="cs" ).x return xi_0, xi_1 diff --git a/tests/integration/test_calibrate_mesmer_m.py b/tests/integration/test_calibrate_mesmer_m.py index 3a595bb0..38a40570 100644 --- a/tests/integration/test_calibrate_mesmer_m.py +++ b/tests/integration/test_calibrate_mesmer_m.py @@ -1,6 +1,5 @@ import importlib -import numpy as np import pytest import xarray as xr @@ -72,22 +71,22 @@ def mask_and_stack(ds, threshold_land): m_time = tas_stacked_m.time # fit harmonic model - # harmonic_model_fit = mesmer.stats.fit_harmonic_model( - # tas_stacked_y.tas, tas_stacked_m.tas - # ) - expected_params = xr.open_dataset( - TEST_PATH / "test-mesmer_m-params.nc", use_cftime=True - ) - hm_preds = mesmer.stats.predict_harmonic_model( - tas_stacked_y.tas, expected_params.hm_coeffs, expected_params.monthly_time - ) - harmonic_model_fit = xr.merge( - [ - expected_params.hm_coeffs.rename("coeffs"), - expected_params.hm_nsel.rename("n_sel"), - hm_preds.rename("predictions"), - ] + harmonic_model_fit = mesmer.stats.fit_harmonic_model( + tas_stacked_y.tas, tas_stacked_m.tas ) + # expected_params = xr.open_dataset( + # TEST_PATH / "test-mesmer_m-params.nc", use_cftime=True + # ) + # hm_preds = mesmer.stats.predict_harmonic_model( + # tas_stacked_y.tas, expected_params.hm_coeffs, expected_params.monthly_time + # ) + # harmonic_model_fit = xr.merge( + # [ + # expected_params.hm_coeffs.rename("coeffs"), + # expected_params.hm_nsel.rename("n_sel"), + # hm_preds.rename("predictions"), + # ] + # ) # train power transformer resids_after_hm = tas_stacked_m - harmonic_model_fit.predictions @@ -142,7 +141,7 @@ def mask_and_stack(ds, threshold_land): TEST_PATH / "test-mesmer_m-params.nc", use_cftime=True ) # np.testing.assert_allclose(expected_params.hm_coeffs.values, calibrated_params.hm_coeffs.values, atol=1e-7) - np.testing.assert_allclose( - expected_params.pt_xi0.values, calibrated_params.pt_xi0.values - ) - xr.testing.assert_allclose(expected_params, calibrated_params) + # np.testing.assert_allclose( + # expected_params.pt_xi0.values, calibrated_params.pt_xi0.values + # ) + xr.testing.assert_allclose(expected_params, calibrated_params, atol=1e-4) diff --git a/tests/test-data/output/tas/mon/test-mesmer_m-params.nc b/tests/test-data/output/tas/mon/test-mesmer_m-params.nc index a060407d..1620a5de 100644 Binary files a/tests/test-data/output/tas/mon/test-mesmer_m-params.nc and b/tests/test-data/output/tas/mon/test-mesmer_m-params.nc differ