Skip to content

Conversation

@spjuhel
Copy link
Collaborator

@spjuhel spjuhel commented Dec 18, 2025

As the Risk trajectory PR is too substantial, this is a fourth split.

This PR builds on PRs #1197 and #1199, introduces RiskTrajectory, the abstract basis for trajectory objects, and the concrete class StaticRiskTrajectory which aims at facilitating the computation of risk metrics over multiple Snapshot objects.

The code is split between a high level interface StaticRiskTrajectory and a low level computing class CalcRiskMetricsPoint.

RiskTrajectory

The rationale behind this class is to hold the common code for any type of risk trajectory:

  • a collection of Snapshot
  • an optional risk discount rate, and associated methods to compute NPV.
  • (abstract) access to risk metrics

CalcRiskMetricPoint

The rationale behind this class is to handle the computation and "data formatting" of different risk metrics over a collection of Snapshot, following an ImpactComputationStrategy (see PR #1199). In this case, without any interpolation, thus it is essentially a wrapper for multiple calls to ImpactCalc or equivalent in ImpactComputationStrategy.

The class contains:

  • @lazy_property properties, dictating how the foundational elements for risk metric should be computed (see below)
  • calc_<risk_metric> methods (which are the ones called by the StaticRiskTrajectory object): these make use of the above and put them in nice and tidy dataframes.

@lazy_property explained

We want to avoid recomputing when unnecessary, thus we have computed metrics and "impact data" as @lazy_property, which cache the results of the computation. For instance, calling static_traj.per_date_aai the first time computes the different required Impact objects, stores them, and then the .aai_agg for each Impact and also store them.

A subsequent call to .per_date_aai return the previously stored value.
In addition, a subsequent call to .per_date_eai will reuse computed Impact objects.

The @lazy_property decorator implements this memoization flow: it returns the cached _metric attribute if available. If not, it computes the value, stores it in _metric, and returns the result.

Attributes that would change metric results (such as the computation strategy or the return periods to compute) make use of @property to reset the cached data when their value is changed by calling _reset_impact_data.

StaticRiskTrajectory

This class is the interface with the user. It receives the list of snapshot to compute metrics for, and gives access to computed risk metrics and plots. It also handles the computation of net present values if a risk discount rate is provided.
It also uses a memoization flow for the "final" results.

PR Author Checklist

PR Reviewer Checklist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants