Skip to content

Commit

Permalink
rename coefs to coeffs (#313)
Browse files Browse the repository at this point in the history
* rename coefs to coeffs

* remove unused imports

* CHANGELOG
  • Loading branch information
mathause authored Sep 29, 2023
1 parent b30b6b1 commit ee3ea68
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ New Features
of the standard deviation (`#306 <https://github.com/MESMER-group/mesmer/issues/306>`_).
By `Mathias Hauser`_.
- Add ``mesmer.stats.auto_regression._draw_auto_regression_correlated_np``: to draw samples of an
auto regression model (`#161 <https://github.com/MESMER-group/mesmer/pull/161>`_).
auto regression model (
`#161 <https://github.com/MESMER-group/mesmer/pull/161>`_ and
`#313 <https://github.com/MESMER-group/mesmer/pull/313>`_).
By `Mathias Hauser`_.
- Extract function to select the order of the auto regressive model: ``mesmer.stats.auto_regression._select_ar_order_xr``
(`#176 <https://github.com/MESMER-group/mesmer/pull/176>`_).
Expand Down
7 changes: 2 additions & 5 deletions mesmer/create_emulations/create_emus_gv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion mesmer/create_emulations/create_emus_lv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 14 additions & 14 deletions mesmer/stats/auto_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
-----
Expand All @@ -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, :]

Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_auto_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit ee3ea68

Please sign in to comment.