Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ chainladder-python/
│ │ ├── sparse.py # COO sparse array wrapper
│ │ ├── cupy.py # CuPy GPU array shim
│ │ ├── dask.py # Dask parallel shim
│ │ ├── utility_functions.py # num_to_nan, set_common_backend, etc.
│ │ ├── utility_functions.py # num_to_nan, set_common_backend, RandomStateMixin, etc.
│ │ ├── weighted_regression.py # WeightedRegression helper
│ │ ├── triangle_weight.py # Triangle weighting helpers
│ │ ├── data/ # Bundled sample datasets (CSV)
Expand Down Expand Up @@ -229,7 +229,7 @@ sklearn.BaseEstimator
├── CaseOutstanding development/outstanding.py
├── TweedieGLM development/glm.py
│ └── BarnettZehnwirth development/barnzehn.py
├── BootstrapODPSample adjustments/bootstrap.py
├── BootstrapODPSample adjustments/bootstrap.py (also extends RandomStateMixin)
├── DisposalRate adjustments/disposal.py (also extends DisposalMixin)
│
└── TailBase tails/base.py — appends tail column, extends DevelopmentBase
Expand All @@ -247,7 +247,7 @@ sklearn.BaseEstimator
├── Chainladder methods/chainladder.py
│ └── MackChainladder methods/mack.py
│
└── Benktander methods/benktander.py
└── Benktander methods/benktander.py (also extends RandomStateMixin)
├── BornhuetterFerguson methods/bornferg.py
├── CapeCod methods/capecod.py
└── ExpectedLoss methods/expectedloss.py
Expand Down
32 changes: 24 additions & 8 deletions chainladder/adjustments/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

from chainladder.methods.chainladder import Chainladder
from chainladder.development import DevelopmentBase, Development
from chainladder.utils.utility_functions import RandomStateMixin
import numpy as np
import pandas as pd
from warnings import warn
import types


class BootstrapODPSample(DevelopmentBase):
class BootstrapODPSample(DevelopmentBase, RandomStateMixin):
"""
Class to generate bootstrap samples of triangles. Currently this Only
supports 'single' triangles (single index and single column).
Expand Down Expand Up @@ -38,15 +39,24 @@ class BootstrapODPSample(DevelopmentBase):
random_state: int, RandomState instance or None, optional (default=None)
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used
by np.random.
If None, ``fit`` draws an integer seed from the RandomState instance
used by np.random and keeps it as ``random_state_``. With several
columns, each column then draws its own seed, so the columns' draws
stay independent.

Attributes
----------
resampled_triangles_: Triangle
A set of triangles represented by each simulation
scale_:
The scale parameter to be used in generating process risk
random_state_: int or RandomState instance
The seed the draws used: ``random_state`` when it is an integer or a
RandomState instance, otherwise the integer ``fit`` drew. Passing it
back as ``random_state`` repeats the run, except on a triangle with
several columns fitted with ``random_state=None``: each column drew its
own seed and ``random_state_`` seeds only the process-variance draws,
so calling ``np.random.seed`` before ``fit`` repeats that run instead.

See Also
--------
Expand Down Expand Up @@ -165,11 +175,17 @@ def __init__(
self.random_state = random_state

def fit(self, X, y=None, sample_weight=None):
self._record_random_state()
if X.shape[1] > 1:
from chainladder.utils.utility_functions import concat

# With random_state=None each column draws its own seed, so the
# columns' residual draws are independent. An integer or a
# RandomState instance is passed to every column.
column_state = None if self.random_state is None else self.random_state_
params = {**self.get_params(), "random_state": column_state}
out = [
BootstrapODPSample(**self.get_params()).fit(X.iloc[:, i])
BootstrapODPSample(**params).fit(X.iloc[:, i])
for i in range(X.shape[1])
]
xp = X.get_array_module(out[0].design_matrix_)
Expand Down Expand Up @@ -257,7 +273,7 @@ def _get_simulation(self, X, exp_incr_triangle):
# Suggestions from Using the ODP Bootstrap Model: A Practitioners Guide
adj_resid_dist = adj_resid_dist[adj_resid_dist != 0]
adj_resid_dist = adj_resid_dist - xp.mean(adj_resid_dist)
random_state = xp.random.RandomState(self.random_state)
random_state = self._get_random_state(xp)
resampled_residual = [
(
random_state.choice(
Expand Down Expand Up @@ -360,7 +376,7 @@ def transform(self, X):
X_new = X.copy()
X_new = self.resampled_triangles_
X_new.scale_ = self.scale_
X_new.random_state = self.random_state
X_new.random_state = self.random_state_
X_new._get_process_variance = types.MethodType(_get_process_variance, X_new)
return X_new

Expand All @@ -371,8 +387,8 @@ def _get_process_variance(self, full_triangle):
# process_triangle = np.nan_to_num(np.array([random_state.poisson(lam=abs(item))*np.sign(np.nan_to_num(item))for item in sim_exp_incr_triangle]))
xp = full_triangle.get_array_module()
lower_tri = full_triangle.cum_to_incr() - self.cum_to_incr()
random_state = xp.random.RandomState(
None if not self.random_state else self.random_state + 1
random_state = RandomStateMixin._check_random_state(
RandomStateMixin._process_variance_seed(self.random_state), xp
)
lower_tri.values = random_state.gamma(
shape=abs(lower_tri.values) / self.scale_, scale=self.scale_
Expand Down
144 changes: 144 additions & 0 deletions chainladder/adjustments/tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,150 @@ def test_hat_adjustment_is_zero_at_corners(genins):
)


def _bootstrap_run(tri, random_state):
"""Return the resampled values and the Chainladder ultimates for one run."""
sims = cl.BootstrapODPSample(n_sims=20, random_state=random_state).fit_transform(
tri
)
return sims.values, cl.Chainladder().fit(sims).ultimate_.values


def _dense(values):
"""Return ``values`` as a numpy array, densifying a sparse array."""
return np.asarray(values.todense() if hasattr(values, "todense") else values)


def _assert_runs_differ(first, second):
"""Check that two runs differ in both the resampled values and the ultimates."""
for a, b in zip(first, second):
assert not np.array_equal(_dense(a), _dense(b), equal_nan=True)


def test_random_state_zero_reproduces_ultimates(raa):
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this and the next two tests, do we have and/or need the converse test, i.e. different local/global seeds lead to different results?

Check that ``random_state=0`` reproduces the process-variance draws.

Seed 0 used to give the process-variance step an unseeded generator, so
the resampled triangles repeated and the ultimates did not (#1568).
"""
first, second = _bootstrap_run(raa, 0), _bootstrap_run(raa, 0)
np.testing.assert_array_equal(first[0], second[0])
np.testing.assert_array_equal(first[1], second[1])


def test_random_state_none_uses_global_generator(raa):
"""
Check that ``random_state=None`` draws from the global ``np.random``
generator, as the docstring says, so ``np.random.seed`` repeats a run.
"""
np.random.seed(1)
first = _bootstrap_run(raa, None)
np.random.seed(1)
second = _bootstrap_run(raa, None)
np.testing.assert_array_equal(first[0], second[0])
np.testing.assert_array_equal(first[1], second[1])


def test_random_state_accepts_random_state_instance(raa):
"""
Check that a ``RandomState`` instance is used as the generator.

The resampled triangles match those from the integer seed the instance
was built with.
"""
from_instance = _bootstrap_run(raa, np.random.RandomState(42))
np.testing.assert_array_equal(
from_instance[0], _bootstrap_run(raa, np.random.RandomState(42))[0]
)
np.testing.assert_array_equal(from_instance[0], _bootstrap_run(raa, 42)[0])


def test_different_seeds_give_different_draws(raa):
"""
Check the converse of the three tests above. Different integer seeds,
different global ``np.random`` seeds with ``random_state=None``, and
``RandomState`` instances built from different seeds give different
resampled triangles and ultimates.
"""
_assert_runs_differ(_bootstrap_run(raa, 0), _bootstrap_run(raa, 1))
np.random.seed(1)
first = _bootstrap_run(raa, None)
np.random.seed(2)
_assert_runs_differ(first, _bootstrap_run(raa, None))
_assert_runs_differ(
_bootstrap_run(raa, np.random.RandomState(42)),
_bootstrap_run(raa, np.random.RandomState(43)),
)


def test_random_state_none_records_the_seed(raa):
"""
Check that ``fit`` records the integer it draws for ``random_state=None``
as ``random_state_``, and that passing it back repeats the run (#1568).
"""
model = cl.BootstrapODPSample(n_sims=20).fit(raa)
seed = model.random_state_
assert isinstance(seed, int)
sims = model.transform(raa)
assert sims.random_state == seed
ultimate = cl.Chainladder().fit(sims).ultimate_.values
repeat = _bootstrap_run(raa, seed)
np.testing.assert_array_equal(sims.values, repeat[0])
np.testing.assert_array_equal(ultimate, repeat[1])


def test_random_state_given_is_recorded(raa):
"""Check that an integer or an instance passed in is kept as ``random_state_``."""
assert cl.BootstrapODPSample(n_sims=5, random_state=42).fit(raa).random_state_ == 42
generator = np.random.RandomState(42)
model = cl.BootstrapODPSample(n_sims=5, random_state=generator).fit(raa)
assert model.random_state_ is generator


def test_random_state_none_independent_columns(raa):
"""
Check that with several columns and ``random_state=None`` each column draws
its own seed, so two identical columns resample differently, that
``np.random.seed`` repeats the run, and that an integer seed is still shared
by every column.
"""
tri = raa.copy()
tri["copy"] = tri["values"]

def resampled(random_state=None):
model = cl.BootstrapODPSample(n_sims=5, random_state=random_state).fit(tri)
return _dense(model.resampled_triangles_.values)

np.random.seed(3)
first = resampled()
assert not np.allclose(np.nan_to_num(first[:, 0]), np.nan_to_num(first[:, 1]))
np.random.seed(3)
np.testing.assert_array_equal(first, resampled())
seeded = resampled(7)
np.testing.assert_array_equal(seeded[:, 0], seeded[:, 1])


def test_check_random_state_on_a_module_with_its_own_generator():
"""
Check that ``RandomStateMixin._check_random_state`` builds a generator
from the array module's own ``random.RandomState`` when it has one, as
cupy does, and returns an instance of that class unchanged. A stub module
stands in for cupy, which the test suite does not install.
"""
from types import SimpleNamespace

from chainladder.utils.utility_functions import RandomStateMixin

class StubRandomState:
def __init__(self, seed=None):
self.seed = seed

xp = SimpleNamespace(random=SimpleNamespace(RandomState=StubRandomState))
built = RandomStateMixin._check_random_state(42, xp)
assert isinstance(built, StubRandomState) and built.seed == 42
assert RandomStateMixin._check_random_state(built, xp) is built


def test_hat_matrix_fallback_leaves_hat_adj_unchanged():
# The hat matrix of this quarterly triangle cannot be computed (#33), so
# fit falls back to residuals without the hat matrix adjustment.
Expand Down
39 changes: 31 additions & 8 deletions chainladder/methods/benktander.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from chainladder.methods import MethodBase
from chainladder.utils.utility_functions import RandomStateMixin
import numpy as np


class Benktander(MethodBase):
class Benktander(MethodBase, RandomStateMixin):
"""
The Benktander (or iterated Bornhuetter-Ferguson) IBNR model

Expand All @@ -28,15 +29,24 @@ class Benktander(MethodBase):
using as a deterministic method.
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used
by np.random.
If None, ``fit`` draws an integer seed from the RandomState instance
used by np.random and keeps it as ``random_state_``, and each
``predict`` draws a new seed the same way, so its aprioris differ from
the fit's. Pass an integer to get the same aprioris from ``fit`` and
every ``predict``. A RandomState instance keeps advancing, so each
``predict`` continues its sequence.

Attributes
----------
ultimate_: Triangle
The ultimate losses per the method
ibnr_: Triangle
The IBNR per the method
random_state_: int, RandomState instance or None
The seed the apriori draws used: ``random_state`` when it is an integer
or a RandomState instance, otherwise the integer ``fit`` drew. Passing
it back as ``random_state`` repeats the draws. It is ``random_state``
itself when ``apriori_sigma`` is 0, since no draw is made.

See Also
--------
Expand Down Expand Up @@ -196,8 +206,12 @@ def fit(self, X, y=None, sample_weight=None):
"""
if sample_weight is None:
raise ValueError("sample_weight is required.")

super().fit(X, y, sample_weight)
self.expectation_ = self._get_benktander_aprioris(X, sample_weight)
self._record_random_state(draws=self.apriori_sigma != 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the relationship between apriori_sigma, random_state and the redraw parameter seems needlessly complicated. do we really need that redraw parameter? is self.random_state is None not identical to redraw in all cases?

self.expectation_ = self._get_benktander_aprioris(
X, sample_weight, self.random_state_
)
self.ultimate_ = self._get_ultimate(self.X_, self.expectation_)
self.process_variance_ = self._include_process_variance()
return self
Expand Down Expand Up @@ -253,17 +267,26 @@ def predict(self, X, sample_weight=None):
if sample_weight is None:
raise ValueError("sample_weight is required.")
X_new = super().predict(X, sample_weight)
X_new.expectation_ = self._get_benktander_aprioris(X, sample_weight)
X_new.expectation_ = self._get_benktander_aprioris(
X, sample_weight, self.random_state
)
X_new.ultimate_ = self._get_ultimate(X_new, X_new.expectation_)
X_new.n_iters = self.n_iters
X_new.apriori = self.apriori
return X_new

def _get_benktander_aprioris(self, X, sample_weight):
"""Private method to establish Benktander Apriori"""
def _get_benktander_aprioris(self, X, sample_weight, seed):
"""
Private method to establish Benktander Apriori

``fit`` passes the seed it recorded as ``random_state_``, so passing
that seed back as ``random_state`` repeats the fit's aprioris.
``predict`` passes ``random_state``, so with ``random_state=None`` it
draws new aprioris instead of repeating the fit's.
"""
xp = X.get_array_module()
if self.apriori_sigma != 0:
random_state = xp.random.RandomState(self.random_state)
random_state = self._check_random_state(seed, xp)
# Draw from lognormal with E[apriori] = self.apriori and SD = self.apriori_sigma.
cov = self.apriori_sigma / self.apriori
sigma_log = np.sqrt(np.log1p(cov**2))
Expand Down
13 changes: 11 additions & 2 deletions chainladder/methods/bornferg.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,24 @@ class BornhuetterFerguson(Benktander):
using as a deterministic method.
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used
by np.random.
If None, ``fit`` draws an integer seed from the RandomState instance
used by np.random and keeps it as ``random_state_``, and each
``predict`` draws a new seed the same way, so its aprioris differ from
the fit's. Pass an integer to get the same aprioris from ``fit`` and
every ``predict``. A RandomState instance keeps advancing, so each
``predict`` continues its sequence.

Attributes
----------
ultimate_: Triangle
The ultimate losses per the method
ibnr_: Triangle
The IBNR per the method
random_state_: int, RandomState instance or None
The seed the apriori draws used: ``random_state`` when it is an integer
or a RandomState instance, otherwise the integer ``fit`` drew. Passing
it back as ``random_state`` repeats the draws. It is ``random_state``
itself when ``apriori_sigma`` is 0, since no draw is made.

See Also
--------
Expand Down
Loading