Likelihood integration ====================== Shared objective ---------------- Concrete models inherit likelihood, sampling, and generation utilities from ``BaseModel`` and override them when their objectives differ. The shared ``compute_loglikelihood`` follows Equation 8 of `The Neural Hawkes Process `_. For scored events after the first observed event, the marked log-likelihood has an event term and an integrated total-intensity term: .. math:: \ell = \sum_{i=1}^{n}\log\lambda_{k_i}(t_i\mid\mathcal H_{t_i}) - \int_{t_0}^{t_n}\sum_k\lambda_k(t\mid\mathcal H_t)\,dt. Here the first observed event is indexed by zero. Intensity at a target event is evaluated using its preceding history. The loss minimizes the negative of this expression. ``compute_loglikelihood`` returns event log terms, positive integral terms, and the helper's event count. Aligning histories and targets ------------------------------ The history at each position predicts the next event. Most model paths use ``time_seqs[:, :-1]`` and ``type_seqs[:, :-1]`` as history inputs, with ``time_delta_seqs[:, 1:]`` for the intervals being scored. For four observed events followed by padding, :doc:`/user_manual/data_loading` produces an alignment like this: .. code-block:: text index: 0 1 2 3 4 dtimes: 0 t_1-t_0 t_2-t_1 t_3-t_2 pad types: e_0 e_1 e_2 e_3 pad non_pad_mask: True True True True False For event ``i``, its own delta advances the state to that event; delta ``i + 1`` advances from it to the next target. Slice the target mask in the same way so padding contributes neither an event nor an integral term: .. code-block:: python event_ll, non_event_ll, num_events = self.compute_loglikelihood( lambda_at_event=lambda_at_event, lambdas_loss_samples=lambda_t_sample, time_delta_seq=time_delta_seqs[:, 1:], seq_mask=batch_non_pad_mask[:, 1:], type_seq=type_seqs[:, 1:], ) Integration choices ------------------- In :meth:`~easy_tpp.model.BaseModel.make_dtime_loss_samples`, ``use_mc_samples: true`` draws independent uniform samples in each interval. The integral is the interval length times the sample mean of total intensity. With ``use_mc_samples: false``, fixed, evenly spaced nodes include both endpoints and the shared likelihood applies the trapezoidal rule. ``loss_integral_num_sample_per_step`` sets the number of nodes or draws; use at least two nodes for the trapezoidal rule. The helper adds a numerical epsilon to intensities before taking logs and integrating. FullyNN uses differences of its cumulative-hazard network; IntensityFree evaluates an inter-event-time density and a categorical mark probability. ITHP selects its own integration method. WSMTHP trains with score matching and uses approximate likelihood for evaluation. See the :doc:`model pages ` for these distinctions. Observation window ------------------ Use ``model_config.loglike_window: observation`` when each sequence was observed through a known end time, including a period with no events after its final event. For a final event at :math:`t_n` and an observation end :math:`T`, the log-likelihood includes the additional survival term .. math:: -\int_{t_n}^{T}\sum_k\lambda_k(t\mid\mathcal H_{t_n})\,dt. This term has no event contribution and does not change ``num_events``. Conditioning on the first observed event is preserved: the full integration range becomes the first observed event through :math:`T`, rather than starting at zero. Use this option for complete observation windows; a sequence stopped at an event count does not establish an event-free tail. Sequence records can include an optional scalar ``window_end``, an absolute time in the same units as ``time_since_start``. It must be finite and at least the last event time. The data loader and collator pass it as a tensor of shape ``[batch_size]``; missing values in a mixed batch become NaN. ``data_specs.rescale_time`` divides it by the same ``data_specs.time_scale`` as event times. For example: .. code-block:: json {"dim_process": 2, "time_since_start": [1.0, 2.0], "time_since_last_event": [0.0, 1.0], "type_event": [0, 1], "window_end": 3.5} .. code-block:: yaml model_config: loglike_window: observation use_mc_samples: false loss_integral_num_sample_per_step: 20 The default ``loglike_window: last_event`` preserves the existing objective even when the records contain ``window_end``. In observation mode, absent or NaN ends fall back to the last event for those sequences, with one warning per model instance. An end equal to the last event gives exactly the default loss and consumes no additional random draws. When right truncation removes later events, an explicit ``window_end`` is capped at the last retained event so the removed span is not scored as event-free. Left truncation preserves the original end. Missing ends remain missing under either truncation strategy. Intensity-based models evaluate the final gap with the complete observed history, using the same MC or trapezoid rule and sample count as their inter-event intervals. FullyNN uses :math:`H(T-t_n)-H(0)` for the final gap, including when ``model_specs.subtract_zero_hazard`` is false. IntensityFree uses its mixture's log survival probability, evaluated in log space to preserve extreme-tail penalties. ITHP also preserves its ``model_specs.integration_method: fixed_grid`` rule when selected. WSMTHP adds the final compensator to its evaluation likelihood and training objective; observation mode requires ``model_specs.with_survival: false``. Combining it with the separate survival classifier raises ``NotImplementedError``. Simulation ``to_records`` exports the retained simulation end as ``window_end``. Capped prefixes end at their final event. For plain ``(times, types)`` pairs, the end defaults to the last event; pass ``to_records(..., t_end=T)`` to supply a known observation boundary. Comparing evaluations --------------------- Likelihoods are conditional on the first observed event in these model paths; padding and unscored events must be excluded consistently. The shared helper counts nonzero event log terms, while some models explicitly count valid target events. Match conditioning, time units, integration settings, and event-count conventions before comparing evaluations. The :doc:`leaderboard_schema` describes how to aggregate test metrics over seeds.