|
| 1 | +import numpy as np |
| 2 | +from numpy.testing import assert_allclose |
| 3 | +import pytest |
| 4 | + |
| 5 | +from arch.bootstrap import IIDBootstrap |
| 6 | +from arch.bootstrap.base import _loo_jackknife |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize("scalar", [True, False]) |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "method", ["norm", "basic", "percentile", "bc", "bca", "studentized"] |
| 12 | +) |
| 13 | +def test_conf_int_reused_statistic_output(scalar, method): |
| 14 | + x = np.random.default_rng(0).standard_exponential((40, 2)) |
| 15 | + if scalar: |
| 16 | + x = x[:, 0] |
| 17 | + output = np.empty(()) if scalar else np.empty(2) |
| 18 | + |
| 19 | + def statistic(y): |
| 20 | + return np.mean(y, axis=0, out=output) |
| 21 | + |
| 22 | + expected = IIDBootstrap(x, seed=1).conf_int( |
| 23 | + lambda y: y.mean(0), reps=100, method=method, studentize_reps=15 |
| 24 | + ) |
| 25 | + actual = IIDBootstrap(x, seed=1).conf_int( |
| 26 | + statistic, reps=100, method=method, studentize_reps=15 |
| 27 | + ) |
| 28 | + assert_allclose(actual, expected) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("method", ["cov", "var"]) |
| 32 | +@pytest.mark.parametrize("scalar", [True, False]) |
| 33 | +@pytest.mark.parametrize("complex_statistic", [False, True]) |
| 34 | +def test_covariance_reused_statistic_output(method, scalar, complex_statistic): |
| 35 | + x = np.arange(40.0).reshape(20, 2) |
| 36 | + if scalar: |
| 37 | + x = x[:, 0] |
| 38 | + factor = 1.0 + 2.0j if complex_statistic else 1.0 |
| 39 | + output = np.empty(() if scalar else 2, dtype=np.result_type(factor)) |
| 40 | + |
| 41 | + def statistic(y): |
| 42 | + return np.multiply(np.mean(y, axis=0), factor, out=output) |
| 43 | + |
| 44 | + expected = getattr(IIDBootstrap(x, seed=1), method)( |
| 45 | + lambda y: y.mean(0) * factor, reps=100, recenter=False |
| 46 | + ) |
| 47 | + actual = getattr(IIDBootstrap(x, seed=1), method)( |
| 48 | + statistic, reps=100, recenter=False |
| 49 | + ) |
| 50 | + assert_allclose(actual, expected) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("scalar", [True, False]) |
| 54 | +def test_jackknife_reused_statistic_output(scalar): |
| 55 | + x = np.arange(40.0).reshape(20, 2) |
| 56 | + if scalar: |
| 57 | + x = x[:, 0] |
| 58 | + output = np.empty(()) if scalar else np.empty(2) |
| 59 | + |
| 60 | + def statistic(y): |
| 61 | + return np.mean(y, axis=0, out=output) |
| 62 | + |
| 63 | + expected = _loo_jackknife(lambda y: y.mean(0), len(x), (x,), {}) |
| 64 | + actual = _loo_jackknife(statistic, len(x), (x,), {}) |
| 65 | + assert_allclose(actual, expected) |
0 commit comments