From e180ee074fc9d97d2a739a45238b6813f19717bf Mon Sep 17 00:00:00 2001 From: Marco Favorito Date: Fri, 1 Sep 2023 10:55:49 +0200 Subject: [PATCH] lint: fix NPY002 in tests/* To reproduce the errors: ``` ruff check --select "NPY" tests ``` This commit updates the tests code so to make them to use np.random.default_rng with a certain seed, rather than relying on global random seed handling, i.e. using np.random.seed. To do so, a new fixture, 'rng', has been added so to avoid the tests code to initialize a np.random.Generator instance manually. This allowed to remove the special case for macOS platforms in TestCalibrate.test_calibrator_calibrate tests. Fix #49. --- tests/conftest.py | 9 ++ tests/test_calibrator.py | 93 +++-------- tests/test_losses/test_fourier.py | 14 +- tests/test_losses/test_gsl.py | 27 ++-- tests/test_losses/test_likelihood.py | 21 ++- tests/test_losses/test_msm.py | 40 +++-- .../test_plot_descriptive_statistics.py | 4 +- tests/test_samplers/test_best_batch.py | 14 +- tests/test_samplers/test_gaussian_process.py | 10 +- tests/test_samplers/test_xgboost.py | 17 +- tests/test_utils/test_base.py | 4 +- .../test_pandas_json_checkpointing.py | 12 +- tests/test_utils/test_time_series.py | 149 +++++++++++++++--- 13 files changed, 242 insertions(+), 172 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 443d3faf..c4357044 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,6 +18,9 @@ import inspect from pathlib import Path +import numpy as np +import pytest + CUR_PATH = Path(inspect.getfile(inspect.currentframe())).parent # type: ignore[arg-type] ROOT_DIR = Path(CUR_PATH, "..").resolve().absolute() DOCS_DIR = ROOT_DIR / "docs" @@ -27,3 +30,9 @@ EXAMPLE_SAVING_FOLDER = ROOT_DIR / "examples" / "saving_folder" DEFAULT_SUBPROCESS_TIMEOUT = 100.0 + + +@pytest.fixture() +def rng() -> np.random.Generator: + """Return random number generator.""" + return np.random.default_rng(seed=11) diff --git a/tests/test_calibrator.py b/tests/test_calibrator.py index 03bb8bcd..79a486df 100644 --- a/tests/test_calibrator.py +++ b/tests/test_calibrator.py @@ -15,7 +15,6 @@ # along with this program. If not, see . """This module contains tests for the Calibrator.calibrate method.""" -import sys from pathlib import Path from unittest.mock import MagicMock, patch @@ -45,74 +44,38 @@ class TestCalibrate: expected_params = np.array( [ - [0.59, 0.36], - [0.63, 0.41], - [0.18, 0.39], + [0.52, 0.01], [0.56, 0.37], - [0.61, 0.53], - [0.54, 0.32], - [0.74, 0.32], + [0.59, 0.36], + [0.55, 0.46], [0.53, 0.46], - [0.55, 0.62], - [0.71, 0.35], - [0.32, 0.93], [0.8, 0.06], [0.99, 1.0], - [0.06, 0.98], - ], - ) - - expected_losses = [ - 0.33400294, - 0.55274918, - 0.55798021, - 0.61712034, - 0.78963342, - 1.31118518, - 1.51682355, - 1.55503666, - 1.68181078, - 1.70075834, - 1.79905545, - 2.07605975, - 2.25201126, - 2.97360386, - ] - - darwin_expected_params = np.array( - [ - [0.59, 0.36], - [0.63, 0.41], - [0.18, 0.39], - [0.56, 0.37], - [0.63, 0.31], - [0.54, 0.32], - [0.37, 0.59], [0.74, 0.32], - [0.53, 0.46], - [0.57, 0.39], + [0.6, 0.42], + [0.18, 0.39], + [0.51, 0.33], + [0.04, 0.99], [0.32, 0.93], - [0.03, 0.36], - [0.8, 0.06], - [0.06, 0.98], + [0.56, 0.24], ], ) - darwin_expected_losses = [ - 0.33400294, - 0.55274918, - 0.55798021, - 0.61712034, - 0.89679611, - 1.31118518, - 1.3961825, - 1.51682355, - 1.55503666, - 1.65968375, - 1.79905545, - 1.92174866, - 2.07605975, - 2.97360386, + expected_losses = [ + 0.8795007, + 0.99224516, + 1.15590624, + 1.24380484, + 1.76330622, + 1.88165325, + 2.30766018, + 2.55676207, + 2.86482802, + 2.88057794, + 2.90585611, + 3.77705872, + 4.47466328, + 5.79615295, ] def setup(self) -> None: @@ -171,14 +134,8 @@ def test_calibrator_calibrate(self, n_jobs: int) -> None: params, losses = cal.calibrate(14) - # This is a temporary workaround to make tests to run also on Windows. - # See: https://github.com/bancaditalia/black-it/issues/49 - if sys.platform == "darwin": - assert np.allclose(params, self.darwin_expected_params) - assert np.allclose(losses, self.darwin_expected_losses) - else: - assert np.allclose(params, self.expected_params) - assert np.allclose(losses, self.expected_losses) + assert np.allclose(params, self.expected_params) + assert np.allclose(losses, self.expected_losses) def test_calibrator_with_check_convergence( self, diff --git a/tests/test_losses/test_fourier.py b/tests/test_losses/test_fourier.py index ae0efe1e..a60b36de 100644 --- a/tests/test_losses/test_fourier.py +++ b/tests/test_losses/test_fourier.py @@ -23,32 +23,30 @@ ) -def test_fourier_ideal_low_pass() -> None: +def test_fourier_ideal_low_pass(rng: np.random.Generator) -> None: """Test the Fourier loss with the ideal low-pass filter.""" - np.random.seed(11) series_real = np.sin(np.linspace(0, 50, 1000))[:, None] - series_sim = series_real + np.random.normal(0, 0.1, series_real.shape) + series_sim = series_real + rng.normal(0, 0.1, series_real.shape) euclidean_loss = np.sqrt(np.sum((series_sim - series_real) ** 2)) # check that for no filter (f=1.0) this loss is approximately equivalent to # the Euclidean loss and that with increasingly aggressive filters (up to # f=0.01) the loss goes towards zero. - expected_losses = [euclidean_loss, 2.23, 0.97, 0.27] + expected_losses = [euclidean_loss, 2.21, 0.97, 0.187] for i, f in enumerate([1.0, 0.5, 0.1, 0.01]): loss_func = FourierLoss(f=f, frequency_filter=ideal_low_pass_filter) loss = loss_func.compute_loss(series_sim[None, :, :], series_real) assert np.isclose(expected_losses[i], loss, atol=0.01) -def test_fourier_gaussian_low_pass() -> None: +def test_fourier_gaussian_low_pass(rng: np.random.Generator) -> None: """Test the Fourier loss with the gaussian low-pass filter.""" - np.random.seed(11) series_real = np.sin(np.linspace(0, 50, 1000))[:, None] - series_sim = series_real + np.random.normal(0, 0.1, series_real.shape) + series_sim = series_real + rng.normal(0, 0.1, series_real.shape) # check that with increasingly aggressive filters (up to f=0.01) the loss # goes towards zero. - expected_losses = [2.75, 0.95, 0.27] + expected_losses = [2.73, 0.95, 0.19] for i, f in enumerate([1.0, 0.1, 0.01]): loss_func = FourierLoss(f=f, frequency_filter=gaussian_low_pass_filter) loss = loss_func.compute_loss(series_sim[None, :, :], series_real) diff --git a/tests/test_losses/test_gsl.py b/tests/test_losses/test_gsl.py index 5c63720c..d812a571 100644 --- a/tests/test_losses/test_gsl.py +++ b/tests/test_losses/test_gsl.py @@ -98,13 +98,12 @@ def test_get_words(self, args: tuple) -> None: # noqa: PLR6301 GslDivLoss.get_words(*args) -def test_gsl_default() -> None: +def test_gsl_default(rng: np.random.Generator) -> None: """Test the Gsl-div loss function.""" - expected_loss = 0.39737637181336855 + expected_loss = 0.3972285978726733 - np.random.seed(11) - series_sim = np.random.normal(0, 1, (100, 3)) - series_real = np.random.normal(0, 1, (100, 3)) + series_sim = rng.normal(0, 1, (100, 3)) + series_real = rng.normal(0, 1, (100, 3)) loss_func = GslDivLoss() loss = loss_func.compute_loss(series_sim[None, :, :], series_real) @@ -112,13 +111,12 @@ def test_gsl_default() -> None: assert np.isclose(expected_loss, loss) -def test_gsl_with_nb_values() -> None: +def test_gsl_with_nb_values(rng: np.random.Generator) -> None: """Test the Gsl-div loss function with nb_values set.""" - expected_loss = 0.4353415724764564 + expected_loss = 0.4354049587629579 - np.random.seed(11) - series_sim = np.random.normal(0, 1, (2, 100, 3)) - series_real = np.random.normal(0, 1, (100, 3)) + series_sim = rng.normal(0, 1, (2, 100, 3)) + series_real = rng.normal(0, 1, (100, 3)) loss_func = GslDivLoss(nb_values=10) loss = loss_func.compute_loss(series_sim, series_real) @@ -126,13 +124,12 @@ def test_gsl_with_nb_values() -> None: assert np.isclose(expected_loss, loss) -def test_gsl_with_nb_word_lengths() -> None: +def test_gsl_with_nb_word_lengths(rng: np.random.Generator) -> None: """Test the Gsl-div loss function with nb_word_lengths set.""" - expected_loss = 0.7210261201578492 + expected_loss = 0.7177347914787273 - np.random.seed(11) - series_sim = np.random.normal(0, 1, (100, 3)) - series_real = np.random.normal(0, 1, (100, 3)) + series_sim = rng.normal(0, 1, (100, 3)) + series_real = rng.normal(0, 1, (100, 3)) loss_func = GslDivLoss(nb_word_lengths=10) loss = loss_func.compute_loss(series_sim[None, :, :], series_real) diff --git a/tests/test_losses/test_likelihood.py b/tests/test_losses/test_likelihood.py index ef732007..e3a0dfcd 100644 --- a/tests/test_losses/test_likelihood.py +++ b/tests/test_losses/test_likelihood.py @@ -19,11 +19,10 @@ from black_it.loss_functions.likelihood import LikelihoodLoss -def test_likelihood_1d() -> None: +def test_likelihood_1d(rng: np.random.Generator) -> None: """Test the computation of the Likelihood in the Likelihood loss in 1d.""" # sample from a Gaussian distribution. - np.random.seed(11) - real_data = np.random.normal(0, 1, size=(7, 1)) + real_data = rng.normal(0, 1, size=(7, 1)) expected_neg_log_likelihood = -np.sum( -0.5 * np.sum(real_data**2, axis=1) - 0.5 * np.log(2.0 * np.pi), @@ -31,37 +30,35 @@ def test_likelihood_1d() -> None: ) expected_likelihood = np.exp(-expected_neg_log_likelihood) - sim_data_ensemble = np.random.normal(0, 1, size=(3, 100000, 1)) + sim_data_ensemble = rng.normal(0, 1, size=(3, 100000, 1)) loss = LikelihoodLoss(h="silverman") neg_log_lik = loss.compute_loss(sim_data_ensemble, real_data) lik = np.exp(-neg_log_lik) assert np.isclose(lik, expected_likelihood, rtol=0.1) -def test_likelihood_2d() -> None: +def test_likelihood_2d(rng: np.random.Generator) -> None: """Test the computation of the Likelihood in the Likelihood loss in 2d.""" # sample from a Gaussian distribution. - np.random.seed(11) - real_data = np.random.normal(0, 1, size=(10, 2)) + real_data = rng.normal(0, 1, size=(10, 2)) expected_neg_log_likelihood = -np.sum( -0.5 * np.sum(real_data**2, axis=1) - 2.0 / 2.0 * np.log(2.0 * np.pi), axis=0, ) expected_likelihood = np.exp(-expected_neg_log_likelihood) - sim_data_ensemble = np.random.normal(0, 1, size=(1, 1000000, 2)) + sim_data_ensemble = rng.normal(0, 1, size=(1, 1000000, 2)) loss = LikelihoodLoss(h=1.0) neg_log_lik = loss.compute_loss(sim_data_ensemble, real_data) lik = np.exp(-neg_log_lik) assert np.isclose(lik, expected_likelihood, rtol=0.1) -def test_likelihood_2d_wsigma() -> None: +def test_likelihood_2d_wsigma(rng: np.random.Generator) -> None: """Test the computation of the Likelihood in the Likelihood loss in 2d.""" # sample from a Gaussian distribution. - np.random.seed(11) sigma, d = 3.0, 2 - real_data = np.random.normal(0, sigma, size=(10, d)) + real_data = rng.normal(0, sigma, size=(10, d)) expected_neg_log_likelihood = -np.sum( -0.5 / sigma**2 * np.sum(real_data**2, axis=1) @@ -70,7 +67,7 @@ def test_likelihood_2d_wsigma() -> None: ) expected_likelihood = np.exp(-expected_neg_log_likelihood) - sim_data_ensemble = np.random.normal(0, sigma, size=(1, 1000000, d)) + sim_data_ensemble = rng.normal(0, sigma, size=(1, 1000000, d)) loss = LikelihoodLoss(h=sigma) neg_log_lik = loss.compute_loss(sim_data_ensemble, real_data) lik = np.exp(-neg_log_lik) diff --git a/tests/test_losses/test_msm.py b/tests/test_losses/test_msm.py index de8c3797..e0ca8a5e 100644 --- a/tests/test_losses/test_msm.py +++ b/tests/test_losses/test_msm.py @@ -23,14 +23,13 @@ from black_it.loss_functions.msm import MethodOfMomentsLoss -def test_msm_default() -> None: +def test_msm_default(rng: np.random.Generator) -> None: """Test the 'method of moments' loss.""" - expected_loss = 2.830647081075395 + expected_loss = 2.054721024744742 - np.random.seed(11) # ensemble size 2, time series length 100, number of variables 3 - series_sim = np.random.normal(0, 1, (2, 100, 3)) - series_real = np.random.normal(0, 1, (100, 3)) + series_sim = rng.normal(0, 1, (2, 100, 3)) + series_real = rng.normal(0, 1, (100, 3)) loss_func = MethodOfMomentsLoss() @@ -39,15 +38,16 @@ def test_msm_default() -> None: assert np.isclose(expected_loss, loss) -def test_msm_default_calculator_custom_covariance_matrix() -> None: +def test_msm_default_calculator_custom_covariance_matrix( + rng: np.random.Generator, +) -> None: """Test the MSM loss when the covariance matrix is not None.""" - expected_loss = 16.49853079135471 + expected_loss = 7.569748247731355 - np.random.seed(11) - series_sim = np.random.normal(0, 1, (2, 100, 3)) - series_real = np.random.normal(0, 1, (100, 3)) + series_sim = rng.normal(0, 1, (2, 100, 3)) + series_real = rng.normal(0, 1, (100, 3)) - random_mat = np.random.rand(18, 18) + random_mat = rng.random(size=(18, 18)) covariance_matrix = random_mat.T.dot(random_mat) loss_func = MethodOfMomentsLoss(covariance_mat=covariance_matrix) @@ -57,19 +57,23 @@ def test_msm_default_calculator_custom_covariance_matrix() -> None: assert np.isclose(expected_loss, loss) -def test_msm_default_calculator_non_symmetric_covariance_matrix() -> None: +def test_msm_default_calculator_non_symmetric_covariance_matrix( + rng: np.random.Generator, +) -> None: """Test the MSM loss raises error when the provided matrix is not symmetric.""" with pytest.raises( ValueError, match="the provided covariance matrix is not valid as it is not a symmetric matrix", ): - MethodOfMomentsLoss(covariance_mat=np.random.rand(2, 3)) + MethodOfMomentsLoss(covariance_mat=rng.random(size=(2, 3))) -def test_msm_default_calculator_wrong_shape_covariance_matrix() -> None: +def test_msm_default_calculator_wrong_shape_covariance_matrix( + rng: np.random.Generator, +) -> None: """Test the MSM loss raises error when the covariance matrix has wrong shape.""" dimension = 20 - random_mat = np.random.rand(dimension, dimension) + random_mat = rng.random(size=(dimension, dimension)) wrong_covariance_matrix = random_mat.T.dot(random_mat) with pytest.raises( ValueError, @@ -93,7 +97,9 @@ def custom_moment_calculator(time_series: NDArray) -> NDArray: assert np.isclose(expected_loss, loss) -def test_msm_custom_calculator_wrong_shape_covariance_matrix() -> None: +def test_msm_custom_calculator_wrong_shape_covariance_matrix( + rng: np.random.Generator, +) -> None: """Test the 'method of moments' loss with a custom calculator and a custom covariance of the wrong shape.""" series_sim = np.array([[0, 1]]).T series_real = np.array([[1, 2]]).T @@ -102,7 +108,7 @@ def custom_moment_calculator(time_series: NDArray) -> NDArray: return np.array([np.mean(time_series)]) dimension = 3 - random_mat = np.random.rand(dimension, dimension) + random_mat = rng.random(size=(dimension, dimension)) wrong_covariance_matrix = random_mat.T.dot(random_mat) loss_func = MethodOfMomentsLoss( diff --git a/tests/test_plot/test_plot_descriptive_statistics.py b/tests/test_plot/test_plot_descriptive_statistics.py index 1cb3c90a..ff3c1839 100644 --- a/tests/test_plot/test_plot_descriptive_statistics.py +++ b/tests/test_plot/test_plot_descriptive_statistics.py @@ -37,6 +37,6 @@ class TestTsStats(BasePlotTest): def setup(self) -> None: """Set up the test.""" - np.random.seed(42) - data = np.random.rand(100) + rng = np.random.default_rng(42) + data = rng.random(100) self.args = [data] diff --git a/tests/test_samplers/test_best_batch.py b/tests/test_samplers/test_best_batch.py index 1d5191fe..2f13c7d2 100644 --- a/tests/test_samplers/test_best_batch.py +++ b/tests/test_samplers/test_best_batch.py @@ -61,7 +61,7 @@ def test_best_batch_2d() -> None: assert np.allclose(expected_params, new_params) -def test_best_batch_clipping() -> None: +def test_best_batch_clipping(rng: np.random.Generator) -> None: """Test the best-batch clipping when sampling.""" lower_bound, upper_bound = 0.499, 0.5 sampler = BestBatchSampler(batch_size=8, random_state=0) @@ -73,19 +73,21 @@ def test_best_batch_clipping() -> None: verbose=False, ) - existing_points = np.random.rand(8, 2) - existing_losses = np.random.rand(8) + existing_points = rng.random(size=(8, 2)) + existing_losses = rng.random(8) new_params = sampler.sample(param_grid, existing_points, existing_losses) assert (new_params == lower_bound).any() or (new_params == upper_bound).any() -def test_best_batch_sample_requires_batch_size_existing_points() -> None: +def test_best_batch_sample_requires_batch_size_existing_points( + rng: np.random.Generator, +) -> None: """Test that BestBatch.sample must be greater or equal than batch size.""" nb_points = 8 batch_size = 16 - existing_points = np.random.rand(nb_points, 2) - existing_losses = np.random.rand(nb_points) + existing_points = rng.random(size=(nb_points, 2)) + existing_losses = rng.random(size=nb_points) param_grid = SearchSpace( parameters_bounds=np.array([[0, 1], [0, 1]]).T, parameters_precision=np.array([0.01, 0.01]), diff --git a/tests/test_samplers/test_gaussian_process.py b/tests/test_samplers/test_gaussian_process.py index 57e95d73..3f2a8d3a 100644 --- a/tests/test_samplers/test_gaussian_process.py +++ b/tests/test_samplers/test_gaussian_process.py @@ -31,24 +31,25 @@ class TestGaussianProcess2D: def setup(self) -> None: """Set up the test.""" - self.xys, self.losses = self._construct_fake_grid() + self.xys, self.losses = self._construct_fake_grid(seed=0) @classmethod def _construct_fake_grid( cls, + seed: int = 0, n: int = 3, ) -> tuple[NDArray[np.float64], NDArray[np.float64]]: """Construct a fake grid of evaluated losses.""" + rng = np.random.default_rng(seed) xs = np.linspace(0, 1, n) ys = np.linspace(0, 1, n) xys_list = [] losses_list = [] - np.random.seed(0) for x in xs: for y in ys: - px = x + np.random.normal(0, 1e-2) - py = y + np.random.normal(0, 1e-2) + px = x + rng.normal(0, 1e-2) + py = y + rng.normal(0, 1e-2) xys_list.append([px, py]) losses_list.append(px**2 + py**2) @@ -115,6 +116,7 @@ def test_gaussian_process_sample_warning_too_large_dataset() -> None: xys, losses, ) = TestGaussianProcess2D._construct_fake_grid( # noqa: SLF001 + seed=0, n=23, ) with pytest.warns( diff --git a/tests/test_samplers/test_xgboost.py b/tests/test_samplers/test_xgboost.py index a19567be..5a7a3c4c 100644 --- a/tests/test_samplers/test_xgboost.py +++ b/tests/test_samplers/test_xgboost.py @@ -76,14 +76,14 @@ def test_clip_losses() -> None: 0.9, # g3 -0.2, # b3 1.01, # g4 - 0.01, + 0.01, # b4 ], - ) # b4 + ) parameter_bounds = [ [0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0], # lower bounds - [0.1, 0.1, 1.0, 1.0, 1.0, 0.0, 1.1, 1.0], - ] # upper bounds + [0.1, 0.1, 1.0, 1.0, 1.0, 0.0, 1.1, 1.0], # upper bounds + ] precisions = [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01] @@ -112,14 +112,7 @@ def test_clip_losses() -> None: assert np.allclose( losses, np.array( - [ - 7.46098998e02, - 5.80544566e17, - 3.40282347e38, - 3.40282347e38, - 3.40282347e38, - 2.94273501e41, - ], + [0.18388932, 0.58118863, 0.84728975, 0.87882275, 0.88818152, 1.2508034], ), ) diff --git a/tests/test_utils/test_base.py b/tests/test_utils/test_base.py index 5b599457..4090ce10 100644 --- a/tests/test_utils/test_base.py +++ b/tests/test_utils/test_base.py @@ -65,13 +65,13 @@ def test_digitize_data() -> None: assert output_array == pytest.approx(expected_output) -def test_is_symmetric() -> None: +def test_is_symmetric(rng: np.random.Generator) -> None: """Test the 'is_symmetric' function.""" assert is_symmetric(np.zeros((0, 0))) assert is_symmetric(np.zeros((4, 4))) assert is_symmetric(np.ones((4, 4))) - assert not is_symmetric(np.random.rand(4, 4)) + assert not is_symmetric(rng.random(size=(4, 4))) @given( diff --git a/tests/test_utils/test_pandas_json_checkpointing.py b/tests/test_utils/test_pandas_json_checkpointing.py index 74308dd2..f7333d3e 100644 --- a/tests/test_utils/test_pandas_json_checkpointing.py +++ b/tests/test_utils/test_pandas_json_checkpointing.py @@ -25,11 +25,13 @@ ) -def test_save_and_load_calibrator_state() -> None: # noqa: PLR0915 +def test_save_and_load_calibrator_state( # noqa: PLR0915 + rng: np.random.Generator, +) -> None: """Test the 'save_calibrator_state' and 'load_calibrator_state' functions.""" parameters_bounds = np.array([[0, 1], [0, 1]]).T parameters_precision = np.array([0.01, 0.01]) - real_data = np.random.randn(100, 5) + real_data = rng.standard_normal(size=(100, 5)) ensemble_size = 5 N = 30 # noqa: N806 D = 2 # noqa: N806 @@ -47,9 +49,9 @@ def test_save_and_load_calibrator_state() -> None: # noqa: PLR0915 n_sampled_params = 10 n_jobs = 1 - params_samp = np.random.rand(10, 2) - losses_samp = np.random.rand(10) - series_samp = np.random.rand(10, 100, 5) + params_samp = rng.random(size=(10, 2)) + losses_samp = rng.random(size=10) + series_samp = rng.random(size=(10, 100, 5)) batch_num_samp = np.arange(10) method_samp = np.arange(10) diff --git a/tests/test_utils/test_time_series.py b/tests/test_utils/test_time_series.py index 09f31a43..250d55e2 100644 --- a/tests/test_utils/test_time_series.py +++ b/tests/test_utils/test_time_series.py @@ -20,31 +20,138 @@ from black_it.utils.time_series import get_mom_ts -def test_get_mom_ts() -> None: +def test_get_mom_ts(rng: np.random.Generator) -> None: """Test 'get_mom_ts'.""" expected_mom_ts = np.asarray( [ - [0.50471071, 0.52523233, 0.48769797, 0.52125415, 0.45391341], - [0.29597164, 0.29840167, 0.29334348, 0.29175012, 0.30658573], - [-0.39247097, -0.53521998, -0.48164217, -0.41514771, 0.64604918], - [-1.04321643, -1.04806895, -1.05729681, -1.04166884, -1.07053431], - [-0.00826626, -0.1310761, -0.01714527, 0.05582763, -0.07196251], - [0.08511583, 0.05062704, -0.10239579, 0.05814917, -0.15244803], - [-0.1573025, -0.03237303, 0.14330391, 0.03944553, 0.03682953], - [-0.13205127, 0.02166404, -0.06635989, 0.01231925, 0.07872452], - [-0.0594593, -0.16828977, -0.08318478, 0.05319515, 0.00255055], - [0.3472227, 0.36920251, 0.33991259, 0.32411733, 0.36455635], - [0.23971816, 0.25552126, 0.24285517, 0.2348814, 0.25863802], - [0.90238066, 0.74258728, 0.80671623, 0.89465546, 0.59934486], - [-0.65135278, -0.96315459, -0.93646035, -0.78143557, -1.02558061], - [0.15463938, 0.19370484, -0.03971417, 0.04101357, -0.05957849], - [0.00551848, 0.05268421, -0.16417644, 0.11792084, -0.03113898], - [-0.11193418, 0.11907523, -0.04560956, 0.03323841, -0.11316762], - [-0.02379168, -0.00181507, -0.00476993, 0.13203911, -0.06712482], - [0.00665585, -0.06428739, -0.11528277, 0.02027778, -0.07112011], + [ + 0.5047481878242738, + 0.4577343452959496, + 0.45960556439046707, + 0.4939260177141695, + 0.49227447674825114, + ], + [ + 0.3043545480614458, + 0.2669628026226539, + 0.28423903693391567, + 0.30245153381716033, + 0.2819981640130968, + ], + [ + -0.4482110315558403, + 0.6542053297744806, + 0.5308172556250059, + -0.3849423235845297, + 0.31557366158556094, + ], + [ + -1.0657862342927136, + -1.0000694770963234, + -1.0101207985128777, + -1.0782685600527042, + -1.0462395326269132, + ], + [ + -0.09320904577520875, + 0.0686169052226043, + 0.1355394841358424, + 0.09653107323762894, + 0.05311636958390115, + ], + [ + 0.12169737118483734, + 0.0810227210130983, + 0.061958779523484865, + 0.08490375108820097, + 0.006039369703180401, + ], + [ + -0.2025899142653773, + -0.001258750058814992, + 0.12789551725231704, + -0.005223658816300541, + -0.051171362779900316, + ], + [ + 0.0895667296886336, + -0.1207015312253511, + 0.0636018350025852, + -0.028626794182293015, + -0.033236042041738946, + ], + [ + -0.03860009031959613, + -0.10752955274899662, + -0.13297311479661147, + 0.1362727715117892, + -0.20066288961998208, + ], + [ + 0.3705043372180348, + 0.2908837829651126, + 0.29957236943742727, + 0.32868245323966144, + 0.3219905265411198, + ], + [ + 0.2532444717435027, + 0.22136038120813056, + 0.22607782802456683, + 0.23746687234565775, + 0.21518362830554183, + ], + [ + 0.6878867186665477, + 0.8516810049012855, + 0.9249968652455888, + 0.8402319231441494, + 0.8433453672087052, + ], + [ + -0.9980698948119241, + -0.8313118851534257, + -0.7294913128812629, + -0.781988185137505, + -0.8992290494498107, + ], + [ + 0.15460527502011878, + 0.07324077575840597, + 0.129091118386242, + 0.12525686451619064, + 0.04116717905886786, + ], + [ + 0.132780107882644, + -0.0652821068259806, + -0.012893435354472009, + -0.038207396068454, + -0.05256884500600915, + ], + [ + 0.1309359433071446, + 0.09360205934372054, + 0.04414471989530231, + -0.0632467584785815, + 0.002402854248802498, + ], + [ + 0.016550810028943094, + 0.03214763642101067, + 0.0070285228164037265, + -0.03352305029619496, + -0.019493316543544734, + ], + [ + -0.08443798877034842, + -0.04227111216992749, + -0.053302703788843504, + 0.10373275400311846, + -0.06054093058906396, + ], ], ) - np.random.seed(42) - time_series = np.random.rand(100, 5) + time_series = rng.random(size=(100, 5)) mom_ts = get_mom_ts(time_series) assert np.allclose(mom_ts, expected_mom_ts)