Source code for reservingmodels.integrations.actuarialpy

r"""Build reserving inputs from the canonical ``actuarialpy.Experience``.

reservingmodels already requires ``actuarialpy`` (it re-exports the triangle
primitives), so these helpers are always available. They turn a claims-listing
``Experience`` -- one row per claim transaction, carrying an incurred/origin
date and a paid amount -- into a cumulative triangle and, in one step, a fitted
:class:`~reservingmodels.BootstrapODP`.
"""
from __future__ import annotations

import pandas as pd

from actuarialpy import ExperienceSet, resolve_amount

from ..bootstrap import BootstrapODP
from ..core import make_completion_triangle


def _resolve_listing(exp):
    if isinstance(exp, ExperienceSet):
        if len(exp.listings) != 1:
            raise ValueError(
                "pass the listing member explicitly (book['claims']); the set "
                f"has {sorted(exp.listings) or 'no'} named listings"
            )
        (exp,) = exp.listings.values()
    return exp


[docs] def triangle_from_experience( exp, *, origin: str, valuation: str, amount_col: str | None = None, cumulative: bool = True, ) -> pd.DataFrame: """Cumulative development triangle from a claims-listing ``Experience``. ``origin`` is the origin-period column (e.g. accident month) and ``valuation`` the transaction/valuation-period column; the paid amount is the bound expense role (or ``amount_col``). Thin wrapper over ``actuarialpy.make_completion_triangle`` that resolves the amount column from the Experience's roles. """ exp = _resolve_listing(exp) frame, col = resolve_amount(exp, amount_col) return make_completion_triangle( frame, origin_col=origin, valuation_col=valuation, amount_col=col, cumulative=cumulative, )
[docs] def fit_bootstrap_from_experience( exp, *, origin: str, valuation: str, amount_col: str | None = None ) -> BootstrapODP: """Fit :class:`~reservingmodels.BootstrapODP` straight from an Experience.""" triangle = triangle_from_experience( exp, origin=origin, valuation=valuation, amount_col=amount_col ) return BootstrapODP.fit(triangle)