## methods.ml_development()


A random forest or gradient boosting fitted to the cells, and each origin's ultimate.


Usage

``` python
methods.ml_development(
    cells,
    *,
    estimator,
    seed=0,
    n_estimators=100,
    max_depth=None,
    min_samples_leaf=None,
    learning_rate=None,
    response="incremental",
    origin="factor",
    calendar="none",
    dev_grain_months=12,
    zero_cells="observed",
    unsupported_factor="raise",
    tail=None
)
```


This is chainladder-python's `DevelopmentML` followed by its `Chainladder`, without pandas or patsy. `cells` is as in [chain_ladder()](methods.chain_ladder.md#ibnr.methods.chain_ladder): CUMULATIVE losses, one row per observed cell, zero or more. The function fits a scikit-learn tree ensemble to one row per cell (its increment by default) with an indicator for each development age and each origin, predicts every cell of the rectangle, and projects each origin's latest cumulative by its own fitted development from its latest age to the last: `ultimate = latest * fitted_cumulative[last age] / fitted_cumulative[latest age]`. It needs scikit-learn, which the `ml` extra installs (`pip install "ibnr[ml]"`); importing `ibnr.methods` does not load it, and the first call does, with the scipy and pandas it loads.

Read these before using the answer:

- It is a point estimate, with no standard error or distribution.
- **Tree models projected this way usually overshoot the chain ladder**, often by a lot, and that comes from the method, not from a defect: the youngest origin has one training row, the trees split it off by its origin indicator, and its predicted increment stays near that one value at every later age. On a 10 x 10 Schedule P paid triangle the forest (seed 42) gives 2.7 times the chain ladder's IBNR and boosting 1.8 times; on MW2014 the forest gives 40 times. It is not a rule: on GenIns the forest gives 0.93 times and boosting 0.87 times.
- A random forest's answer depends on `seed` (from 1.5% to 3.6% of the total, as the standard deviation over 20 seeds on six public triangles). Gradient boosting's does not with these settings: it looks at every feature at every split and uses every row.
- `response="cumulative"` gave a negative total IBNR with the forest on all six test triangles, because the fitted cumulatives fall with age.

Options:

- `estimator`: `"random_forest"` or `"gradient_boosting"`, required (the two differ by 52% on the Schedule P triangle above). For a Tweedie GLM, chainladder-python's third estimator, use [tweedie_glm()](methods.tweedie_glm.md#ibnr.methods.tweedie_glm).
- `seed`: passed to scikit-learn as `random_state`, unchanged, so the same integer gives chainladder-python's numbers. A whole number from 0 to 4294967295; 0 by default. `None` is refused, because a forest with no seed gives a different answer on every call.
- `n_estimators` (100), `max_depth` (`None`: unlimited for the forest, 3 for boosting), `min_samples_leaf` (the forest only; `None` is 1) and `learning_rate` (boosting only; `None` is 0.1): scikit-learn's settings of the same names, the others at scikit-learn's defaults. A setting that does not apply to the estimator is refused rather than ignored.
- `response`: `"incremental"` (the default) fits each cell's increment, the first age's being its cumulative; `"cumulative"` fits the cumulative.
- `origin`: `"factor"` (the default) gives the model an indicator per origin; `"none"` leaves origins out, so every origin shares one fitted pattern.
- `calendar`: `"none"` (the default) or `"trend"`, the calendar period (in development steps) as one numeric column. These two are [tweedie_glm()](methods.tweedie_glm.md#ibnr.methods.tweedie_glm)'s design words; the four designs are chainladder-python's `C(development) + C(origin)` (the default), `C(development)`, `C(development) + valuation` and `C(development) + C(origin) + valuation`.
- `zero_cells`: `"observed"` (the default here, unlike the link-ratio methods) trains on every observed cell, a zero cumulative included. `"missing"` leaves out every cell whose cumulative is zero, as chainladder-python does. The default differs because under `"missing"` an age whose cells are all zero leaves the model nothing to fit there, which happens on 208 of the 722 clrd paid triangles with losses; under `"observed"` it never happens.
- `unsupported_factor`: under `zero_cells="missing"`, what to do at an age whose cells are all zero: `"raise"` (the default) refuses; `"unity"` develops by a factor of 1 into it (a fitted increment of 0). `development.unity_fallback` marks the factor INTO such an age, on the row of the age before it, so an all-zero first age has no row to show on and shows only as `n_trained` 0.
- `dev_grain_months`: as in [chain_ladder()](methods.chain_ladder.md#ibnr.methods.chain_ladder).
- `tail`: not supported yet; anything but `None` is refused. Each origin is projected to the last observed development age.

Returns a [ReserveResult](methods.ReserveResult.md#ibnr.methods.ReserveResult) whose `link_ratios` and `coefficients` are `None`:

- `origins`: `origin`, `origin_period`, `latest_dev_lag`, [latest](kernels.MackFit.md#ibnr.kernels.MackFit.latest), [ultimate](kernels.MackFit.md#ibnr.kernels.MackFit.ultimate) and `ibnr` (the projection above), [fitted_latest](kernels.MLDevelopmentFit.md#ibnr.kernels.MLDevelopmentFit.fitted_latest) and [fitted_ultimate](kernels.MLDevelopmentFit.md#ibnr.kernels.MLDevelopmentFit.fitted_ultimate) (the fitted cumulative at the latest age and at the last), [cdf](kernels.TweedieFit.md#ibnr.kernels.TweedieFit.cdf) (their ratio; 1.0 at the last age) and [model_ibnr](kernels.TweedieFit.md#ibnr.kernels.TweedieFit.model_ibnr) (the sum of the fitted future increments, which differs from `ibnr` wherever the fitted cumulative at the latest age differs from the actual one, for a tree model nearly always). These four are null for an origin the model cannot place: with `origin="factor"` and `zero_cells="missing"`, an origin whose cells are all zero, which has no training row (its ultimate is 0). [cdf](kernels.TweedieFit.md#ibnr.kernels.TweedieFit.cdf) is null too where the fitted cumulative at the latest age is zero or less and the latest is 0.
- `development`: `dev_lag`; `factor`, [cdf](kernels.TweedieFit.md#ibnr.kernels.TweedieFit.cdf) and `pct_reported` when every origin shares one fitted pattern (as with `origin="none"` and `calendar="none"`), null otherwise, when each origin's own are in `cells`; `n_trained` (int64, the training rows at the age); and `unity_fallback` (bool, whether the factor from this age to the next is the 1 of `unsupported_factor="unity"`; null at the last age).
- `cells`: one row per cell of the full rectangle, observed or not: `origin`, `origin_period`, `dev_lag`, `observed` and `trained` (bool), `increment` (null when not observed), `fitted_increment`, `fitted_cumulative`, `factor` (the origin's fitted factor to the next age; null at the last age or where the fitted cumulative is zero or less) and [cdf](kernels.TweedieFit.md#ibnr.kernels.TweedieFit.cdf) (to the last age; null where the fitted cumulative is zero or less). The fitted columns are null for an origin the model cannot place.
- `totals`: [latest](kernels.MackFit.md#ibnr.kernels.MackFit.latest), [ultimate](kernels.MackFit.md#ibnr.kernels.MackFit.ultimate), `ibnr`, [model_ibnr](kernels.TweedieFit.md#ibnr.kernels.TweedieFit.model_ibnr), and the settings that produced the answer: `estimator`, `seed`, `n_estimators`, `max_depth` (null for an unlimited forest), `min_samples_leaf` (null for boosting), `learning_rate` (null for the forest), `response`, `origin_term`, `calendar_term` and `zero_cells` (strings), `n_training_rows` and `scikit_learn_version`, because boosting's last digits changed between scikit-learn 1.6.1 and 1.9.0.

Refused, besides the refusals of [chain_ladder()](methods.chain_ladder.md#ibnr.methods.chain_ladder)'s `cells`: a setting outside its values or one that does not apply to the estimator (`invalid_option`); a triangle whose cells are all zero, as [tweedie_glm()](methods.tweedie_glm.md#ibnr.methods.tweedie_glm) refuses it, and fewer than 2 cells to fit (both `not_identified`); under `zero_cells="missing"` with `unsupported_factor="raise"`, an age whose cells are all zero (`no_link_ratio`); fitted values that are not finite numbers, from amounts near the largest double (`result_not_finite`); a still-developing origin with losses whose fitted cumulative at its latest age is zero or less, so its factor to ultimate is undefined, and an ultimate below zero (both `negative_projection`), in that order; and a tail (`not_supported`). Negative increments are fitted like any others. `docs/coming-from-chainladder.md` lists where the answers differ from chainladder-python's.
