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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions chainladder/adjustments/tests/test_berqsherm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chainladder as cl
import numpy as np

import chainladder as cl


def test_preserve_diagonal():
triangle = cl.load_sample("berqsherm").loc["Auto"]
Expand All @@ -16,7 +17,7 @@ def test_preserve_diagonal():
xp.nansum((berq_triangle.latest_diagonal - triangle.latest_diagonal).values)
== 0
)
assert berq_triangle != triangle
assert not berq_triangle.equals(triangle)


def test_adjusted_values():
Expand Down
7 changes: 4 additions & 3 deletions chainladder/adjustments/tests/test_disposal.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

import chainladder as cl
from typing import TYPE_CHECKING

import numpy as np
import pytest

from typing import TYPE_CHECKING
import chainladder as cl

if TYPE_CHECKING:
from chainladder.core import Triangle
Expand Down Expand Up @@ -141,4 +142,4 @@ def test_setting_incr(raa: Triangle) -> None:
dr = cl.DisposalRate(n_periods=4).fit(raa, sample_weight=ult)
orig_disposal_rate_ = dr.disposal_rate_
dr.incr_disposal_rate_ = dr.incr_disposal_rate_ * 2
assert dr.disposal_rate_ == orig_disposal_rate_ * 2
assert dr.disposal_rate_.equals(orig_disposal_rate_ * 2)
5 changes: 3 additions & 2 deletions chainladder/adjustments/tests/test_trend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chainladder as cl
import numpy as np

import chainladder as cl


def test_trend1(clrd):
tri = clrd[["CumPaidLoss", "EarnedPremDIR"]].sum()
Expand All @@ -19,7 +20,7 @@ def test_trend1(clrd):
)
.ibnr_
)
assert np.round(lhs, 0) == np.round(rhs, 0)
assert np.round(lhs, 0).equals(np.round(rhs, 0))


def test_trend2(raa):
Expand Down
7 changes: 4 additions & 3 deletions chainladder/core/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
# 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 dill
import json

import dill
import pandas as pd

from sklearn.base import BaseEstimator
Expand Down Expand Up @@ -44,7 +45,7 @@ def to_pickle(self, path, protocol=None):
restored = cl.read_pickle(path)
os.remove(path)
print(restored.shape)
print(restored == raa)
print(restored.equals(raa))

.. testoutput::

Expand Down Expand Up @@ -78,7 +79,7 @@ def to_json(self):
raa = cl.load_sample('raa')
payload = json.loads(raa.to_json())
print(sorted(payload.keys()))
print(cl.read_json(raa.to_json()) == raa)
print(cl.read_json(raa.to_json()).equals(raa))

.. testoutput::

Expand Down
47 changes: 22 additions & 25 deletions chainladder/core/tests/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_arithmetic_ndarray_other_backend(raa: Triangle) -> None:
other = np.ones(sparse_raa.shape)
result = sparse_raa + other
assert result.array_backend == "sparse"
assert result.set_backend("numpy") == raa + 1
assert result.set_backend("numpy").equals(raa + 1)


def test_arithmetic_coo_other_backend(raa: Triangle) -> None:
Expand All @@ -53,7 +53,7 @@ def test_arithmetic_coo_other_backend(raa: Triangle) -> None:
other = COO.from_numpy(np.ones(numpy_raa.shape))
result = numpy_raa + other
assert result.array_backend == "sparse"
assert result.set_backend("numpy") == raa + 1
assert result.set_backend("numpy").equals(raa + 1)


def test_arithmetic_grain_mismatch_raises(raa: Triangle, qtr: Triangle) -> None:
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_non_overlapping_odims(raa: Triangle) -> None:
a = raa.iloc[..., :5, :]
b = raa.iloc[..., 5:, :]
result = a + b
assert result == raa
assert result.equals(raa)


def test_arithmetic_union_val_tri(raa: Triangle) -> None:
Expand All @@ -117,7 +117,7 @@ def test_arithmetic_union_val_tri(raa: Triangle) -> None:
result = a + b
assert isinstance(result.ddims, pd.DatetimeIndex)
assert result.shape == val_raa.shape
assert result == val_raa
assert result.equals(val_raa)


def test_origin_broadcasting(raa: Triangle) -> None:
Expand All @@ -137,22 +137,22 @@ def test_origin_broadcasting(raa: Triangle) -> None:
single_origin["values"] = 500
result = raa + single_origin
assert result.shape == raa.shape
assert result == raa + 500
assert result.equals(raa + 500)


def test_arithmetic_union(raa):
assert raa.shape == (raa - raa[raa.valuation < "1987"]).shape
assert raa[raa.valuation < "1986"] + raa[raa.valuation >= "1986"] == raa
assert (raa[raa.valuation < "1986"] + raa[raa.valuation >= "1986"]).equals(raa)


def test_arithmetic_across_keys(qtr):
assert (qtr.sum(1) - qtr.iloc[:, 0]) == qtr.iloc[:, 1]
assert (qtr.sum(1) - qtr.iloc[:, 0]).equals(qtr.iloc[:, 1])


def test_arithmetic_1(raa):
x = raa
assert -(((x / x) + 0) * x) == -(+x)
assert 1 - (x / x) == 0 * x * 0
assert (-(((x / x) + 0) * x)).equals(-(+x))
assert (1 - (x / x)).equals(0 * x * 0)


def test_eq_non_triangle(raa: Triangle) -> None:
Expand Down Expand Up @@ -258,7 +258,7 @@ def test_vector_division(raa: Triangle) -> None:
for i in range(raa.shape[2]):
orig = raa.iloc[..., i : i + 1, :]
ld = raa.latest_diagonal.iloc[..., i : i + 1, :]
assert result.iloc[..., i : i + 1, :] == ld / orig
assert result.iloc[..., i : i + 1, :].equals(ld / orig)


def test_multiindex_broadcast(clrd):
Expand Down Expand Up @@ -312,15 +312,15 @@ def test_index_broadcasting5(clrd):
a = clrd["CumPaidLoss"].iloc[:300]
b = clrd["CumPaidLoss"].iloc[300:]
c = a + b
assert c.sort_index() == clrd["CumPaidLoss"].sort_index()
assert c.sort_index().equals(clrd["CumPaidLoss"].sort_index())


def test_index_broadacsting6(clrd):
a = clrd["CumPaidLoss"].iloc[:100]
b = clrd["CumPaidLoss"].iloc[50:150]
c = clrd["CumPaidLoss"].iloc[50:100]
d = a + b - c
assert d.sort_index() == clrd["CumPaidLoss"].iloc[:150].sort_index()
assert d.sort_index().equals(clrd["CumPaidLoss"].iloc[:150].sort_index())


def test_index_broadcasting_ambiguous(clrd: Triangle) -> None:
Expand Down Expand Up @@ -361,11 +361,10 @@ def test_prep_columns_reindexes_superset(clrd: Triangle) -> None:
y = clrd[["CumPaidLoss", "IncurLoss"]]
for result in [x + y, y + x]:
assert set(result.columns) == {"CumPaidLoss", "EarnedPremNet", "IncurLoss"}
assert (
result[["CumPaidLoss", "IncurLoss"]]
== clrd[["CumPaidLoss", "IncurLoss"]] * 2
assert result[["CumPaidLoss", "IncurLoss"]].equals(
clrd[["CumPaidLoss", "IncurLoss"]] * 2
)
assert result["EarnedPremNet"] == clrd["EarnedPremNet"]
assert result["EarnedPremNet"].equals(clrd["EarnedPremNet"])


def test_overlapping_columns_and_origins_union_both_axes(clrd: Triangle) -> None:
Expand Down Expand Up @@ -397,23 +396,21 @@ def test_overlapping_columns_and_origins_union_both_axes(clrd: Triangle) -> None
both = both[both.origin <= "1993"]
expected = c[c.origin >= "1990"]
expected = expected[expected.origin <= "1993"]
assert both["IncurLoss"] == expected["IncurLoss"] * 2
assert both["IncurLoss"].equals(expected["IncurLoss"] * 2)

a_only = result[result.origin <= "1989"]
assert a_only["IncurLoss"] == c[c.origin <= "1989"]["IncurLoss"]
assert a_only["IncurLoss"].equals(c[c.origin <= "1989"]["IncurLoss"])

b_only = result[result.origin >= "1994"]
assert b_only["IncurLoss"] == c[c.origin >= "1994"]["IncurLoss"]
assert b_only["IncurLoss"].equals(c[c.origin >= "1994"]["IncurLoss"])

assert (
result["CumPaidLoss"][result.origin <= "1993"]
== c["CumPaidLoss"][c.origin <= "1993"]
assert result["CumPaidLoss"][result.origin <= "1993"].equals(
c["CumPaidLoss"][c.origin <= "1993"]
)
assert np.isnan(result["CumPaidLoss"][result.origin >= "1994"].values).all()

assert (
result["EarnedPremDIR"][result.origin >= "1990"]
== c["EarnedPremDIR"][c.origin >= "1990"]
assert result["EarnedPremDIR"][result.origin >= "1990"].equals(
c["EarnedPremDIR"][c.origin >= "1990"]
)
assert np.isnan(result["EarnedPremDIR"][result.origin <= "1989"].values).all()

Expand Down
44 changes: 29 additions & 15 deletions chainladder/core/tests/test_grain.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

import chainladder as cl
from typing import TYPE_CHECKING

import numpy as np
import pytest

from typing import TYPE_CHECKING
import chainladder as cl

if TYPE_CHECKING:
from chainladder import Triangle
Expand Down Expand Up @@ -32,28 +33,41 @@ def test_grain(qtr):


def test_grain_returns_valid_tri(qtr):
assert qtr.grain("OYDY").latest_diagonal == qtr.latest_diagonal
assert qtr.grain("OYDY").latest_diagonal.equals(qtr.latest_diagonal)


def test_grain_increm_arg(qtr):
clrd_i = qtr["incurred"].cum_to_incr()
a = clrd_i.grain("OYDY").incr_to_cum()
assert a == qtr["incurred"].grain("OYDY")
assert a.equals(qtr["incurred"].grain("OYDY"))


def test_commutative(qtr, atol):
full = cl.Chainladder().fit(qtr).full_expectation_
assert qtr.grain("OYDY").val_to_dev() == qtr.val_to_dev().grain("OYDY")
assert qtr.cum_to_incr().grain(
"OYDY"
).val_to_dev() == qtr.val_to_dev().cum_to_incr().grain("OYDY")
assert qtr.grain(
"OYDY"
).cum_to_incr().val_to_dev().incr_to_cum() == qtr.val_to_dev().grain("OYDY")
assert full.grain("OYDY").val_to_dev() == full.val_to_dev().grain("OYDY")
assert full.cum_to_incr().grain(
"OYDY"
).val_to_dev() == full.val_to_dev().cum_to_incr().grain("OYDY")
assert qtr.grain("OYDY").val_to_dev().equals(qtr.val_to_dev().grain("OYDY"))
assert (
qtr
.cum_to_incr()
.grain("OYDY")
.val_to_dev()
.equals(qtr.val_to_dev().cum_to_incr().grain("OYDY"))
)
assert (
qtr
.grain("OYDY")
.cum_to_incr()
.val_to_dev()
.incr_to_cum()
.equals(qtr.val_to_dev().grain("OYDY"))
)
assert full.grain("OYDY").val_to_dev().equals(full.val_to_dev().grain("OYDY"))
assert (
full
.cum_to_incr()
.grain("OYDY")
.val_to_dev()
.equals(full.val_to_dev().cum_to_incr().grain("OYDY"))
)
a = full.grain("OYDY").cum_to_incr().val_to_dev().incr_to_cum()
b = full.val_to_dev().grain("OYDY")
assert abs(a - b).max().max().max() < atol
Expand Down
Loading
Loading