diff --git a/chainladder/adjustments/bootstrap.py b/chainladder/adjustments/bootstrap.py index f7d985488..cd6f6791c 100644 --- a/chainladder/adjustments/bootstrap.py +++ b/chainladder/adjustments/bootstrap.py @@ -191,7 +191,7 @@ def fit(self, X, y=None, sample_weight=None): xp = X.get_array_module() if len(X) != 1: raise ValueError("Only single index triangles are supported") - if type(X.ddims) is not np.ndarray: + if X.is_val_tri: raise ValueError("Triangle must be expressed with development lags") obj = Development( n_periods=self.n_periods, diff --git a/chainladder/adjustments/tests/test_bootstrap.py b/chainladder/adjustments/tests/test_bootstrap.py index 7fee0daf8..f5b7d9382 100644 --- a/chainladder/adjustments/tests/test_bootstrap.py +++ b/chainladder/adjustments/tests/test_bootstrap.py @@ -1,7 +1,7 @@ +import pytest import chainladder as cl import numpy as np import pandas as pd -import pytest def test_bs_sample(raa): @@ -30,6 +30,13 @@ def test_multi_index(clrd): pd.testing.assert_frame_equal(resampled_triangles.index, expected) +def test_bootstrap_odp_val_triangle_error(raa): + with pytest.raises( + ValueError, match="Triangle must be expressed with development lags" + ): + cl.BootstrapODPSample().fit(raa.dev_to_val()) + + def test_hat_adjustment_is_zero_at_corners(genins): """ Check that the hat adjustment factor is exactly 0 at the two corner cells, diff --git a/chainladder/core/axis.py b/chainladder/core/axis.py index 86ac3c434..578acc590 100644 --- a/chainladder/core/axis.py +++ b/chainladder/core/axis.py @@ -3,8 +3,10 @@ from __future__ import annotations from typing import TYPE_CHECKING, Any, Callable +import numpy as np import pandas as pd +from chainladder.core.base import TriangleBase from chainladder.utils.utility_functions import _get_axis_name, _get_axis_number if TYPE_CHECKING: @@ -113,6 +115,23 @@ def __set__(self, obj: Triangle, value: Any) -> None: if hasattr(obj, "virtual_columns"): obj._set_slicers() + def set_unchecked(self, obj: Triangle, value: Any) -> None: + """ + Set the axis from a value, skipping the length check. + + Used when an axis is widened ahead of ``values`` (arithmetic broadcast) + or when slicing/reindexing internal coordinates. + """ + if self.fset is _set_origin or self.fset is _set_development: + raw = self.fset(obj, value, check=False) + else: + raw = self.fset(obj, value) if self.fset else value + if not hasattr(obj, "_axes"): + obj._axes = {} + obj._axes[self.axis] = raw + if hasattr(obj, "virtual_columns"): + obj._set_slicers() + def _set_columns(obj: Triangle, value: Any) -> pd.Index: """ @@ -130,12 +149,17 @@ def _set_columns(obj: Triangle, value: Any) -> pd.Index: pd.Index A pandas Index named ``"columns"``. """ - if isinstance(value, str): + if isinstance(value, (str, bytes)) or not hasattr(value, "__len__"): value = [value] - if hasattr(obj, "values") and obj.values is not None: - obj._len_check(range(obj.values.shape[1]), value) - elif hasattr(obj, "_axes") and (1 in obj._axes or "columns" in obj._axes): - obj._len_check(obj.columns, value) + if hasattr(obj, "_len_check"): + if hasattr(obj, "values") and obj.values is not None: + obj._len_check(range(obj.values.shape[1]), value) + elif ( + not isinstance(obj, TriangleBase) + and hasattr(obj, "_axes") + and (1 in obj._axes or "columns" in obj._axes) + ): + obj._len_check(obj.columns, value) return pd.Index(value, name="columns") @@ -157,8 +181,247 @@ def _set_index(obj: Triangle, value: Any) -> pd.DataFrame: """ if not isinstance(value, pd.DataFrame): raise TypeError("index must be a pandas DataFrame") - if hasattr(obj, "values") and obj.values is not None: - obj._len_check(range(obj.values.shape[0]), value) - elif hasattr(obj, "_axes") and (0 in obj._axes or "index" in obj._axes): - obj._len_check(obj.index, value) + if hasattr(obj, "_len_check"): + if hasattr(obj, "values") and obj.values is not None: + obj._len_check(range(obj.values.shape[0]), value) + elif ( + not isinstance(obj, TriangleBase) + and hasattr(obj, "_axes") + and (0 in obj._axes or "index" in obj._axes) + ): + obj._len_check(obj.index, value) return value.copy().reset_index(drop=True) + + +def _get_origin(obj: Triangle, raw: Any) -> pd.PeriodIndex | pd.Series: + """ + Transform raw origin timestamp array into public PeriodIndex or Series. + """ + if obj.is_pattern and len(raw) == 1: + return pd.Series(["(All)"]) + if raw is None: + return None + freq = { + "S": "2Q", + "H": "2Q", + }.get(obj.origin_grain, obj.origin_grain) + freq = freq if (freq == "M" or "-" in freq) else freq + "-" + obj.origin_close + return pd.DatetimeIndex(raw, name="origin").to_period(freq=freq) + + +def _set_origin(obj: Triangle, value: Any, check: bool = True) -> np.ndarray: + """ + Validate and transform assigned origin periods into internal timestamps array. + """ + if isinstance(value, (str, bytes)) or not hasattr(value, "__len__"): + value = [value] + if check and hasattr(obj, "_len_check"): + if hasattr(obj, "values") and obj.values is not None: + obj._len_check(range(obj.values.shape[2]), value) + elif ( + not isinstance(obj, TriangleBase) + and hasattr(obj, "_axes") + and (2 in obj._axes or "origin" in obj._axes) + ): + raw_orig = obj._axes.get(2, obj._axes.get("origin")) + if raw_orig is not None: + obj._len_check(raw_orig, value) + if isinstance(value, pd.PeriodIndex): + return value.to_timestamp().values + if isinstance(value, pd.DatetimeIndex): + return value.values + if isinstance(value, np.ndarray) and value.dtype.kind == "M": + return value + if len(value) == 1 and ( + (isinstance(value[0], str) and value[0] == "(All)") + or (hasattr(value, "iloc") and str(value.iloc[0]) == "(All)") + ): + if hasattr(obj, "_axes") and (2 in obj._axes or "origin" in obj._axes): + raw_orig = obj._axes.get(2, obj._axes.get("origin")) + if raw_orig is not None and len(raw_orig) == 1: + return raw_orig + return pd.date_range("2000", periods=1).values + origin_grain = getattr(obj, "origin_grain", "Y") + origin_close = getattr(obj, "origin_close", "DEC") + freq = { + "S": "2Q", + "H": "2Q", + }.get(origin_grain, origin_grain) + freq = freq if (freq == "M" or "-" in freq) else freq + "-" + origin_close + value = pd.PeriodIndex(value, freq=freq) + return value.to_timestamp().values + + +def _get_development(obj: Triangle, raw: Any) -> pd.Series: + """ + Transform raw development lags/timestamps into public Series. + """ + if raw is None: + return None + dev = raw.copy() + if obj.is_val_tri: + formats = {"Y": "%Y", "S": "%YQ%q", "Q": "%YQ%q", "M": "%Y-%m"} + dev = dev.to_period(freq=obj.development_grain.replace("S", "2Q")).strftime( + formats[obj.development_grain] + ) + elif obj.is_pattern: + offset = obj._dstep()["M"][obj.development_grain] + if obj.is_ultimate: + dev[-1] = dev[-2] + offset + if obj.is_cumulative: + dev = ["{}-Ult".format(dev[i]) for i in range(len(dev))] + else: + dev = ["{}-{}".format(dev[i], dev[i] + offset) for i in range(len(dev))] + return pd.Series(list(dev), name="development") + + +def _set_development(obj: Triangle, value: Any, check: bool = True) -> Any: + """ + Validate and transform assigned development periods into internal lag/timestamp array. + """ + if isinstance(value, (str, bytes)) or not hasattr(value, "__len__"): + value = [value] + if check and hasattr(obj, "_len_check"): + if hasattr(obj, "values") and obj.values is not None: + obj._len_check(range(obj.values.shape[3]), value) + elif ( + not isinstance(obj, TriangleBase) + and hasattr(obj, "_axes") + and (3 in obj._axes or "development" in obj._axes) + ): + raw_dev = obj._axes.get(3, obj._axes.get("development")) + if raw_dev is not None: + obj._len_check(raw_dev, value) + if isinstance(value, pd.DatetimeIndex): + return value + if isinstance(value, pd.PeriodIndex): + return value.to_timestamp(how="end") + if isinstance(value, np.ndarray) and value.dtype.kind == "M": + return pd.DatetimeIndex(value) + + val_arr = np.array(value) + if val_arr.dtype.kind in ("i", "u"): + return val_arr.astype(int) + if val_arr.dtype.kind == "f": + if np.all(np.equal(np.mod(val_arr, 1), 0)): + return val_arr.astype(int) + return val_arr + + if getattr(obj, "is_val_tri", False): + # Try matching existing DatetimeIndex labels or parsing as PeriodIndex/DatetimeIndex + if hasattr(obj, "_axes") and isinstance(obj._axes.get(3), pd.DatetimeIndex): + current_dt = obj._axes[3] + current_labels = list(_get_development(obj, current_dt)) + val_list_str = [str(v) for v in val_arr] + if all(v in current_labels for v in val_list_str): + indices = [current_labels.index(v) for v in val_list_str] + return current_dt[indices] + + try: + freq = getattr(obj, "development_grain", "Y").replace("S", "2Q") + return pd.PeriodIndex(val_arr.astype(str), freq=freq).to_timestamp( + how="end" + ) + except (ValueError, TypeError): + try: + return pd.DatetimeIndex(val_arr) + except (ValueError, TypeError): + pass + return val_arr + + # Non-valuation triangle: development axis represents numeric lags or pattern intervals + val_list = [str(v).strip() for v in val_arr] + try: + lags = [] + for s in val_list: + if s.lower() == "ult": + lags.append(9999) + elif "-" in s: + parts = s.split("-") + if len(parts) == 2: + p0 = parts[0].strip() + p1 = parts[1].strip() + try: + n0 = int(p0) if p0.isdigit() else float(p0) + except ValueError: + n0 = None + if p1.lower() == "ult": + n1 = 9999 + else: + try: + n1 = int(p1) if p1.isdigit() else float(p1) + except ValueError: + n1 = None + if ( + n0 is not None + and n1 is not None + and (n0 < 1000 or n0 == 9999) + and n1 <= 9999 + ): + lags.append(n0) + else: + raise ValueError(f"Not a development lag pattern: {s}") + else: + raise ValueError(f"Not a development lag pattern: {s}") + else: + try: + val_num = int(s) + except ValueError: + val_num = float(s) + if val_num >= 1000 and val_num != 9999: + raise ValueError(f"Year passed to development lag: {s}") + lags.append(val_num) + + raw_dev = getattr(obj, "_axes", {}).get(3) + if raw_dev is None and hasattr(obj, "_axes") and isinstance(obj._axes, dict): + raw_dev = obj._axes.get("development") + is_ult = getattr(obj, "is_ultimate", False) or ( + raw_dev is not None + and len(raw_dev) == len(lags) + and len(raw_dev) > 0 + and raw_dev[-1] == 9999 + ) + if is_ult and len(lags) > 0: + lags[-1] = 9999 + lag_arr = np.array(lags) + if lag_arr.dtype.kind == "f" and np.all(np.equal(np.mod(lag_arr, 1), 0)): + lag_arr = lag_arr.astype(int) + return lag_arr + except (ValueError, TypeError): + pass + + # If not parseable as numeric lags / patterns, try parsing as valuation periods / dates + try: + freq = getattr(obj, "development_grain", "Y").replace("S", "2Q") + return pd.PeriodIndex(val_arr.astype(str), freq=freq).to_timestamp(how="end") + except (ValueError, TypeError): + try: + return pd.DatetimeIndex(val_arr) + except (ValueError, TypeError): + pass + + return val_arr + + +def _set_axis(obj: Triangle, axis: int | str, value: Any, check: bool = True) -> None: + """ + Set values on the specified axis of a Triangle instance. + + Parameters + ---------- + obj : Triangle + The Triangle instance to update. + axis : int or str + Axis identifier (0..3 or 'index', 'columns', 'origin', 'development'). + value : Any + New axis values. + check : bool, default True + Whether to perform length and consistency validation. + """ + axis_name = _get_axis_name(axis) + if not check: + descriptor = getattr(type(obj), axis_name, None) + if isinstance(descriptor, TriangleAxis): + descriptor.set_unchecked(obj, value) + return + setattr(obj, axis_name, value) diff --git a/chainladder/core/base.py b/chainladder/core/base.py index f06531e38..e3f360751 100644 --- a/chainladder/core/base.py +++ b/chainladder/core/base.py @@ -26,6 +26,7 @@ from chainladder.utils.cupy import cp from chainladder.utils.dask import dp from chainladder.utils.sparse import sp +from chainladder.utils.utility_functions import _get_axis_name from chainladder.adjustments.disposal import DisposalMixin @@ -359,19 +360,19 @@ def _factorize_index( return unique_keys.drop(labels="index", axis=1).values, index_idx @staticmethod - def _set_odims( + def _factorize_origin( data_agg: DataFrame, date_axes: DataFrame ) -> tuple[np.ndarray, np.ndarray]: - odims: np.ndarray = np.sort(date_axes["__origin__"].unique()) + origin: np.ndarray = np.sort(date_axes["__origin__"].unique()) orig_idx: np.ndarray = TriangleBase._set_index( - col=data_agg["__origin__"], unique=odims + col=data_agg["__origin__"], unique=origin ) - return odims, orig_idx + return origin, orig_idx @staticmethod - def _set_ddims( + def _factorize_development( data_agg: DataFrame, date_axes: DataFrame ) -> tuple[ArrayLike, np.ndarray]: @@ -380,21 +381,21 @@ def _set_ddims( data_agg["__origin__"], data_agg["__development__"] ) - ddims: ArrayLike = np.sort( + development: ArrayLike = np.sort( TriangleBase._development_lag( date_axes["__origin__"], date_axes["__development__"] ).unique() ) - dev_idx: np.ndarray = TriangleBase._set_index(dev_lag, ddims) + dev_idx: np.ndarray = TriangleBase._set_index(dev_lag, development) else: - ddims: ArrayLike = pd.DatetimeIndex( + development: ArrayLike = pd.DatetimeIndex( [data_agg["__development__"].max()], name="valuation" ) dev_idx = np.zeros((len(data_agg), 1)) - return ddims, dev_idx + return development, dev_idx @staticmethod def _set_values( @@ -733,20 +734,30 @@ def _auto_sparse(self) -> Triangle: @property def valuation(self): - ddims = self.ddims + dev = self._axes[3] if self.is_val_tri: - out = pd.DataFrame(np.repeat(self.ddims.values[None], len(self.odims), 0)) + out = pd.DataFrame(np.repeat(dev.values[None], len(self._axes[2]), 0)) return pd.DatetimeIndex(out.unstack().values) - ddim_arr = ddims - ddims[0] - origin = np.minimum(self.odims, np.datetime64(self.valuation_date)) - val_array = origin.astype("datetime64[M]") + np.timedelta64(ddims[0], "M") + dev_arr = dev - dev[0] + dev_0 = ( + int(np.round(dev[0])) + if isinstance(dev[0], (int, float, np.number)) + else dev[0] + ) + origin = np.minimum(self._axes[2], np.datetime64(self.valuation_date)) + val_array = origin.astype("datetime64[M]") + np.timedelta64(dev_0, "M") val_array = val_array.astype(__dt64_dtype__) - np.timedelta64(1, __dt64_unit__) val_array = val_array[:, None] - s = slice(None, -1) if ddims[-1] == 9999 else slice(None, None) - val_array = ( - val_array.astype("datetime64[M]") + ddim_arr[s][None, :] + 1 - ).astype(__dt64_dtype__) - np.timedelta64(1, __dt64_unit__) - if ddims[-1] == 9999: + s = slice(None, -1) if dev[-1] == 9999 else slice(None, None) + dev_delta = ( + np.round(dev_arr[s]).astype(int) + if np.asarray(dev_arr).dtype.kind == "f" + else dev_arr[s] + ) + val_array = (val_array.astype("datetime64[M]") + dev_delta[None, :] + 1).astype( + __dt64_dtype__ + ) - np.timedelta64(1, __dt64_unit__) + if dev[-1] == 9999: ult = np.repeat(np.datetime64(options.ULT_VAL), val_array.shape[0])[:, None] val_array = np.concatenate( ( @@ -862,10 +873,7 @@ def compute(self, *args, **kwargs): return self def _get_axis_value(self, axis): - axis = self._get_axis(axis) - return {0: self.index, 1: self.columns, 2: self.origin, 3: self.development}[ - axis - ] + return getattr(self, _get_axis_name(axis)) def is_chainladder(estimator): diff --git a/chainladder/core/common.py b/chainladder/core/common.py index d84fad6f9..2cd128da4 100644 --- a/chainladder/core/common.py +++ b/chainladder/core/common.py @@ -53,11 +53,16 @@ def _get_full_triangle(X, ultimate, is_cumulative=True): emergence.valuation_date = pd.to_datetime(options.ULT_VAL) emergence.values = emergence.values.cumprod(axis=3) - 1 - # Shifting the CDFs by development age, and renaming the last column as 9999 - emergence.ddims = ( - emergence.ddims + {"Y": 12, "Q": 3, "S": 6, "M": 1}[emergence.development_grain] + new_dev = ( + ( + emergence.development + + {"Y": 12, "Q": 3, "S": 6, "M": 1}[emergence.development_grain] + ) + .to_numpy() + .copy() ) - emergence.ddims[-1] = 9999 + new_dev[-1] = 9999 + emergence.development = new_dev emergence.values = emergence.values / num_to_nan(emergence.values[..., -1:]) ld = X.incr_to_cum().latest_diagonal cum_run_off = ld + emergence * (ultimate - ld) diff --git a/chainladder/core/correlation.py b/chainladder/core/correlation.py index 4a4394891..e44bf696b 100644 --- a/chainladder/core/correlation.py +++ b/chainladder/core/correlation.py @@ -146,7 +146,7 @@ def __init__(self, triangle, p_critical: float = 0.5): # remove last column because it was not part of the comparison with m2 numerator.values = numerator.values[..., :-1] - numerator.ddims = numerator.ddims[:-1] + numerator.development = numerator.development[:-1] # n_dev_periods is the number of development periods in the triangle # ("I" in Mack (1994)). @@ -358,7 +358,7 @@ def p_z_lower(z: int, n: int, p: float = 0.5) -> float: z_critical = triangle.dev_to_val().sum("origin") z_critical = z_critical[z_critical.valuation > triangle.valuation.min()] * 0 z_critical.values = np.array(self.probs) < p_critical - z_critical.odims = triangle.odims[0:1] + z_critical.origin = triangle.origin[0:1] self.z_critical = z_critical self.z = self.z_critical.copy() self.z.values = z diff --git a/chainladder/core/dunders.py b/chainladder/core/dunders.py index 4234b2714..094a40a95 100644 --- a/chainladder/core/dunders.py +++ b/chainladder/core/dunders.py @@ -165,42 +165,51 @@ def _prep_origin_development(self, obj, other): (m == n) or (m == 1) or (n == 1) for m, n in zip(obj.shape[-2:][::-1], other.shape[-2:][::-1]) ) - if (len(obj.odims), len(obj.ddims)) == (len(other.odims), len(other.ddims)): - if np.all(obj.ddims != other.ddims) and len(obj.ddims) > 1: + if (len(obj._axes[2]), len(obj._axes[3])) == ( + len(other._axes[2]), + len(other._axes[3]), + ): + if np.all(obj._axes[3] != other._axes[3]) and len(obj._axes[3]) > 1: is_broadcastable = False - if np.all(obj.odims != other.odims) and len(obj.odims) > 1: + if np.all(obj._axes[2] != other._axes[2]) and len(obj._axes[2]) > 1: is_broadcastable = False - if len(other.odims) == 1 and len(obj.odims) > 1: - other.odims = obj.odims + if len(other._axes[2]) == 1 and len(obj._axes[2]) > 1: + other._axes[2] = obj._axes[2] other.origin_grain = obj.origin_grain - elif len(obj.odims) == 1 and len(other.odims) > 1: - obj.odims = other.odims + elif len(obj._axes[2]) == 1 and len(other._axes[2]) > 1: + obj._axes[2] = other._axes[2] obj.origin_grain = other.origin_grain - if len(other.ddims) == 1 and len(obj.ddims) > 1: - other.ddims = obj.ddims + if len(other._axes[3]) == 1 and len(obj._axes[3]) > 1: + other._axes[3] = obj._axes[3] other.development_grain = obj.development_grain - elif len(obj.ddims) == 1 and len(other.ddims) > 1: - obj.ddims = other.ddims + elif len(obj._axes[3]) == 1 and len(other._axes[3]) > 1: + obj._axes[3] = other._axes[3] obj.development_grain = other.development_grain if not is_broadcastable: # If broadcasting doesn't work, union axes similar to pandas - ddims = pd.concat( + dev = pd.concat( ( - pd.Series(obj.ddims, index=obj.ddims), - pd.Series(other.ddims, index=other.ddims), + pd.Series(obj._axes[3], index=obj._axes[3]), + pd.Series(other._axes[3], index=other._axes[3]), ), axis=1, ).sort_index() - odims = pd.concat( + orig = pd.concat( ( - pd.Series(obj.odims, index=obj.odims), - pd.Series(other.odims, index=other.odims), + pd.Series(obj._axes[2], index=obj._axes[2]), + pd.Series(other._axes[2], index=other._axes[2]), ), axis=1, sort=False, ).sort_index() - o_arr0, o_arr1 = odims[0].isna().values, odims[1].isna().values - d_arr0, d_arr1 = ddims[0].isna().values, ddims[1].isna().values + o_arr0, o_arr1 = ( + orig.iloc[:, 0].isna().values, + orig.iloc[:, 1].isna().values, + ) + d_arr0, d_arr1 = ( + dev.iloc[:, 0].isna().values, + dev.iloc[:, 1].isna().values, + ) # rol = right hand side, origin, lower rol = int(np.where(~o_arr1 == 1)[0].min()) roh = int(np.where(~o_arr1 == 1)[0].max() + 1) @@ -214,16 +223,16 @@ def _prep_origin_development(self, obj, other): other_arr = xp.zeros(( other.shape[0], other.shape[1], - len(odims), - len(ddims), + len(orig), + len(dev), )) other_arr[:] = xp.nan other_arr[:, :, rol:roh, rdl:rdh] = other.values obj_arr = xp.zeros(( obj.shape[0], obj.shape[1], - len(odims), - len(ddims), + len(orig), + len(dev), )) obj_arr[:] = xp.nan obj_arr[:, :, lol:loh, ldl:ldh] = obj.values @@ -239,17 +248,17 @@ def _prep_origin_development(self, obj, other): other_arr.shape = ( other.shape[0], other.shape[1], - len(odims), - len(ddims), + len(orig), + len(dev), ) - obj_arr.shape = (obj.shape[0], obj.shape[1], len(odims), len(ddims)) - obj.odims = np.array(odims.index) - if isinstance(obj.ddims, pd.DatetimeIndex): - obj.ddims = pd.DatetimeIndex(ddims.index) - else: - obj.ddims = np.array(ddims.index) + obj_arr.shape = (obj.shape[0], obj.shape[1], len(orig), len(dev)) obj.values = obj_arr other.values = other_arr + obj._set_axis("origin", np.array(orig.index), check=False) + if isinstance(obj._axes.get(3), pd.DatetimeIndex): + obj._set_axis("development", pd.DatetimeIndex(dev.index), check=False) + else: + obj._set_axis("development", np.array(dev.index), check=False) return obj, other @staticmethod diff --git a/chainladder/core/pandas.py b/chainladder/core/pandas.py index ecab025f2..cd00d00ad 100644 --- a/chainladder/core/pandas.py +++ b/chainladder/core/pandas.py @@ -19,7 +19,7 @@ _deprecated_drop_argument, _warn_dask_parallel_deprecated, ) -from chainladder.utils.utility_functions import concat, num_to_nan +from chainladder.utils.utility_functions import _get_axis_number, concat, num_to_nan try: import dask.bag as db @@ -268,10 +268,10 @@ def to_frame( out["columns"] = obj.columns[values.coords[1]] missing_cols: list = list(set(self.columns) - set(out["columns"])) if origin_as_datetime: - out["origin"] = obj.odims[values.coords[2]] + out["origin"] = obj._axes[2][values.coords[2]] else: out["origin"] = obj.origin[values.coords[2]] - out["development"] = obj.ddims[values.coords[3]] + out["development"] = obj.development.iloc[values.coords[3]].values out["values"] = values.data out: DataFrame = pd.pivot_table( out, index=obj.key_labels + ["origin", "development"], columns="columns" @@ -283,8 +283,8 @@ def to_frame( valuation_series = pd.DataFrame( obj.valuation.values.reshape(obj.shape[-2:], order="F"), - index=obj.odims if origin_as_datetime else obj.origin, - columns=obj.ddims, + index=obj._axes[2] if origin_as_datetime else obj.origin, + columns=obj.development, ).unstack() valuation_series.name = "valuation" valuation: DataFrame = valuation_series.reset_index().rename( @@ -401,7 +401,7 @@ def hvplot(self, *args: Any, **kwargs: Any) -> Any: return df.hvplot(*args, **kwargs) @staticmethod - def _get_axis( + def _get_axis_number( axis: Literal["index", "columns", "origin", "development"] | int | None, ) -> int: """ @@ -412,30 +412,31 @@ def _get_axis( axis: Literal['index', 'columns', 'origin', 'development'] | int | None String or integer representation of the requested axis. If supplied as a string, returns the integer representation. If - supplied as an integer, returns the same integer. + supplied as an integer, returns the canonical positive integer. Returns ------- int - The integer representation of the requested axis + The integer representation of the requested axis (0..3). """ - - ax = { - **{0: 0, 1: 1, 2: 2, 3: 3}, - **{-1: 3, -2: 2, -3: 1, -4: 0}, - **{"index": 0, "columns": 1, "origin": 2, "development": 3}, - } - + if axis is None: + return 0 try: - return ax[axis] - except KeyError: - if axis is None: - return 0 - else: - raise ValueError( - "Invalid axis specified. Please specify the correct string or " - "integer representation of the desired axis." - ) + return _get_axis_number(axis) + except ValueError as err: + raise ValueError( + "Invalid axis specified. Please specify the correct string or " + "integer representation of the desired axis." + ) from err + + @staticmethod + def _get_axis( + axis: Literal["index", "columns", "origin", "development"] | int | None, + ) -> int: + """ + Backward-compatible alias for _get_axis_number. + """ + return TrianglePandas._get_axis_number(axis) def dropna(self) -> Triangle: """ @@ -1122,7 +1123,7 @@ def drop( keep = self._validate_contiguous_drop( result.development, ax_labels, "development", errors ) - result = result._slice(keep, "ddims") + result = result._slice(keep, "development") if result.is_val_tri: result.valuation_date = min( result.valuation.max(), result.valuation_date @@ -1480,11 +1481,11 @@ def _triangle_aggregate( ) if axis == 1 and obj.values.shape[axis] == 1 and len(obj.columns) > 1: obj.columns = pd.Index([0], name="columns") - if axis == 2 and obj.values.shape[axis] == 1 and len(obj.odims) > 1: - obj.odims = obj.odims[0:1] - # If axis is development, set the ddims to be the valuation date. - if axis == 3 and obj.values.shape[axis] == 1 and len(obj.ddims) > 1: - obj.ddims = pd.DatetimeIndex( + if axis == 2 and obj.values.shape[axis] == 1 and len(obj.origin) > 1: + obj.origin = obj.origin[0:1] + # If axis is development, set the development axis to be the valuation date. + if axis == 3 and obj.values.shape[axis] == 1 and len(obj.development) > 1: + obj._axes[3] = pd.DatetimeIndex( [self.valuation_date], dtype=__dt64_dtype__, freq=None ) obj._set_slicers() @@ -1544,15 +1545,15 @@ def aggregate(i, obj, axis, v): if self.axis == 1: obj.columns = group_index if self.axis == 2: - odims = self.obj._to_datetime( + orig_dt = self.obj._to_datetime( pd.Series(self.groups.indices.keys()).to_frame(), [0] ) - if odims is None: + if orig_dt is None: raise ValueError("Group labels could not be converted to origin dates.") - obj.origin_grain = self.obj._get_grain(odims) + obj.origin_grain = self.obj._get_grain(orig_dt) split = obj.origin_grain.split("-") obj.origin_grain = {"2Q": "S"}.get(split[0], split[0]) - obj.odims = odims.values + obj._axes[2] = orig_dt.values obj._set_slicers() if auto_sparse: obj = obj._auto_sparse() diff --git a/chainladder/core/slice.py b/chainladder/core/slice.py index 8cde99684..283fbb93b 100644 --- a/chainladder/core/slice.py +++ b/chainladder/core/slice.py @@ -66,7 +66,8 @@ def get_idx(self, idx: tuple[_AxisKey, _AxisKey, _AxisKey, _AxisKey]) -> Triangl # Set the new dimension values. obj.index = obj.index.iloc[i_idx].reset_index(drop=True) obj.columns = obj.columns[c_idx] - obj.odims, obj.ddims = obj.odims[o_idx], obj.ddims[d_idx] + obj._set_axis("origin", obj._axes[2][o_idx], check=False) + obj._set_axis("development", obj._axes[3][d_idx], check=False) from chainladder import options if obj.valuation_date < pd.to_datetime(options.ULT_VAL) or not getattr( @@ -793,14 +794,14 @@ def __getitem__( # Case development axis. if isinstance(key, pd.Series) and key.name == "development": - return self._slice(key, "ddims") + return self._slice(key, "development") # Case ndarray, could be valuation or origin. if isinstance(key, np.ndarray): # Case valuation, inferred by size of ndarray obtained by filtering on valuation date. if len(key) == np.prod(self.shape[-2:]) and self.shape[-1] > 1: return self._slice_valuation(key) # Otherwise, assume case origin. - return self._slice(key, "odims") + return self._slice(key, "origin") # Case index. if isinstance(key, pd.Series): if pd.api.types.is_bool_dtype(key): @@ -1000,7 +1001,7 @@ def _slice_valuation(self: TriangleProtocol, key: np.ndarray) -> Triangle: def _slice( self: TriangleProtocol, key: pd.Series | np.ndarray, - axis: Literal["ddims", "odims"], + axis: Literal["development", "origin", "ddims", "odims"], ) -> Triangle: """ Private method for handling of origin/development slicing. @@ -1009,18 +1010,22 @@ def _slice( ---------- key: pd.Series | np.ndarray Array specifying the slice to extract. - axis: Literal['ddims', 'odims'] - The axis to which the slice would apply, `ddims` for development, `odims` for origin. + axis: Literal['development', 'origin', 'ddims', 'odims'] + The axis to which the slice would apply, 'development' or 'origin'. """ # Update the axis, then the values. obj = self.copy() - setattr(obj, axis, getattr(obj, axis)[key]) - # noinspection PyProtectedMember - slicer = ..., _LocBase._contig_slice(np.arange(len(key))[key]) - if axis == "odims": - slicer = tuple(list(slicer) + [slice(None)]) - obj.values = obj.values[slicer] + if axis in ("origin", "odims"): + new_axis = obj._axes[2][key] + slicer = ..., _LocBase._contig_slice(np.arange(len(key))[key]), slice(None) + obj.values = obj.values[slicer] + obj._set_axis("origin", new_axis, check=False) + else: + new_axis = obj._axes[3][key] + slicer = ..., _LocBase._contig_slice(np.arange(len(key))[key]) + obj.values = obj.values[slicer] + obj._set_axis("development", new_axis, check=False) return obj def _set_slicers(self: TriangleProtocol) -> None: diff --git a/chainladder/core/tests/test_arithmetic.py b/chainladder/core/tests/test_arithmetic.py index d88bde567..f5ad0fe56 100644 --- a/chainladder/core/tests/test_arithmetic.py +++ b/chainladder/core/tests/test_arithmetic.py @@ -115,14 +115,14 @@ def test_arithmetic_union_val_tri(raa: Triangle) -> None: a = val_raa[val_raa.valuation < "1987"] b = val_raa[val_raa.valuation >= "1987"] result = a + b - assert isinstance(result.ddims, pd.DatetimeIndex) + assert isinstance(result._axes[3], pd.DatetimeIndex) assert result.shape == val_raa.shape assert result.equals(val_raa) def test_origin_broadcasting(raa: Triangle) -> None: """ - Adding a single-origin triangle to a multi-origin triangle broadcasts odims. + Adding a single-origin triangle to a multi-origin triangle broadcasts origin. Parameters ---------- diff --git a/chainladder/core/tests/test_correlation.py b/chainladder/core/tests/test_correlation.py index 3bb9d0b0b..f879b6219 100644 --- a/chainladder/core/tests/test_correlation.py +++ b/chainladder/core/tests/test_correlation.py @@ -112,5 +112,5 @@ def test_val_corr_incomplete_triangle(xyz: Triangle) -> None: z_critical = ( xyz["Paid"].valuation_correlation(p_critical=0.1, total=False).z_critical ) - assert z_critical.values.shape[-1] == len(z_critical.ddims) + assert z_critical.values.shape[-1] == len(z_critical.development) assert repr(z_critical) diff --git a/chainladder/core/tests/test_grain.py b/chainladder/core/tests/test_grain.py index e371413eb..8f1aead16 100644 --- a/chainladder/core/tests/test_grain.py +++ b/chainladder/core/tests/test_grain.py @@ -112,12 +112,12 @@ def test_annual_trailing(prism): # (limit data to November) tri = tri[tri.valuation < tri.valuation_date].incr_to_cum() tri = tri.grain("OQDQ", trailing=True).grain("OYDY") - assert np.all(tri.ddims[:4] == np.array([12, 24, 36, 48])) + assert np.all(tri.development[:4] == np.array([12, 24, 36, 48])) def test_development_age(raa: Triangle) -> None: - assert (raa.ddims == list(range(12, 121, 12))).all() + assert (raa.development == list(range(12, 121, 12))).all() def test_development_age_quarterly(qtr: Triangle) -> None: - assert (qtr.ddims == list(range(3, 136, 3))).all() + assert (qtr.development == list(range(3, 136, 3))).all() diff --git a/chainladder/core/tests/test_triangle.py b/chainladder/core/tests/test_triangle.py index d7421018d..65f277c9c 100644 --- a/chainladder/core/tests/test_triangle.py +++ b/chainladder/core/tests/test_triangle.py @@ -26,6 +26,7 @@ from chainladder.utils.sparse import COO from chainladder.utils.utility_functions import ( _axis_orders, + _axis_to_axis_number, _get_axis_name, _get_axis_number, date_delta_adjustment, @@ -342,7 +343,7 @@ def test_origin_and_value_setters(raa): assert np.all(( np.all(raa2.origin == raa.origin), np.all(raa2.development == raa.development), - np.all(raa2.odims == raa.odims), + np.all(raa2._axes[2] == raa._axes[2]), np.all(raa2.columns == raa.columns), )) @@ -493,8 +494,17 @@ def test_triangle_axes(raa): assert axes[3].equals(raa.development) -def test_axis_mappings(): +def test_axis_mappings(raa): """Verify axis number and name resolutions in chainladder.core.typing.""" + from chainladder.core.typing import ( + _axis_orders as typing_axis_orders, + _axis_to_axis_number as typing_axis_to_axis_number, + _get_axis_name as typing_get_axis_name, + _get_axis_number as typing_get_axis_number, + ) + + assert typing_axis_orders == _axis_orders + assert typing_axis_to_axis_number == _axis_to_axis_number for idx, name in enumerate(_axis_orders): assert _get_axis_number(idx) == idx assert _get_axis_number(idx - 4) == idx @@ -502,6 +512,12 @@ def test_axis_mappings(): assert _get_axis_name(idx) == name assert _get_axis_name(idx - 4) == name assert _get_axis_name(name) == name + assert typing_get_axis_number(idx) == idx + assert typing_get_axis_name(idx) == name + assert raa._get_axis_number(idx) == idx + assert raa._get_axis_number(name) == idx + assert raa._get_axis(idx) == idx + assert raa._get_axis(name) == idx with pytest.raises( ValueError, match="No axis named invalid for object type Triangle" @@ -511,13 +527,20 @@ def test_axis_mappings(): with pytest.raises(ValueError, match=r"No axis named 5 for object type Triangle"): _get_axis_number(5) + with pytest.raises(ValueError, match="Invalid axis specified"): + raa._get_axis_number("invalid") + def test_triangle_axis_descriptor(): """Verify TriangleAxis descriptor correctly uses integer axis keys for index and columns.""" assert isinstance(cl.Triangle.index, TriangleAxis) assert cl.Triangle.index.__doc__ is not None assert isinstance(cl.Triangle.columns, TriangleAxis) + assert isinstance(cl.Triangle.origin, TriangleAxis) + assert isinstance(cl.Triangle.development, TriangleAxis) assert cl.Triangle.columns.__doc__ is not None + assert cl.Triangle.origin.__doc__ is not None + assert cl.Triangle.development.__doc__ is not None class MockTriangle: def __init__(self): @@ -588,6 +611,367 @@ def _len_check(self, target, val): m.columns = ["a", "b"] +def test_set_axis(raa): + """Verify Triangle._set_axis sets index, columns, origin, and development.""" + tri = raa.copy() + # Test setting index via int and str + new_idx = pd.DataFrame({"Company": ["Test"]}) + tri._set_axis(0, new_idx) + pd.testing.assert_frame_equal(tri.index, new_idx) + tri._set_axis("index", new_idx) + pd.testing.assert_frame_equal(tri.index, new_idx) + + # Test setting columns via int and str + tri._set_axis(1, ["ColA"]) + assert list(tri.columns) == ["ColA"] + tri._set_axis("columns", ["ColB"]) + assert list(tri.columns) == ["ColB"] + + # Test setting origin via int and str + orig_years = [str(y) for y in range(1981, 1991)] + tri._set_axis(2, orig_years) + assert list(tri.origin.year) == list(range(1981, 1991)) + tri._set_axis("origin", orig_years) + assert list(tri.origin.year) == list(range(1981, 1991)) + + # Test setting development via int and str + devs = list(range(1, 11)) + tri._set_axis(3, devs) + assert list(tri.development) == devs + tri._set_axis("development", devs) + assert list(tri.development) == devs + + # Invalid axis raises ValueError + with pytest.raises(ValueError): + tri._set_axis("invalid", [1]) + + +def test_centralized_axis_edge_cases(raa): + """Cover edge cases for TriangleAxis origin, development, and legacy pickles.""" + # 1. origin on 1-origin pattern returns Series(["(All)"]) + dev = cl.Development().fit(raa) + assert dev.ldf_.origin.iloc[0] == "(All)" + assert len(dev.ldf_.origin) == 1 + + # 2. _set_origin and _set_development on mock without values + class MockTriangleNoValues: + origin_grain = "Y" + origin_close = "DEC" + development_grain = "Y" + is_pattern = False + is_val_tri = False + _axes = { + 2: pd.period_range("2000", periods=3, freq="Y-DEC").to_timestamp().values, + 3: np.array([12, 24, 36]), + } + origin = cl.Triangle.origin + development = cl.Triangle.development + + def _len_check(self, a, b): + if len(a) != len(b): + raise ValueError("Length mismatch") + + mock = MockTriangleNoValues() + mock.origin = ["2001", "2002", "2003"] + assert len(mock._axes[2]) == 3 + with pytest.raises(ValueError, match="Length mismatch"): + mock.origin = ["2001"] + + mock.development = [12, 24, 36] + assert len(mock._axes[3]) == 3 + with pytest.raises(ValueError, match="Length mismatch"): + mock.development = [12] + + # 3. Development scalar/string assignment for single column and pattern + single_col_tri = raa.iloc[:, :, :, :1].copy() + single_col_tri.development = 12 + assert list(single_col_tri.development) == [12] + assert not single_col_tri.is_val_tri + + single_col_tri.development = "12" + assert list(single_col_tri.development) == [12] + assert not single_col_tri.is_val_tri + + single_col_tri.development = "1" + assert list(single_col_tri.development) == [1] + assert not single_col_tri.is_val_tri + + pattern_tri = dev.ldf_.iloc[:, :, :, :1].copy() + pattern_tri.development = "1-2" + assert list(pattern_tri._axes[3]) == [1] + assert not pattern_tri.is_val_tri + + single_row_tri = raa.iloc[:, :, :1, :].copy() + single_row_tri.origin = 2000 + assert len(single_row_tri.origin) == 1 + + # 4. Valuation triangle development and non-cumulative pattern development + val_tri = raa.dev_to_val() + assert list(val_tri.development) == [str(y) for y in range(1981, 1991)] + assert list(raa.link_ratio.development)[0] == "12-24" + assert list(dev.cdf_.development)[0] == "12-Ult" + + # 5. Pattern with is_ultimate True + class MockPatternUltimate: + is_pattern = True + is_ultimate = True + is_val_tri = False + is_cumulative = False + development_grain = "Y" + development = cl.Triangle.development + _axes = {3: np.array([12, 24, 9999])} + + def _dstep(self): + return {"M": {"Y": 12}} + + mock_ult = MockPatternUltimate() + assert list(mock_ult.development) == ["12-24", "24-36", "36-48"] + mock_ult.development = mock_ult.development + assert mock_ult._axes[3][-1] == 9999 + assert list(mock_ult._axes[3]) == [12, 24, 9999] + + # 6. Triangle.origin and Triangle.development setters on uninitialized instance + t_uninit = cl.Triangle.__new__(cl.Triangle) + t_uninit.origin = np.array(["2000-01-01"], dtype="datetime64[ns]") + assert 2 in t_uninit._axes + + t2_uninit = cl.Triangle.__new__(cl.Triangle) + t2_uninit.development = np.array([12, 24]) + assert 3 in t2_uninit._axes + + # 7. Redundant legacy keys in __setstate__ + state_redundant = raa.__dict__.copy() + raw_odims = raa._axes[2].copy() + raw_ddims = raa._axes[3].copy() + state_redundant["_axes"] = { + 0: raa.index.copy(), + 1: raa.columns.copy(), + 2: raw_odims.copy(), + 3: raw_ddims.copy(), + } + state_redundant["_odims"] = raw_odims.copy() + state_redundant["odims"] = raw_odims.copy() + state_redundant["_ddims"] = raw_ddims.copy() + state_redundant["ddims"] = raw_ddims.copy() + restored_red = cl.Triangle.__new__(cl.Triangle) + restored_red.__setstate__(state_redundant) + assert "_odims" not in restored_red.__dict__ + assert "odims" not in restored_red.__dict__ + assert "_ddims" not in restored_red.__dict__ + assert "ddims" not in restored_red.__dict__ + + # 8. Unprefixed origin/development legacy pickle and fallthrough + state_no_axes = raa.__dict__.copy() + state_no_axes["_axes"] = {} + restored_none = cl.Triangle.__new__(cl.Triangle) + restored_none.__setstate__(state_no_axes) + assert 2 not in restored_none._axes + assert 3 not in restored_none._axes + + # 9. _get_axis_number and _get_axis edge cases + assert raa._get_axis_number(None) == 0 + assert raa._get_axis(None) == 0 + for neg, pos in [(-1, 3), (-2, 2), (-3, 1), (-4, 0)]: + assert raa._get_axis_number(neg) == pos + assert raa._get_axis(neg) == pos + + # 10. Development roundtrip and sort_axis preservation of valuation and pattern types + val_tri = raa.dev_to_val() + val_tri.development = val_tri.development + assert val_tri.is_val_tri + assert isinstance(val_tri._axes[3], pd.DatetimeIndex) + assert val_tri.valuation is not None + + shuffled_val = val_tri.iloc[..., [2, 1, 0]].sort_axis(3) + assert shuffled_val.is_val_tri + assert isinstance(shuffled_val._axes[3], pd.DatetimeIndex) + assert list(shuffled_val.development) == list(val_tri.development[:3]) + + pattern_tri = raa.link_ratio + pattern_tri.development = pattern_tri.development + assert pattern_tri.is_pattern + assert pattern_tri._axes[3].dtype.kind == "i" + assert pattern_tri.valuation is not None + + # Ultimate pattern preserves 9999 sentinel lag across development round-trips + pattern_ult = raa.link_ratio + pattern_ult._axes[3][-1] = 9999 + assert pattern_ult.is_ultimate + pattern_ult.development = pattern_ult.development + assert pattern_ult.is_ultimate + assert pattern_ult._axes[3][-1] == 9999 + + # 11. Pattern origin round-trip and "(All)" assignment + orig_label = dev.ldf_.origin + dev.ldf_.origin = orig_label + assert dev.ldf_.origin.iloc[0] == "(All)" + dev.ldf_.origin = "(All)" + assert dev.ldf_.origin.iloc[0] == "(All)" + + # 12. Pattern development sort orders numerically (12-24 before 108-120) + dev_10yr = cl.load_sample("ABC") + ldf_10yr = cl.Development().fit(dev_10yr).ldf_ + shuffled_ldf = ldf_10yr.iloc[..., [8, 0]].sort_axis(3) + assert list(shuffled_ldf.development) == [ + list(ldf_10yr.development)[0], + list(ldf_10yr.development)[8], + ] + + pattern_ult.is_cumulative = True + pattern_ult.development = pattern_ult.development + assert pattern_ult.is_ultimate + assert pattern_ult._axes[3][-1] == 9999 + + tri_lags = raa.copy() + tri_lags._axes[3] = np.array([12, 120, 24, 36, 48, 60, 72, 84, 96, 108]) + sorted_lags = tri_lags.sort_axis(3) + assert list(sorted_lags.development) == [12, 24, 36, 48, 60, 72, 84, 96, 108, 120] + + # 13. Additional axis branch coverage for edge cases + # TriangleAxis descriptor on class vs instance without _axes + assert isinstance(cl.Triangle.origin, cl.core.axis.TriangleAxis) + + class EmptyObj: + pass + + empty = EmptyObj() + with pytest.raises(AttributeError, match="has no attribute 'origin'"): + _ = cl.Triangle.origin.__get__(empty, EmptyObj) + + # _get_origin and _get_development when raw is None + class MockNoneAxes: + is_pattern = False + is_val_tri = False + origin_grain = "S" + origin_close = "DEC" + development_grain = "S" + _axes = {2: None, 3: None} + origin = cl.Triangle.origin + development = cl.Triangle.development + + none_mock = MockNoneAxes() + assert none_mock.origin is None + assert none_mock.development is None + + # origin_grain variations (S, H) and development_grain (Q, M) + class MockGrainAxes: + is_pattern = False + is_val_tri = True + origin_grain = "H" + origin_close = "DEC" + development_grain = "Q" + _axes = { + 2: pd.to_datetime(["2000-06-30"]).values, + 3: pd.DatetimeIndex(["2000-06-30"]), + } + origin = cl.Triangle.origin + development = cl.Triangle.development + + grain_mock = MockGrainAxes() + assert grain_mock.origin is not None + assert "2000" in grain_mock.development.iloc[0] + grain_mock.development_grain = "M" + assert "2000-06" in grain_mock.development.iloc[0] + + # _set_origin with PeriodIndex, DatetimeIndex, and datetime64 ndarray + t_orig = raa.copy() + t_orig.origin = pd.period_range("2000", periods=10, freq="Y-DEC") + assert len(t_orig._axes[2]) == 10 + t_orig.origin = pd.date_range("2000", periods=10, freq="YS") + assert len(t_orig._axes[2]) == 10 + t_orig.origin = pd.date_range("2000", periods=10, freq="YS").values + assert len(t_orig._axes[2]) == 10 + + # (All) origin fallback when _axes does not have 2 + class MockAllNoAxes: + origin_grain = "Y" + origin_close = "DEC" + _axes = {} + origin = cl.Triangle.origin + + all_mock = MockAllNoAxes() + all_mock.origin = "(All)" + assert len(all_mock._axes[2]) == 1 + + # _set_development with PeriodIndex, DatetimeIndex, and datetime64 ndarray + val_tri_cov = raa.dev_to_val() + val_tri_cov.development = pd.date_range("2000", periods=10, freq="YS") + assert len(val_tri_cov._axes[3]) == 10 + val_tri_cov.development = pd.period_range("2000", periods=10, freq="Y") + assert len(val_tri_cov._axes[3]) == 10 + val_tri_cov.development = pd.date_range("2000", periods=10, freq="YS").values + assert len(val_tri_cov._axes[3]) == 10 + + # non-val tri development with PeriodIndex and datetime64 + non_val = raa.copy() + non_val.development = pd.period_range("2000", periods=10, freq="Y") + assert len(non_val._axes[3]) == 10 + non_val.development = pd.date_range("2000", periods=10, freq="YS").values + assert len(non_val._axes[3]) == 10 + + # _set_development valuation string parsing + val_single = val_tri_cov.iloc[:, :, :1, :1].copy() + val_single.development = "2005" + assert val_single.is_val_tri + + # _set_development parse error fallback + non_val_single = raa.iloc[:, :, :1, :1].copy() + non_val_single.development = "unparseable_string" + assert non_val_single._axes[3][0] == "unparseable_string" + + # _set_columns and _set_index non-TriangleBase mock + class MockFullAxes: + _axes = { + 0: pd.DataFrame({"a": [1]}), + 1: pd.Index(["v"]), + 2: np.array([1]), + 3: np.array([12]), + } + index = cl.Triangle.index + columns = cl.Triangle.columns + origin = cl.Triangle.origin + development = cl.Triangle.development + + def _len_check(self, a, b): + if len(a) != len(b): + raise ValueError("Len mismatch") + + full_mock = MockFullAxes() + full_mock.index = pd.DataFrame({"a": [2]}) + with pytest.raises(TypeError, match="index must be a pandas DataFrame"): + full_mock.index = "not a df" + full_mock.columns = "new_col" + assert list(full_mock.columns) == ["new_col"] + + # _set_axis helper + cl.core.axis._set_axis(raa, 0, raa.index) + cl.core.axis._set_axis(raa, "columns", raa.columns) + cl.core.axis._set_axis(raa, 2, raa.origin) + cl.core.axis._set_axis(raa, "development", raa.development) + + # Legacy pickle state key variations (odims/ddims string keys) + state_named_axes = raa.__dict__.copy() + state_named_axes["_axes"] = { + "index": raa.index.copy(), + "columns": raa.columns.copy(), + "origin": raa._axes[2].copy(), + "development": raa._axes[3].copy(), + } + restored_named = cl.Triangle.__new__(cl.Triangle) + restored_named.__setstate__(state_named_axes) + assert 2 in restored_named._axes + assert 3 in restored_named._axes + + state_legacy_bare = raa.__dict__.copy() + state_legacy_bare["_axes"] = {} + state_legacy_bare["odims"] = raa._axes[2].copy() + state_legacy_bare["ddims"] = raa._axes[3].copy() + restored_bare = cl.Triangle.__new__(cl.Triangle) + restored_bare.__setstate__(state_legacy_bare) + assert 2 in restored_bare._axes + assert 3 in restored_bare._axes + + def test_triangle_index_property(raa): """Verify Triangle.index descriptor and slicer reset.""" tri = raa.copy() @@ -680,11 +1064,15 @@ class TestLegacyPickleCompatibility: """Pickles saved prior to kdims/vdims migration should unpickle cleanly with warning.""" def test_legacy_kdims_vdims_migration(self, raa): - # 1. Simulate legacy serialized Triangle containing kdims and vdims + # 1. Simulate legacy serialized Triangle containing kdims, vdims, odims, and ddims + raw_odims = raa._axes[2] + raw_ddims = raa._axes[3] state = raa.__dict__.copy() state.pop("_axes", None) state["kdims"] = raa.index.values state["vdims"] = raa.columns.values + state["odims"] = raw_odims + state["ddims"] = raw_ddims restored = cl.Triangle.__new__(cl.Triangle) with pytest.warns( @@ -696,19 +1084,27 @@ def test_legacy_kdims_vdims_migration(self, raa): assert isinstance(restored.index, pd.DataFrame) assert restored.index.equals(raa.index) assert list(restored.columns) == list(raa.columns) + assert restored.origin.equals(raa.origin) + assert (restored.development == raa.development).all() assert restored.loc["Total"].shape == raa.loc["Total"].shape assert restored.equals(raa) assert 0 in restored._axes assert restored._axes[0].equals(raa.index) assert 1 in restored._axes assert restored._axes[1].equals(raa.columns) + assert 2 in restored._axes + assert 3 in restored._axes def test_legacy_pickle_fallback_without_keys(self, raa): # 2. Simulate fallback when no kdims/vdims keys are found (no warning) + raw_odims = raa._axes[2] + raw_ddims = raa._axes[3] state_empty = raa.__dict__.copy() state_empty.pop("_axes", None) state_empty.pop("kdims", None) state_empty.pop("vdims", None) + state_empty["_odims"] = raw_odims + state_empty["_ddims"] = raw_ddims restored_empty = cl.Triangle.__new__(cl.Triangle) with warnings.catch_warnings(): @@ -718,13 +1114,19 @@ def test_legacy_pickle_fallback_without_keys(self, raa): assert list(restored_empty.index.columns) == ["Total"] assert 0 in restored_empty._axes assert 1 in restored_empty._axes + assert 2 in restored_empty._axes + assert 3 in restored_empty._axes def test_migrated_pickle_roundtrip(self, raa): # 3. Verify re-pickling the migrated instance works without warnings + raw_odims = raa._axes[2] + raw_ddims = raa._axes[3] state = raa.__dict__.copy() state.pop("_axes", None) state["kdims"] = raa.index.values state["vdims"] = raa.columns.values + state["odims"] = raw_odims + state["ddims"] = raw_ddims restored = cl.Triangle.__new__(cl.Triangle) with pytest.warns( @@ -738,6 +1140,8 @@ def test_migrated_pickle_roundtrip(self, raa): assert roundtripped.equals(raa) assert 0 in roundtripped._axes assert 1 in roundtripped._axes + assert 2 in roundtripped._axes + assert 3 in roundtripped._axes def test_sort_axis_columns_out_of_order(raa): @@ -3358,6 +3762,163 @@ def test_set_development_bare_years_unaffected_by_age_support() -> None: assert list(tri.development) == ["1970"] +def test_set_development_fractional_ages(raa: Triangle) -> None: + """ + Assigning fractional ages to development preserves float values without integer truncation. + """ + tri = raa.copy() + fractional_ages = [12.5, 24.5, 36.5, 48.5, 60.5, 72.5, 84.5, 96.5, 108.5, 120.5] + tri.development = fractional_ages + assert np.allclose(tri.development.values, fractional_ages) + + +def test_set_development_fractional_pattern_round_trip(raa: Triangle) -> None: + """ + Hyphenated pattern labels with fractional ages round-trip through the development setter. + """ + tri = raa.copy() + tri.development = [ + "12.5-24.5", + "24.5-36.5", + "36.5-48.5", + "48.5-60.5", + "60.5-72.5", + "72.5-84.5", + "84.5-96.5", + "96.5-108.5", + "108.5-120.5", + "120.5-Ult", + ] + assert tri._axes[3][0] == 12.5 + assert tri._axes[3][-1] == 120.5 + + +def test_set_development_period_index_valuation_and_valuation_property( + raa: Triangle, +) -> None: + """Assigning a PeriodIndex to development expresses the axis in valuation periods and valuation works.""" + tri = raa.copy() + assert not tri.is_val_tri + p = pd.period_range("2000", periods=10, freq="Y") + tri.development = p + assert tri.is_val_tri + assert isinstance(tri.valuation, pd.DatetimeIndex) + + tri_val = raa.dev_to_val() + assert tri_val.is_val_tri + tri_val.development = pd.period_range( + "2000", periods=len(tri_val.development), freq="Y" + ) + assert tri_val.is_val_tri + assert isinstance(tri_val.valuation, pd.DatetimeIndex) + + +def test_sort_axis_fractional_pattern(raa: Triangle) -> None: + """sort_axis(3) on a pattern triangle with fractional development ages sorts correctly.""" + tri = raa.copy() + tri.is_pattern = True + tri.development = [ + "24.5-36.5", + "12.5-24.5", + "48.5-60.5", + "36.5-48.5", + "72.5-84.5", + "60.5-72.5", + "96.5-108.5", + "84.5-96.5", + "120.5-Ult", + "108.5-120.5", + ] + sorted_tri = tri.sort_axis(3) + assert sorted_tri._axes[3][0] == 12.5 + assert sorted_tri._axes[3][-1] == 120.5 + + +def test_axis_descriptor_set_unchecked(raa: Triangle) -> None: + """Test TriangleAxis.set_unchecked descriptor for index and columns.""" + from chainladder.core.axis import TriangleAxis + + tri = raa.copy() + TriangleAxis(1).set_unchecked(tri, ["Losses"]) + assert tri.columns[0] == "Losses" + TriangleAxis(0).set_unchecked(tri, tri.index) + assert tri.index.equals(raa.index) + + +def test_set_index_non_dataframe_raises(raa: Triangle) -> None: + """Assigning a non-DataFrame to index raises TypeError.""" + tri = raa.copy() + with pytest.raises(TypeError, match="index must be a pandas DataFrame"): + tri.index = "invalid" + + +def test_set_origin_all_pattern(raa: Triangle) -> None: + """Assigning '(All)' to a pattern triangle origin sets and returns '(All)' label.""" + tri_sum = raa.sum() + tri_sum.is_pattern = True + tri_sum.origin = ["(All)"] + assert str(tri_sum.origin.iloc[0]) == "(All)" + + +def test_set_development_ultimate_token(raa: Triangle) -> None: + """Assigning a pattern ending with 'Ult' maps to the 9999 ultimate sentinel.""" + tri_pat = raa.copy() + tri_pat.development = ["12-24"] * 9 + ["Ult"] + assert tri_pat._axes[3][-1] == 9999 + + +def test_set_development_year_strings_fallback_valuation(raa: Triangle) -> None: + """Passing calendar year strings to development falls back to valuation periods.""" + tri_yr = raa.copy() + tri_yr.development = [str(y) for y in range(2000, 2010)] + assert tri_yr.is_val_tri + assert isinstance(tri_yr.valuation, pd.DatetimeIndex) + + +def test_set_development_non_numeric_pattern_fallback(raa: Triangle) -> None: + """Non-numeric and multi-segment pattern strings fall through without crashing.""" + tri = raa.copy() + tri.development = ["abc-def"] * 10 + assert tri._axes[3][0] == "abc-def" + + tri.development = ["1-2-3-4"] * 10 + assert tri._axes[3][0] == "1-2-3-4" + + +def test_set_development_whole_floats_convert_to_int(raa: Triangle) -> None: + """Float development lags that are whole numbers normalize to integers.""" + tri = raa.copy() + tri.development = [12.0, 24.0, 36.0, 48.0, 60.0, 72.0, 84.0, 96.0, 108.0, 120.0] + assert np.issubdtype(tri._axes[3].dtype, np.integer) + + +def test_set_development_valuation_string_labels(raa: Triangle) -> None: + """Re-assigning matching valuation labels or ISO date strings preserves valuation mode.""" + tri_val = raa.dev_to_val() + tri_val.development = list(tri_val.development) + assert tri_val.is_val_tri + + tri_val.development = [f"{y}-12-31" for y in range(1981, 1991)] + assert tri_val.is_val_tri + + +def test_fractional_ages_predict_and_tails(raa: Triangle) -> None: + """Predict and tail fitting handle fractional development ages without integer conversion errors.""" + import chainladder as cl + + tri = raa.copy() + tri.development = [12.5, 24.5, 36.5, 48.5, 60.5, 72.5, 84.5, 96.5, 108.5, 120.5] + model = cl.Chainladder().fit(tri) + pred = model.predict(tri) + assert pred is not None + + dev_tri = cl.Development().fit_transform(tri) + tail = cl.TailConstant(1.05).fit(dev_tri) + assert tail.ldf_ is not None + assert tail.ldf_._axes[3][0] == 12.5 + assert tail.ldf_._axes[3][-1] == 132.5 + + def test_input_validation_non_numeric_columns_raises() -> None: """ Initialize a triangle with reported losses as an invalid string data type. Should raise a TypeError. diff --git a/chainladder/core/triangle.py b/chainladder/core/triangle.py index bb76492a5..a1fcdb994 100644 --- a/chainladder/core/triangle.py +++ b/chainladder/core/triangle.py @@ -11,9 +11,16 @@ import numpy as np import warnings from chainladder.core.base import TriangleBase -from chainladder.core.axis import TriangleAxis -from chainladder.core.axis import _set_columns -from chainladder.core.axis import _set_index +from chainladder.core.axis import ( + TriangleAxis, + _get_development, + _get_origin, + _set_axis, + _set_columns, + _set_development, + _set_index, + _set_origin, +) from chainladder.utils.sparse import sp from chainladder.core.slice import VirtualColumns from chainladder.core.correlation import DevelopmentCorrelation, ValuationCorrelation @@ -44,7 +51,6 @@ from pandas import DataFrame, Series from chainladder.core.style import Styler from chainladder.core.typing import BackendArray - from numpy.typing import ArrayLike from pandas._libs.tslibs.timestamps import Timestamp # noqa from pandas.core.interchange.dataframe_protocol import DataFrame as DataFrameXchg @@ -491,6 +497,33 @@ class Triangle(TriangleBase): fset=_set_columns, doc="Represents the column axis of the triangle.", ) + origin = TriangleAxis( + 2, + fget=_get_origin, + fset=_set_origin, + doc="Represents the origin axis of the triangle.", + ) + development = TriangleAxis( + 3, + fget=_get_development, + fset=_set_development, + doc="Represents the development axis of the triangle.", + ) + + def _set_axis(self, axis: int | str, value: Any, check: bool = True) -> None: + """ + Assign new labels to the specified axis. + + Parameters + ---------- + axis : int or str + The target axis (0..3 or 'index', 'columns', 'origin', 'development'). + value : Any + The new labels/values for the axis. + check : bool, default True + If False, store value without length validation. + """ + _set_axis(self, axis, value, check) @_deprecated_rename_argument("development", "valuation", remove_in_version="v2.0") @_deprecated_rename_argument( @@ -653,16 +686,14 @@ def __init__( data_agg[index[0]] = "Total" index_idx: np.ndarray - self.odims: np.ndarray orig_idx: np.ndarray - self.ddims: ArrayLike dev_idx: np.ndarray index_values, index_idx = self._factorize_index(data_agg, index) self.index = pd.DataFrame(list(index_values), columns=index) self.columns = columns - self.odims, orig_idx = self._set_odims(data_agg, date_axes) - self.ddims, dev_idx = self._set_ddims(data_agg, date_axes) + self.origin, orig_idx = self._factorize_origin(data_agg, date_axes) + self.development, dev_idx = self._factorize_development(data_agg, date_axes) # Set remaining triangle properties. val_date: Timestamp = data_agg["__development__"].max() @@ -699,24 +730,23 @@ def __init__( ] # Coerce malformed triangles to something more predictable. - check_origin: np.ndarray = ( - pd - .period_range( - start=self.odims.min(), - end=self.valuation_date, - freq=self.origin_grain.replace("S", "2Q") - + ("" if self.origin_grain == "M" else "-" + self.origin_close), - ) - .to_timestamp() - .values - ) - if ( - len(check_origin) != len(self.odims) + not self.is_pattern and pd.to_datetime(options.ULT_VAL) != self.valuation_date - and not self.is_pattern ): - self.odims: np.ndarray = check_origin + check_origin: np.ndarray = ( + pd + .period_range( + start=self.origin.to_timestamp().min(), + end=self.valuation_date, + freq=self.origin_grain.replace("S", "2Q") + + ("" if self.origin_grain == "M" else "-" + self.origin_close), + ) + .to_timestamp() + .values + ) + if len(check_origin) != len(self.origin): + self.origin = check_origin # Set the Triangle values. coords: np.ndarray @@ -743,8 +773,8 @@ def __init__( shape=( len(self.index), len(self.columns), - len(self.odims), - len(self.ddims), + len(self.origin), + len(self.development), ), ) ), @@ -761,15 +791,15 @@ def __init__( # Deal with special properties if self.is_pattern: obj = self.dropna() - self.odims = obj.odims - self.ddims = obj.ddims self.values = obj.values + self.origin = obj.origin + self.development = obj.development if ult: - obj = concat((self.dev_to_val().iloc[..., : len(ult.odims), :], ult), -1) + obj = concat((self.dev_to_val().iloc[..., : len(ult.origin), :], ult), -1) obj = obj.val_to_dev() - self.odims = obj.odims - self.ddims = obj.ddims self.values = obj.values + self.origin = obj.origin + self.development = obj.development self.valuation_date = pd.Timestamp(options.ULT_VAL) @staticmethod @@ -810,7 +840,7 @@ def _split_ult( cumulative=cumulative, duplicates=duplicates, ) - ult.ddims = pd.DatetimeIndex([options.ULT_VAL]) + ult.development = pd.DatetimeIndex([options.ULT_VAL]) data = data[data[valuation[0]] != options.ULT_VAL] return data, ult @@ -841,145 +871,6 @@ def axes(self) -> list: def columns_label(self) -> list: return self.columns.to_list() - @property - def origin(self): - """ - Origin periods of the Triangle as a ``PeriodIndex``. - - The frequency of the index reflects ``origin_grain`` (e.g. annual, - quarterly, monthly). When the Triangle holds aggregated patterns with a - single origin row, the property returns ``Series(['(All)'])`` instead. - - Returns - ------- - PeriodIndex or Series - One entry per origin period of the Triangle. - - Examples - -------- - Annual-origin Triangle. - - .. testsetup:: - - import chainladder as cl - - .. testcode:: - - tr = cl.load_sample('ukmotor') - print(tr.origin.year.tolist()) - - .. testoutput:: - - [2007, 2008, 2009, 2010, 2011, 2012, 2013] - - The number of origin periods matches ``Triangle.shape[-2]``. - - .. testcode:: - - print(len(tr.origin) == tr.shape[-2]) - - .. testoutput:: - - True - """ - if self.is_pattern and len(self.odims) == 1: - return pd.Series(["(All)"]) - else: - freq = { - "S": "2Q", - "H": "2Q", - }.get(self.origin_grain, self.origin_grain) - freq = freq if freq == "M" else freq + "-" + self.origin_close - return pd.DatetimeIndex(self.odims, name="origin").to_period(freq=freq) - - @origin.setter - def origin(self, value) -> None: - self._len_check(self.origin, value) - freq = { - "S": "2Q", - }.get(self.origin_grain, self.origin_grain) - freq = freq if freq == "M" else freq + "-" + self.origin_close - value = pd.PeriodIndex(list(value), freq=freq) - self.odims = value.to_timestamp().values - - @property - def development(self): - """ - Development periods of the Triangle as a Series. - - For a development-lag Triangle (``is_val_tri=False``), values are - integer lags expressed in months from the start of the origin period. - For a valuation Triangle (``is_val_tri=True``), values are calendar - valuation labels formatted at ``development_grain``. For pattern - Triangles, labels carry their from/to development range (e.g. - ``"12-24"`` or ``"108-Ult"``). - - Returns - ------- - Series - One entry per development column of the Triangle. - - Examples - -------- - Annual-grain development on a loss Triangle is reported as month lags. - - .. testsetup: - - import chainladder as cl - - .. testcode:: - - tr = cl.load_sample('ukmotor') - print(tr.development.tolist()) - - .. testoutput:: - - [12, 24, 36, 48, 60, 72, 84] - - On a valuation Triangle the labels become calendar periods. - - .. testcode:: - - print(tr.dev_to_val().development.tolist()) - - .. testoutput:: - - ['2007', '2008', '2009', '2010', '2011', '2012', '2013'] - - On a link-ratio (pattern) Triangle the labels span the from/to lags. - - .. testcode:: - - print(tr.link_ratio.development.tolist()) - - .. testoutput:: - - ['12-24', '24-36', '36-48', '48-60', '60-72', '72-84'] - """ - ddims = self.ddims.copy() - if self.is_val_tri: - formats = {"Y": "%Y", "S": "%YQ%q", "Q": "%YQ%q", "M": "%Y-%m"} - ddims = ddims.to_period( - freq=self.development_grain.replace("S", "2Q") - ).strftime(formats[self.development_grain]) - elif self.is_pattern: - offset = self._dstep()["M"][self.development_grain] - if self.is_ultimate: - ddims[-1] = ddims[-2] + offset - if self.is_cumulative: - ddims = ["{}-Ult".format(ddims[i]) for i in range(len(ddims))] - else: - ddims = [ - "{}-{}".format(ddims[i], ddims[i] + offset) - for i in range(len(ddims)) - ] - return pd.Series(list(ddims), name="development") - - @development.setter - def development(self, value): - self._len_check(self.development, value) - self.ddims = np.array([value] if type(value) is str else value) - @property def style(self) -> Styler: """ @@ -1061,7 +952,7 @@ def is_val_tri(self): True """ - return isinstance(self.ddims, pd.DatetimeIndex) + return isinstance(self._axes.get(3), pd.DatetimeIndex) @property def is_full(self) -> bool: @@ -1300,7 +1191,7 @@ def latest_diagonal(self) -> Triangle: # already one column wide, so the development age would survive; the # column is the latest valuation either way, so say so here rather # than widening the shared rule for every development aggregation. - obj.ddims = pd.DatetimeIndex( + obj.development = pd.DatetimeIndex( [self.valuation_date], dtype=__dt64_dtype__, freq=None ) return obj @@ -1350,7 +1241,7 @@ def link_ratio(self) -> Triangle: if not obj.is_full: obj = obj[obj.valuation < obj.valuation_date] if hasattr(obj, "w_"): - w_ = obj.w_[..., : len(obj.odims), :] + w_ = obj.w_[..., : len(obj.origin), :] obj = obj * w_ if obj.shape == w_.shape else obj obj.is_pattern = True obj.is_cumulative = False @@ -1640,7 +1531,7 @@ def _val_dev(self, sign, inplace=False): if ( sign == -1 and obj.shape[-1] == 1 - and obj.ddims[0] == obj.valuation_date + and obj._axes[3][0] == obj.valuation_date and (obj.values.coords[-2] == 0).all() ): # Unique edge case #239: val_to_dev measures a single latest-diagonal @@ -1649,17 +1540,17 @@ def _val_dev(self, sign, inplace=False): offset = offset[-1:] * sign offset = offset[obj.values.coords[-2]] * sign # [0] obj.values.coords[-1] = obj.values.coords[-1] + offset - ddims = obj.valuation[obj.valuation <= obj.valuation_date] - ddims = len(ddims.drop_duplicates()) - if ddims == 1 and sign == -1: - ddims = len(obj.odims) + dev_len = obj.valuation[obj.valuation <= obj.valuation_date] + dev_len = len(dev_len.drop_duplicates()) + if dev_len == 1 and sign == -1: + dev_len = len(obj.origin) if obj.values.density > 0: if obj.values.coords[-1].min() < 0: obj.values.coords[-1] = obj.values.coords[-1] - min( obj.values.coords[-1].min(), min_slide ) - ddims = np.max([np.max(obj.values.coords[-1]) + 1, ddims]) - obj.values.shape = tuple(list(obj.shape[:-1]) + [ddims]) + dev_len = np.max([np.max(obj.values.coords[-1]) + 1, dev_len]) + obj.values.shape = tuple(list(obj.shape[:-1]) + [dev_len]) if not options.AUTO_SPARSE or backend == "cupy": obj = obj.set_backend(backend) else: @@ -1742,11 +1633,11 @@ def dev_to_val(self, inplace=False): else: obj = self obj = obj._val_dev(1, inplace) - ddims = obj.valuation[obj.valuation <= obj.valuation_date] - obj.ddims = ddims.drop_duplicates().sort_values() + val_dev = obj.valuation[obj.valuation <= obj.valuation_date] + obj.development = val_dev.drop_duplicates().sort_values() if self.is_full: if self.is_ultimate: - ultimate.ddims = pd.DatetimeIndex(ultimate.valuation[0:1]) + ultimate.development = pd.DatetimeIndex(ultimate.valuation[0:1]) obj = concat((obj, ultimate), -1) if is_cumulative: obj = obj.incr_to_cum(inplace=inplace) @@ -1800,18 +1691,29 @@ def val_to_dev(self, inplace=False): return self.copy() if self.is_ultimate and self.shape[-1] > 1: ultimate = self.iloc[..., -1:] - ultimate.ddims = np.array([9999]) + ultimate.development = np.array([9999]) obj = self.iloc[..., :-1]._val_dev(-1, inplace) else: obj = self.copy()._val_dev(-1, inplace) val_0 = obj.valuation[0] - if self.ddims.shape[-1] == 1 and self.ddims[0] == self.valuation_date: - origin_0 = pd.to_datetime(obj.odims[-1]) + if isinstance(obj.origin, pd.PeriodIndex): + origin_ts = obj.origin.to_timestamp() + if len(self.development) == 1 and self.valuation[0] == self.valuation_date: + origin_0 = pd.to_datetime(origin_ts[-1]) + else: + origin_0 = pd.to_datetime(origin_ts[0]) else: - origin_0 = pd.to_datetime(obj.odims[0]) + origin_0 = pd.to_datetime( + obj._axes[2][-1] + if ( + len(self.development) == 1 + and self.valuation[0] == self.valuation_date + ) + else obj._axes[2][0] + ) lag_0 = (val_0.year - origin_0.year) * 12 + val_0.month - origin_0.month + 1 scale = self._dstep()["M"][obj.development_grain] - obj.ddims = np.arange(obj.values.shape[-1]) * scale + lag_0 + obj.development = np.arange(obj.values.shape[-1]) * scale + lag_0 prune = obj[obj.origin == obj.origin.max()] if self.is_ultimate and self.shape[-1] > 1: obj = obj.iloc[..., : (prune.valuation <= prune.valuation_date).sum()] @@ -2000,11 +1902,14 @@ def grain(self, grain="", trailing=False, inplace=False): if dgrain_old == "S": d_start = d_start + pd.DateOffset(months=-3) - if len(obj.ddims) > 1 and obj.origin.to_timestamp(how="s")[0] != d_start: + if ( + len(obj.development) > 1 + and obj.origin.to_timestamp(how="s")[0] != d_start + ): addl_ts = ( pd .period_range( - obj.odims[0], + obj.origin.to_timestamp()[0], obj.valuation[0], freq=dgrain_old.replace("S", "2Q"), )[:-1] @@ -2012,7 +1917,7 @@ def grain(self, grain="", trailing=False, inplace=False): .values ) addl = obj.iloc[..., -len(addl_ts) :] * 0 - addl.ddims = addl_ts + addl.development = addl_ts obj = concat((addl, obj), axis=-1) obj.values = num_to_nan(obj.values) @@ -2025,10 +1930,10 @@ def grain(self, grain="", trailing=False, inplace=False): if obj.is_cumulative: obj = obj.iloc[..., d] else: - ddims = obj.ddims[d] + dev = obj.development[d] d2 = [d[0]] * (d[0] + 1) + list(np.repeat(np.array(d[1:]), step)) obj = obj.groupby(d2, axis=3).sum() - obj.ddims = ddims + obj.development = dev obj.development_grain = dgrain_new @@ -2192,8 +2097,16 @@ def copy(self): return X def __setstate__(self, state: dict) -> None: - """Migrate legacy pickled instances with 'kdims' and 'vdims' to '_axes'.""" - has_legacy_keys = any(k in state for k in ("kdims", "vdims")) + """Migrate legacy pickled instances with 'kdims'/'vdims' to '_axes' and 'odims'/'ddims' to private equivalents.""" + has_legacy_keys = any( + k in state + for k in ( + "kdims", + "vdims", + "odims", + "ddims", + ) + ) if has_legacy_keys: warnings.warn( "A legacy pickled Triangle instance was detected. Please re-save your " @@ -2217,6 +2130,26 @@ def __setstate__(self, state: dict) -> None: state["_axes"][1] = pd.Index(raw_cols, name="columns") else: state["_axes"][1] = pd.Index(["values"], name="columns") + if 2 not in state["_axes"]: + if "_odims" in state: + state["_axes"][2] = state.pop("_odims") + elif "origin" in state["_axes"]: + state["_axes"][2] = state["_axes"].pop("origin") + elif "odims" in state: + state["_axes"][2] = state.pop("odims") + else: + state.pop("_odims", None) + state.pop("odims", None) + if 3 not in state["_axes"]: + if "_ddims" in state: + state["_axes"][3] = state.pop("_ddims") + elif "development" in state["_axes"]: + state["_axes"][3] = state["_axes"].pop("development") + elif "ddims" in state: + state["_axes"][3] = state.pop("ddims") + else: + state.pop("_ddims", None) + state.pop("ddims", None) self.__dict__.update(state) def development_correlation(self, p_critical=0.5): @@ -2500,15 +2433,26 @@ def sort_axis(self, axis): obj.values = obj.values[:, list(sort), ...] obj.columns = self.columns[list(sort)] if axis == 2: - sort = pd.Series(self.odims).sort_values().index - if np.any(sort != pd.Series(self.odims).index): + sort = pd.Series(self.origin).sort_values().index + if np.any(sort != pd.Series(self.origin).index): obj.values = obj.values[..., list(sort), :] - obj.odims = obj.odims[list(sort)] + obj.origin = self.origin[list(sort)] if axis == 3: - sort = self.development.sort_values().index - if np.any(sort != self.development.index): + if self.is_pattern: + + def _parse_dev_sort(x): + try: + return float(str(x).split("-")[0].strip()) + except ValueError: + return float("inf") + + sort = self.development.map(_parse_dev_sort).sort_values().index + else: + sort = pd.Series(self.development).sort_values().index + if np.any(sort != pd.Series(self.development).index): obj.values = obj.values[..., list(sort)] - obj.ddims = obj.ddims[list(sort)] + obj.development = self.development.iloc[list(sort)].values + obj._set_slicers() return obj def reindex(self, columns=None, fill_value=np.nan): diff --git a/chainladder/core/typing.py b/chainladder/core/typing.py index 29496b61c..1d9edff14 100644 --- a/chainladder/core/typing.py +++ b/chainladder/core/typing.py @@ -14,6 +14,13 @@ TypeAlias, ) +from chainladder.utils.utility_functions import ( + _axis_orders as _axis_orders, + _axis_to_axis_number as _axis_to_axis_number, + _get_axis_name as _get_axis_name, + _get_axis_number as _get_axis_number, +) + if TYPE_CHECKING: from collections.abc import Callable from numpy import ndarray @@ -122,7 +129,9 @@ def drop( def val_to_dev(self) -> Triangle: ... def _repr_format(self, origin_as_datetime: bool = False) -> pd.DataFrame: ... def _slice( - self, key: pd.Series | np.ndarray, axis: Literal["ddims", "odims"] + self, + key: pd.Series | np.ndarray, + axis: Literal["development", "origin", "ddims", "odims"], ) -> Triangle: ... def _slice_valuation(self, key: np.ndarray) -> Triangle: ... def to_frame( @@ -164,3 +173,5 @@ def __setitem__( key: str | int, value: int | float | TriangleSlicer | Callable[[Triangle], TriangleSlicer], ) -> None: ... + def _get_axis_number(self, axis: int | str | None) -> int: ... + def _get_axis(self, axis: int | str | None) -> int: ... diff --git a/chainladder/development/clark.py b/chainladder/development/clark.py index ec45bf93b..e1b35eee8 100644 --- a/chainladder/development/clark.py +++ b/chainladder/development/clark.py @@ -215,11 +215,11 @@ def G_(self, age): # noqa: N802 if isinstance(age, (int, float, xp.int64, xp.float64)): age = xp.array([age]).astype("float64") if isinstance(age, list): - age = xp.array([age]).astype("float64") + age = xp.array(age).astype("float64") obj = self.incremental_act_.copy() - obj.odims = obj.odims[0:1] obj.values = 1 / self._G(age) - obj.ddims = age + obj.origin = self.incremental_act_.origin[0:1] + obj.development = age return obj def fit(self, X, y=None, sample_weight=None): @@ -260,13 +260,15 @@ def fit(self, X, y=None, sample_weight=None): age_offset = {"Y": 6.0, "S": 3, "Q": 1.5, "M": 0.5}[X.development_grain] age_interval = {"Y": 12.0, "S": 6.0, "Q": 3.0, "M": 1.0}[X.development_grain] nans = nan_triangle.reshape(1, -1)[0] - age = xp.tile(X.ddims, len(X.odims))[~xp.isnan(nans)].astype("float64") + age = xp.tile(X.development.values, len(X.origin))[~xp.isnan(nans)].astype( + "float64" + ) age_end = age - age_offset age_start = xp.maximum(age_end - age_interval, 0).astype("float64") - origin = np.repeat(X.odims, len(X.ddims))[~xp.isnan(nans)] + origin = np.repeat(X.origin, len(X.development))[~xp.isnan(nans)] latest_diagonal = obj[X.valuation == X.valuation_date].sum("origin") - latest_age = latest_diagonal.ddims.astype("float64") - latest_origin = X.odims + latest_age = latest_diagonal.development.values.astype("float64") + latest_origin = X.origin latest_diagonal = latest_diagonal.values[..., 0, :] index = xp.argsort(latest_origin) @@ -325,8 +327,8 @@ def ldf(age): latest_age - age_offset, theta=params[..., 1:2], omega=params[..., 0:1] ) obj.values = cdf[..., :-1] / cdf[..., 1:] - obj.ddims = X.link_ratio.ddims - obj.odims = obj.odims[0:1] + obj.development = X.link_ratio.development + obj.origin = obj.origin[0:1] obj.is_pattern = True obj.is_cumulative = False obj._set_slicers() @@ -356,8 +358,11 @@ def ldf(age): self.incremental_fits_.array_backend = "numpy" self.incremental_fits_.values = ( ( - 1 / self._G(X.ddims - age_offset) - - 1 / self._G(xp.maximum(X.ddims - age_offset - age_interval, 0)) + 1 / self._G(X.development.values - age_offset) + - 1 + / self._G( + xp.maximum(X.development.values - age_offset - age_interval, 0) + ) ) * ultimate_[..., ::-1] * nan_triangle @@ -413,7 +418,7 @@ def scale_(self): ) df = xp.nansum(self.incremental_fits_.nan_triangle) - 2 if self.method_ == "ldf": - df = df - len(self.incremental_fits_.odims) + df = df - len(self.incremental_fits_.origin) else: df = df - 1 if scale.shape != (): diff --git a/chainladder/development/constant.py b/chainladder/development/constant.py index b51e110c7..5b5018fec 100644 --- a/chainladder/development/constant.py +++ b/chainladder/development/constant.py @@ -169,31 +169,31 @@ def fit(self, X, y=None, sample_weight=None): else: raise ValueError("callable axis needs to be 0 or 1") pattern_length = len(sample_pattern) - pattern_ddims = sorted(sample_pattern.keys()) + pattern_dev = sorted(sample_pattern.keys()) patterns_dict = None else: patterns_dict = self._normalize_patterns(self.patterns, self.style) pattern_length = len(patterns_dict) - pattern_ddims = sorted(patterns_dict.keys()) + pattern_dev = sorted(patterns_dict.keys()) # pattern supplied is much shorter than the triangle - if pattern_length < len(obj.ddims) - 1: + if pattern_length < len(obj.development) - 1: obj = obj.iloc[..., 0, :-1] * 0 + 1 # pattern supplied is exactly one short of the triangle - elif pattern_length == len(obj.ddims) - 1: + elif pattern_length == len(obj.development) - 1: obj = obj.iloc[..., 0, :-1] * 0 + 1 # pattern supplied is exactly the same length as the triangle - elif pattern_length == len(obj.ddims): + elif pattern_length == len(obj.development): obj = obj.iloc[..., 0, :] * 0 + 1 # pattern supplied is longer than the triangle else: obj = obj.iloc[..., 0, :] * 0 + 1 - extra = len(pattern_ddims) - len(obj.ddims) + extra = len(pattern_dev) - len(obj.development) if extra > 0: tail = xp.ones(obj.shape)[..., -1:] tail = xp.repeat(tail, extra, -1) obj.values = xp.concatenate((obj.values, tail), -1) - obj.ddims = np.array(pattern_ddims) + obj.development = np.array(pattern_dev) obj._set_slicers() if callable(self.patterns): @@ -202,7 +202,7 @@ def fit(self, X, y=None, sample_weight=None): ldf = ( pd .concat(ldf.apply(pd.DataFrame, index=[0]).values, axis=0) - .fillna(1)[obj.ddims] + .fillna(1)[obj.development] .values ) ldf = xp.array(ldf[:, None, None, :]) @@ -211,14 +211,14 @@ def fit(self, X, y=None, sample_weight=None): ldf = ( pd .concat(ldf.apply(pd.DataFrame, index=[0]).values, axis=0) - .fillna(1)[obj.ddims] + .fillna(1)[obj.development] .values ) ldf = xp.array(ldf[None, :, None, :]) else: raise ValueError("callable axis needs to be 0 or 1") else: - ldf = xp.array([patterns_dict.get(item, 1.0) for item in obj.ddims]) + ldf = xp.array([patterns_dict.get(item, 1.0) for item in obj.development]) ldf = ldf[None, None, None, :] if self.style == "cdf": @@ -236,7 +236,12 @@ def fit(self, X, y=None, sample_weight=None): @staticmethod def _normalize_patterns(patterns, style="ldf"): """Normalize pattern input (dict, Series, DataFrame, Triangle, estimator) into a dict.""" - is_triangle = hasattr(patterns, "odims") and hasattr(patterns, "ddims") + is_triangle = ( + not isinstance(patterns, (pd.DataFrame, pd.Series, dict)) + and hasattr(patterns, "shape") + and len(patterns.shape) == 4 + and hasattr(patterns, "development") + ) if not is_triangle: if hasattr(patterns, "cdf_") and style == "cdf": patterns = patterns.cdf_ @@ -259,18 +264,20 @@ def _normalize_patterns(patterns, style="ldf"): ) patterns = patterns.ldf_ - if hasattr(patterns, "odims") and hasattr(patterns, "ddims"): - if ( - hasattr(patterns, "shape") - and len(patterns.shape) >= 3 - and patterns.shape[2] > 1 - ): + if ( + not isinstance(patterns, (pd.DataFrame, pd.Series, dict)) + and hasattr(patterns, "shape") + and len(patterns.shape) == 4 + and hasattr(patterns, "development") + ): + if patterns.shape[2] > 1: raise ValueError( "Triangle pattern must have a single origin row (e.g. model.ldf_ or model.cdf_)." ) vals = patterns.values[0, 0, 0, :] vals = vals.todense() if hasattr(vals, "todense") else np.asarray(vals) - s = pd.Series(vals, index=patterns.ddims, dtype=float) + dev = patterns.development + s = pd.Series(vals, index=dev, dtype=float) elif isinstance(patterns, pd.DataFrame): if patterns.shape[0] == 1: s = patterns.iloc[0].astype(float) @@ -291,7 +298,9 @@ def _normalize_patterns(patterns, style="ldf"): parsed_index = [] for x in s.index: try: - parsed_index.append(int(str(x).split("-")[0])) + part = str(x).split("-")[0].strip() + val = float(part) + parsed_index.append(int(val) if val.is_integer() else val) except (ValueError, TypeError): parsed_index.append(x) s.index = parsed_index diff --git a/chainladder/development/development.py b/chainladder/development/development.py index a97dc5582..f27172918 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -565,7 +565,7 @@ def _param_property(self, X, params, idx): obj = X[X.origin == X.origin.min()] xp = X.get_array_module() obj.values = xp.ones(obj.shape)[..., :-1] * params[..., idx : idx + 1, :] - obj.ddims = X.link_ratio.ddims + obj.development = X.link_ratio.development obj.valuation_date = pd.to_datetime(options.ULT_VAL) obj.is_pattern = True obj.is_cumulative = False diff --git a/chainladder/development/incremental.py b/chainladder/development/incremental.py index 0d7195af3..5f51c8cb4 100644 --- a/chainladder/development/incremental.py +++ b/chainladder/development/incremental.py @@ -211,7 +211,7 @@ def fit(self, X, y=None, sample_weight=None): Returns the instance itself. """ # check dev lag - if not isinstance(X.ddims, np.ndarray): + if X.is_val_tri: raise ValueError("Triangle must be expressed with development lags") # convert to numpy if X.array_backend == "sparse": @@ -263,7 +263,7 @@ def fit(self, X, y=None, sample_weight=None): self.zeta_ = self._param_property(x, self.params_.slope_[..., 0][..., None, :]) # to consolidate under full_triangle_ - y_ = xp.repeat(self.zeta_.values, len(x.odims), -2) + y_ = xp.repeat(self.zeta_.values, len(x.origin), -2) obj = x.copy() keeps = ( 1 diff --git a/chainladder/development/munich.py b/chainladder/development/munich.py index cad2efd34..32eab21bb 100644 --- a/chainladder/development/munich.py +++ b/chainladder/development/munich.py @@ -354,8 +354,8 @@ def _get_mcl_cdf(self, X, munich_full_triangle_): """ xp = X.get_array_module() obj = X.cdf_.copy() - obj.values = xp.repeat(obj.values, len(X.odims), 2) - obj.odims = X.odims + obj.values = xp.repeat(obj.values, len(X.origin), 2) + obj.origin = X.origin if type(self.paid_to_incurred) is tuple: p_to_i = [self.paid_to_incurred] else: @@ -380,7 +380,7 @@ def _set_ldf(self, X, cdf): ldf_tri = ldf_tri[..., :-1] / ldf_tri[..., 1:] obj = cdf.copy() obj.values = ldf_tri - obj.ddims = X.link_ratio.ddims + obj.development = X.link_ratio.development obj.is_pattern = True obj.is_cumulative = False obj._set_slicers() @@ -400,9 +400,9 @@ def _reshape(self, measure): @property def lambda_(self): obj = self.ldf_.copy() - obj.odims = obj.odims[0:1] - obj.ddims = obj.ddims[0:1] obj.values = self._reshape("lambda_coef_") + obj.origin = obj.origin[0:1] + obj.development = obj.development[0:1] return obj.to_frame(origin_as_datetime=False) @property @@ -421,14 +421,14 @@ def basic_sigma_(self): def resids_(self): obj = self.ldf_.copy() obj.values = self._reshape("residual_") - obj.odims = self.cdf_.odims[: obj.values.shape[2]] + obj.origin = self.cdf_.origin[: obj.values.shape[2]] return obj @property def q_(self): obj = self.rho_.copy() - obj.odims = self.cdf_.odims obj.values = self._reshape("q_f_") + obj.origin = self.cdf_.origin[: obj.values.shape[2]] return obj @property @@ -437,6 +437,6 @@ def q_resids_(self): obj.values = self._reshape("q_resid_")[ ..., : self.residual_.shape[-2], : self.residual_.shape[-1] ] - obj.odims = obj.odims[: obj.values.shape[2]] - obj.ddims = obj.ddims[: obj.values.shape[3]] + obj.origin = obj.origin[: obj.values.shape[2]] + obj.development = obj.development[: obj.values.shape[3]] return obj diff --git a/chainladder/development/outstanding.py b/chainladder/development/outstanding.py index 184a19fd3..6a49c5f8a 100644 --- a/chainladder/development/outstanding.py +++ b/chainladder/development/outstanding.py @@ -196,13 +196,13 @@ def _set_ldf(self, X): xp = case_ldf_.get_array_module() # Broadcast triangle shape case_ldf_ = case_ldf_ * case_tri.latest_diagonal / case_tri.latest_diagonal - case_ldf_.odims = case_tri.odims + case_ldf_.origin = case_tri.origin case_ldf_.is_pattern = False case_ldf_.values = xp.concatenate( (xp.ones(list(case_ldf_.shape[:-1]) + [1]), case_ldf_.values), axis=-1 ) - case_ldf_.ddims = case_tri.ddims + case_ldf_.development = case_tri.development case_ldf_.valuation_date = case_ldf_.valuation.max() case_ldf_ = case_ldf_.dev_to_val().set_backend(self.case_ldf_.array_backend) @@ -222,7 +222,7 @@ def _set_ldf(self, X): case_tri = ( (case_ldf_ * nans.values * case_tri.latest_diagonal.values) .val_to_dev() - .iloc[..., : len(case_tri.ddims)] + .iloc[..., : len(case_tri.development)] ) ld = ( case_tri[case_tri.valuation == X.valuation_date] @@ -234,7 +234,7 @@ def _set_ldf(self, X): self.paid_ldf_ * ld ).values paid = case_tri.iloc[..., :-1] * patterns - paid.ddims = case_tri.ddims[1:] + paid.development = case_tri.development[1:] paid.valuation_date = pd.Timestamp(options.ULT_VAL) # Create a full triangle of incurrds to support a multiplicative LDF paid = (paid_tri.cum_to_incr() + paid).incr_to_cum() @@ -254,7 +254,7 @@ def _set_ldf(self, X): # Convert the paid/incurred to multiplicative LDF dev = (dev.iloc[..., -1] / dev).iloc[..., :-1] dev.valuation_date = pd.Timestamp(options.ULT_VAL) - dev.ddims = X.link_ratio.ddims + dev.development = X.link_ratio.development dev.is_pattern = True dev.is_cumulative = True @@ -452,7 +452,7 @@ def fit(self, X: Triangle, y: Any = None, sample_weight: Any = None): # 4. Implied Case CDF formula (Friedland Exhibit III): # Case_CDF = [((CDF_rep - 1.0) * CDF_paid) / (CDF_paid - CDF_rep)] + 1.0 case_cdf_dict = {} - for d in obj.ddims: + for d in obj.development: if d in rep_s and d in paid_s: r = float(rep_s[d]) p = float(paid_s[d]) @@ -520,13 +520,20 @@ def _extract_cdf_series(pattern: Any, style: str = "cdf") -> pd.Series: s = pattern.iloc[:, 0].astype(float) else: raise ValueError("DataFrame pattern must have 1 row or 1 column.") - elif hasattr(pattern, "values") and hasattr(pattern, "ddims"): - s = pd.Series(pattern.values[0, 0, 0, :], index=pattern.ddims, dtype=float) + elif hasattr(pattern, "values") and hasattr(pattern, "development"): + s = pd.Series( + pattern.values[0, 0, 0, :], index=pattern.development, dtype=float + ) else: raise TypeError(f"Unsupported pattern type: {type(pattern)}") + def _parse_age(x): + part = str(x).split("-")[0].strip() + val = float(part) + return int(val) if val.is_integer() else val + try: - s.index = [int(str(x).split("-")[0]) for x in s.index] + s.index = [_parse_age(x) for x in s.index] except (ValueError, TypeError): pass s = s.sort_index() diff --git a/chainladder/development/tests/test_constant.py b/chainladder/development/tests/test_constant.py index 848fd818c..83cab1c6f 100644 --- a/chainladder/development/tests/test_constant.py +++ b/chainladder/development/tests/test_constant.py @@ -481,13 +481,14 @@ def test_constant_incr(): def test_constant_series_pattern(raa): dev = cl.Development().fit(raa) # Series with integer index - s_int = pd.Series(dev.ldf_.values[0, 0, 0, :], index=dev.ldf_.ddims) + s_int = pd.Series(dev.ldf_.values[0, 0, 0, :], index=raa.development[:-1]) model_int = cl.DevelopmentConstant(patterns=s_int, style="ldf").fit(raa) assert np.allclose(dev.ldf_.values, model_int.ldf_.values) # Series with interval string index (e.g. '12-24') s_str = pd.Series( - dev.ldf_.values[0, 0, 0, :], index=[f"{d}-{d + 12}" for d in dev.ldf_.ddims] + dev.ldf_.values[0, 0, 0, :], + index=dev.ldf_.development, ) model_str = cl.DevelopmentConstant(patterns=s_str, style="ldf").fit(raa) assert np.allclose(dev.ldf_.values, model_str.ldf_.values) @@ -501,12 +502,12 @@ def test_constant_series_pattern(raa): def test_constant_dataframe_pattern(raa): dev = cl.Development().fit(raa) # 1-row DataFrame - df_row = pd.DataFrame([dev.ldf_.values[0, 0, 0, :]], columns=dev.ldf_.ddims) + df_row = pd.DataFrame([dev.ldf_.values[0, 0, 0, :]], columns=raa.development[:-1]) model_row = cl.DevelopmentConstant(patterns=df_row, style="ldf").fit(raa) assert np.allclose(dev.ldf_.values, model_row.ldf_.values) # 1-column DataFrame - df_col = pd.DataFrame(dev.ldf_.values[0, 0, 0, :], index=dev.ldf_.ddims) + df_col = pd.DataFrame(dev.ldf_.values[0, 0, 0, :], index=raa.development[:-1]) model_col = cl.DevelopmentConstant(patterns=df_col, style="ldf").fit(raa) assert np.allclose(dev.ldf_.values, model_col.ldf_.values) @@ -580,3 +581,57 @@ def test_constant_invalid_pattern_types(raa): # Unsupported pattern type with pytest.raises(TypeError, match="Unsupported pattern type"): cl.DevelopmentConstant(patterns=[1.2, 1.1]).fit(raa) + + +def test_constant_incr_cumulative_none(): + raa_incr = cl.load_sample("raa").cum_to_incr() + raa_incr.is_cumulative = None + reported_patterns = { + 12: 4.0, + 24: 2.9, + 36: 1.8, + 48: 1.4, + 60: 1.2, + 72: 1.1, + 84: 1.03, + 96: 1.02, + 108: 1.005, + } + + result = cl.DevelopmentConstant( + patterns=reported_patterns, style="cdf" + ).fit_transform(raa_incr) + + assert np.all( + np.round(result.cdf_.to_frame().values.flatten(), 6) + == np.array([4.0, 2.9, 1.8, 1.4, 1.2, 1.1, 1.03, 1.02, 1.005]) + ) + + +def test_constant_dataframe_with_origin_development_columns(raa): + # A 1-row DataFrame containing 'origin' and 'development' as columns + # should be treated as a DataFrame pattern, not misdetected as a Triangle + df_pattern = pd.DataFrame([{"origin": 1.5, "development": 1.2, "36": 1.1}]) + norm = cl.DevelopmentConstant._normalize_patterns(df_pattern) + assert isinstance(norm, dict) + assert norm["origin"] == 1.5 + assert norm["development"] == 1.2 + + # Multi-row DataFrame with 'origin' and 'development' columns raises ValueError + # for invalid DataFrame shape rather than IndexError/AttributeError from triangle branch + df_multi = pd.DataFrame([ + {"origin": 1.5, "development": 1.2}, + {"origin": 1.3, "development": 1.1}, + ]) + with pytest.raises( + ValueError, match="DataFrame pattern must have 1 row or 1 column" + ): + cl.DevelopmentConstant._normalize_patterns(df_multi) + + # to_frame(keepdims=True) produces columns ['origin', 'development', 'values'] + dev = cl.Development().fit(raa) + df_keepdims = dev.ldf_.to_frame(keepdims=True) + with pytest.raises( + ValueError, match="DataFrame pattern must have 1 row or 1 column" + ): + cl.DevelopmentConstant._normalize_patterns(df_keepdims) diff --git a/chainladder/development/tests/test_incremental.py b/chainladder/development/tests/test_incremental.py index 58c6ed3c1..df91685f7 100644 --- a/chainladder/development/tests/test_incremental.py +++ b/chainladder/development/tests/test_incremental.py @@ -90,3 +90,13 @@ def test_pipeline(): assert np.array_equal( dev1.zeta_.values, dev2.named_steps.drop_hilo.zeta_.values, True ) + + +def test_incremental_additive_val_triangle_error(): + tri = cl.load_sample("ia_sample") + with pytest.raises( + ValueError, match="Triangle must be expressed with development lags" + ): + cl.IncrementalAdditive().fit( + tri["loss"].dev_to_val(), sample_weight=tri["exposure"].latest_diagonal + ) diff --git a/chainladder/development/tests/test_munich.py b/chainladder/development/tests/test_munich.py index 868b09b10..5fa9366fb 100644 --- a/chainladder/development/tests/test_munich.py +++ b/chainladder/development/tests/test_munich.py @@ -17,3 +17,12 @@ def test_mcl_rollforward(): munich = cl.MunichAdjustment(paid_to_incurred=[("paid", "incurred")]).fit(mcl_prior) new = munich.transform(mcl) _ = cl.Chainladder().fit(new).ultimate_ + + +def test_munich_properties(): + mcl = cl.load_sample("mcl") + munich = cl.MunichAdjustment(paid_to_incurred=[("paid", "incurred")]).fit(mcl) + assert munich.lambda_ is not None + assert munich.resids_ is not None + assert munich.q_ is not None + assert munich.q_resids_ is not None diff --git a/chainladder/development/tests/test_outstanding.py b/chainladder/development/tests/test_outstanding.py index 656c75c0f..64c19078c 100644 --- a/chainladder/development/tests/test_outstanding.py +++ b/chainladder/development/tests/test_outstanding.py @@ -233,6 +233,23 @@ def test_approach_2_pattern_input_types(): ) assert m_str._extract_cdf_series(s_str_rep).index.tolist() == ["dev_a", "dev_b"] + # Pattern with .development and .values + class MockDevPattern: + def __init__(self, vals, devs): + self.values = vals + self.development = devs + + vals_rep = np.array([[[[1.1, 1.05]]]]) + vals_paid = np.array([[[[1.3, 1.15]]]]) + mock_dev_rep = MockDevPattern(vals_rep, [12, 24]) + mock_dev_paid = MockDevPattern(vals_paid, [12, 24]) + m_mock_dev = cl.ImpliedCaseDevelopment( + reported_pattern=mock_dev_rep, + paid_pattern=mock_dev_paid, + style="cdf", + ).fit(case) + assert m_mock_dev.case_cdf_ is not None + # Triangle with development ages not present in benchmark patterns (hits case_cdf_dict[d] = 1.0 fallback) rep_partial = {12: 1.1} paid_partial = {12: 1.3} diff --git a/chainladder/methods/base.py b/chainladder/methods/base.py index 543d8c815..efef42240 100644 --- a/chainladder/methods/base.py +++ b/chainladder/methods/base.py @@ -24,7 +24,7 @@ def validate_X(self, X): # noqa: N802 obj = X.copy() if "ldf_" not in obj: obj = Development().fit_transform(obj) - if len(obj.ddims) - len(obj.ldf_.ddims) == 1: + if len(obj.development) - len(obj.ldf_.development) == 1: obj = TailConstant().fit_transform(obj) return obj.val_to_dev() @@ -39,7 +39,7 @@ def _set_ult_attr(self, ultimate): xp = ultimate.get_array_module() if ultimate.array_backend != "sparse": ultimate.values[~xp.isfinite(ultimate.values)] = xp.nan - ultimate.ddims = pd.DatetimeIndex([options.ULT_VAL]) + ultimate.development = pd.DatetimeIndex([options.ULT_VAL]) ultimate.virtual_columns.columns = {} ultimate.is_cumulative = True ultimate._set_slicers() @@ -102,7 +102,13 @@ def predict(self, X, sample_weight=None): """ X_new = X.val_to_dev() - if sum(X_new.ddims > self.ldf_.ddims.max()) > 0: + + def _parse_age(x): + s = str(x).split("-")[0].strip() + return float(s) + + ldf_max_age = max(_parse_age(x) for x in self.ldf_.development) + if sum(X_new.development > ldf_max_age) > 0: raise ValueError("X has ages that exceed those available in model.") # Before the line below, which borrows self.X_'s index when both sides # are a single row and so would erase what the caller actually passed. diff --git a/chainladder/methods/benktander.py b/chainladder/methods/benktander.py index add140083..c7ba1917a 100644 --- a/chainladder/methods/benktander.py +++ b/chainladder/methods/benktander.py @@ -298,5 +298,5 @@ def _get_ultimate(self, X, expectation): b = cdf[-1, ...] * xp.nan_to_num(expectation.set_backend(backend).values) ultimate.values = num_to_nan(a + b) ultimate.array_backend = backend - ultimate.ddims = self.cdf_.ddims[: ultimate.shape[-1]] + ultimate.development = self.cdf_.development[: ultimate.shape[-1]] return self._set_ult_attr(ultimate) diff --git a/chainladder/methods/mack.py b/chainladder/methods/mack.py index cbf9ac9d2..01bdd944c 100644 --- a/chainladder/methods/mack.py +++ b/chainladder/methods/mack.py @@ -312,7 +312,7 @@ def _get_full_std_err_(self, X=None): avg = {"regression": 0, "volume": 1, "simple": 2} avg = [avg.get(item, item) for item in X.average_] val = xp.broadcast_to(xp.array(avg + [avg[-1]]), X.shape) - weight = xp.sqrt(full.values[..., : len(X.ddims)] ** (2 - val)) + weight = xp.sqrt(full.values[..., : len(X.development)] ** (2 - val)) obj.values = X.sigma_.values / num_to_nan(weight) w = lxp.concatenate( (X.w_, lxp.ones((val.shape[0], val.shape[1], val.shape[2], 1))), 3 @@ -359,13 +359,12 @@ def total_process_risk_(self): return (self.process_risk_**2).sum(axis="origin").sqrt() def _mack_recursion(self, est, X=None): - obj = X.copy() backend = X.array_backend - xp = obj.get_array_module() - risk_arr = xp.zeros((*X.shape[:3], 1)) if est == "total_parameter_risk_": + obj = X.iloc[:, :, 0:1, :].copy() + xp = obj.get_array_module() + risk_arr = xp.zeros((*obj.shape[:3], 1)) nans = None - risk_arr = risk_arr[..., 0:1, :] future_std_err = ( X._full_triangle_ - X[X.valuation < X.valuation_date] ).iloc[:, :, :, : X.shape[3]] * X.std_err_.values @@ -373,11 +372,13 @@ def _mack_recursion(self, est, X=None): t1_t = xp.nan_to_num( future_std_err.sum("origin").set_backend(backend).values ) - obj.odims = obj.odims[0:1] else: + obj = X.copy() + xp = obj.get_array_module() + risk_arr = xp.zeros((*X.shape[:3], 1)) nans = xp.nan_to_num(X.nan_triangle[None, None]) nans = 1 - xp.concatenate((nans, xp.zeros((1, 1, X.shape[2], 1))), 3) - full_tri = X._full_triangle_.values[..., : len(X.ddims)] + full_tri = X._full_triangle_.values[..., : len(X.development)] if est == "parameter_risk_": # std_err_ is always numpy t1_t = ( @@ -386,7 +387,7 @@ def _mack_recursion(self, est, X=None): else: t1_t = xp.nan_to_num(full_tri) * self._get_full_std_err_(X).values extend = X.ldf_.shape[-1] - X.shape[-1] + 1 - ldf = X.ldf_.values[..., : len(X.ddims) - 1] + ldf = X.ldf_.values[..., : len(X.development) - 1] tail = X.cdf_.values[..., -extend : -extend + 1] ldf = xp.array(X.ldf_.get_array_module().concatenate((ldf, tail), -1)) # Recursive Mack Formula @@ -398,7 +399,9 @@ def _mack_recursion(self, est, X=None): t_tot = t_tot * nans[..., i + 1 : i + 2] risk_arr = xp.concatenate((risk_arr, xp.nan_to_num(t_tot)), 3) obj.values = risk_arr - obj.ddims = X._full_triangle_.ddims[list(range(X.shape[-1])) + [-1]] + obj.development = X._full_triangle_.development.iloc[ + list(range(X.shape[-1])) + [-1] + ].values obj.valuation_date = X._full_triangle_.valuation_date obj._set_slicers() return obj @@ -722,6 +725,6 @@ def summary_(self): self.mack_std_err_.set_backend(backend).values[..., -1:], ) obj.values = obj.get_array_module().concatenate(cols, 3) - obj.ddims = ["Latest", "IBNR", "Ultimate", "Mack Std Err"] + obj.development = ["Latest", "IBNR", "Ultimate", "Mack Std Err"] obj._set_slicers() return obj diff --git a/chainladder/methods/tests/test_capecod.py b/chainladder/methods/tests/test_capecod.py index 0d42feaf1..211ffe871 100644 --- a/chainladder/methods/tests/test_capecod.py +++ b/chainladder/methods/tests/test_capecod.py @@ -186,7 +186,10 @@ def test_capecod_onlevel_friedland_exhibit_ii(): floored_cdf = xyz_reported_dev.cdf_.maximum(1.0) floored_patterns = dict( - zip([int(age) for age in floored_cdf.ddims], floored_cdf.values.flatten()) + zip( + [int(str(age).split("-")[0]) for age in floored_cdf.development], + floored_cdf.values.flatten(), + ) ) sample_weight = onlevel.fit_transform(xyz_premium) diff --git a/chainladder/methods/tests/test_predict.py b/chainladder/methods/tests/test_predict.py index 85ff7a015..3b9f8aec2 100644 --- a/chainladder/methods/tests/test_predict.py +++ b/chainladder/methods/tests/test_predict.py @@ -423,3 +423,12 @@ def test_odd_shaped_triangle(): .sum() ) assert abs(ult1 - ult2) < 1e-5 + + +def test_predict_age_exceeds_model(): + tri = cl.load_sample("genins") + model = cl.Chainladder().fit(tri.iloc[..., :-2]) + with pytest.raises( + ValueError, match="X has ages that exceed those available in model" + ): + model.predict(tri) diff --git a/chainladder/tails/base.py b/chainladder/tails/base.py index 4797f5f15..698ac7727 100644 --- a/chainladder/tails/base.py +++ b/chainladder/tails/base.py @@ -24,26 +24,36 @@ def fit(self, X, y=None, sample_weight=None): "M": (12 * m, 1), "S": (2 * m, 6), }[obj.development_grain] - t_ddims = [ - (item + 1) * self._ave_period[1] + obj.ldf_.ddims[-1] + + def _parse_age(val): + s = str(val).split("-")[0].strip() + f = float(s) + return int(f) if f.is_integer() else f + + ldf_last_lag = _parse_age(obj.ldf_.development.iloc[-1]) + t_dev = [ + (item + 1) * self._ave_period[1] + ldf_last_lag for item in range(self._ave_period[0] + 1) ] - ddims = np.concatenate( - (obj.ldf_.ddims, t_ddims), + ldf_ages = [_parse_age(d) for d in obj.ldf_.development] + dev = np.concatenate( + (ldf_ages, t_dev), 0, ) self.ldf_ = obj.ldf_.copy() tail = xp.ones(self.ldf_.shape)[..., -1:] tail = xp.repeat(tail, self._ave_period[0] + 1, -1) self.ldf_.values = xp.concatenate((self.ldf_.values, tail), -1) - self.ldf_.ddims = ddims + self.ldf_.development = dev if hasattr(obj, "sigma_"): zeros = (obj.sigma_.iloc[..., -1:] * 0).values self.sigma_ = getattr(obj, "sigma_").copy() self.sigma_.values = xp.concatenate((self.sigma_.values, zeros), -1) self.std_err_ = getattr(obj, "std_err_").copy() self.std_err_.values = xp.concatenate((self.std_err_.values, zeros), -1) - self.sigma_.ddims = self.std_err_.ddims = self.ldf_.ddims[: obj.shape[2]] + self.sigma_.development = self.std_err_.development = self.ldf_.development[ + : self.sigma_.values.shape[3] + ] self.sigma_._set_slicers() self.std_err_._set_slicers() if hasattr(obj, "average_"): diff --git a/chainladder/tails/bondy.py b/chainladder/tails/bondy.py index 9c97fefa3..7c3f7f9a7 100644 --- a/chainladder/tails/bondy.py +++ b/chainladder/tails/bondy.py @@ -148,19 +148,25 @@ def fit(self, X, y=None, sample_weight=None): super().fit(X, y, sample_weight) if self.earliest_age is None: - earliest_age = X.ddims[-2] + earliest_age = X.development.iloc[-2] else: - earliest_age = X.ddims[ + earliest_age = X.development.iloc[ int( self.earliest_age / ({"Y": 12, "S": 6, "Q": 3, "M": 1}[X.development_grain]) ) - 1 ] - attachment_age = self.attachment_age if self.attachment_age else X.ddims[-2] + attachment_age = ( + self.attachment_age if self.attachment_age else X.development.iloc[-2] + ) obj = Development().fit_transform(X) if "ldf_" not in X else X b_optimized = [] - initial = xp.where(obj.ddims == earliest_age)[0][0] if earliest_age else 0 + initial = ( + xp.where(obj.development.values == earliest_age)[0][0] + if earliest_age + else 0 + ) for num in range(len(obj.columns)): b0 = (xp.ones(obj.ldf_.shape[0]) * 0.5)[:, None] data = xp.log(obj.ldf_.values[:, num, 0, initial:]) @@ -178,14 +184,17 @@ def fit(self, X, y=None, sample_weight=None): [item.reshape(-1, 2)[:, 1:2] for item in b_optimized], axis=1 )[..., None, None] ) - if sum(X.ddims > earliest_age) > 1: - tail = xp.exp(self.earliest_ldf_ * self.b_ ** (len(obj.ldf_.ddims) - 1)) + if sum(X.development > earliest_age) > 1: + tail = xp.exp( + self.earliest_ldf_ * self.b_ ** (len(obj.ldf_.development) - 1) + ) else: tail = self.ldf_.values[..., 0, initial] tail = tail ** (self.b_ / (1 - self.b_)) f0 = self.ldf_.values[..., 0:1, initial : initial + 1] fitted = f0 ** ( - self.b_ ** (np.arange(sum(X.ddims >= earliest_age))[None, None, None, :]) + self.b_ + ** (np.arange(sum(X.development >= earliest_age))[None, None, None, :]) ) fitted = xp.concatenate( (fitted, fitted[..., -1:] ** (self.b_ / (1 - self.b_))), axis=-1 @@ -200,8 +209,8 @@ def fit(self, X, y=None, sample_weight=None): ) self.ldf_.values = xp.concatenate( ( - self.ldf_.values[..., : sum(X.ddims <= attachment_age)], - fitted[..., -sum(X.ddims >= attachment_age) :], + self.ldf_.values[..., : sum(X.development <= attachment_age)], + fitted[..., -sum(X.development >= attachment_age) :], ), axis=-1, ) diff --git a/chainladder/tails/clark.py b/chainladder/tails/clark.py index f3740f505..be4aa48a5 100644 --- a/chainladder/tails/clark.py +++ b/chainladder/tails/clark.py @@ -1,6 +1,7 @@ # This Source Code Form is subject to the terms of the Mozilla Public # 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/. +import numpy as np from chainladder.tails import TailBase from chainladder.development.clark import ClarkLDF @@ -152,7 +153,14 @@ def fit(self, X, y=None, sample_weight=None): model = ClarkLDF(growth=self.growth).fit(X, sample_weight=sample_weight) xp = X.get_array_module() age_offset = {"Y": 6.0, "S": 3, "Q": 1.5, "M": 0.5}[X.development_grain] - fitted = 1 / model.G_(self.ldf_.ddims - age_offset) + + def _parse_age(d): + s = str(d).split("-")[0].strip() + f = float(s) + return int(f) if f.is_integer() else f + + ldf_ages = np.array([_parse_age(d) for d in self.ldf_.development]) + fitted = 1 / model.G_(ldf_ages - age_offset) fitted = xp.concatenate( ( fitted.values[..., :-1] / fitted.values[..., 1:], @@ -161,11 +169,13 @@ def fit(self, X, y=None, sample_weight=None): axis=-1, ) fitted = xp.repeat(fitted, self.ldf_.values.shape[2], 2) - attachment_age = self.attachment_age if self.attachment_age else X.ddims[-2] + attachment_age = ( + self.attachment_age if self.attachment_age else X.development.iloc[-2] + ) self.ldf_.values = xp.concatenate( ( - self.ldf_.values[..., : sum(self.ldf_.ddims < attachment_age)], - fitted[..., -sum(self.ldf_.ddims >= attachment_age) :], + self.ldf_.values[..., : sum(ldf_ages < attachment_age)], + fitted[..., -sum(ldf_ages >= attachment_age) :], ), axis=-1, ) diff --git a/chainladder/tails/constant.py b/chainladder/tails/constant.py index 4cc94957e..d720d4c70 100644 --- a/chainladder/tails/constant.py +++ b/chainladder/tails/constant.py @@ -149,9 +149,9 @@ def fit(self, X, y=None, sample_weight=None): xp = self.ldf_.get_array_module() tail = self.tail if self.attachment_age: - attach_idx = xp.min(xp.where(X.ddims >= self.attachment_age)) + attach_idx = xp.min(xp.where(X.development.values >= self.attachment_age)) else: - attach_idx = len(X.ddims) - 1 + attach_idx = len(X.development) - 1 self = self._apply_decay(X, tail, attach_idx) obj = Development().fit_transform(X) if "ldf_" not in X else X self._get_tail_stats(obj) diff --git a/chainladder/tails/curve.py b/chainladder/tails/curve.py index bb90db9fa..4d82ad918 100644 --- a/chainladder/tails/curve.py +++ b/chainladder/tails/curve.py @@ -252,9 +252,9 @@ def fit(self, X, y=None, sample_weight=None): ) tail = self._predict_tail(extrapolate) if self.attachment_age: - attach_idx = xp.min(xp.where(X.ddims >= self.attachment_age)) + attach_idx = xp.min(xp.where(X.development.values >= self.attachment_age)) else: - attach_idx = len(X.ddims) - 1 + attach_idx = len(X.development) - 1 self.ldf_.values = xp.concatenate( (self.ldf_.values[..., :attach_idx], tail[..., attach_idx:]), -1 ) diff --git a/chainladder/tails/tests/test_clark.py b/chainladder/tails/tests/test_clark.py index 8011c51fb..8f3d385c4 100644 --- a/chainladder/tails/tests/test_clark.py +++ b/chainladder/tails/tests/test_clark.py @@ -21,3 +21,13 @@ def test_truncation_age(genins: Triangle, atol: float) -> None: assert np.allclose( long_truncation.values[..., -1], no_truncation.values[..., -1], atol=atol ) + + +def test_clark_g_list_of_ages(raa: Triangle) -> None: + """ + Validate that passing a Python list of ages to G_ works without length mismatch. + """ + model = cl.ClarkLDF().fit(raa) + g = model.G_([12, 24, 36]) + assert g.shape[-1] == 3 + assert list(g.development) == [12.0, 24.0, 36.0] diff --git a/chainladder/tails/tests/test_constant.py b/chainladder/tails/tests/test_constant.py index aa34e4583..a9a6de7a6 100644 --- a/chainladder/tails/tests/test_constant.py +++ b/chainladder/tails/tests/test_constant.py @@ -18,3 +18,9 @@ def test_constant_balances(qtr): ) == 1.050 ) + + +def test_constant_attachment_age(): + tri = cl.load_sample("genins") + tail = cl.TailConstant(tail=1.05, attachment_age=48).fit(tri) + assert tail.ldf_ is not None diff --git a/chainladder/tails/tests/test_exponential.py b/chainladder/tails/tests/test_exponential.py index 7f11769cb..3e5622222 100644 --- a/chainladder/tails/tests/test_exponential.py +++ b/chainladder/tails/tests/test_exponential.py @@ -50,7 +50,9 @@ def test_fit_period(tail_sample: Triangle) -> None: assert ( round( cl - .TailCurve(fit_period=(tail_sample.ddims[-7], None), extrap_periods=10) + .TailCurve( + fit_period=(tail_sample.development.iloc[-7], None), extrap_periods=10 + ) .fit(dev) .cdf_["paid"] .set_backend("numpy", inplace=True) @@ -265,6 +267,11 @@ def test_errors_validation(tail_sample: Triangle) -> None: cl.TailCurve(errors="Ignore").fit_transform(tail_sample) +def test_tail_curve_attachment_age(tail_sample: Triangle) -> None: + tail = cl.TailCurve(attachment_age=48).fit(tail_sample) + assert tail.ldf_ is not None + + def test_no_log_warning_when_only_some_tails_exceed_one(clrd: Triangle) -> None: """ A tail at or below 1.0 must not reach ``log(tail - 1)``. diff --git a/chainladder/utils/tests/test_utilities.py b/chainladder/utils/tests/test_utilities.py index 041073152..016e9e4d7 100644 --- a/chainladder/utils/tests/test_utilities.py +++ b/chainladder/utils/tests/test_utilities.py @@ -62,8 +62,8 @@ def test_triangle_json_io(clrd): assert clrd.equals(clrd2) assert clrd.index.equals(clrd2.index) assert np.all(clrd.columns == clrd2.columns) - assert np.all(clrd.odims == clrd2.odims) - assert np.all(clrd.ddims == clrd2.ddims) + assert np.all(clrd.origin == clrd2.origin) + assert np.all(clrd.development == clrd2.development) assert np.all(clrd.valuation == clrd2.valuation) @@ -631,12 +631,12 @@ def test_concat_ignore_index_axes(raa: Triangle) -> None: o1 = raa.iloc[:, :, :5, :] o2 = raa.iloc[:, :, 5:, :] res2 = cl.concat([o1, o2], axis=2, ignore_index=True) - assert len(res2.odims) == len(raa.odims) + assert len(res2.origin) == len(raa.origin) d1 = raa.iloc[:, :, :, :5] d2 = raa.iloc[:, :, :, 5:] res3 = cl.concat([d1, d2], axis=3, ignore_index=True) - assert len(res3.ddims) == len(raa.ddims) + assert len(res3.development) == len(raa.development) def test_concat_axis_1_index_dtype_relaxed(raa: Triangle) -> None: diff --git a/chainladder/utils/utility_functions.py b/chainladder/utils/utility_functions.py index 1e92d9b59..8e480f5e1 100644 --- a/chainladder/utils/utility_functions.py +++ b/chainladder/utils/utility_functions.py @@ -734,10 +734,10 @@ def concat( ) ) if axis != 2: - a = np.array([obj.odims for obj in objs]) + a = np.array([obj._axes[2] for obj in objs]) assert np.all(a == a[0]) if axis != 3: - a = np.array([obj.ddims for obj in objs]) + a = np.array([obj._axes[3] for obj in objs]) assert np.all(a == a[0]) out = copy.deepcopy(objs[0]) @@ -766,20 +766,20 @@ def concat( out.columns = new_axis elif axis == 2: if ignore_index: - new_axis = np.arange(sum([len(obj.odims) for obj in objs])) + new_axis = np.arange(sum([len(obj.origin) for obj in objs])) else: - new_axis = np.concatenate([obj.odims for obj in objs]) + new_axis = np.concatenate([obj._axes[2] for obj in objs]) assert len(new_axis) == len(set(new_axis)) - out.odims = new_axis + out._axes[2] = new_axis elif axis == 3: if ignore_index: - new_axis = np.arange(sum([len(obj.ddims) for obj in objs])) + new_axis = np.arange(sum([len(obj.development) for obj in objs])) else: - new_axis = np.concatenate([obj.ddims for obj in objs]) + new_axis = np.concatenate([obj._axes[3] for obj in objs]) assert len(new_axis) == len(set(new_axis)) - out.ddims = new_axis - if out.ddims.dtype == __dt64_dtype__ and type(out.ddims) is np.ndarray: - out.ddims = pd.DatetimeIndex(out.ddims) + out._axes[3] = new_axis + if out._axes[3].dtype == __dt64_dtype__ and type(out._axes[3]) is np.ndarray: + out._axes[3] = pd.DatetimeIndex(out._axes[3]) out.valuation_date = pd.Series([obj.valuation_date for obj in objs]).max() out._set_slicers()