|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | + |
| 5 | +from lennart_epp.analysis.memory import ( |
| 6 | + _compute_autocovariance, |
| 7 | + _compute_mean, |
| 8 | + _compute_variance, |
| 9 | + check_stat_diff_close, |
| 10 | + compute_acf, |
| 11 | + compute_hurst_exponent, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def test_series(): |
| 17 | + """Generate a random time series for testing.""" |
| 18 | + rng = np.random.default_rng(2837) |
| 19 | + return rng.standard_normal(100) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def test_df(test_series): |
| 24 | + """Generate a DataFrame containing the test series.""" |
| 25 | + return pd.DataFrame({"close_price": test_series}) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def precomputed_values(test_series): |
| 30 | + """Precompute mean and variance tests.""" |
| 31 | + mean_series = np.mean(test_series) |
| 32 | + variance_series = np.sum((test_series - mean_series) ** 2) |
| 33 | + return mean_series, variance_series |
| 34 | + |
| 35 | + |
| 36 | +def test_compute_mean(test_series): |
| 37 | + """Test whether _compute_mean returns the correct mean.""" |
| 38 | + assert _compute_mean(test_series) == pytest.approx(np.mean(test_series), rel=1e-6) |
| 39 | + |
| 40 | + |
| 41 | +def test_compute_variance(test_series, precomputed_values): |
| 42 | + """Test whether _compute_variance returns the correct variance.""" |
| 43 | + mean_series, expected_variance = precomputed_values |
| 44 | + assert _compute_variance(test_series, mean_series) == pytest.approx( |
| 45 | + expected_variance, rel=1e-6 |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_compute_autocovariance_lag_1(test_series, precomputed_values): |
| 50 | + """Test whether _compute_autocovariance returns the correct value for lag=1.""" |
| 51 | + mean_series, _ = precomputed_values |
| 52 | + lag = 1 |
| 53 | + expected_autocov = np.sum( |
| 54 | + (test_series[lag:] - mean_series) |
| 55 | + * (test_series[: len(test_series) - lag] - mean_series) |
| 56 | + ) |
| 57 | + assert _compute_autocovariance(test_series, mean_series, lag) == pytest.approx( |
| 58 | + expected_autocov, rel=1e-6 |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def test_compute_autocovariance_lag_5(test_series, precomputed_values): |
| 63 | + """Test whether _compute_autocovariance returns the correct value for lag=10.""" |
| 64 | + mean_series, _ = precomputed_values |
| 65 | + lag = 10 |
| 66 | + expected_autocov = np.sum( |
| 67 | + (test_series[lag:] - mean_series) |
| 68 | + * (test_series[: len(test_series) - lag] - mean_series) |
| 69 | + ) |
| 70 | + assert _compute_autocovariance(test_series, mean_series, lag) == pytest.approx( |
| 71 | + expected_autocov, rel=1e-6 |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def test_compute_acf_output_structure(test_df): |
| 76 | + """Test whether compute_acf returns a dictionary with 'acf' and 'lags' keys.""" |
| 77 | + result = compute_acf(test_df, column="close_price", lags=10) |
| 78 | + assert all(key in result for key in ("acf", "lags")) |
| 79 | + |
| 80 | + |
| 81 | +expected_length = 11 |
| 82 | + |
| 83 | + |
| 84 | +def test_compute_acf_length(test_df): |
| 85 | + """Test whether compute_acf returns arrays of correct length.""" |
| 86 | + result = compute_acf(test_df, column="close_price", lags=10) |
| 87 | + assert len(result["acf"]) == expected_length |
| 88 | + assert len(result["lags"]) == expected_length |
| 89 | + |
| 90 | + |
| 91 | +def test_compute_hurst_exponent_output(test_df): |
| 92 | + """Test if compute_hurst_exponent returns dictionary with 'Hurst Exponent' key.""" |
| 93 | + result = compute_hurst_exponent(test_df, column="close_price") |
| 94 | + assert isinstance(result, dict) |
| 95 | + assert "Hurst Exponent" in result |
| 96 | + |
| 97 | + |
| 98 | +def test_compute_hurst_exponent_range(test_df): |
| 99 | + """Test whether the computed Hurst Exponent is within the expected range [0,1].""" |
| 100 | + hurst_value = compute_hurst_exponent(test_df, column="close_price")[ |
| 101 | + "Hurst Exponent" |
| 102 | + ] |
| 103 | + assert 0 <= hurst_value <= 1 |
| 104 | + |
| 105 | + |
| 106 | +def test_check_stat_diff_close_returns_dict(test_df): |
| 107 | + """Ensure check_stat_diff_close returns a dictionary.""" |
| 108 | + result = check_stat_diff_close(test_df, column="close_price") |
| 109 | + assert isinstance(result, dict) |
| 110 | + |
| 111 | + |
| 112 | +def test_check_stat_diff_close_has_expected_keys(test_df): |
| 113 | + """Ensure the returned dictionary has the correct keys.""" |
| 114 | + result = check_stat_diff_close(test_df, column="close_price") |
| 115 | + expected_keys = {"ADF Test Statistic", "P-Value", "Is Stationary"} |
| 116 | + assert set(result.keys()) == expected_keys |
| 117 | + |
| 118 | + |
| 119 | +def test_check_stat_diff_close_raises_error_for_missing_column(test_df): |
| 120 | + """Ensure a ValueError is raised if the column does not exist.""" |
| 121 | + with pytest.raises(ValueError, match="Column .* not found in dataframe."): |
| 122 | + check_stat_diff_close(test_df, column="non_existent_column") |
0 commit comments