diff --git a/chainladder/development/constant.py b/chainladder/development/constant.py index b51e110c7..d512b6384 100644 --- a/chainladder/development/constant.py +++ b/chainladder/development/constant.py @@ -23,7 +23,9 @@ class DevelopmentConstant(DevelopmentBase): A representation of development factors indexed by age (in months). Can be a dictionary of age:value, a pandas Series, a 1-row or 1-column DataFrame, a 1D Triangle pattern (such as another estimator's ``ldf_`` or ``cdf_``), - or a fitted estimator. When passing a fitted estimator, the pattern matching + or a fitted estimator. A Triangle or estimator pattern must have one index + row and one column. For patterns that vary by index or column, pass a + callable. When passing a fitted estimator, the pattern matching ``style`` (``ldf_`` or ``cdf_``) is used. If the estimator only provides one pattern attribute (e.g. only ``cdf_`` or only ``ldf_``), it is used as a convenience fallback and a UserWarning is issued if ``style`` does not match. @@ -268,6 +270,13 @@ def _normalize_patterns(patterns, style="ldf"): raise ValueError( "Triangle pattern must have a single origin row (e.g. model.ldf_ or model.cdf_)." ) + if patterns.shape[0] > 1 or patterns.shape[1] > 1: + raise ValueError( + "Triangle pattern must have one index row and one column, but it " + f"has {patterns.shape[0]} index row(s) and {patterns.shape[1]} " + "column(s). Select one, or pass a callable with callable_axis to " + "use a different pattern for each index row or column." + ) 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) diff --git a/chainladder/development/tests/test_constant.py b/chainladder/development/tests/test_constant.py index 848fd818c..4f3a9e72d 100644 --- a/chainladder/development/tests/test_constant.py +++ b/chainladder/development/tests/test_constant.py @@ -553,6 +553,27 @@ def __init__(self, ldf): assert np.allclose(dev.ldf_.values, model_only_ldf.cdf_.values) +def test_constant_pattern_with_several_index_rows_or_columns(clrd): + # A pattern fit to several lines or measures holds one vector each, so + # DevelopmentConstant raises instead of giving every line the first one. + tri = clrd.groupby("LOB").sum()[["CumPaidLoss", "IncurLoss"]] + dev = cl.Development().fit(tri) + for pattern in ( + dev.ldf_, + dev, + dev.ldf_["CumPaidLoss"], + dev.ldf_.loc["wkcomp"], + ): + with pytest.raises(ValueError, match="one index row and one column"): + cl.DevelopmentConstant(patterns=pattern, style="ldf").fit(tri) + + # One line and one measure still work, and apply to every line. + one = dev.ldf_.loc["wkcomp"]["CumPaidLoss"] + model = cl.DevelopmentConstant(patterns=one, style="ldf").fit(tri["CumPaidLoss"]) + assert model.ldf_.shape == (6, 1, 1, 9) + assert np.allclose(model.ldf_.values, one.values) + + def test_constant_invalid_pattern_types(raa): # Invalid DataFrame shape (>1 row and >1 col) df_invalid = pd.DataFrame([[1.2, 1.1], [1.0, 1.0]], columns=[12, 24])