Skip to content

cdf: hazard and cumulative hazard at a target match the returned curve - #181

Closed
gitRasheed wants to merge 1 commit into
rasheed/py-var-root-tolerancefrom
rasheed/py-cumhaz-target
Closed

gitRasheed wants to merge 1 commit into
rasheed/py-var-root-tolerancefrom
rasheed/py-cumhaz-target

Conversation

@gitRasheed

Copy link
Copy Markdown
Collaborator

R's univariate NNS.CDF computed target.value for "hazard" from the survival at the data point nearest the target, and for "cumulative hazard" by refitting the curve with NNS.reg and reading it at the target. Neither matched the curve it returned. OVVO-Financial/NNS#123 (merged) applies the curve's own transformation to the survival at the target. This slice mirrors it.

Stacked on #180; merge with a merge commit.

Changes

  • _univariate_cdf (cdf.py): hazard is min(max(Point.est / max(1 - F(t), 1e-10), 0), 1e6) and cumulative hazard max(-log(max(1 - F(t), 1e-10)), 0), with F the LPM ratio at the target. target.value now equals the curve at every data point and lies between its neighbours elsewhere, and the hazard is clamped like the curve. The multivariate path already worked this way and is unchanged.
  • Before and after, with after equal to R: nns_cdf(1:4, 1, target=2.5, type="cumulative hazard") 6.17 -> 0.693, and type="hazard" 0.387 -> 0.580.

Tests

  • New invariants mirroring R's test-cdf-target-value.R: target value equals the curve at each data point for all four types and degrees 0 and 1, lies between neighbouring curve values at midpoints, the hazard between points is finite and in [0, 1e6], and the cumulative hazard at 19 and 10 on 1:20 is -log(.05) and -log(.5). R's skewed sample is rlnorm under set.seed(11), so I used a NumPy lognormal sample of the same size rounded the same way. 7 of these fail on the old code.
  • 2 NNS.CDF.custom cache entries predate the R change (the two target values above). I refilled them from an R build of the cache's base plus only WIP: Port the canonical R vignette curriculum to Python #123's R/Partial_Moments.R hunk. The other 37 came back identical, none dropped; docs/parity.md records it.
  • 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#123

…e (R cumhaz-target)

Mirrors R NNS.CDF (OVVO-Financial/NNS cumhaz-target). The univariate
target.value for "hazard" divided the fitted hazard at the target by the
survival at the nearest data point, and "cumulative hazard" refit the
curve with NNS.reg and read it at the target. Both now apply the curve's
own transformation to the survival at the target:
  hazard:            min(max(Point.est / max(1 - F(t), 1e-10), 0), 1e6)
  cumulative hazard: max(-log(max(1 - F(t), 1e-10)), 0)
so target.value equals the curve at every data point, lies between its
neighbours elsewhere, and the hazard is clamped like the curve. The
multivariate path already worked this way.

Before -> after (= R):
  nns_cdf(1:4, 1, target=2.5, type="cumulative hazard")  6.17 -> 0.693
  nns_cdf(1:4, 1, target=2.5, type="hazard")             0.387 -> 0.580

2 NNS.CDF.custom cache entries (those two target values) predate the R
change and were refilled from the cache's R build plus that change alone;
the other 37 came back identical.
@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 b85a3c63bbca75fb2d8e44ac7b07907eea8d7727 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-cumhaz-target 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