symphony_dt.forecast¶
symphony_dt.forecast ¶
Forecasting: the exact route of the study and an experimental fast route.
Exact route. Mask each test participant's observations after an anchor (split_at_anchor), fit the
population model on the masked data, thin the chains (thin_draws) and run the generated-quantities
program on the masked targets (forecast_exact). The forecast is y_rep_c: the observation model
applied to the posterior latent path, whose post-anchor steps carry no data and are therefore draws
from the dynamics.
Fast route (EXPERIMENTAL). Condition a new participant's latent path on each population posterior
draw by importance sampling, with no refit (forecast_fast). It approximates the exact route in two
ways: the population draws do not learn from the new participant, and the participant's posterior
is represented by a finite set of weighted particles. Tests compare it with the exact route on
synthetic data.
HoldoutSplit
dataclass
¶
The fit data (test histories masked after the anchor), the target data and target metadata.
Validation: TESTED.
write ¶
write(directory: str | Path) -> dict[str, Path]
Write stan_data_fit.json, stan_data_targets.json, targets_meta.csv and test_index.json.
Validation: TESTED.
Forecast
dataclass
¶
Forecast draws for a set of targets, with summaries and export.
draws is (draws, targets). targets has one row per target with at least k (endpoint index)
and usually i, t, horizon and y.
Validation: TESTED.
quantiles ¶
quantiles(
q: Sequence[float] = (0.025, 0.25, 0.5, 0.75, 0.975),
) -> pd.DataFrame
Return the targets with one column per requested quantile.
Validation: TESTED.
interval ¶
interval(level: float = 0.95) -> pd.DataFrame
Return the targets with lower and upper of the central interval at level.
Validation: TESTED.
event_probability ¶
event_probability(
last_values: ArrayLike,
thresholds: Mapping[int, tuple[str, float]]
| None = None,
) -> NDArray[np.float64]
Return per-target event probabilities against each target's last observed value.
Targets whose endpoint has no event rule get NaN. Defaults: +5 UPDRS-III, -2 MoCA.
Validation: TESTED.
to_frame ¶
to_frame() -> pd.DataFrame
Return the draws in long format: one row per target and draw.
Validation: TESTED.
to_csv ¶
to_csv(path: str | Path) -> Path
Write the quantile table (not the draws) to CSV.
Validation: TESTED.
to_netcdf ¶
to_netcdf(path: str | Path) -> Path
Write the draws and targets to netCDF via xarray.
Validation: TESTED.
plot ¶
plot(participant: int | None = None, ax: Any = None) -> Any
Plot forecast intervals by horizon; see symphony_dt.plots.plot_forecast.
Validation: TESTED.
split_at_anchor ¶
split_at_anchor(
stan_data: StanData | Mapping[str, Any],
test_index: Sequence[int],
*,
anchor_years: float = 2.0,
zero_dose_std: float = 0.0,
cohort: Sequence[str] | None = None,
sex: Sequence[int] | None = None,
) -> HoldoutSplit
Mask every test participant's observations later than anchor_years after baseline.
The step grid, covariates and every training observation are kept, so the latent path at a
masked node is driven by the dynamics alone. Each masked observation becomes a target. A
UPDRS-III target carries the participant's last kept UPDRS-III LEDD forward, or
zero_dose_std when there is none. The anchor time of a participant is its last kept
observation at or before the anchor on any endpoint, zero when there is none.
This reimplements the study's fit_holdout.split from its written design; the original
exists only on the study's cluster. Validation: TESTED.
thin_draws ¶
thin_draws(
csv_files: Sequence[str | Path],
output: str | Path,
*,
total_keep: int = 400,
) -> Path
Write one CmdStan CSV holding every THIN-th draw of every chain, THIN = draws x chains / keep.
Keeps chain 1's comment header and adaptation block and the column header, which must be
identical across chains. Raises when the kept total differs from total_keep.
Validation: VALIDATED. Port of the study's thin_fold.py.
read_gq_csv ¶
read_gq_csv(
path: str | Path,
n_targets: int,
prefix: str = "y_rep_c",
) -> NDArray[np.float64]
Read the prefix.1 to prefix.n columns of a generate_quantities CSV as (draws, targets).
Raises when the columns do not match the target count and order or are not contiguous.
Validation: VALIDATED. Port of the study's read_gq.
run_standalone_gq ¶
run_standalone_gq(
exe_file: str | Path,
data: str | Path,
fitted_params: str | Path,
output_csv: str | Path,
*,
seed: int,
) -> Path
Run CmdStan's standalone generate_quantities method directly on a fitted-parameters CSV.
The binary is called without cmdstanpy's CSV validation, because a thinned file pooled over chains carries no per-chain timing footer, which CmdStan itself does not need.
Validation: TESTED.
forecast_exact ¶
forecast_exact(
fit: Any,
split: HoldoutSplit,
*,
total_keep: int = 400,
seed: int = 20261021,
output_dir: str | Path | None = None,
model: str = "pd_v2",
) -> Forecast
Forecast the masked targets from a fit of split.fit with the generated-quantities program.
Thins the chains to total_keep draws, runs standalone generate_quantities on the target data
with seed, and returns the y_rep_c draws. fit is a FitResult of split.fit.
Validation: TESTED. The route is the study's design; the GQ program is the package's rewrite.
global_draws_from_csv ¶
global_draws_from_csv(
csv_files: Sequence[str | Path],
n_pathway: int = 0,
max_draws: int | None = None,
) -> list[dict[str, Any]]
Read the global parameters of a pd_v2 fit as a list of parameter dicts, one per draw.
Validation: TESTED.
forecast_fast ¶
forecast_fast(
population: Any,
history: TwinDataset,
horizons_years: Sequence[float],
*,
standardisation: Standardisation | None = None,
config: ModelConfig | None = None,
n_particles: int = 2000,
n_draws: int = 400,
seed: int = 0,
ledd_std: float | None = None,
pathway: Sequence[str] = (),
) -> Forecast
Forecast one new participant from population draws by importance sampling, with no refit.
population is a FitResult, a list of chain CSV paths, or a list of parameter dicts.
history holds one participant. For each population draw, n_particles latent paths are drawn
from the prior, weighted by the likelihood of the history and one is resampled; it is then
propagated horizons_years beyond the last visit with fresh innovations. standardisation must
be the one used for the population fit. LEDD at the horizons is the last visit's value unless
ledd_std is given.
Validation: EXPERIMENTAL. An approximation of the exact route, compared with it in tests.