Skip to content
Merged
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
18 changes: 9 additions & 9 deletions chainladder/utils/tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,16 @@
est = cl.Chainladder().fit(dev.transform(prism["Incurred"]))
lhs = cl.model_diagnostics(est, groupby=['Line'])
rhs = cl.model_diagnostics(cl.Chainladder().fit(dev.transform(prism["Incurred"].groupby('Line').sum())))
assert np.allclose(lhs['Ultimate'].values,rhs['Ultimate'].values,atol=atol, equal_nan=True)
assert np.allclose(np.nan_to_num(lhs['IBNR'].values),np.nan_to_num(rhs['IBNR'].values),atol=atol, equal_nan=True)
assert np.allclose(lhs['Ultimate'].values, rhs['Ultimate'].values,atol=atol, equal_nan=True)

Check failure on line 192 in chainladder/utils/tests/test_utilities.py

View workflow job for this annotation

GitHub Actions / ruff check

ruff (missing-whitespace)

chainladder/utils/tests/test_utilities.py:192:70: missing-whitespace: Missing whitespace after `,` help: Add missing whitespace
assert np.allclose(np.nan_to_num(lhs['IBNR'].values), np.nan_to_num(rhs['IBNR'].values),atol=atol, equal_nan=True)

Check failure on line 193 in chainladder/utils/tests/test_utilities.py

View workflow job for this annotation

GitHub Actions / ruff check

ruff (missing-whitespace)

chainladder/utils/tests/test_utilities.py:193:92: missing-whitespace: Missing whitespace after `,` help: Add missing whitespace


def test_concat_immutability(raa):
u = cl.Chainladder().fit(raa).ultimate_
l = raa.latest_diagonal
u.columns = l.columns
latest = raa.latest_diagonal
u.columns = latest.columns
u_new = copy.deepcopy(u)
cl.concat((l, u), axis=3)
cl.concat((latest, u), axis=3)
assert u == u_new


Expand Down Expand Up @@ -680,7 +680,7 @@
"""
options = cl.Options()
assert options.ARRAY_BACKEND == "numpy"
assert options.AUTO_SPARSE == True
assert options.AUTO_SPARSE
assert options.ARRAY_PRIORITY == ["dask", "sparse", "cupy", "numpy"]
assert isinstance(options.ULT_VAL, str)

Expand Down Expand Up @@ -1271,6 +1271,6 @@
Testing new path that allows weights on full triangles
'''
ult = cl.Chainladder().fit(raa)
tw = cl.TriangleWeight(n_periods = 4).fit(raa)
tw_full = cl.TriangleWeight(n_periods = 4).fit(ult.full_triangle_)
assert tw.w_.iloc[:,:,:,0] == tw_full.w_.iloc[:,:,:,0]
tw = cl.TriangleWeight(n_periods=4).fit(raa)
tw_full = cl.TriangleWeight(n_periods=4).fit(ult.full_triangle_)
assert tw.w_.iloc[:, :, :, 0] == tw_full.w_.iloc[:, :, :, 0]
25 changes: 12 additions & 13 deletions chainladder/utils/triangle_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import numpy as np
import pandas as pd
from chainladder.utils.sparse import sp
from sklearn.base import BaseEstimator, TransformerMixin
import warnings

Expand All @@ -19,7 +18,7 @@
from chainladder.core.typing import TriangleProtocol


class TriangleWeight(BaseEstimator,TransformerMixin):
class TriangleWeight(BaseEstimator, TransformerMixin):
"""
Helper class that produces a triangle of weights based on pattern selections

Expand Down Expand Up @@ -123,7 +122,7 @@ def fit(self, X: TriangleProtocol, y: None = None, sample_weight: None = None):
# in practie, there is no realistic use case for a sparse triangle to need weights
backend = "numpy" if X.array_backend in ["sparse", "numpy"] else "cupy"
obj = X.set_backend(backend)
self.w_ = self._set_weight_func(X=obj,secondary_rank=sample_weight)
self.w_ = self._set_weight_func(X=obj, secondary_rank=sample_weight)
return self

def transform(self, X: TriangleProtocol) -> Triangle:
Expand All @@ -146,8 +145,8 @@ def transform(self, X: TriangleProtocol) -> Triangle:

def _cascade_param(
self,
size:int,
param: bool | int | float | str | None | list[bool|int|float|str|None],
size: int,
param: bool | int | float | str | None | list[bool | int | float | str | None],
default_param: bool | int | float | str | None
) -> np.ndarray:
"""
Expand Down Expand Up @@ -237,7 +236,7 @@ def _assign_n_periods_weight_func(self, X: TriangleProtocol) -> np.ndarray:
dev_len = X.shape[3]
n_periods_param = self._cascade_param(dev_len, self.n_periods, -1)

#helper function that generates the weights for individual n_periods
# helper function that generates the weights for individual n_periods
def _assign_n_periods_weight_int(X, n_periods):
xp = X.get_array_module()
val_offset = {
Expand All @@ -250,10 +249,10 @@ def _assign_n_periods_weight_int(X, n_periods):
return X.values * 0 + 1
else:
z = -n_periods * val_offset[X.development_grain][X.origin_grain]
#adding new path to handle full triangle (e.g. full_triangle_, ultimate_, et.c)
# adding new path to handle full triangle (e.g. full_triangle_, ultimate_, et.c)
if X.is_full:
w = X.copy()
w.values[:,:,:-n_periods,:] = np.nan
w.values[:, :, :-n_periods, :] = np.nan
else:
val_date_min = X.valuation[X.valuation <= X.valuation_date]
val_date_min = val_date_min.drop_duplicates().sort_values()
Expand Down Expand Up @@ -346,8 +345,8 @@ def _drop_n_func(

# getting weights that are within the max and min ranks
w = (
X_ranks < max_rank[:,:,None,:]
) & (X_ranks > min_rank[:,:,None,:] - 1)
X_ranks < max_rank[:, :, None, :]
) & (X_ranks > min_rank[:, :, None, :] - 1)

# NOTE: The "Some exclusions have been ignored..." UserWarning below is
# asserted by the test suite (see chainladder/development/tests/
Expand Down Expand Up @@ -484,8 +483,8 @@ def _drop_x_func(self, X: TriangleProtocol) -> np.ndarray:
w = ~np.isnan(X_val)

# weights without considering preserve
index_array_weights = (X_val < drop_above_array[:,:,None,:]) & (
X_val > drop_below_array[:,:,None,:]
index_array_weights = (X_val < drop_above_array[:, :, None, :]) & (
X_val > drop_below_array[:, :, None, :]
)

# counting remaining factors
Expand All @@ -494,7 +493,7 @@ def _drop_x_func(self, X: TriangleProtocol) -> np.ndarray:
# applying preserve
warning_flag = np.any(valid_count < preserve_array)
w = np.where(
valid_count[:,:,None,:] < preserve_array[:,:,None,:], w, index_array_weights
valid_count[:, :, None, :] < preserve_array[:, :, None, :], w, index_array_weights
)

# NOTE: The "Some exclusions have been ignored..." UserWarning below is
Expand Down
6 changes: 3 additions & 3 deletions chainladder/utils/weighted_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def infer_x_w(self):

def fit(
self,
X:BackendArray,
y:BackendArray|None=None,
sample_weight:BackendArray|None=None,
X: BackendArray,
y: BackendArray | None = None,
sample_weight: BackendArray | None = None,
average: Literal["volume", "simple", "regression", "geometric"] | None = None
):
"""
Expand Down
Loading
Loading