Skip to content

var: integer-degree VaR roots no longer depend on the data's units - #180

Closed
gitRasheed wants to merge 1 commit into
rasheed/py-copula-degenerate-targetfrom
rasheed/py-var-root-tolerance
Closed

gitRasheed wants to merge 1 commit into
rasheed/py-copula-degenerate-targetfrom
rasheed/py-var-root-tolerance

Conversation

@gitRasheed

Copy link
Copy Markdown
Collaborator

R's integer-degree LPM.VaR and UPM.VaR accepted an interval end when the polynomial residual (1 - p) * LPM - p * UPM was within sqrt(eps). That residual scales with x^degree, so small-unit data took the first end it tested: LPM.VaR(.5, 4, c(-.003, -.002, -.001, .001, .002, .003)) was -0.001 instead of 0. OVVO-Financial/NNS#125 (merged) tests the dimensionless ratio gap instead and solves the root to rounding. This slice mirrors it.

Stacked on #179; merge with a merge commit.

Changes

  • _lpm_var_integer_interval (var.py) accepts an end when LPM / (LPM + UPM) - p is within eps^0.75. The root uses the repo's port of R's zeroin2 (the one _var_uniroot already uses) at eps^2 times the interval width, as R's uniroot call now does, because a tolerance on t does not bound the ratio where it is steep. Without a sign change, which only rounding at a break produces, it takes the endpoint with the smaller gap instead of minimising |f| with minimize_scalar.
  • A zero LPM + UPM (subnormal data) gives a NaN gap instead of raising ZeroDivisionError; the existing hypothesis range test found that.
  • Before and after, with after equal to R main: the symmetric 1e-3 case -0.001 -> 0; lpm_var([.01, .05, .5, .95], 3, 1e-4 * x) for an 8-point skewed x went from interval ends [-2e-5, 4e-5, 1.2e-4, 1.2e-4] to [9.20e-6, 5.44e-5, 1.61e-4, 2.88e-4]. Python matches R main to 1e-15 relative; the last-ulp differences come from np.sum vs R's long-double sum in the prefix powers, which predate this change.

Tests

  • New invariants mirroring R's test-var-root-tolerance.R: the symmetric small-scale median, scale equivariance and ratio inversion over degrees 1 to 5 at scales 1e-4, 1 and 1e4, and the steep 99999-zeros case. R's data comes from set.seed(42), so I drew the same shapes from NumPy; these are property tests. Plus R main's literal roots for degrees 2 and 3 at rtol 1e-13.
  • No cache entry moves. The LPM.VaR and UPM.VaR shards and the ARMA and ANOVA entries that use VaR still pass.
  • Gates on the top of this stack (rasheed/ss-constant-group, which includes this slice), cache only: native 2935 passed, 4 skipped, 3 xfailed, 2 xpassed. With NNS_DISABLE_NATIVE=1: 2913 passed, 29 skipped, 2 xpassed. The 3 xfails are mode: break tied discrete modes by gravity, not their mean (mirrors R #111) #178's strict ones and the 2 xpasses the existing non-strict live-R VAR xfails. tests/parity/test_practical_examples.py against the usual R library: 9 passed, 2 xfailed, as on mode: break tied discrete modes by gravity, not their mean (mirrors R #111) #178. ruff check is clean on every changed file.

Review

GPT-6.1 Sol reviewed the #143, #125 and #123 mirrors together: SHIP.

Mirrors OVVO-Financial/NNS#125

… var-root-tolerance)

Mirrors R LPM.VaR / UPM.VaR (OVVO-Financial/NNS var-root-tolerance). The
integer-degree solver accepted an interval end when the polynomial residual
(1 - p) * LPM - p * UPM was within sqrt(eps). That residual scales with
x^degree, so data of order 1e-3 at degree 4 took the first end it saw:
lpm_var(0.5, 4, [-.003, -.002, -.001, .001, .002, .003]) was -0.001.

The end test now uses the dimensionless ratio gap LPM / (LPM + UPM) - p at
eps^0.75, and the root is solved to rounding (R's zeroin port, the one
_var_uniroot uses, at eps^2 times the interval width) because a tolerance on
t does not bound the ratio where it is steep. Without a sign change, which
only rounding at a break produces, it takes the endpoint with the smaller
gap instead of minimising |f|.

Before -> after (= R main):
  lpm_var(.5, 4, symmetric 1e-3 data)                  -0.001 -> 0
  lpm_var([.01, .05, .5, .95], 3, 1e-4 * x)  [-2e-5, 4e-5, 1.2e-4, 1.2e-4]
                                   -> [9.20e-6, 5.44e-5, 1.61e-4, 2.88e-4]

No cache entry moves: the LPM.VaR and UPM.VaR shards, and the ARMA and ANOVA
entries that use VaR, still match.
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Incorporated through NNS R 14.0 integration #192, merged as bd19c3303222af46da79d8c97f258248eb5674d2. Verified exact head f220c41653a199c0285c21e99bfbd18c4717321c is an ancestor of main; original commits and authorship are preserved. The integration includes the subsequent R14 compatibility corrections and passed the complete GitHub Python 3.11-3.14 matrix. This supersedes the separate landing, not the work or review history.

@github-actions github-actions Bot closed this Nov 7, 2026
@github-actions
github-actions Bot deleted the rasheed/py-var-root-tolerance branch November 7, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant