Skip to content

Commit

Permalink
bug fix in valuation slice
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogaardt committed Mar 31, 2019
1 parent 3d885d0 commit 6544877
Show file tree
Hide file tree
Showing 45 changed files with 302 additions and 48 deletions.
11 changes: 6 additions & 5 deletions chainladder/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,15 @@ def rename(self, axis, value):
A value of 0 <= axis <= 4 corresponding to axes 'index',
'columns', 'origin', 'development' respectively. Both the
int and str representation can be used.
value: list
value: list or str
List of new labels to be assigned to the axis. List must be of
same length of the specified axis.
Returns
-------
Triangle with relabeled axis.
"""
value = [value] if type(value) is str else value
if axis == 'index' or axis == 0:
self.index = value
if axis == 'columns' or axis == 1:
Expand Down Expand Up @@ -479,12 +480,12 @@ def to_frame(self, *args, **kwargs):
elif len(axes) == 2 or len(axes) == 1:
tri = np.squeeze(self.values)
axes_lookup = {0: self.kdims, 1: self.vdims,
2: self.odims, 3: self.ddims}
2: self.origin, 3: self.ddims}
if len(axes) == 2:
return pd.DataFrame(tri, index=axes_lookup[axes[0]],
columns=axes_lookup[axes[1]])
columns=axes_lookup[axes[1]]).fillna(0)
if len(axes) == 1:
return pd.Series(tri, index=axes_lookup[axes[0]])
return pd.Series(tri, index=axes_lookup[axes[0]]).fillna(0)
else:
raise ValueError('len(index) and len(columns) must be 1.')

Expand Down Expand Up @@ -699,7 +700,7 @@ def _slice_origin(self, key):

def _slice_valuation(self, key):
obj = copy.deepcopy(self)
# obj.valuation_date = obj.valuation[key].max()
obj.valuation_date = min(obj.valuation[key].max().to_timestamp(how='e'), obj.valuation_date)
key = key.reshape(self.shape[-2:], order='f')
nan_tri = np.ones(self.shape[-2:])
nan_tri = key*nan_tri
Expand Down
21 changes: 2 additions & 19 deletions chainladder/development/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,11 @@
import pandas as pd
import copy
import warnings
from sklearn.base import BaseEstimator
from sklearn.base import BaseEstimator, TransformerMixin
from chainladder import WeightedRegression
from chainladder.core import IO

class DevelopmentBase(BaseEstimator, IO):
def fit_transform(self, X, y=None, sample_weight=None):
""" Equivalent to fit(X).transform(X)
Parameters
----------
X : Triangle-like
Set of LDFs based on the model.
y : Ignored
sample_weight : Ignored
Returns
-------
X_new : New triangle with transformed attributes.
"""
self.fit(X, y, sample_weight)
return self.transform(X)

class DevelopmentBase(BaseEstimator, TransformerMixin, IO):
@staticmethod
def _get_cdf(obj):
if obj.__dict__.get('ldf_', None) is None:
Expand Down
21 changes: 2 additions & 19 deletions chainladder/development/munich.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
============================
"""

from sklearn.base import BaseEstimator
from sklearn.base import BaseEstimator, TransformerMixin
from chainladder.utils.weighted_regression import WeightedRegression
from chainladder.development import Development
from chainladder.core import IO
import numpy as np
import copy


class MunichAdjustment(BaseEstimator, IO):
class MunichAdjustment(BaseEstimator, TransformerMixin, IO):
"""Applies the Munich Chainladder adjustment to a set of paid/incurred
ldfs.
Expand Down Expand Up @@ -73,23 +73,6 @@ def transform(self, X):
X.ldf_ = self.ldf_
return X

def fit_transform(self, X, y=None, sample_weight=None):
""" Equivalent to fit(X).transform(X)
Parameters
----------
X : Triangle-like
Set of LDFs to which the munich adjustment will be applied.
y : Ignored
sample_weight : Ignored
Returns
-------
X_new : New triangle with transformed attributes.
"""
self.fit(X, y, sample_weight)
return self.transform(X)

def _get_p_to_i_object(self, obj):
paid = obj[list(self.paid_to_incurred.keys())[0]]
for item in list(self.paid_to_incurred.keys())[1:]:
Expand Down
4 changes: 2 additions & 2 deletions chainladder/tails/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import copy
import numpy as np
from sklearn.base import BaseEstimator
from sklearn.base import BaseEstimator, TransformerMixin
from chainladder.development import DevelopmentBase, Development


class TailBase(BaseEstimator):
class TailBase(BaseEstimator, TransformerMixin):
''' Base class for all tail methods. Tail objects are equivalent
to development objects with an additional set of tail statistics'''

Expand Down
Binary file modified docs/auto_examples/auto_examples_jupyter.zip
Binary file not shown.
Binary file modified docs/auto_examples/auto_examples_python.zip
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions docs/auto_examples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ Gallery of Chainladder Functionality

/auto_examples/plot_bf_apriori_from_cl

.. raw:: html

<div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how you can slice triangle objects to perform a typical &#x27;Actual vs Ex...">

.. only:: html

.. figure:: /auto_examples/images/thumb/sphx_glr_plot_ave_analysis_thumb.png

:ref:`sphx_glr_auto_examples_plot_ave_analysis.py`

.. raw:: html

</div>


.. toctree::
:hidden:

/auto_examples/plot_ave_analysis

.. raw:: html

<div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how you can can use the Overdispersed Poisson Bootstrap sampler and g...">
Expand Down
54 changes: 54 additions & 0 deletions docs/auto_examples/plot_ave_analysis.ipynb
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# Actual Vs Expected Analysis\n\n\nThis example demonstrates how you can slice triangle objects to perform a\ntypical 'Actual vs Expected' analysis. We will use Medical Malpractice\npayment patterns for the demo.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import chainladder as cl\nimport seaborn as sns\nsns.set_style('whitegrid')\n\n# Load the data\ntri_1997 = cl.load_dataset('clrd')\ntri_1997 = tri_1997.groupby('LOB').sum().loc['medmal']['CumPaidLoss']\n\n# Create a triangle as of the previous valuation and build IBNR model\ntri_1996 = tri_1997[tri_1997.valuation < '1997']\nmodel_1996 = cl.Chainladder().fit(cl.TailCurve().fit_transform(tri_1996))\n\n# Slice the expected losses from the 1997 calendar period of the model\nave = model_1996.full_triangle_.dev_to_val()\nave = ave[ave.development == '1997'].rename('columns', 'Expected')\n\n# Slice the actual losses from the 1997 calendar period for prior AYs\nave['Actual'] = tri_1997.latest_diagonal[tri_1997.origin < '1997']\nave['Actual - Expected'] = ave['Actual'] - ave['Expected']\n\n# Plotting\nave.to_frame().T.plot(y='Actual - Expected', kind='bar', legend=False) \\\n .set(title='Calendar Period 1997 Performance',\n xlabel='Accident Period', ylabel='Actual - Expected');"
]
}
],
"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
}
34 changes: 34 additions & 0 deletions docs/auto_examples/plot_ave_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
===========================
Actual Vs Expected Analysis
===========================
This example demonstrates how you can slice triangle objects to perform a
typical 'Actual vs Expected' analysis. We will use Medical Malpractice
payment patterns for the demo.
"""

import chainladder as cl
import seaborn as sns
sns.set_style('whitegrid')

# Load the data
tri_1997 = cl.load_dataset('clrd')
tri_1997 = tri_1997.groupby('LOB').sum().loc['medmal']['CumPaidLoss']

# Create a triangle as of the previous valuation and build IBNR model
tri_1996 = tri_1997[tri_1997.valuation < '1997']
model_1996 = cl.Chainladder().fit(cl.TailCurve().fit_transform(tri_1996))

# Slice the expected losses from the 1997 calendar period of the model
ave = model_1996.full_triangle_.dev_to_val()
ave = ave[ave.development == '1997'].rename('columns', 'Expected')

# Slice the actual losses from the 1997 calendar period for prior AYs
ave['Actual'] = tri_1997.latest_diagonal[tri_1997.origin < '1997']
ave['Actual - Expected'] = ave['Actual'] - ave['Expected']

# Plotting
ave.to_frame().T.plot(y='Actual - Expected', kind='bar', legend=False) \
.set(title='Calendar Period 1997 Performance',
xlabel='Accident Period', ylabel='Actual - Expected');
1 change: 1 addition & 0 deletions docs/auto_examples/plot_ave_analysis.py.md5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d7f8f36282dc3592d0247e73221f94ea
84 changes: 84 additions & 0 deletions docs/auto_examples/plot_ave_analysis.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
.. note::
:class: sphx-glr-download-link-note

Click :ref:`here <sphx_glr_download_auto_examples_plot_ave_analysis.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_plot_ave_analysis.py:


===========================
Actual Vs Expected Analysis
===========================

This example demonstrates how you can slice triangle objects to perform a
typical 'Actual vs Expected' analysis. We will use Medical Malpractice
payment patterns for the demo.




.. image:: /auto_examples/images/sphx_glr_plot_ave_analysis_001.png
:class: sphx-glr-single-img





.. code-block:: python
import chainladder as cl
import seaborn as sns
sns.set_style('whitegrid')
# Load the data
tri_1997 = cl.load_dataset('clrd')
tri_1997 = tri_1997.groupby('LOB').sum().loc['medmal']['CumPaidLoss']
# Create a triangle as of the previous valuation and build IBNR model
tri_1996 = tri_1997[tri_1997.valuation < '1997']
model_1996 = cl.Chainladder().fit(cl.TailCurve().fit_transform(tri_1996))
# Slice the expected losses from the 1997 calendar period of the model
ave = model_1996.full_triangle_.dev_to_val()
ave = ave[ave.development == '1997'].rename('columns', 'Expected')
# Slice the actual losses from the 1997 calendar period for prior AYs
ave['Actual'] = tri_1997.latest_diagonal[tri_1997.origin < '1997']
ave['Actual - Expected'] = ave['Actual'] - ave['Expected']
# Plotting
ave.to_frame().T.plot(y='Actual - Expected', kind='bar', legend=False) \
.set(title='Calendar Period 1997 Performance',
xlabel='Accident Period', ylabel='Actual - Expected');
**Total running time of the script:** ( 0 minutes 1.788 seconds)


.. _sphx_glr_download_auto_examples_plot_ave_analysis.py:


.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download
:download:`Download Python source code: plot_ave_analysis.py <plot_ave_analysis.py>`
.. container:: sphx-glr-download
:download:`Download Jupyter notebook: plot_ave_analysis.ipynb <plot_ave_analysis.ipynb>`
.. only:: html

.. rst-class:: sphx-glr-signature

`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
Binary file added docs/auto_examples/plot_ave_analysis_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_benktander_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_bf_apriori_from_cl_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_bootstrap_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_capecod_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_development_periods_codeobj.pickle
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/auto_examples/plot_exhibits.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Sample Excel Exhibit functionality\n\n\nThis example demonstrates some of the flexibility of the ``Exhibits`` class. It\ncreates an Excel file called 'clrd.xlsx' that includes various statistics on\nindustry development patterns for each line of business in the CAS loss reserve\ndatabase.\n\nSee `Exhibits<exhibits>` for more detail.\n\n\n"
"\n# Sample Excel Exhibit functionality\n\n\nThis example demonstrates some of the flexibility of the ``Exhibits`` class. It\ncreates an Excel file called 'clrd.xlsx' that includes various statistics on\nindustry development patterns for each line of business in the CAS loss reserve\ndatabase.\n\nOutput can be viewed online in `Google Sheets <https://docs.google.com/spreadsheets/d/1fwHK1Sys6aHDhEhFO6stVJtmZVKEcXXBsmJLSLIBLJY/edit#gid=1190415861>`_.\n\nSee `Exhibits<exhibits>` for more detail.\n\n\n"
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions docs/auto_examples/plot_exhibits.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
industry development patterns for each line of business in the CAS loss reserve
database.
Output can be viewed online in `Google Sheets <https://docs.google.com/spreadsheets/d/1fwHK1Sys6aHDhEhFO6stVJtmZVKEcXXBsmJLSLIBLJY/edit#gid=1190415861>`_.
See :ref:`Exhibits<exhibits>` for more detail.
.. _exhibit_example:
Expand Down
2 changes: 1 addition & 1 deletion docs/auto_examples/plot_exhibits.py.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5a6be949f7c7265f53489c50d24e5d17
f5234948aa65a93976f2a87b9155dd61
4 changes: 3 additions & 1 deletion docs/auto_examples/plot_exhibits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ creates an Excel file called 'clrd.xlsx' that includes various statistics on
industry development patterns for each line of business in the CAS loss reserve
database.

Output can be viewed online in `Google Sheets <https://docs.google.com/spreadsheets/d/1fwHK1Sys6aHDhEhFO6stVJtmZVKEcXXBsmJLSLIBLJY/edit#gid=1190415861>`_.

See :ref:`Exhibits<exhibits>` for more detail.

.. _exhibit_example:
Expand Down Expand Up @@ -77,7 +79,7 @@ See :ref:`Exhibits<exhibits>` for more detail.
# Create Excel File
exhibits.to_excel('clrd.xlsx')
**Total running time of the script:** ( 0 minutes 2.915 seconds)
**Total running time of the script:** ( 0 minutes 4.548 seconds)


.. _sphx_glr_download_auto_examples_plot_exhibits.py:
Expand Down
Binary file modified docs/auto_examples/plot_exhibits_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_mack_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_munich_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_triangle_from_pandas_codeobj.pickle
Binary file not shown.
Binary file modified docs/auto_examples/plot_triangle_slicing_codeobj.pickle
Binary file not shown.
Empty file.
Empty file.
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions docs/modules/generated/chainladder.Chainladder.examples
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ Examples using ``chainladder.Chainladder``

* :ref:`sphx_glr_auto_examples_plot_bf_apriori_from_cl.py`

.. raw:: html

<div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how you can slice triangle objects to perform a typical &#x27;Actual vs Ex...">

.. only:: html

.. figure:: /auto_examples/images/thumb/sphx_glr_plot_ave_analysis_thumb.png

:ref:`sphx_glr_auto_examples_plot_ave_analysis.py`

.. raw:: html

</div>

.. only:: not html

* :ref:`sphx_glr_auto_examples_plot_ave_analysis.py`

.. raw:: html

<div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how to adjust LDFs by the relationship between Paid and Incurred usin...">
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading

0 comments on commit 6544877

Please sign in to comment.