Skip to content

Commit

Permalink
Addressed #420 and #491 (#507)
Browse files Browse the repository at this point in the history
* Added labels

* Added xyz to test data

* Added polars-lts-cpu since it supports more cpus

* Removed debugger

* Addressed 420 and 491

* Removed debugger
  • Loading branch information
kennethshsu authored Apr 5, 2024
1 parent 583760a commit 38e6760
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 58 deletions.
17 changes: 11 additions & 6 deletions chainladder/core/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

class TriangleDisplay:
def __repr__(self):
# try:
# self.values
# except:
# print("Triangle is empty")
# return
try:
self.values
except:
return "Empty Triangle."

if (self.values.shape[0], self.values.shape[1]) == (1, 1):
data = self._repr_format()
return data.to_string()

else:
return self._summary_frame().__repr__()

Expand All @@ -40,6 +40,11 @@ def _summary_frame(self):

def _repr_html_(self):
"""Jupyter/Ipython HTML representation"""
try:
self.values
except:
return "Triangle is empty."

if (self.values.shape[0], self.values.shape[1]) == (1, 1):
data = self._repr_format()
fmt_str = self._get_format_str(data)
Expand Down Expand Up @@ -150,7 +155,7 @@ def heatmap(self, cmap="coolwarm", low=0, high=0, axis=0, subset=None):
)
output_xnan = re.sub("<td.*nan.*td>", "<td></td>", default_output)
else:
raise ValueError("heatmap only works with single triangles")
raise ValueError("heatmap() only works with a single triangle")
if HTML:
return HTML(output_xnan)
elif HTML is None:
Expand Down
13 changes: 13 additions & 0 deletions chainladder/core/tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ def test_to_frame(raa):

except:
assert False


def test_labels(xyz):
assert (
xyz.valuation_date.strftime("%Y-%m-%d %H:%M:%S.%f")
== "2008-12-31 23:59:59.999999"
)
assert xyz.origin_grain == "Y"
assert xyz.development_grain == "Y"
assert xyz.shape == (1, 5, 11, 11)
assert xyz.index_label == ["Total"]
assert xyz.columns_label == ["Incurred", "Paid", "Reported", "Closed", "Premium"]
assert xyz.origin_label == ["AccidentYear"]
35 changes: 16 additions & 19 deletions chainladder/core/tests/test_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def test_halfyear_grain():
{"AccMo": [201409, 201503, 201603], "ValMo": [202203] * 3, "value": [100] * 3}
)
assert cl.Triangle(
data=data, origin="AccMo", development="ValMo", columns="value"
data=data, origin="AccMo", development="ValMo", columns="value", cumulative=True
).shape == (1, 1, 16, 1)


Expand Down Expand Up @@ -775,21 +775,18 @@ def test_halfyear_development():
)
) == cl.Triangle

assert (
cl.Triangle(
data=pd.DataFrame(data, columns=["origin", "val_date", "idx", "value"]),
index="idx",
columns="value",
origin="origin",
development="val_date",
cumulative=True,
) ==
cl.Triangle(
data=df_polars,
index="idx",
columns="value",
origin="origin",
development="val_date",
cumulative=True,
)
)
assert cl.Triangle(
data=pd.DataFrame(data, columns=["origin", "val_date", "idx", "value"]),
index="idx",
columns="value",
origin="origin",
development="val_date",
cumulative=True,
) == cl.Triangle(
data=df_polars,
index="idx",
columns="value",
origin="origin",
development="val_date",
cumulative=True,
)
7 changes: 1 addition & 6 deletions chainladder/utils/utility_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,11 @@ def parallelogram_olf(
)

rate_changes = pd.Series(np.array(values), np.array(date))
# print("rate_changes:\n", rate_changes)
rate_changes = rate_changes.reindex(date_idx, fill_value=0)
# print("rate_changes:\n", rate_changes)

cum_rate_changes = np.cumprod(1 + rate_changes.values)
cum_rate_changes = pd.Series(cum_rate_changes, rate_changes.index)
# print("cum_rate_changes:\n", cum_rate_changes)
crl = cum_rate_changes[-1]
# print("crl:", crl)

cum_avg_rate_non_leaps = cum_rate_changes
cum_avg_rate_leaps = cum_rate_changes
Expand All @@ -216,8 +213,6 @@ def parallelogram_olf(
cum_avg_rate_leaps = (
cum_avg_rate_leaps + cum_avg_rate_leaps.shift(1).values
) / 2
# print("cum_avg_rate_non_leaps\n", cum_avg_rate_non_leaps)
# print("cum_avg_rate_leaps\n", cum_avg_rate_leaps)

dropdates_num = {
"M": 12,
Expand Down
64 changes: 37 additions & 27 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,86 @@
import pytest
import chainladder as cl


def pytest_generate_tests(metafunc):
if "raa" in metafunc.fixturenames:
metafunc.parametrize(
"raa", ["normal_run", "sparse_only_run"], indirect=True)
metafunc.parametrize("raa", ["normal_run", "sparse_only_run"], indirect=True)
if "qtr" in metafunc.fixturenames:
metafunc.parametrize(
"qtr", ["normal_run", "sparse_only_run"], indirect=True)
metafunc.parametrize("qtr", ["normal_run", "sparse_only_run"], indirect=True)
if "clrd" in metafunc.fixturenames:
metafunc.parametrize(
"clrd", ["normal_run", "sparse_only_run"], indirect=True)
metafunc.parametrize("clrd", ["normal_run", "sparse_only_run"], indirect=True)
if "genins" in metafunc.fixturenames:
metafunc.parametrize(
"genins", ["normal_run", "sparse_only_run"], indirect=True)
metafunc.parametrize("genins", ["normal_run", "sparse_only_run"], indirect=True)
if "prism_dense" in metafunc.fixturenames:
metafunc.parametrize(
"prism_dense", ["normal_run", "sparse_only_run"], indirect=True)
"prism_dense", ["normal_run", "sparse_only_run"], indirect=True
)
if "prism" in metafunc.fixturenames:
metafunc.parametrize("prism", ["normal_run"], indirect=True)
if "xyz" in metafunc.fixturenames:
metafunc.parametrize("xyz", ["normal_run", "sparse_only_run"], indirect=True)


@pytest.fixture
def raa(request):
if request.param == "sparse_only_run":
cl.options.set_option('ARRAY_BACKEND', 'sparse')
cl.options.set_option("ARRAY_BACKEND", "sparse")
else:
cl.options.set_option('ARRAY_BACKEND', 'numpy')
return cl.load_sample('raa')
cl.options.set_option("ARRAY_BACKEND", "numpy")
return cl.load_sample("raa")


@pytest.fixture
def qtr(request):
if request.param == "sparse_only_run":
cl.options.set_option('ARRAY_BACKEND', 'sparse')
cl.options.set_option("ARRAY_BACKEND", "sparse")
else:
cl.options.set_option('ARRAY_BACKEND', 'numpy')
return cl.load_sample('quarterly')
cl.options.set_option("ARRAY_BACKEND", "numpy")
return cl.load_sample("quarterly")


@pytest.fixture
def clrd(request):
if request.param == "sparse_only_run":
cl.options.set_option('ARRAY_BACKEND', 'sparse')
cl.options.set_option("ARRAY_BACKEND", "sparse")
else:
cl.options.set_option('ARRAY_BACKEND', 'numpy')
return cl.load_sample('clrd')
cl.options.set_option("ARRAY_BACKEND", "numpy")
return cl.load_sample("clrd")


@pytest.fixture
def genins(request):
if request.param == "sparse_only_run":
cl.options.set_option('ARRAY_BACKEND', 'sparse')
cl.options.set_option("ARRAY_BACKEND", "sparse")
else:
cl.options.set_option('ARRAY_BACKEND', 'numpy')
return cl.load_sample('genins')
cl.options.set_option("ARRAY_BACKEND", "numpy")
return cl.load_sample("genins")


@pytest.fixture
def prism(request):
cl.options.set_option('ARRAY_BACKEND', 'numpy')
return cl.load_sample('prism')
cl.options.set_option("ARRAY_BACKEND", "numpy")
return cl.load_sample("prism")


@pytest.fixture
def prism_dense(request):
if request.param == "sparse_only_run":
cl.options.set_option('ARRAY_BACKEND', 'sparse')
cl.options.set_option("ARRAY_BACKEND", "sparse")
else:
cl.options.set_option("ARRAY_BACKEND", "numpy")
return cl.load_sample("prism").sum()


@pytest.fixture
def xyz(request):
if request.param == "sparse_only_run":
cl.options.set_option("ARRAY_BACKEND", "sparse")
else:
cl.options.set_option('ARRAY_BACKEND', 'numpy')
return cl.load_sample('prism').sum()
cl.options.set_option("ARRAY_BACKEND", "numpy")
return cl.load_sample("xyz")


@pytest.fixture
def atol(): return 1e-4
def atol():
return 1e-4
1 change: 1 addition & 0 deletions environment-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:

- pandas
- polars
- polars-lts-cpu
- scikit-learn
- sparse
- numba
Expand Down

0 comments on commit 38e6760

Please sign in to comment.