Prediction and generation#
Next-event prediction#
Evaluation computes predictions when a configured metric requires them.
Intensity-based models default to Thinning Algorithm for Sampling Event Sequences to sample
the next inter-event time and derive a mark prediction. IntensityFree samples
its inter-event-time distribution directly for one-step prediction. Enable
rmse and acc in trainer_config.metrics; see Evaluate a Model
for checkpoint and test-split loading.
Choosing the one-step predictor#
Set model_config.thinning.one_step_predictor to thinning (the default)
or quadrature. Quadrature evaluates intensities on a deterministic time
grid and integrates the survival function, without drawing random samples:
model_config:
thinning:
one_step_predictor: quadrature
quadrature_points: 128
predict_median: false
dtime_max: 5
Both paths use the same per-position window
W = max(previous_interval * dtime_max, previous_interval + dtime_max).
The time estimand is the mean of min(next_interval, W), including the
probability of surviving to the window end. It is not the mean conditional
on observing an event inside the window, nor the unrestricted waiting-time
mean. Comparisons with labels beyond the window therefore retain truncation
effects under either predictor.
Quadrature uses quadrature_points positive grid points (default 128,
minimum 2) plus zero. The grid follows the thinning log-grid transform
W * expm1(6*u) / expm1(6) for equally spaced u in [0, 1],
including both endpoints, regardless of thinning.bound_grid. Cumulative
trapezoids integrate the total intensity to obtain the cumulative hazard
H(t); survival is S(t) = exp(-H(t)). A second trapezoid integration
computes integral_0^W S(t) dt for the mean. Padded prediction positions
are zero and never reach the intensity API.
Thinning estimates this same time mean with num_sample random draws.
With sufficiently fine quadrature and valid thinning bounds, differences
in time means reflect Monte Carlo noise. Finite quadrature also introduces
integration error: increase quadrature_points to check convergence,
especially for rapidly changing intensities or long windows. Quadrature
evaluates quadrature_points + 1 times per real history position in chunks
of at most min(num_exp, 64); its cost is fixed by the grid rather than
the number of accepted proposals. More grid points increase computation;
chunking bounds intensity-query memory. Use evaluation mode for deterministic
model intensities, as the runner does.
For type prediction, quadrature integrates each marked density
lambda_k(t) * S(t) and normalises by 1 - S(W). acc uses the type
with the largest resulting probability, conditional on an event before
W. If the event probability is at most machine epsilon, it uses the
largest intensity at W. The default thinning path also assigns types to
draws censored at W; consequently type predictions can differ when the
survival probability at the window end is appreciable.
The model method normally returns (mean, type). With
thinning.predict_median: true, quadrature additionally returns
(mean, type, median). The median is the first survival crossing of 0.5,
interpolated in cumulative hazard within its grid segment, or W when
there is no crossing. rmse always uses the first output, the truncated
mean, which minimises squared error for the truncated waiting time. The
median minimises absolute error for that waiting time. Custom metrics receive
the extra median as predictions[2]; both time outputs are restored to
the original time units by the runner. For example, register a median-based
MAE and include median_mae in trainer_config.metrics:
import numpy as np
from easy_tpp.utils import MetricsHelper
@MetricsHelper.register("median_mae", MetricsHelper.MINIMIZE)
def median_mae(predictions, labels, **kwargs):
errors = np.abs(predictions[2] - labels[0])
return errors[kwargs["seq_mask"]].mean()
Custom time metrics using predictions[0] continue to score the mean.
predict_median only adds an output with quadrature. IntensityFree keeps
its own distribution-based one-step predictor and ignores these quadrature
options. Recursive multi-step generation continues to use thinning for all
models, including when one-step quadrature is selected.
Recursive generation#
Use stage="gen" and supply a compatible checkpoint. Keep its sidecar
files so saved time scales and IntensityFree statistics can be restored.
Generation recursively appends sampled events to the observed history:
from easy_tpp.config_factory import Config
from easy_tpp.runner import Runner
config = Config.preset(
"NHP", "taxi", stage="gen",
overrides={"model_config": {
"pretrained_model_dir": "./checkpoints/<run-id>/models/saved_model",
"thinning": {"num_step_gen": 5},
}},
)
Runner.build_from_config(config).run()
The standard runner writes the generated output to pred.pkl in the
working directory.
thinning.num_step_gen sets the horizon; generated arrays have shape
[batch, num_step_gen] and exclude the last observed event. Generation
uses evaluation mode without parameter gradients. The sampler has a finite
window and can return its boundary when no event is accepted; consult the
thinning page for censoring and grid-bound limitations. IntensityFree uses
its distribution-derived hazard for recursive thinning.
See Colab notebooks for a walkthrough of multi-step generation and Simulation for known-process simulation with analytic likelihoods.