inst/validation/python/README.md

Independent Python implementation

The scripts in this folder compute the expected values used in tests/testthat. They are written independently of the R code and use different numerical methods:

| Quantity | R code | Python reference | |---|---|---| | State probabilities, forward clock | closed-form transitions and Gauss-Legendre rules | products of matrix exponentials (scipy.linalg.expm) | | State probabilities, reset clock | closed-form transitions and Gauss-Legendre rules | adaptive quadrature (scipy.integrate.quad) over the progression time, transitions by expm | | Expected events, log-rank mean and information | accrual handled through cumulative integrals and Gauss-Legendre rules | adaptive quadrature with the enrolled fraction inside the integrand | | Score covariance under the alternative | per-patient influence functions | formulas of Luo et al. (2019, Section 3.1) as printed, by nested quadrature | | Bounds and stopping probabilities | mvtnorm::pmvnorm() (Miwa algorithm; Genz-Bretz algorithm with a fixed seed for four or more coordinates whose covariance is not Markov) | recursive conditioning with adaptive quadrature; for covariance matrices that are not Markov, exact recursion for a Markov control plus a quasi-Monte Carlo correction (ref_markov.py) | | Moments of the score, the observed variance and the event indicator (studentized covariance, stage 2) | cumulative functions on Gauss-Legendre rules, entry by Gauss-Legendre rules | cumulative functions by an ODE solver (solve_ivp), entry by adaptive quadrature | | Products of the PFS and OS functionals | four terms of counting-process integrals over the PFS and OS times (covariance note, equations (6)-(9)) | Fleischer model: conditioning on the death time; scenarios with switching: Monte Carlo with 10 million patients | | Time derivatives of the score mean, information and events | integrals against the accrual density | central differences of the reference moments | | Rejection probabilities of two endpoints (gs_prob_mtp()) | sums of rectangle probabilities over the outcomes of the first endpoint (Miwa algorithm; Genz-Bretz algorithm with a fixed seed for rectangles over both endpoints with four or more coordinates and for other rectangles of four or more coordinates whose covariance is not Markov) | numerical integration over the analyses of the joint density, with the decisions applied on the sample path (canonical joint distribution); for two looks, the formulas of Glimm et al. (2010) as one-dimensional integrals; Monte Carlo with 2 million trials | | Designs with two endpoints or with looks driven by the events of different endpoints (ms_power(), stage 2) | .look_times(), .design_core2() and .fwer_core() with the functions above | analysis times by root finding on the reference events, moments and covariance from the references above, bounds by recursive conditioning, rejection probabilities as sums over the sample-path outcomes of rectangle probabilities by recursive conditioning with adaptive quadrature (any covariance matrix); Monte Carlo with 4 million trials | | Model options of stage 3 (switch clock, AFT effect of the switch, switch before progression, censoring of PFS only) | closed forms on the pieces of the intensities (.post_general(), .pre_states(), .from_state5()) and Gauss-Legendre rules | adaptive quadrature over the switch, progression and switch-before-progression times (ref_model5.py); products of matrix exponentials for Markov models | | Products of the PFS and OS functionals with a switch before progression or a censoring of PFS only | terms C1 to C4 of the covariance note split by the progression-free state (addendum of the covariance note) | Markov models: state probabilities, generator and transition matrices with nested adaptive quadrature over the PFS and OS times (ref_cross5.py); other models: Monte Carlo with 10 million patients | | Rejection probabilities of two endpoints with recycling of the level (gs_prob_mtp() with upper_recycled, stage 3) | sums of rectangle probabilities over the outcomes at each analysis (.mtp_graph()) | numerical integration over the analyses of the joint density, with the decisions of Algorithm 1 of Maurer and Bretz (2013) applied on the sample path (canonical joint distribution, ref_graph.py); Monte Carlo simulation of the algorithm with 2 million trials | | Number of events and sample size for a target power with recycling of the level or with switching from a calendar time (ms_size(), stage 3) | .solve_power() (doubling or halving, a grid of 8 points and uniroot()) on .design_core2() and .design_core() | one look: moments from the references above (expected_design.joint(), ref_calendar.Design.look()), rejection probabilities of the procedure with recycling as one-dimensional integrals of the bivariate normal density (adaptive quadrature), root finding (brentq) over the time of the look and the closed form of the sample size |

The influence-function derivation used in the R code was checked against the formulas of Luo et al. (2019) in Python (function influence_cov() in ref_logrank.py): for scenario B, the covariance at months 25 and 15 and the variance at month 25 agree to about 1e-14 (results/alt_check.csv).

Files

Running

python3 expected_values.py            # all parts (about 30 minutes)
python3 expected_values.py gs power   # selected parts
python3 expected_zeta.py              # stage 2 moments (about 10 minutes)
python3 expected_mtp.py               # rejection probabilities of two endpoints (about 2 minutes)
python3 expected_design.py            # designs with two endpoints (about 30 minutes)
python3 expected_genz.py              # rectangles over both endpoints (about 2 minutes)
python3 simulate_ms.py                # validation scenarios V6-V8 (about 15 minutes)
python3 simulate_two.py               # PFS and OS together, V3, V4, V6, V7 (about 30 minutes)
python3 danzer_check.py               # estimator of Danzer et al. (2025) (about 2 minutes)
python3 simulate_type1.py             # V2 and V7 under the null hypothesis (about 11 minutes)
python3 simulate_expected.py          # expected values when the trial stops at the bounds, E1-E3 (about 4 minutes)
python3 expected_model5.py            # model options of stage 3 (about 10 minutes)
python3 expected_graph.py             # recycling of the level, stage 3 (about 4 minutes)
python3 expected_calendar.py          # switching from a calendar time, stage 3 (about 4 minutes)
python3 expected_size_stage3.py       # ms_size() with recycling and switching from a calendar time (about 2 minutes)
python3 expected_stage4.py            # expected values and conditional power of version 0.4.0 (about 30 minutes)
python3 expected_stage4.py cond_power # conditional power only (about 25 minutes)

The results in results/ were computed with Python 3.13, numpy 2.5.3 and scipy 1.18.1.



Try the DesignComplexSurvival package in your browser

Any scripts or data that you put into this service are public.

DesignComplexSurvival documentation built on Nov. 23, 2026, 5:07 p.m.