-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
644 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# 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/. | ||
|
||
from sklearn.base import BaseEstimator, TransformerMixin | ||
from chainladder.core.io import EstimatorIO | ||
import pandas as pd | ||
|
||
|
||
class Trend(BaseEstimator, TransformerMixin, EstimatorIO): | ||
""" | ||
Estimator to create and apply trend factors to a Triangle object. This | ||
is commonly used for estimators like `CapeCod`. | ||
Parameters | ||
---------- | ||
trend : list-like | ||
The list containing the annual trends expressed as a decimal. For example, | ||
5% decrease should be stated as -0.05 | ||
date : str | ||
A list-like set of dates at which trends start. If None then the valuation | ||
date of the triangle is assumed. | ||
axis : str (options: [‘origin’, ‘valuation’]) | ||
The axis on which to apply the trend | ||
Attributes | ||
---------- | ||
trend_ : | ||
A triangle representation of the trend factors | ||
""" | ||
|
||
def __init__(self, trend=0.0, end_date=None, axis='origin'): | ||
self.trend = trend if type(trend) is list else [trend] | ||
self.end_date = end_date if type(end_date) is list else [end_date] | ||
self.axis = axis | ||
|
||
def fit(self, X, y=None, sample_weight=None): | ||
"""Fit the model with X. | ||
Parameters | ||
---------- | ||
X : Triangle-like | ||
Data to which the model will be applied. | ||
y : Ignored | ||
sample_weight : Ignored | ||
Returns | ||
------- | ||
self : object | ||
Returns the instance itself. | ||
""" | ||
date = [item if item else X.valuation_date.strftime('%Y-%m-%d') for item in self.end_date] | ||
date = pd.to_datetime(date).tolist() | ||
self.trend_ = X.copy() | ||
for i, trend in enumerate(self.trend): | ||
self.trend_ = self.trend_.trend(trend, self.axis, date[i]) | ||
self.trend_ = self.trend_ / X | ||
return self | ||
|
||
def transform(self, X, y=None, sample_weight=None): | ||
""" If X and self are of different shapes, align self to X, else | ||
return self. | ||
Parameters | ||
---------- | ||
X : Triangle | ||
The triangle to be transformed | ||
Returns | ||
------- | ||
X_new : New triangle with transformed attributes. | ||
""" | ||
X_new = X.copy() | ||
triangles = ["trend_"] | ||
for item in triangles: | ||
setattr(X_new, item, getattr(self, item)) | ||
X_new._set_slicers() | ||
return X_new |
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+25.9 KB
docs/auto_examples/images/thumb/sphx_glr_plot_capecod_onlevel_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n# Include On-leveling into Cape Cod\n\n\nThis example demonstrates how to incorporate on-leveling into the `CapeCod`\nestimator. The on-level approach emulates the approach taken by Friedland in\n\"Estimating Unpaid Claims Using Basic Techniques\" Chapter 10. The `ParallelogramOLF`\nestimator is new in chainladder 0.7.9 as is the `xyz` triangle.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import chainladder as cl\nimport pandas as pd\n\n# Grab a triangle\nxyz = cl.load_sample('xyz')\n\n# Premium on-leveling factors\nrate_history = pd.DataFrame({\n 'date': ['1/1/1999', '1/1/2000', '1/1/2001', '1/1/2002', '1/1/2003',\n '1/1/2004', '1/1/2005', '1/1/2006', '1/1/2007', '1/1/2008'],\n 'rate_change': [.02, .02, .02, .02, .05, .075, .15, .1, -.2, -.2]\n})\n\n# Loss on-leveling factors\ntort_reform = pd.DataFrame({\n 'date': ['1/1/2006', '1/1/2007'],\n 'rate_change': [-0.1067, -.25]\n})\n\n# In addition to development, include onlevel estimator in pipeline for loss\npipe = cl.Pipeline(steps=[\n ('olf', cl.ParallelogramOLF(tort_reform, change_col='rate_change', date_col='date', vertical_line=True)),\n ('dev', cl.Development(n_periods=2)),\n ('model', cl.CapeCod(trend=0.034))\n])\n\n# Define X\nX = cl.load_sample('xyz')['Incurred']\n\n# Separately apply on-level factors for premium\nsample_weight = cl.ParallelogramOLF(\n rate_history, change_col='rate_change', date_col='date',\n vertical_line=True).fit_transform(xyz['Premium'].latest_diagonal)\n\n# Fit Cod Estimator\npipe.fit(X, sample_weight=sample_weight).named_steps.model.ultimate_\n\n# Create a Cape Cod pipeline without onleveling\npipe2 = cl.Pipeline(steps=[\n ('dev', cl.Development(n_periods=2)),\n ('model', cl.CapeCod(trend=0.034))\n])\n\n\n# Finally fit Cod Estimator without on-leveling\npipe2.fit(X, sample_weight=xyz['Premium'].latest_diagonal).named_steps.model.ultimate_\n\n# Plot results\ncl.concat((\n pipe.named_steps.model.ultimate_.rename('columns', ['With On-level']),\n pipe2.named_steps.model.ultimate_.rename('columns', ['Without On-level'])), 1).T.plot(\n title='Cape Cod sensitivity to on-leveling', grid=True);" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
===================================== | ||
Include On-leveling into Cape Cod | ||
===================================== | ||
This example demonstrates how to incorporate on-leveling into the `CapeCod` | ||
estimator. The on-level approach emulates the approach taken by Friedland in | ||
"Estimating Unpaid Claims Using Basic Techniques" Chapter 10. The `ParallelogramOLF` | ||
estimator is new in chainladder 0.7.9 as is the `xyz` triangle. | ||
""" | ||
|
||
import chainladder as cl | ||
import pandas as pd | ||
|
||
# Grab a triangle | ||
xyz = cl.load_sample('xyz') | ||
|
||
# Premium on-leveling factors | ||
rate_history = pd.DataFrame({ | ||
'date': ['1/1/1999', '1/1/2000', '1/1/2001', '1/1/2002', '1/1/2003', | ||
'1/1/2004', '1/1/2005', '1/1/2006', '1/1/2007', '1/1/2008'], | ||
'rate_change': [.02, .02, .02, .02, .05, .075, .15, .1, -.2, -.2] | ||
}) | ||
|
||
# Loss on-leveling factors | ||
tort_reform = pd.DataFrame({ | ||
'date': ['1/1/2006', '1/1/2007'], | ||
'rate_change': [-0.1067, -.25] | ||
}) | ||
|
||
# In addition to development, include onlevel estimator in pipeline for loss | ||
pipe = cl.Pipeline(steps=[ | ||
('olf', cl.ParallelogramOLF(tort_reform, change_col='rate_change', date_col='date', vertical_line=True)), | ||
('dev', cl.Development(n_periods=2)), | ||
('model', cl.CapeCod(trend=0.034)) | ||
]) | ||
|
||
# Define X | ||
X = cl.load_sample('xyz')['Incurred'] | ||
|
||
# Separately apply on-level factors for premium | ||
sample_weight = cl.ParallelogramOLF( | ||
rate_history, change_col='rate_change', date_col='date', | ||
vertical_line=True).fit_transform(xyz['Premium'].latest_diagonal) | ||
|
||
# Fit Cod Estimator | ||
pipe.fit(X, sample_weight=sample_weight).named_steps.model.ultimate_ | ||
|
||
# Create a Cape Cod pipeline without onleveling | ||
pipe2 = cl.Pipeline(steps=[ | ||
('dev', cl.Development(n_periods=2)), | ||
('model', cl.CapeCod(trend=0.034)) | ||
]) | ||
|
||
|
||
# Finally fit Cod Estimator without on-leveling | ||
pipe2.fit(X, sample_weight=xyz['Premium'].latest_diagonal).named_steps.model.ultimate_ | ||
|
||
# Plot results | ||
cl.concat(( | ||
pipe.named_steps.model.ultimate_.rename('columns', ['With On-level']), | ||
pipe2.named_steps.model.ultimate_.rename('columns', ['Without On-level'])), 1).T.plot( | ||
title='Cape Cod sensitivity to on-leveling', grid=True); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6c44959848b170466aae812e7d76378e |
Oops, something went wrong.