Skip to content

BUG: Debias the variance ratio for a known zero drift - #1043

Open
QuantIntellect wants to merge 1 commit into
bashtage:mainfrom
QuantIntellect:fix-variance-ratio-known-zero-drift
Open

QuantIntellect wants to merge 1 commit into
bashtage:mainfrom
QuantIntellect:fix-variance-ratio-known-zero-drift

Conversation

@QuantIntellect

Copy link
Copy Markdown

With trend="n" the drift is known to be 0 (mu = 0, unitroot.py L2161-L2162). The debiased variances, which are the default, still apply Lo and MacKinlay's corrections for an estimated drift: the one-period sum of squares is divided by nq − 1, and the q-period sum by m = q(nq − q + 1)(1 − q/nq) (L2179-L2182). Under the null, with the drift known to be 0, the two sums of squares have expected values nqσ² and q(nq − q + 1)σ², so the divisors that make the estimates unbiased are nq and q(nq − q + 1). As a result the ratio is inflated by exactly (nq − 1)/(nq − q). That is small when lags is much smaller than nq (1.004 for 1,000 increments and lags=5), but it is 1.10 for ten years of monthly data with lags=12.

Under the null, with 5,000 Gaussian random walks without drift for each row:

nq, lags Mean VR, trend="n" Mean VR, trend="c" 5% rejections, trend="n" 5% rejections, trend="c"
100, 10 1.096 0.996 8.9% 5.3%
60, 12 1.242 1.010 14.0% 6.4%
250, 20 1.095 1.011 8.3% 5.5%

On the same draws with this change, trend="n" has mean ratios of 0.997, 1.011 and 1.012, and rejects 5.2%, 6.3% and 5.4% of the time, in line with trend="c". With Gaussian increments the mean ratio is exactly 1 with this change, and exactly (nq − 1)/(nq − q) before it, so the remaining differences in the means are simulation noise.

This goes back to when VarianceRatio was added (dce161d). The only test that checks the ratio's value with trend="n", test_variance_ratio_no_constant, uses debiased=False.

Change

For trend="n", the debiased variances divide by nq and q(nq − q + 1), so vr, stat and pvalue change for trend="n" with the default arguments. trend="c", debiased=False and overlap=False are unchanged. I left the extra observation that debiased=True requires in place, although trend="n" no longer needs it.

Tests

  • test_variance_ratio_no_constant_debiased checks the ratio against the formula.
  • test_variance_ratio_debiased_centered checks, for each trend, that the mean ratio over 1,000 Gaussian random walks without drift is within 0.06 of 1. The "c" case passes on main too.

The first test and the "n" case of the second fail on main, and all of them pass with the change. pytest arch/tests -m "not slow" passes (31426 passed, 319 skipped), and ruff, black, isort and numpydoc are clean.

With trend="n" the drift is known to be 0, but the debiased variance
estimates still divided by nq - 1 and by Lo and MacKinlay's m, which
correct for an estimated drift. Divide by nq and q(nq - q + 1), which
make the estimates unbiased when the drift is known to be 0. The ratio
was inflated by (nq - 1) / (nq - q), so the test rejected a random walk
too often.
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.50%. Comparing base (d3e858f) to head (d6da935).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1043   +/-   ##
=======================================
  Coverage   99.50%   99.50%           
=======================================
  Files         154      154           
  Lines       34868    34886   +18     
  Branches     2639     2640    +1     
=======================================
+ Hits        34694    34712   +18     
  Misses         98       98           
  Partials       76       76           
Flag Coverage Δ
adder 99.44% <100.00%> (+<0.01%) ⬆️
subtractor 99.44% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant