Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion arch/bootstrap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ def _single_optimal_block(x: Float64Array) -> tuple[float, float]:
Estimated optimal window length for circular bootstrap
"""
nobs = x.shape[0]
eps = x - x.mean(0)
# The ratios below are scale invariant but form fourth powers of the
# data's scale. Rescale by a power of two before centering.
_, exponent = np.frexp(np.max(np.abs(x)))
scaled = np.ldexp(x, -exponent)
eps = scaled - scaled.mean(0)
b_max = np.ceil(min(3 * np.sqrt(nobs), nobs / 3))
kn = max(5, int(np.log10(nobs)))
m_max = int(np.ceil(np.sqrt(nobs))) + kn
Expand Down
10 changes: 10 additions & 0 deletions arch/tests/bootstrap/test_block_length.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
from numpy.testing import assert_allclose
import pandas as pd
import pytest

from arch.bootstrap.base import optimal_block_length

Expand Down Expand Up @@ -30,3 +31,12 @@ def test_block_length():

bl = optimal_block_length(np.asarray(df))
assert tuple(bl.index) == (0, 1)


@pytest.mark.parametrize("exponent", [-400, 400])
def test_block_length_is_invariant_to_scale(exponent):
x = np.array([2.0, 5.0, 4.0, 0.0, 5.0, 5.0, 5.0, -5.0, -1.0, 1.0, -2.0, -1.0])
expected = optimal_block_length(x)
with np.errstate(over="raise", divide="raise", invalid="raise"):
actual = optimal_block_length(np.ldexp(x, exponent))
assert_allclose(actual, expected, rtol=1e-13, equal_nan=False)
Loading