From ee3ea6873ed7b46dc969338d6d6544f73855abb0 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Fri, 29 Sep 2023 20:27:14 +0200 Subject: [PATCH] rename coefs to coeffs (#313) * rename coefs to coeffs * remove unused imports * CHANGELOG --- CHANGELOG.rst | 4 +++- mesmer/create_emulations/create_emus_gv.py | 7 ++---- mesmer/create_emulations/create_emus_lv.py | 3 ++- mesmer/stats/auto_regression.py | 28 +++++++++++----------- tests/unit/test_auto_regression.py | 12 +++++----- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cb1974f9..e026e217 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -30,7 +30,9 @@ New Features of the standard deviation (`#306 `_). By `Mathias Hauser`_. - Add ``mesmer.stats.auto_regression._draw_auto_regression_correlated_np``: to draw samples of an - auto regression model (`#161 `_). + auto regression model ( + `#161 `_ and + `#313 `_). By `Mathias Hauser`_. - Extract function to select the order of the auto regressive model: ``mesmer.stats.auto_regression._select_ar_order_xr`` (`#176 `_). diff --git a/mesmer/create_emulations/create_emus_gv.py b/mesmer/create_emulations/create_emus_gv.py index 42cdd8de..1befc1a5 100644 --- a/mesmer/create_emulations/create_emus_gv.py +++ b/mesmer/create_emulations/create_emus_gv.py @@ -147,9 +147,6 @@ def create_emus_gv_AR(params_gv, nr_emus_v, nr_ts_emus_v, seed): - [scen] (2d array (emus, time) of global variability emulation time series) """ - # ensure reproducibility - np.random.seed(seed) - # buffer so that initial start at 0 does not influence overall result # Should this buffer be based on the length of ar_lags instead of hard-coded? buffer = 50 @@ -171,8 +168,8 @@ def create_emus_gv_AR(params_gv, nr_emus_v, nr_ts_emus_v, seed): emus_gv = _draw_auto_regression_correlated_np( intercept=ar_int, - # reshape to n_coefs x n_cells - coefs=ar_coefs[:, np.newaxis], + # reshape to n_coefs x n_cells (cell was squeezed in train_gv.train_gv_AR) + coeffs=ar_coefs[:, np.newaxis], covariance=AR_std_innovs**2, # pass the (co-)variance! n_samples=nr_emus_v, n_ts=nr_ts_emus_v, diff --git a/mesmer/create_emulations/create_emus_lv.py b/mesmer/create_emulations/create_emus_lv.py index dd35b4a5..f5c3aeb8 100644 --- a/mesmer/create_emulations/create_emus_lv.py +++ b/mesmer/create_emulations/create_emus_lv.py @@ -187,7 +187,8 @@ def create_emus_lv_AR1_sci(emus_lv, params_lv, preds_lv, cfg): emus_ar = _draw_auto_regression_correlated_np( intercept=params_lv["AR1_int"][targ], # reshape to n_coefs x n_cells - coefs=params_lv["AR1_coef"][targ][np.newaxis, :], + # (coeffs was squeezed in train_lv.train_lv_AR1_sci) + coeffs=params_lv["AR1_coef"][targ][np.newaxis, :], covariance=params_lv["loc_ecov_AR1_innovs"][targ], n_samples=nr_emus_v, n_ts=nr_ts_emus_stoch_v, diff --git a/mesmer/stats/auto_regression.py b/mesmer/stats/auto_regression.py index d2ba2c9d..460e5063 100644 --- a/mesmer/stats/auto_regression.py +++ b/mesmer/stats/auto_regression.py @@ -80,26 +80,26 @@ def _select_ar_order_np(data, maxlag, ic="bic"): def _draw_auto_regression_correlated_np( - *, intercept, coefs, covariance, n_samples, n_ts, seed, buffer + *, intercept, coeffs, covariance, n_samples, n_ts, seed, buffer ): """ Draw time series of an auto regression process with possibly spatially-correlated innovations Creates `n_samples` auto-correlated time series of order `ar_order` and length - `n_ts` for each set of `n_coefs` coefficients (typically one set for each grid - point), the resulting array has shape n_samples x n_ts x n_coefs. The innovations + `n_ts` for each set of `n_coeffs` coefficients (typically one set for each grid + point), the resulting array has shape n_samples x n_ts x n_coeffs. The innovations can be spatially correlated. Parameters ---------- - intercept : float or ndarray of length n_coefs + intercept : float or ndarray of length n_coeffs Intercept of the model. - coefs : ndarray of shape ar_order x n_coefs + coeffs : ndarray of shape ar_order x n_coeffs The coefficients of the autoregressive process. Must be a 2D array with the autoregressive coefficients along axis=0, while axis=1 contains all independent coefficients. - covariance : float or ndarray of shape n_coefs x n_coefs + covariance : float or ndarray of shape n_coeffs x n_coeffs The (co-)variance array. Must be symmetric and positive-semidefinite. n_samples : int Number of samples to draw for each set of coefficients. @@ -115,7 +115,7 @@ def _draw_auto_regression_correlated_np( ------- out : ndarray Drawn realizations of the specified autoregressive process. The array has shape - n_samples x n_ts x n_coefs. + n_samples x n_ts x n_coeffs. Notes ----- @@ -128,26 +128,26 @@ def _draw_auto_regression_correlated_np( intercept = np.asarray(intercept) covariance = np.atleast_2d(covariance) - # coeffs assumed to be ar_order x n_coefs - ar_order, n_coefs = coefs.shape + # coeffs assumed to be ar_order x n_coeffs + ar_order, n_coeffs = coeffs.shape - # arbitrary lags? see https://github.com/MESMER-group/mesmer/issues/164 + # arbitrary lags? no, see: https://github.com/MESMER-group/mesmer/issues/164 ar_lags = np.arange(1, ar_order + 1, dtype=int) # ensure reproducibility (TODO: clarify approach to this, see #35) np.random.seed(seed) - # innovations has shape (n_samples, n_ts + buffer, n_coefs) + # innovations has shape (n_samples, n_ts + buffer, n_coeffs) innovations = np.random.multivariate_normal( - mean=np.zeros(n_coefs), + mean=np.zeros(n_coeffs), cov=covariance, size=[n_samples, n_ts + buffer], ) - out = np.zeros([n_samples, n_ts + buffer, n_coefs]) + out = np.zeros([n_samples, n_ts + buffer, n_coeffs]) for t in range(ar_order + 1, n_ts + buffer): - ar = np.sum(coefs * out[:, t - ar_lags, :], axis=1) + ar = np.sum(coeffs * out[:, t - ar_lags, :], axis=1) out[:, t, :] = intercept + ar + innovations[:, t, :] diff --git a/tests/unit/test_auto_regression.py b/tests/unit/test_auto_regression.py index 461d4eb0..47fb6f76 100644 --- a/tests/unit/test_auto_regression.py +++ b/tests/unit/test_auto_regression.py @@ -85,7 +85,7 @@ def test_draw_auto_regression_correlated_np_shape(ar_order, n_cells, n_samples, result = mesmer.stats.auto_regression._draw_auto_regression_correlated_np( intercept=intercept, - coefs=coefs, + coeffs=coefs, covariance=covariance, n_samples=n_samples, n_ts=n_ts, @@ -103,7 +103,7 @@ def test_draw_auto_regression_deterministic_intercept(intercept): result = mesmer.stats.auto_regression._draw_auto_regression_correlated_np( intercept=intercept, - coefs=np.array([[0]]), + coeffs=np.array([[0]]), covariance=[0], n_samples=1, n_ts=3, @@ -117,7 +117,7 @@ def test_draw_auto_regression_deterministic_intercept(intercept): result = mesmer.stats.auto_regression._draw_auto_regression_correlated_np( intercept=np.array([[0, intercept]]), - coefs=np.array([[0, 0]]), + coeffs=np.array([[0, 0]]), covariance=np.zeros((2, 2)), n_samples=1, n_ts=1, @@ -134,7 +134,7 @@ def test_draw_auto_regression_deterministic_coefs_buffer(): result = mesmer.stats.auto_regression._draw_auto_regression_correlated_np( intercept=1, - coefs=np.array([[1]]), + coeffs=np.array([[1]]), covariance=[0], n_samples=1, n_ts=4, @@ -151,7 +151,7 @@ def test_draw_auto_regression_deterministic_coefs_buffer(): for i, buffer in enumerate([1, 2]): result = mesmer.stats.auto_regression._draw_auto_regression_correlated_np( intercept=1, - coefs=np.array([[0.5]]), + coeffs=np.array([[0.5]]), covariance=[0], n_samples=1, n_ts=4, @@ -166,7 +166,7 @@ def test_draw_auto_regression_random(): result = mesmer.stats.auto_regression._draw_auto_regression_correlated_np( intercept=1, - coefs=np.array([[0.375], [0.125]]), + coeffs=np.array([[0.375], [0.125]]), covariance=0.5, n_samples=1, n_ts=4,