Skip to content

Commit df9ca56

Browse files
committed
BUG: Snapshot statistic outputs before reusing callbacks
1 parent a93ff73 commit df9ca56

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎arch/bootstrap/base.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def _loo_jackknife(
332332
kwargs_copy[k] = v[items]
333333
if extra_kwargs is not None:
334334
kwargs_copy.update(extra_kwargs)
335-
results.append(func(*args_copy, **kwargs_copy))
335+
results.append(np.array(func(*args_copy, **kwargs_copy)))
336336
return np.array(results)
337337

338338

@@ -1219,8 +1219,8 @@ def _construct_bootstrap_estimates(
12191219
semi = True
12201220

12211221
kwargs = _add_extra_kwargs(self._kwargs, extra_kwargs)
1222-
# Allows lists, tuples, scalars and 0-d arrays
1223-
base = np.asarray(func(*self._args, **kwargs))
1222+
# Snapshot lists, tuples, scalars and arrays before evaluating func again.
1223+
base = np.array(func(*self._args, **kwargs))
12241224
if real_only:
12251225
_check_real_statistic(base)
12261226

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)