Skip to content

Commit

Permalink
try different pt method
Browse files Browse the repository at this point in the history
  • Loading branch information
veni-vidi-vici-dormivi committed Aug 30, 2024
1 parent 388b46e commit 0f0f66c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
3 changes: 2 additions & 1 deletion mesmer/stats/_harmonic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions mesmer/stats/_power_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 19 additions & 20 deletions tests/integration/test_calibrate_mesmer_m.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import importlib

import numpy as np
import pytest
import xarray as xr

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Binary file modified tests/test-data/output/tas/mon/test-mesmer_m-params.nc
Binary file not shown.

0 comments on commit 0f0f66c

Please sign in to comment.