## reserving.LatestDiagonal


A latest diagonal on its own, one value per origin at its own age, as a


Usage

``` python
reserving.LatestDiagonal()
```


bordereau holds it, in one or several segments; projected with a selected pattern from elsewhere by the [fit_diagonal](reserving.CapeCod.md#prospicio.reserving.CapeCod.fit_diagonal) of [ChainLadder](reserving.ChainLadder.md#prospicio.reserving.ChainLadder), [ExpectedLoss](reserving.ExpectedLoss.md#prospicio.reserving.ExpectedLoss), [BornhuetterFerguson](reserving.BornhuetterFerguson.md#prospicio.reserving.BornhuetterFerguson), [Benktander](reserving.Benktander.md#prospicio.reserving.Benktander) and [CapeCod](reserving.CapeCod.md#prospicio.reserving.CapeCod) (`docs/design/reserving-v02.md`, decisions 15 to 19).

Each origin is given once per segment, at a positive age, with its latest cumulative value (0 for an origin with nothing reported yet). The ages may be off the pattern's grid (18 months against an annual pattern): the fit reads them on the pattern's own ages, `log(cdf)` linear between them. An origin older than the pattern's end is an error unless [past_pattern_end](reserving.LatestDiagonal.md#prospicio.reserving.LatestDiagonal.past_pattern_end) says how to develop it.

Segments are named by key columns, as a [Triangle](reserving.Triangle.md#prospicio.reserving.Triangle)'s are, and sorted by label; within a segment the origins keep the order given. Rows run segment by segment in that order, as [to_frame()](reserving.MackFit.md#prospicio.reserving.MackFit.to_frame) lists them.


## Parameters


`origin: array - like`  
Any date in each row's origin period, or integer years.

`age: array - like`  
Each row's age in months: from the start of the origin period to the end of the valuation month (an accident year 2024 at December 2024 is 12 months).

`latest: array - like`  
Each row's latest cumulative value.

`exposure: array - like`  
Each row's exposure (premium), for the expected-loss methods.

`keys: dict of str to array-like`  
Key columns by name, as [Triangle.from_long](reserving.Triangle.md#prospicio.reserving.Triangle.from_long) takes them.

`origin_grain: (Y, S, Q, M) = ``"Y"`  

`past_pattern_end: (reject, no_development, tail) = ``"reject"`  
How an origin older than the pattern's end (its last age; for age-to-age factors, the end of their span) is developed: `"reject"` is an error that names it; `"no_development"` a factor of 1 there, with no [tail](reserving.Mack.md#prospicio.reserving.Mack.tail) (a cumulative pattern's level at its end is then rescaled to 100% for every origin, so with a level other than 100% the young origins move too); `"tail"` the method's [tail](reserving.Mack.md#prospicio.reserving.Mack.tail) continued from the pattern's end at one step per origin period, a constant ([attachment_age](reserving.TailCurve.md#prospicio.reserving.TailCurve.attachment_age) unset) or one estimated on the pattern's own factors.


## Examples

``` python
>>> import datetime
>>> from prospicio.reserving import BornhuetterFerguson, ChainLadder, LatestDiagonal
>>> d = LatestDiagonal.at_valuation(
...     [2022, 2023, 2024], datetime.date(2024, 12, 31), [900.0, 600.0, 150.0],
...     exposure=[1000.0, 1000.0, 1000.0],
... )
>>> d.ages
```

\[36, 24, 12\]

``` python
>>> cl = ChainLadder(ldf={12: 2.0, 24: 1.25, 36: 1.0}, ldf_kind="cdf")
>>> cl.fit_diagonal(d).ultimate
```

\[900.0, 750.0, 300.0\]

``` python
>>> bf = BornhuetterFerguson(apriori=0.7, ldf={12: 2.0, 24: 1.25, 36: 1.0}, ldf_kind="cdf")
>>> round(bf.fit_diagonal(d).ultimate[2], 9)
```

500.0


## Attributes

| Name | Description |
|----|----|
| [ages](#ages) | Age of each row in months. |
| [exposure](#exposure) | Exposure of each row; `None` when not given. |
| [index](#index) | Label of each segment, as [Triangle.index](reserving.Triangle.md#prospicio.reserving.Triangle.index). |
| [keys](#keys) | Names of the key columns; empty without keys. |
| [latest](#latest) | Latest value of each row. |
| [origin_grain](#origin_grain) | Origin grain: `"Y"`, `"S"`, `"Q"` or `"M"`. |
| [origins](#origins) | Origin period of each row. |
| [past_pattern_end](#past_pattern_end) | The rule for origins older than the pattern's end. |

------------------------------------------------------------------------


#### ages


Age of each row in months.


`ages: list[int]`


------------------------------------------------------------------------


#### exposure


Exposure of each row; `None` when not given.


`exposure: list[float] | None`


------------------------------------------------------------------------


#### index


Label of each segment, as [Triangle.index](reserving.Triangle.md#prospicio.reserving.Triangle.index).


`index: list[Any]`


------------------------------------------------------------------------


#### keys


Names of the key columns; empty without keys.


`keys: list[str]`


------------------------------------------------------------------------


#### latest


Latest value of each row.


`latest: list[float]`


------------------------------------------------------------------------


#### origin_grain


Origin grain: `"Y"`, `"S"`, `"Q"` or `"M"`.


`origin_grain: str`


------------------------------------------------------------------------


#### origins


Origin period of each row.


`origins: list[str]`


------------------------------------------------------------------------


#### past_pattern_end


The rule for origins older than the pattern's end.


`past_pattern_end: str`


## Methods

| Name | Description |
|----|----|
| [at_valuation()](#at_valuation) | A diagonal valued at one month: each origin's age is the months from |
| [from_frame()](#from_frame) | A diagonal from a data frame, one row per segment and origin. |
| [from_triangle()](#from_triangle) | The latest diagonal of a triangle column, in every segment: each |
| [segment()](#segment) | The one segment whose keys have the given values, as |
| [to_frame()](#to_frame) | The rows as a pandas DataFrame: the key columns, then `origin`, |
| [with_exposure()](#with_exposure) | The diagonal with an exposure per row, in the row order of |
| [with_past_pattern_end()](#with_past_pattern_end) | The diagonal with another rule for origins older than the pattern's |

------------------------------------------------------------------------


#### at_valuation()


A diagonal valued at one month: each origin's age is the months from


Usage

``` python
at_valuation(
    origin,
    valuation,
    latest,
    exposure=None,
    keys=None,
    origin_grain="Y",
    past_pattern_end="reject"
)
```


its start to the end of the valuation month (an accident year 2024 at June 2024 is six months).


##### Parameters


`origin: array - like`  
Any date in each row's origin period, or integer years.

`valuation: date`  
Any date in the valuation month (`datetime.date`, a pandas `Timestamp` or a numpy `datetime64`); an integer is an error, since a year would read as January.

`latest: Any`  
As for the constructor.

`exposure: Any`  
As for the constructor.

`keys: Any`  
As for the constructor.

`origin_grain: Any`  
As for the constructor.

`past_pattern_end: Any`  
As for the constructor.


##### Returns


`LatestDiagonal`  


##### Raises


`TypeError`  
If [valuation](reserving.Triangle.md#prospicio.reserving.Triangle.valuation) is not a date.

`ValueError`  
If the valuation is before an origin starts (naming the row), an origin is given twice in a segment, or a column has another length.


------------------------------------------------------------------------


#### from_frame()


A diagonal from a data frame, one row per segment and origin.


Usage

``` python
from_frame(
    data,
    origin,
    latest,
    age=None,
    valuation=None,
    exposure=None,
    keys=None,
    origin_grain="Y",
    past_pattern_end="reject"
)
```


Columns are looked up with `data[name]`, so a pandas or Polars DataFrame works, as does a dict of columns.


##### Parameters


`data: DataFrame or dict`  

`origin: str`  
Name of the origin column (dates or integer years).

`latest: str`  
Name of the latest-value column.

`age: str = None`  
Name of the age column, in months.

`valuation: date or str = None`  
The valuation month of every row (a date), or the name of a column of each row's valuation date. Give exactly one of `age` and [valuation](reserving.Triangle.md#prospicio.reserving.Triangle.valuation).

`exposure: str = None`  
Name of the exposure column.

`keys: str or list of str = None`  
Names of the key columns.

`origin_grain: str = ``"Y"`  
As for the constructor.

`past_pattern_end: str = ``"Y"`  
As for the constructor.


##### Returns


`LatestDiagonal`  


##### Examples

``` python
>>> import datetime
>>> from prospicio.reserving import LatestDiagonal
>>> df = {
...     "lob": ["b", "a", "a"],
...     "year": [2024, 2023, 2024],
...     "paid": [50.0, 400.0, 150.0],
... }
>>> d = LatestDiagonal.from_frame(
...     df, "year", "paid", valuation=datetime.date(2024, 12, 31), keys="lob"
... )
>>> d.index, d.ages
```

(\['a', 'b'\], \[24, 12, 12\])

------------------------------------------------------------------------


#### from_triangle()


The latest diagonal of a triangle column, in every segment: each


Usage

``` python
from_triangle(triangle, column, exposure=None, past_pattern_end="reject")
```


origin at the age of its last observed cell, with its cumulative value there, as chainladder-python's [latest_diagonal](reserving.Triangle.md#prospicio.reserving.Triangle.latest_diagonal). With [exposure](reserving.ClarkFit.md#prospicio.reserving.ClarkFit.exposure), each origin also takes that column's last observed value. This is how a fit is applied to another book: `source.to_chain_ladder().fit_diagonal(LatestDiagonal.from_triangle(book, "paid"))` is chainladder-python's `fit(source).predict(book)`.


##### Parameters


`triangle: Triangle`  
With any number of segments.

`column: str`  

`exposure: str = None`  

`past_pattern_end: (reject, no_development, tail) = ``"reject"`  


##### Returns


`LatestDiagonal`  


##### Raises


`ValueError`  
If a column is unknown, an origin has no observed cell, or an exposure is not finite and positive; with keys, the message names the segment.


------------------------------------------------------------------------


#### segment()


The one segment whose keys have the given values, as


Usage

``` python
segment(**keys)
```


[ChainLadderFit.segment](reserving.ChainLadderFit.md#prospicio.reserving.ChainLadderFit.segment).


------------------------------------------------------------------------


#### to_frame()


The rows as a pandas DataFrame: the key columns, then `origin`,


Usage

``` python
to_frame()
```


`age`, [latest](reserving.MackFit.md#prospicio.reserving.MackFit.latest) and, when given, [exposure](reserving.ClarkFit.md#prospicio.reserving.ClarkFit.exposure).


------------------------------------------------------------------------


#### with_exposure()


The diagonal with an exposure per row, in the row order of


Usage

``` python
with_exposure(exposure)
```


[to_frame()](reserving.MackFit.md#prospicio.reserving.MackFit.to_frame): the segments in order and, within one, its origins in the order given.


------------------------------------------------------------------------


#### with_past_pattern_end()


The diagonal with another rule for origins older than the pattern's


Usage

``` python
with_past_pattern_end(rule)
```


end: `"reject"`, `"no_development"` or `"tail"`.
