================================================ Thinning Algorithm for Sampling Event Sequences ================================================ EasyTPP's ``EventSampler`` in ``easy_tpp/model/thinning.py`` samples independent next-event times with piecewise-constant thinning. The construction follows Lewis and Shedler (1979) / Ogata (1981): propose events from a dominating rate and accept each proposal with probability equal to the total model intensity divided by that rate. Implementation ============== For every history interval, ``draw_next_time_one_step`` operates on the finite window ``[0, dtime_boundary]``: 1. Evaluate total intensity (summed over event types) at ``G = max(num_samples_boundary, 21)`` grid points, with spacing selected by ``bound_grid`` (``log`` by default since 0.6.0, or ``linear``). Each of the ``G - 1`` segments receives a constant bound equal to its larger endpoint total intensity multiplied by ``over_sample_rate``. Bounds have a positive numerical floor so zero-intensity segments can be traversed. 2. Multiply each bound by its own segment width and accumulate these masses to obtain a piecewise-linear cumulative-bound function. Draw chunks of ``num_exp`` independent Exp(1) increments in cumulative-bound space, accumulate them from the current cursor, and invert that function. A proposal can cross any number of segments in one pass. 3. Evaluate marked intensities only for active, in-window proposals, sum over marks, and accept the first proposal whose uniform draw times its local bound is less than the total intensity. 4. Continue rejected streams from their cumulative-bound cursor. ``num_exp`` controls chunk size; exhausting a chunk does not end sampling. 5. If an evaluated proposal exceeds its segment bound, enlarge the violated segment's bound and restart all ``num_sample`` streams for that history interval, discarding even previously accepted draws from that interval. Other intervals retain their progress. ``patience_counter`` limits these retries per interval; exceeding it raises ``RuntimeError``. 6. If a stream crosses the window end without an accepted event, return the window boundary. This is right censoring, with a point mass equal to the probability of no event before the boundary. Reaching ``max_iterations`` proposal-loop passes per call with unfinished streams raises ``RuntimeError``; its default is 10000. The return value contains ``num_sample`` times per history and equal weights ``1 / num_sample``. Prediction averages these times; marked intensities at the predicted time are normalized over event types for mark prediction. Exactness and approximate grid bounds ===================================== Sampling is exact when every per-segment bound dominates the total intensity throughout its segment (with censoring at the window end). For a monotone or convex total intensity, its maximum on a segment is at an endpoint, so the endpoint construction is valid when ``over_sample_rate >= 1``. For general neural intensities, grid-derived bounds are an approximation. A narrow peak between grid points can be missed. Violation retries correct observed violations, but do not guarantee discovery of an unobserved peak or exact sampling. Raise ``num_samples_boundary`` and/or ``over_sample_rate`` to improve coverage; neither provides a universal guarantee for arbitrary intensities. The model formulas explain when endpoint bounds apply: * In ``easy_tpp/model/rmtpp.py``, ``RMTPP.evolve_and_get_intentsity`` implements Eq. 11 as .. math:: \lambda_k(u \mid h) = \exp\!\left(\min\{a_k(h) + w_k u + b_k,\ \log(10^5)\}\right). Each marked intensity is monotone in elapsed time ``u``, so its maximum is at an endpoint. For a single mark, or time weights all of the same sign, the total is also monotone and the sampler's endpoint bounds are valid, giving exact sampling. Without active clamping the sum of exponentials is convex, which also gives valid endpoint bounds. With mixed-sign weights and active clamping, the total need not be monotone or convex: the maximum of the endpoint totals used by this sampler need not dominate an interior peak. Thus exactness for arbitrary marked RMTPP parameters cannot be inferred solely from each mark's monotonicity. Summing the per-mark endpoint maxima would be a valid alternative bound, but is not the current algorithm. * In ``easy_tpp/model/thp.py``, ``THP.compute_states_at_sample_times`` implements Eq. 6, followed by ``self.softplus`` in ``compute_intensities_at_sample_times``: .. math:: z_k(u \mid h) &= \alpha_k u + [W h]_k + b_k, \\ \lambda_k(u \mid h) &= \operatorname{softplus}_{\beta_k}(z_k(u \mid h)). Softplus of an affine function is convex, and summing over marks preserves convexity, including when slopes have different signs. The total maximum on each segment is therefore at an endpoint. Endpoint bounds with ``over_sample_rate >= 1`` are valid and sampling is exact. Configuration and prediction ============================ With a ``thinning`` block in ``model_config``, intensity-based models use ``BaseModel.predict_one_step_at_every_event`` for next-event prediction. Set ``base_config.stage: gen`` for multi-step generation; ``num_step_gen`` controls its horizon. The same sampler applies at each step. The caller supplies ``dtime_boundary``; ``dtime_max`` supplies the default window for bound computation when no boundary is provided. ``thinning.bound_grid`` accepts ``log`` or ``linear``. Version 0.6.0 defaults to ``log``, concentrating grid points near the event to reduce the proposal mass from a rapidly decaying post-event intensity spike over a long window. For ``u = linspace(0, 1, G)`` and boundary ``T``, its edges are ``T * expm1(6 * u) / expm1(6)``. The shape parameter 6 is fixed; both endpoints remain exactly 0 and ``T``. With ``T = 1000`` and 21 points, the first segment is about 0.87 time units wide, compared with 50 for the linear grid. ``linear`` retains the equal-width grid and seeded sampling behavior from earlier releases. Both grids use each segment's width for cumulative-bound integration and inversion. Log spacing does not eliminate the possibility of missing an interior peak; the same bound-coverage caveats apply. For one-step prediction, windows with a padded current or next event have boundary zero. Prediction groups right-padded sequences by real length so neither bound estimation nor proposal or mark evaluation includes those positions. Padded output positions retain their shape and are ignored by the existing metric masks. Multi-step prediction trims each group to its real history and uses the current anchor's mask; forward generation does not require a real next-event label. .. code-block:: yaml thinning: num_sample: 1 num_exp: 500 over_sample_rate: 5 num_samples_boundary: 21 bound_grid: log dtime_max: 5 patience_counter: 5 max_iterations: 10000 num_step_gen: 5