from decimal import Decimal, ROUND_HALF_UP
import pandas as pd
from great_tables import GT
from prospicio.credibility import LimitedFluctuation
from prospicio.pricing import (
LossRatioExperience,
Period,
Provisions,
PurePremiumExperience,
RateHistory,
credibility_weighted,
premium_based_expenses,
trend_factor,
trended_present_rates,
)
from prospicio.reserving import ChainLadder, Triangle
def half_up(x, dp):
"""Round half up to dp places, as the book's exhibits do (after clearing binary noise at 10 places)."""
return float(Decimal(f"{x:.10f}").quantize(Decimal(1).scaleb(-dp), rounding=ROUND_HALF_UP))
def calendar(year):
return Period.calendar(float(year), float(year + 1))
def row(item, ours, d, book, bd=None, alt=None, ad=None):
"""One line of a comparison: prospicio at d decimals, the book at bd, an optional unrounded value at ad."""
return (item, ours, d, book, d if bd is None else bd, float("nan") if alt is None else alt, d if ad is None else ad)
def compare(rows, title, subtitle, alt=None):
"""A table of prospicio against the book; `alt` names an extra column for an unrounded value."""
frame = pd.DataFrame({"item": [r[0] for r in rows], "prospicio": [r[1] for r in rows],
"book": [r[3] for r in rows], "alt": [r[5] for r in rows]})
table = GT(frame if alt else frame.drop(columns="alt"), rowname_col="item").tab_header(title, subtitle)
for k, (_, _, d, _, bd, _, ad) in enumerate(rows):
table = table.fmt_number("prospicio", rows=[k], decimals=d).fmt_number("book", rows=[k], decimals=bd)
if alt:
table = table.fmt_number("alt", rows=[k], decimals=ad)
return table.sub_missing(missing_text="").cols_label(alt=alt) if alt else table
def year_table(years, fit, book, total, title, subtitle, total_decimals):
"""Premium, losses and loss ratio by year with a total row, prospicio beside the book."""
n = len(years)
frame = pd.DataFrame({
"year": [str(y) for y in years] + ["total"],
"premium": [*fit.projected_premium_by_year, fit.projected_premium],
"book_premium": [*book[0], total[0]],
"losses": [*fit.projected_losses_by_year, fit.projected_losses],
"book_losses": [*book[1], total[1]],
"ratio": [*(100 * x for x in fit.loss_ratio_by_year), 100 * fit.loss_ratio],
"book_ratio": [*book[2], total[2]],
})
by_year = list(range(n))
return (GT(frame, rowname_col="year")
.tab_header(title, subtitle)
.fmt_number(["premium", "losses"], rows=by_year, decimals=0)
.fmt_number("premium", rows=[n], decimals=total_decimals)
.fmt_number("losses", rows=[n], decimals=0)
.fmt_integer(["book_premium", "book_losses"])
.fmt_number("ratio", rows=by_year, decimals=1)
.fmt_number("ratio", rows=[n], decimals=2)
.fmt_number("book_ratio", decimals=1)
.tab_spanner("premium", ["premium", "book_premium"])
.tab_spanner("losses", ["losses", "book_losses"])
.tab_spanner("loss ratio (%)", ["ratio", "book_ratio"])
.cols_label(premium="prospicio", book_premium="book", losses="prospicio", book_losses="book",
ratio="prospicio", book_ratio="book"))
# Appendix A, Current Rate Level: six-month policies, the book's cumulative indices.
crl = RateHistory.from_levels([2011.25, 2012.5, 2013.75, 2014.5, 2015.75, 2016.0],
[0.9500, 1.0450, 1.0973, 1.0754, 1.1292, 1.1857])
a_on_level = [half_up(1.1857 / half_up(crl.rate_level(calendar(y), 0.5).average_level, 4), 4)
for y in range(2011, 2016)]
# Premium Trend-3: to the latest average written premium of 115.35, then 2% for 2 years.
ep_crl = [1_364_916.59, 1_405_728.94, 1_448_424.45, 1_492_177.86, 1_536_267.03]
exposures = [12_900, 13_020, 13_130, 13_258, 13_380]
projected = half_up(trend_factor(0.02, 2.0), 4)
a_premium_trend = [half_up(half_up(115.35 / half_up(ep / x, 2), 4) * projected, 4)
for ep, x in zip(ep_crl, exposures)]
# Loss Development: reported losses and paid ALAE, accident years 2009 to 2015.
columns = [
[705_088, 712_475, 714_196, 764_101, 774_384, 785_068, 797_866],
[725_592, 753_295, 763_913, 861_114, 846_167, 821_509],
[738_686, 782_248, 855_150, 884_498, 835_120],
[753_027, 800_258, 874_106, 867_184],
[732_239, 813_949, 856_495],
]
ages = [15, 27, 39, 51, 63]
tri = Triangle.from_long([2009 + i for col in columns for i in range(len(col))],
[a for a, col in zip(ages, columns) for _ in col],
{"reported": [float(x) for col in columns for x in col]})
# The straight average excluding the highest and lowest link ratio, selected to 4 places.
hi_lo = [half_up(f, 4) for f in ChainLadder(average="simple", drop_high=1, drop_low=1).fit(tri, "reported").ldf]
cdf = ChainLadder(ldf=dict(zip(ages, hi_lo)), ldf_kind="ldf").fit(tri, "reported").cdf
a_development = [half_up(c, 4) for c in cdf][::-1]
# Loss Trend-5: -0.5% to 6/30/2015, then +0.5% for 2.25 years to 9/30/2017.
a_loss_trend = [half_up(half_up(trend_factor(-0.005, 2015 - y), 4) * half_up(trend_factor(0.005, 2.25), 4), 4)
for y in range(2011, 2016)]
# Appendix C: annual policies, and the net trend over lengths rounded to 2 places.
mm = RateHistory([2011.75, 2014 + 2 / 12, 2015.5], [-0.076, 0.146, 0.136])
c_on_level = []
for y in range(2010, 2015):
lv = mm.rate_level(calendar(y), 1.0)
c_on_level.append(half_up(half_up(lv.current_level, 4) / half_up(lv.average_level, 4), 4))
c_net_trend = [half_up(trend_factor(0.089, t), 4) for t in (6.83, 5.83, 4.83, 3.83, 2.83)]
book = {
"A on-level": "1.2161 1.2176 1.1311 1.0892 1.0991",
"A premium trend": "1.1342 1.1116 1.0879 1.0663 1.0452",
"A development": "1.0000 0.9799 1.0003 1.0282 1.0966",
"A loss trend": "0.9912 0.9962 1.0012 1.0062 1.0113",
"C on-level": "1.2029 1.2058 1.2724 1.3018 1.2391",
"C net trend": "1.7902 1.6439 1.5095 1.3862 1.2729",
}
ours = dict(zip(book, [a_on_level, a_premium_trend, a_development, a_loss_trend, c_on_level, c_net_trend]))
rows = []
for name, values in ours.items():
rows += [(name, *values), (name + ", book", *map(float, book[name].split()))]
factors = pd.DataFrame(rows, columns=["factor", *[f"y{k}" for k in range(1, 6)]])
(GT(factors, rowname_col="factor")
.tab_header("The indications' factors, recomputed", "Rounded to four places; each pair is prospicio, then the book")
.fmt_number([f"y{k}" for k in range(1, 6)], decimals=4)
.cols_label(**{f"y{k}": f"year {k}" for k in range(1, 6)}))