• Home
  • Readings
  • Github
  • MIES
  • TmVal
  • About
Gene Dan's Blog

Category Archives: Mies

No. 144: MIES – Intertemporal Choice

26 July, 2020 10:11 PM / Leave a Comment / Gene Dan

This entry is part of a series dedicated to MIES – a miniature insurance economic simulator. The source code for the project is available on GitHub.

Current Status

This week, I’ve been continuing my work on incorporating risk into the consumer behavior component of MIES. The next step in this process involves the concept of intertemporal choice, an interpretation of the budget constraint problem whereby a consumer can shift consumption from one time period to another by means of savings and loans. The content of this post follows chapter 10 of Varian.

For example, a person can consume more in a future period by saving money. A person can also increase their consumption today by taking out a loan, which comes at a cost of future consumption because they have to pay interest. When making decisions between current and future consumption, we also have to think about time value of money. When I was reading through Varian, I was happy to see that many of the concepts I learned from the financial mathematics actuarial exam were also discussed by Varian – such as bonds, annuities, and perpetuities – albeit in much less detail.

This inspired me to create a new repo to handle time value of money computations, which is not yet ready for its own series of posts, but for which you can see the initial work here. I had intended to make this repo further out in the future, but I got excited and started early.

Also relevant, is the concept of actuarial communication. Now that I’m blogging more about actuarial work, I will need to be able to write the notation here. There are some \LaTeX packages that can render actuarial notation, such as actuarialsymbol. Actuaries are still in the stone age when it comes to sharing technical work over the Internet, not out of ignorance, since many actuaries are familiar with \LaTeX, but out of corporate inertia in getting the right tools at work (which I can suppose be due to failure to persuade the right people) and lack of momentum and willingness as many people simply just try to make do with using ASCII characters to express mathematical notation. I think this is a major impediment to adding rigor to practical actuarial work, which many young analysts complain about when they first start working, as they notice that spreadsheet models tend to be a lot more dull than what they see on the exams.

I was a bit anxious in trying to get the actuarialsymbol package working since, although I knew how to get it working on my desktop, I wasn’t sure if it would work with WordPress or Anki, a study tool that I use. Fortunately, it does work! For example, the famous annuity symbol can be rendered with the command \ax{x:\angln}:

Rendered by QuickLaTeX.com

That was easy. There’s no reason why intraoffice email can’t support this, so I hope that it encourages you to pick it up as well.

The Statics Module

Up until now, testing new features has been cumbersome since many of the previous demos I have written about required existing simulation data. That is, in order to test things like intertemporal choice, I would first need to set up a simulation, run it, and then use the results as inputs into the new functions, classes, or methods.

That really shouldn’t be necessary, especially since many of the concepts I have been making modules for apply to economics in general, and not just to insurance. To solve this problem, I created the statics module, which is named after the process of comparative statics, which examines how behavior changes when an exogenous variable in the model changes (aka all the charts I’ve been making about MIES).

The statics module currently has one class, Consumption, which can return attributes such as the optimal consumption of a person given a budget and utility function:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# used for comparative statics
import plotly.graph_objects as go
 
from plotly.offline import plot
 
from econtools.budget import Budget
from econtools.utility import CobbDouglas
 
 
class Consumption:
    def __init__(
            self,
            budget: Budget,
            utility: CobbDouglas
    ):
        self.budget = budget
        self.income = self.budget.income
        self.utility = utility
        self.optimal_bundle = self.get_consumption()
        self.fig = self.get_consumption_figure()
 
    def get_consumption(self):
        optimal_bundle = self.utility.optimal_bundle(
            p1=self.budget.good_x.adjusted_price,
            p2=self.budget.good_y.adjusted_price,
            m=self.budget.income
        )
 
        return optimal_bundle
 
    def get_consumption_figure(self):
        fig = go.Figure()
        fig.add_trace(self.budget.get_line())
        fig.add_trace(self.utility.trace(
            k=self.optimal_bundle[2],
            m=self.income / self.budget.good_x.adjusted_price * 1.5
        ))
 
        fig.add_trace(self.utility.trace(
            k=self.optimal_bundle[2] * 1.5,
            m=self.income / self.budget.good_x.adjusted_price * 1.5
        ))
 
        fig.add_trace(self.utility.trace(
            k=self.optimal_bundle[2] * .5,
            m=self.income / self.budget.good_x.adjusted_price * 1.5
        ))
 
        fig['layout'].update({
            'title': 'Consumption',
            'title_x': 0.5,
            'xaxis': {
                'title': 'Amount of ' + self.budget.good_x.name,
                'range': [0, self.income / self.budget.good_x.adjusted_price * 1.5]
            },
            'yaxis': {
                'title': 'Amount of ' + self.budget.good_y.name,
                'range': [0, self.income * 1.5]
            }
        })
 
        return fig
 
    def show_consumption(self):
        plot(self.fig)

A lot of the code here is the same as that which can be found in the Person class. However, instead of needing to instantiate a person to do comparative statics, I can just use the Consumption class directly from the statics module. This should make creating and testing examples much easier.

Since much of the code in statics is the same as in the Person class, that gives me a hint that I can make things more maintainable by refactoring the code. I would think the right thing to do is to have the Person class use the Consumption class in the statics module, rather than the other way around.

The Intertemporal Class

The intertemporal budget constraint is:

    \[c_1 + c_2/(1+r) = m_1 + m_2/(1+r)\]

Note that this has the same form as the endowment budget constraint:

    \[p_1 x_1 + p_2 x_2 = p_1 m_1 + p_2 m_2 \]

With the difference being that the two endowment goods are now replaced by consumption in times 1 and 2, represented by the cs and the prices, the ps are now replaced by discounted unit prices. The subscript 1 represents the current time and the subscript 2 represents the future time, with the price of future consumption being discounted to present value via the interest rate, r.

The consumer can shift consumption between periods 1 and 2 via saving and lending, subject to the constraint that the amount saved during the first period cannot exceed their first period income, and the amount borrowed during the first period cannot exceed the present value of the income of the second period.

Since the intertemporal budget constraint is a form of the endowment constraint, we can modify the Endowment class in MIES to accommodate this type of consumption. I have created a subclass called Intertemporal that inherits from the Endowment class:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Intertemporal(Endowment):
    def __init__(
            self,
            good_x: Good,
            good_y: Good,
            good_x_quantity: float,
            good_y_quantity: float,
            interest_rate: float = 0,
            inflation_rate: float = 0
            ):
        Endowment.__init__(
            self,
            good_x,
            good_y,
            good_x_quantity,
            good_y_quantity,
        )
        self.interest_rate = interest_rate
        self.inflation_rate = inflation_rate
        self.good_y.interest_rate = self.interest_rate
        self.good_y.inflation_rate = self.inflation_rate

The main difference here is that the Intertemporal class can accept an interest rate and an inflation rate to adjust the present value of future consumption.

Example

As an example, suppose we have a person who makes 5 dollars in each of time periods 1 and 2. The market interest rate is 10% and their utility function takes the Cobb Douglas form of:

    \[u(x_1, x_2) = x_1^{.5} x_2^{.5}\]

which means they will spend half of the present value of the endowment as consumption in period 1:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from econtools.budget import Budget, Intertemporal, Good
from econtools.statics import Consumption
from econtools.utility import CobbDouglas
 
# test if intercepts plot appropriately with an interest rate of 10%
m1 = Good(price=1, name='Time 1 Consumption')
 
m2 = Good(price=1, name='Time 2 Consumption')
 
endowment = Intertemporal(
    good_x=m1,
    good_y=m2,
    good_x_quantity=5,
    good_y_quantity=5,
    interest_rate=.10
)
 
budget = Budget.from_endowment(endowment, name='budget')
 
utility = CobbDouglas(.5, 0.5)
 
consumption = Consumption(budget=budget, utility=utility)
 
consumption.show_consumption()

The main thing that sticks out here is that the slope of the budget constraint has changed to reflect the adjustment of income to present value. The x-axis intercept is slightly less than 10 because the present value of income is slightly less than 10, and the y-axis intercept is slightly more than 10 because if a person saved all of their time 1 income, they would receive interest of 5 * .1 = .5, making maximum consumption in period 2 10.5.

Since the person allocates half of the present value of the endowment to time 1 consumption, this means they will spend (5 + 5/1.1) * .5 = 4.77 in period one, saving 5 – 4.77 = .23, which then grows to .23 * (1 + .1) = .25 in period 2, which allows for a time 2 consumption of 5 + .25 = 5.25. This is verified by calling the optimal_bundle() method of the Consumption class:

Python
1
2
consumption.optimal_bundle
Out[7]: (4.7727272727272725, 5.25, 5.005678593539359)

Further Improvements

The Varian chapter on intertemporal choice briefly explores present value calculations for various payment streams, such as bonds and perpetuities. I first made a small attempt at creating a tvm module, but quickly realized that the subject of time value of money is much more complex than what is introduced in Varian, since I know that other texts go further in depth, and hence it may be necessary to split a new repo off from MIES so that it can be distributed separately. This repo is called TmVal, the early stages of which I have uploaded here.

Neither of these are ready for demonstration, but you can click on the links if you are interested in seeing what I have done. The next chapter of Varian covers asset markets, which at first glance seems to just be some examples of economic models, so I’m not sure if it has any features I would like to add to MIES. There is still more work to be done on refactoring the code, so I may do that, or move further into risk aversion, or do some more work on TmVal.

Posted in: Actuarial, MIES / Tagged: economics, insurance, intertemporal choice, MIES

No. 143: MIES – Endowments

19 July, 2020 11:57 PM / Leave a Comment / Gene Dan

This entry is part of a series dedicated to MIES – a miniature insurance economic simulator. The source code for the project is available on GitHub.

Current Status

Last week, I took a break from MIES to focus on PCDM, a relational database specification for the P&C insurance industry. This week, I’m back to making progress on the consumer behavior portion of MIES, by shifting the focus from personal income to the personal endowment as the main financial constraint underlying purchasing decisions.

In short, an endowment is the consumer’s assets. When making consumption choices, people can use their income to purchase goods and services, but they can also draw from assets that they have accumulated over time, such as from savings and checking accounts, and by selling goods that they own. Furthermore, by taking the endowment into consideration, we will now be able to model situation when a person might not have a regular income, but can still make purchases using their assets (such as unemployed or retired persons who are not working).

In the context of insurance, the endowment is important because people purchase insurance to indemnify themselves against events that might damage or reduce the value of their assets. In the absence of the endowment, we would ignore an important determinant of insurance purchasing behavior. Incorporating wealth into MIES will take some time, and the textbook material I need to work on spans five chapters of Varian. Therefore, I estimate that this process will take me at least a month to do:

  1. Endowment
  2. Intertemporal Choice
  3. Asset Markets
  4. Uncertainty
  5. Risky Assets

These concepts will involve making some substantial changes to the utility functions as well. For now I’ll start with the endowment, which required me to modify the Budget, Slutsky, and Hicks classes of MIES.

The Endowment Class

An endowment is a bundle of goods or services that has a value based on the sum product of their prices and quantities:

    \[p_1 \omega_1 + p_2 \omega_2 = m \]

Where m represents income, each omega represents the quantity of each good, and each p represents the price. Rather than treat income as a flow quantity from an external source, in this interpretation of consumer choice theory we, we treat income as a stock quantity that includes the assets of of the consumer – that is, what the consumer has to spend at a certain point of time depends on the valuation of their assets.

This definition of income loosens the assumption of fixed income that I had made until now. This is because changes in asset values can now impact a person’s income. For example, if a person has a car and a house, their depreciation or appreciation changes the amount the person can sell them for on the market.

The good news is that MIES already has much of the machinery already coded up to allow us to work with endowments, so the new class definition is quite simple:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Endowment:
    def __init__(
            self,
            good_x: Good,
            good_y: Good,
            good_x_quantity: float,
            good_y_quantity: float,
            ):
        self.good_x = good_x
        self.good_y = good_y
        self.good_x_quantity = good_x_quantity
        self.good_y_quantity = good_y_quantity
 
    @property
    def income(self):
        income = self.good_x.price * self.good_x_quantity + self.good_y.price * self.good_y_quantity
        return income

An endowment takes two goods, and their quantities. Upon initialization, Python will automatically calculate the Endowment’s value by multiplying the prices of the goods by the quantities supplied. I wrote this function as a property decorator, which was introduced to fix a bug I discovered when working with the Budget class. Earlier, changing the price of a good failed to change the budget constraint of a consumer, but the property decorator will now dynamically calculate certain attributes that depend on the price, such as income in the case of an endowment.

To illustrate, we can define two goods, each with a price of 1. We then initialize an endowment with a quantity of 5 for each of these goods:

1
2
3
4
5
6
7
from econtools.budget import Endowment, Good
 
good_1 = Good(price=1, name='good_1')
 
good_2 = Good(price=1, name='good_2')
 
endowment = Endowment(good_x=good_1, good_y=good_2, good_x_quantity=5, good_y_quantity=5)

Now we can check that the income was properly calculated by calling endowment.income. Since each good has a price of 1, and there are 5 of each good, the income should be 5 x 1 + 5 x 1 = 10:

Python
1
2
endowment.income
Out[4]: 10

Now that we have the Endowment class defined, we need to modify the other classes that used goods, such as the Budget class. Previously, the Budget class accepted two goods, an income amount, and a name to refer to the budget. Now I would like the Budget class to an accept an endowment as an alternative to specifying each good individually. The tricky part here is that in the former case, the class needs to be able to keep income fixed when the prices of goods change, but in the latter case, the income needs to change dynamically based on the prices of the goods.

To handle this, I created an alternative constructor called from_endowment() that lets you pass an endowment to the Budget class to initialize a budget object. I also created another constructor called from_bundle() that lets you define a Budget the old way more explicitly, to make it more obvious to anyone reading the code whether the budget was initialized with an endowment or individual goods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Budget:
    def __init__(
            self,
            good_x,
            good_y,
            income,
            name=None,
            endowment=None
    ):
        self.good_x = good_x
        self.good_y = good_y
        self.income = income
        self.x_lim = self.income / (min(self.good_x.adjusted_price, self.good_x.price)) * 1.2
        self.y_lim = self.income / (min(self.good_y.adjusted_price, self.good_y.price)) * 1.2
        self.name = name
        self.endowment = endowment
 
        if endowment is not None:
            self.__check_endowment_consistency()
 
    @classmethod
    def from_bundle(
            cls,
            good_x,
            good_y,
            income,
            name=None
    ):
        return cls(
            good_x,
            good_y,
            income,
            name
        )
 
    @classmethod
    def from_endowment(
            cls,
            endowment: Endowment,
            name=None
    ):
        good_x = endowment.good_x
        good_y = endowment.good_y
        income = endowment.income
 
        return cls(
            good_x,
            good_y,
            income,
            name,
            endowment
        )
 
    def __check_endowment_consistency(self):
        # raise exception if endowment is not consistent with its components
        if self.endowment.good_x != self.good_x:
            raise Exception("Endowment good_x inconsistent with budget good_x. "
                            "It is recommended to use the from_endowment alternative "
                            "constructor when supplying an endowment")
 
        if self.endowment.good_y != self.good_y:
            raise Exception("Endowment good_y inconsistent with budget good_y. "
                            "It is recommended to use the from_endowment alternative "
                            "constructor when supplying an endowment")
 
        if (self.endowment.income != (self.endowment.good_x_quantity * self.good_x.price +
                                      self.endowment.good_y_quantity * self.good_y.price)) | \
                (self.endowment.income != self.income):
 
            raise Exception("Endowment income inconsistent with supplied good prices. "
                            "It is recommended to use the from_endowment alternative "
                            "constructor when supplying an endowment")
 
        if self.endowment.good_x.price != self.good_x.price:
            raise Exception("Endowment good_x price inconsistent with budget good_x price. "
                            "It is recommended to use the from_endowment alternative "
                            "constructor when supplying an endowment")
 
        if self.endowment.good_y.price != self.good_y.price:
            raise Exception("Endowment good_y price inconsistent with budget good_y price. "
                            "It is recommended to use the from_endowment alternative "
                            "constructor when supplying an endowment")
...

And lastly, I added some consistency checks to make sure that the endowment value equals the sum product of the prices and quantities of the goods provided. The reason why these checks are here is because a person can still use the default constructor to specify each good individually along with an endowment, just based on how the arguments are defined. While this is possible, I would discourage doing this since 1) it’s less explicit than using the alternative constructors, 2) supplying the individual goods along with the endowment is redundant, and 3) it can lead to errors being thrown.

To initialize a budget by passing an endowment, simply use the alternative constructor:

Python
1
budget_endowment = Budget.from_endowment(endowment=endowment)

Slutsky Decomposition

The loosening of assumptions brought about by the endowment introduces some changes to the Slutsky equation. In the examples I provided a few weeks ago, we assumed that income remained fixed when prices changed. Since changes in price now change the value of the endowment, we must now account for this change in the Slutsky equation. The derivation of this modified form can be found in Varian, so I’ll skip to the result:

    \[\frac{\Delta x_1}{\Delta p_1} = \frac{\Delta x_1^s}{\Delta p_1} + (\omega_1 - x_1)\frac{\Delta x_1^m}{\Delta m}\]

The Slutsky equation can now be explained by three effects: the substitution and ordinary income effects, which are the same as before, and an endowment effect, which models how consumer choice changes when the value of the endowment changes.

Like the Budget class, the Slutsky class has been modified to take budgets that were constructed from individual goods or an endowment. Plotting the Slutsky class is now quite bit messier, since a new budget line, bundle, and utility curve are now added to an already crowded plot.

I have not yet gotten endowments to work within the context of insurance, so the image below comes from a modified version of an example provided in Varian where a milk producer faces a $1 increase in the price of milk – his endowment increases in value, and hence income. However with the graph as cluttered as it is, it can be hard to visually isolate the effects:

It does look better with a larger plotting area if you try it with MIES, but not so much when I have to shrink the image to fit it within the margins here.

Posted in: Actuarial, MIES

No. 141: MIES – Premium and Claim Transactions

5 July, 2020 10:28 PM / Leave a Comment / Gene Dan

This entry is part of a series dedicated to MIES – a miniature insurance economic simulator. The source code for the project is available on GitHub.

Current Status

Up until now, I’ve been able to demonstrate basic consumer behavior and certain market phenomena seen in the insurance industry. However, there’s been a major problem with MIES in that no money has actually changed hands in any of the simulations we’ve seen so far. Consumers have simply switched carriers depending on price, which allowed me to demonstrate adverse selection, but not much else. Consumption decisions involving insurance depend on wealth, but since there was no way to calculate wealth in MIES, its ability to model these decisions was limited.

The next view chapters in Varian place a heavy emphasis on wealth and risk tolerance, so this week, I made the decision to work on incorporating transactions before diving deeper into consumer behavior.

At first glance, transactions might seem like a simple thing to implement, after all, why not just keep a running cash balance for each entity, and then add and subtract payments as needed? The problem with this method is the same problem that leads companies to use double-entry accounting. Transactions are more complicated than simply sending money from one place to another. Loans are generated and capital is invested, which creates liabilities that must be considered when trying to calculate the wealth of an entity. Even something as simple as a premium payment is effectively a loan to an insurance company that needs to have a liability recorded (the unearned premium reserve), in addition to an increase in cash to the insurer.

Therefore, I’ve had to draw on my basic knowledge of accounting, which made me uneasy since I’m not an accountant myself. However, in order to get MIES to model the phenomena I want to model, and to answer the questions I have about insurance, I need to implement double-entry accounting, and eventually, statutory accounting rules. I ran across a post on Hacker News titled, ‘It’s OK for your open source library to be a bit shitty,’ which encouraged me to keep moving forward with the project despite the amount of discomfort I have.

I have certainly found many errors in MIES from past versions and there will likely be many more, including in this post. However, there’s not a lot of open source actuarial stuff out there, or in particular, open source actuarial simulations incorporating both economics concepts and double-entry accounting. Or, if there are packages out there, they aren’t easy to find. Thus, I’ve taken the step to put something out there, awaiting any feedback for things that need to fixed, and then making improvements. If this ever proves to be something useful, younger generations will create even better tools in the future.

The Bank Class

Banks facilitate transactions between insurers, brokers, customers, and other banks. While it may eventually be possible to define more than one bank per simulation, all the examples in the near future will have a single bank at which entities can deposit money and send payments to each other.

Schema

As with all the other entities, each bank comes with its own database:

Here, a bank can accept three types of customers. The customer table represents the customer superclass and references the person, insurer, and bank subclasses. Each customer can have an optional number of accounts. Any of these customers can send transactions to any other customer, including themself. Notice that the transaction table has two relationships to the account table, since the debit and credit account fields for each transaction both point to the account table.

The SQLAlchemy mapping is defined below:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Date, Float, String
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
 
 
Base = declarative_base()
 
 
class Account(Base):
    __tablename__ = 'account'
 
    account_id = Column(
        Integer,
        primary_key=True
    )
 
    customer_id = Column(
        Integer,
        ForeignKey('customer.customer_id')
    )
 
    account_type = Column(String)
 
    transaction_debit = relationship(
        'Transaction',
        primaryjoin='Transaction.debit_account == Account.account_id',
        back_populates='account_debit'
    )
 
    transaction_credit = relationship(
        'Transaction',
        primaryjoin='Transaction.credit_account == Account.account_id',
        back_populates='account_credit'
    )
 
    def __repr__(self):
        return "<Account(" \
               "customer_id='%s', " \
               "account_type='%s', " \
               ")>" % (
                   self.customer_id,
                   self.account_type
               )
 
 
class Transaction(Base):
    __tablename__ = 'transaction'
 
    transaction_id = Column(
        Integer,
        primary_key=True
    )
 
    debit_account = Column(
        Integer,
        ForeignKey('account.account_id')
    )
 
    credit_account = Column(
        Integer,
        ForeignKey('account.account_id')
    )
 
    transaction_date = Column(Date)
 
    transaction_amount = Column(Float)
 
    account_debit = relationship(
        "Account",
        primaryjoin='Transaction.debit_account == Account.account_id',
        back_populates='transaction_debit'
    )
 
    account_credit = relationship(
        "Account",
        primaryjoin='Transaction.credit_account == Account.account_id',
        back_populates='transaction_credit'
    )
 
    def __repr__(self):
        return "<Transaction(" \
               "debit_account='%s', " \
               "credit_account='%s', " \
               "transaction_date='%s', " \
               "transaction_amount='%s'" \
               ")>" % (
                   self.debit_account,
                   self.credit_account,
                   self.transaction_date,
                   self.transaction_amount,
               )
 
 
class Customer(Base):
    __tablename__ = 'customer'
 
    customer_id = Column(
        Integer,
        primary_key=True
    )
    customer_type = Column(String)
 
    person = relationship(
        'Person',
        primaryjoin='Customer.customer_id == Person.customer_id',
        back_populates='customer'
    )
 
    insurer = relationship(
        'Insurer',
        primaryjoin='Customer.customer_id == Insurer.customer_id',
        back_populates='customer'
    )
 
    bank = relationship(
        'Bank',
        primaryjoin='Customer.customer_id == Bank.customer_id',
        back_populates='customer'
    )
 
 
class Person(Base):
    __tablename__ = 'person'
 
    person_id = Column(
        Integer,
        primary_key=True
    )
 
    customer_id = Column(
        Integer,
        ForeignKey('customer.customer_id')
    )
 
    customer = relationship(
        'Customer',
        primaryjoin='Person.customer_id == Customer.customer_id',
        back_populates='person',
        uselist=True
    )
 
 
class Insurer(Base):
    __tablename__ = 'insurer'
 
    insurer_id = Column(
        Integer,
        primary_key=True
    )
 
    customer_id = Column(
        Integer,
        ForeignKey('customer.customer_id')
    )
 
    customer = relationship(
        'Customer',
        primaryjoin='Insurer.customer_id == Customer.customer_id',
        back_populates='insurer',
        uselist=True
    )
 
 
class Bank(Base):
    __tablename__ = 'bank'
 
    bank_id = Column(
        Integer,
        primary_key=True
    )
 
    customer_id = Column(
        Integer,
        ForeignKey('customer.customer_id')
    )
 
    customer = relationship(
        'Customer',
        primaryjoin='Bank.customer_id == Customer.customer_id',
        back_populates='bank',
        uselist=True
    )

Bank Methods

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import datetime as dt
import os
import pandas as pd
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
 
import mies.schema.bank as bank
from mies.schema.bank import Account, Customer, Insurer, Person, Transaction
from mies.schema.bank import Bank as BankTable
from mies.utilities.connections import connect_universe
from mies.utilities.queries import query_bank_id
 
 
class Bank:
    def __init__(self, starting_capital, bank_name, path='db/banks/', date_established=dt.datetime(1, 12, 31)):
        if not os.path.exists(path):
            os.makedirs(path)
        self.engine = sa.create_engine(
            'sqlite:///' + path + bank_name + '.db',
            echo=True
        )
        session = sessionmaker(bind=self.engine)
        bank.Base.metadata.create_all(self.engine)
        self.session = session()
        self.connection = self.engine.connect()
        self.name = bank_name
        self.date_established = date_established
        self.id = self.__register()
        self.get_customers(self.id, 'bank')
        self.cash_account = self.assign_account(
            customer_id=self.id,
            account_type='cash'
        )
        self.capital_account = self.assign_account(self.id, 'capital')
        self.liability_account = self.assign_account(self.id, 'liability')
        self.make_transaction(
            self.cash_account,
            self.capital_account,
            self.date_established,
            starting_capital
        )
 
    def __register(self):
        # populate universe company record
        insurer_table = pd.DataFrame([[self.name]], columns=['bank_name'])
        session, connection = connect_universe()
        insurer_table.to_sql(
            'bank',
            connection,
            index=False,
            if_exists='append'
        )
        bank_id = query_bank_id(self.name)
        return bank_id
 
    def get_customers(self, ids, customer_type):
        new_customers = pd.DataFrame()
        new_customers[customer_type + '_id'] = pd.Series(ids)
        new_customers['customer_type'] = customer_type
 
        objects = []
        for index, row in new_customers.iterrows():
            if customer_type == 'person':
                customer_type_table = Person(person_id=row[customer_type + '_id'])
            elif customer_type == 'insurer':
                customer_type_table = Insurer(insurer_id=row[customer_type + '_id'])
            else:
                customer_type_table = BankTable(bank_id=row[customer_type + '_id'])
 
            customer = Customer(
                customer_type=customer_type
            )
            customer_type_table.customer.append(customer)
            objects.append(customer_type_table)
 
        self.session.add_all(objects)
        self.session.commit()
 
    def assign_accounts(self, customer_ids, account_type):
        """
        assign multiple accounts given customer ids
        """
        new_accounts = pd.DataFrame()
        new_accounts['customer_id'] = customer_ids
        new_accounts['account_type'] = account_type
 
        new_accounts.to_sql(
            'account',
            self.connection,
            index=False,
            if_exists='append'
        )
 
    def assign_account(self, customer_id, account_type):
        """
        assign a single account for a customer
        """
        account = Account(customer_id=int(customer_id), account_type=account_type)
        self.session.add(account)
        self.session.commit()
        return account.account_id
 
    def make_transaction(self, debit_account, credit_account, transaction_date, transaction_amount):
        """
        make a single transaction
        """
        transaction = Transaction(
            debit_account=int(debit_account),
            credit_account=int(credit_account),
            transaction_date=transaction_date,
            transaction_amount=transaction_amount
        )
        self.session.add(transaction)
        self.session.commit()
        return transaction.transaction_id
 
    def make_transactions(self, data: pd.DataFrame):
        """
        accepts a DataFrame to make multiple transactions
        need debit, credit, transaction date, transaction amount
        """
        data['debit_account'] = data['debit_account'].astype(int)
        data['credit_account'] = data['credit_account'].astype(int)
        data.to_sql(
            'transaction',
            self.connection,
            index=False,
            if_exists='append'
        )

Upon initialization, a bank will register itself as a customer, this allows it to have its own accounts, as well as to accept deposits from other customers. The reason why it needs to have its own accounts is because each transaction will need to have corresponding debit and credit accounts, and transactions between a bank and its customers will sometimes have one of the bank’s own accounts involved.

The method bank.get_customers() takes a list of IDs, which can be either those of people, insurers, or banks. For each of these entity IDs, the bank will create its own identifier for the customer, which can differ between that customer’s underlying ID. For example, a person with a person ID of 1, and an insurer with an insurer ID of 1, will have separate customer ids.

The method bank.assign_accounts() takes a list of customer IDs, a type of account (such as cash), and then creates that type of account for each customer.

The method bank.make_transactions() will take a list of transactions, each of which have a debit account, credit account, transaction date, and transaction volume defined, and then store them in the transactions table.

Now that we have our bank defined, I’ll walk through the insurance underwriting/claim cycle and show how the transactions that are currently available in MIES work. First, we’ll import the necessary modules, create an environment, make a population of 1000 people, and then create a bank called ‘blargo’ that has 4B in starting capital:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
import datetime as dt
 
from mies.entities.bank import Bank
from mies.entities.broker import Broker
from mies.entities.god import God
from mies.entities.insurer import Insurer
from mies.utilities.queries import query_population
from utilities.queries import query_customers_by_person_id
 
ahura = God()
ahura.make_population(1000)
 
blargo = Bank(4000000, 'blargo')

Starting Wealth

Before we can issue policies to customers, we need to have a starting amount of wealth for each person so that they can actually pay for their policies. Furthermore, we also need each person to have a bank account from which they can issue payments to their insurers. To take care of these two steps, we’ll first send the person IDs to the bank, which will then create corresponding customer IDs and then establish one cash account for each customer:

Python
1
2
3
blargo.get_customers(ids=ids, customer_type='person')
customer_ids = query_customers_by_person_id(ids, 'blargo')
blargo.assign_accounts(customer_ids=customer_ids, account_type='cash')

This action populates two tables. The person table contains a record of all 1000 people, and the customer table has 1001 records, since each person becomes a customer, but the bank itself is already its own customer:

In the next step, we’ll use a new method called grant_wealth() which the environment uses to give each person a starting amount of wealth. Since wealth is not evenly distributed in society, I’ve drawn these values from the Pareto distribution:

1
ahura.grant_wealth(person_ids=ids, bank=blargo, transaction_date=pricing_date)

The method is defined as follows:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class God:
...
    def grant_wealth(
            self,
            person_ids,
            bank: Bank,
            transaction_date
    ):
        """
        assign an initial amount of starting wealth per person
        """
        accounts = query_accounts_by_person_id(
            person_ids,
            bank.name,
            'cash'
        )
        accounts['transaction_amount'] = pareto.rvs(
            b=1,
            scale=pm.person_params['income'],
            size=accounts.shape[0],
        )
        accounts = accounts[[
            'account_id',
            'transaction_amount'
        ]]
        accounts = accounts.rename(columns={
            'account_id': 'debit_account'
        })
        accounts['credit_account'] = bank.liability_account
        accounts['transaction_date'] = transaction_date
        bank.make_transactions(accounts)
...

This action deposits wealth in each person’s cash account. This is marked as the debit side of the transaction, the credit side is the liability that the bank takes on by accepting the deposits, since deposits are loans to the bank:

Policy Inception

Before we issue policies, we need to create two more entities in the simulation. One, a broker called ‘rayon’ and an insurer called ‘company_1.’ Initializing a company now requires two new arguments, a bank with which it associates, and its inception date. These new arguments are used to create bank accounts for the insurer:

Python
1
2
3
4
5
6
7
8
9
rayon = Broker()
 
company_1 = Insurer(4000000, blargo, pricing_date, 'company_1')
 
company_1_formula = 'incurred_loss ~ ' \
                    'age_class + ' \
                    'profession + ' \
                    'health_status + ' \
                    'education_level'

We’ll now use the broker rayon to place the customers with the insurer. This method also has a bank as a new argument, which will be used to facilitate the transactions:

Python
1
2
3
4
5
rayon.place_business(
        pricing_date,
        blargo,
        company_1
    )

This action creates a policy record for each insurer, which I won’t show here since I’ve already done so in a previous post. What has changed however, is that each person needs to pay the premium to the insurer up front. The broker is able to tell the bank to create a transaction for each premium payment. This time, the debit account is the insurer’s cash account, and the credit account is the person’s cash account. In the picture below, we see that there are 1000 additional transactions starting with transaction_id 1003 (1002 was used to seed insurer capital). The debit account on all these transactions is account ID 1004, which is the insurer’s cash account:

In reality, a corresponding liability should also be created in the insurer’s accounting system. This is called the unearned premium reserve which is what the insured is entitled to recieve if their policy gets canceled. This feature is not yet implemented in MIES, but it’s an important liability to consider in insurance.

Loss Occurrence

Now that we have our policies issued, we’re ready to simulate some losses:

Python
1
2
3
event_date = pricing_date + dt.timedelta(days=1)
 
ahura.smite(event_date)

This action produced 57 loss events, which can be found in the events table in the universe database:

Claim Reporting

These losses are not considered claims until they are reported to the insurer. Otherwise, the insurer has no knowledge that they occurred:

Python
1
rayon.report_claims(event_date)

This method has changed from last week. The loss amounts are now reported as case reserves, which are estimates made by an insurer on how much they will need to pay for the claim. This is now distinguished from paid losses, which are the actual payments the insurer makes to the insured:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Broker:
...
    def report_claims(self, report_date):
        # match events to policies in which they are covered
 
        events = query_events_by_report_date(report_date)
 
        policies = query_all_policies()
 
        claims = events.merge(
            policies,
            on='person_id',
            how='left'
        )
        claims = claims.query(
            'event_date >= effective_date '
            'and event_date <= expiration_date'
        )
 
        claims = claims.drop([
            'effective_date',
            'expiration_date',
            'premium',
            'company_id'
        ], axis=1)
 
        companies = get_company_names()
 
        for company in companies:
 
            # register claims by id
 
            reported_claims = claims[claims['company_name'] == company]
 
            reported_claims = reported_claims.rename(columns={
                'event_date': 'occurrence_date'
            })
 
            reported_claims = reported_claims.drop(['company_name'], axis=1)
 
            session, connection = connect_company(company)
 
            objects = []
            for index, row in reported_claims.iterrows():
                claim = Claim(
                    policy_id=row['policy_id'],
                    person_id=row['person_id'],
                    event_id=row['event_id'],
                    occurrence_date=row['occurrence_date'],
                    report_date=row['report_date']
                )
                open_claim = ClaimTransaction(
                    transaction_date=row['report_date'],
                    transaction_type='open claim',
                    transaction_amount=0
                )
                case_reserve = ClaimTransaction(
                    transaction_date=row['report_date'],
                    transaction_type='set case reserve',
                    transaction_amount=row['ground_up_loss']
                )
                claim.claim_transaction.append(open_claim)
                claim.claim_transaction.append(case_reserve)
                objects.append(claim)
 
            session.add_all(objects)
            session.commit()
 
            connection.close()

This action creates two transactions for each claim. One transaction, called ‘open claim’ signals that a claim has been created. Another transaction, called ‘set case reserve,’ sets a case reserve for each claim. Since there are 57 losses, there are 57 claims, and 114 transactions:

Notice that we have some fairly restrictive assumptions for the simulation. The case reserves are equal to the ground up losses, and all 57 losses are reported to and known by the insurer immediately. This is not the case in the real world, where an insurer does not know how much claims will cost until they are settled, and may not know about claims until many years after they have occurred. We’ll need to revisit this problem in the future, since estimating claim amounts, including those on unreported claims, is a core function of actuarial science.

Claim Settlement

Let’s close these claims by issuing payments:

Python
1
company_1.pay_claims(event_date + dt.timedelta(days=1))

Insurer.pay_claims() is a new method used to send checks to the insureds for indemnification:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    def pay_claims(self, transaction_date):
        # send checks to bank
        case_reserves = query_open_case_reserves(self.company_name)
 
        accounts_to_pay = query_accounts_by_person_id(
            case_reserves['person_id'],
            self.bank.name, 'cash'
        )
 
        case_reserves = case_reserves.merge(
            accounts_to_pay,
            on='person_id',
            how='left'
        )
 
        case_reserves['transaction_date'] = transaction_date
 
        case_reserves = case_reserves.rename(columns={
            'case reserve': 'transaction_amount',
            'account_id': 'debit_account'
        })
 
        case_reserves['credit_account'] = self.cash_account
 
        payments = case_reserves[[
            'debit_account',
            'credit_account',
            'transaction_date',
            'transaction_amount']].copy()
 
        self.bank.make_transactions(payments)
 
        # people then use checks to pay their for their own losses
 
        payments['credit_account'] = payments['debit_account']
        payments['debit_account'] = self.bank.liability_account
 
        self.bank.make_transactions(payments)
 
 
        # reduce case reserves
 
        objects = []
 
        for index, row in case_reserves.iterrows():
            reserve_takedown = ClaimTransaction(
                claim_id = row['claim_id'],
                transaction_date=row['transaction_date'],
                transaction_type='reduce case reserve',
                transaction_amount=row['transaction_amount']
            )
            claim_payment = ClaimTransaction(
                claim_id=row['claim_id'],
                transaction_date=row['transaction_date'],
                transaction_type='claim payment',
                transaction_amount=row['transaction_amount']
            )
            close_claim = ClaimTransaction(
                claim_id=row['claim_id'],
                transaction_date=row['transaction_date'],
                transaction_type='close claim',
                transaction_amount=0
            )
            objects.append(reserve_takedown)
            objects.append(claim_payment)
            objects.append(close_claim)
 
        self.session.add_all(objects)
        self.session.commit()

There’s a lot going on here, so I’ll break it down. The payments to the insureds are handled first. We can see this by going to the transactions table in the bank database:

Notice that there are 114 additional transactions, two for each of the 57 claims. One transaction sends a payment from the insurer to the customer. You can see this since the debit account is the cash account of the customer, and the credit accont (1004) is the cash account of the insurer. The other transaction is a payment from the insured to whomever they owe money to due to the loss. The debit side of the transaction is now a reduction in the bank’s liability account, and the credit amount is a reduction in cash equal to the claim amount for the insured:

Next, three transactions are entered for each claim into the insurer’s database:

  1. Reduce case reserve
  2. Claim payment
  3. Close claim

‘Reduce case reserve’ is a reduction in the claims reserve to zero, signifying that the insurer no longer owes money to the insured. The ‘claim payment’ is a corresponding transaction representing the actual payment, and ‘close claim’ is a transaction that indicates that the claim is now closed. The insurer now has 5 claims transactions for each claim, 57 x 5 = 285 transactions in total. These five transactions are: 1) open claim, 2) set case reserve, 3) reduce case reserve 4) claim payment, and 5) close claim.

One source of confusion I had is that this way of recording transactions does not have the double-entry accounting that you’d see in a general ledger. Indeed, this form is more common for actuarial pricing modelers who do not usually need to get into the finer details of debits and credits. However, I’m leaning towards changing the claims transaction tables to also be double-entry, since doing so also makes things easier to program and avoids questions with negative values. For example, I had to think about whether to record case reserve reductions as a positive or negative value. This confusion is not present if I record them as credit and debit amounts.

Consumer Income

Each person now gets a paycheck during each period. This can be issued by the environment class:

Python
1
ahura.send_paychecks(person_ids=ids, bank=blargo, transaction_date=event_date + dt.timedelta(days=1))

There’s nothing too special about this method, it just debits each person’s cash account by their income amount:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class God:
...
    def send_paychecks(self, person_ids, bank: Bank, transaction_date):
        incomes = query_incomes(person_ids)
        incomes.columns = ['person_id', 'transaction_amount']
 
        accounts = query_accounts_by_person_id(
            person_ids,
            bank.name,
            'cash'
        )
 
        accounts = accounts.merge(incomes, on='person_id', how='left')
 
        accounts = accounts.rename(columns={
            'account_id': 'debit_account'
        })
        accounts['credit_account'] = bank.liability_account
        accounts['transaction_date'] = transaction_date
        accounts = accounts.drop([
            'person_id',
            'customer_id'
        ], axis=1)
        bank.make_transactions(accounts)

An additional 1000 transactions have been recorded, one for each customer. As with wealth, the debit side of the transaction is the person’s cash account, with a corresponding credit to the bank’s liability account:

Policy Pricing

Now that the claims have been reported and settled, the insurer can use this information to recalibrate the premium for each customer. However, unlike last week, claim amounts are not stored as a single column called ‘incurred_loss’, but now must be calculated from the transaction amounts. To handle this, I created a set of queries that can be used to return the case reserves, paid losses, and incurred amounts for each claim:

Python
1
2
3
4
from utilities.queries import query_case_by_claim
from utilities.queries import query_paid_by_claim
from utilities.queries import query_incurred_by_claim
from utilities.queries import query_pricing_model_data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
def query_case_by_claim(company_name):
    session, connection = connect_company(company_name)
 
    claim_policy = session.query(
        Claim.claim_id,
        Claim.policy_id
    ).subquery()
 
    case_set = session.query(
        ClaimTransaction.claim_id,
        func.sum(ClaimTransaction.transaction_amount).label('set')
    ).filter(ClaimTransaction.transaction_type == 'set case reserve').group_by(ClaimTransaction.claim_id).subquery()
 
    case_takedown = session.query(
        ClaimTransaction.claim_id,
        func.sum(ClaimTransaction.transaction_amount).label('takedown')
    ).filter(ClaimTransaction.transaction_type == 'reduce case reserve').group_by(ClaimTransaction.claim_id).subquery()
 
    case_query = session.query(
        case_set.c.claim_id,
        (func.ifnull(case_set.c.set, 0) - func.ifnull(case_takedown.c.takedown, 0)).label('case_reserve')
    ).outerjoin(case_takedown, case_set.c.claim_id == case_takedown.c.claim_id).subquery()
 
    claim_case = session.query(
        claim_policy.c.claim_id,
        claim_policy.c.policy_id,
        func.ifnull(case_query.c.case_reserve, 0).label('case_reserve')
    ).outerjoin(case_query, claim_policy.c.claim_id == case_query.c.claim_id).statement
 
    case_reserve = pd.read_sql(claim_case, connection)
 
    connection.close()
 
    return case_reserve
 
 
def query_paid_by_claim(company_name):
    session, connection = connect_company(company_name)
 
    claim_policy = session.query(
        Claim.claim_id,
        Claim.policy_id
    ).subquery()
 
    payment_query = session.query(
        ClaimTransaction.claim_id,
        func.sum(ClaimTransaction.transaction_amount).label('paid_loss')
    ).filter(ClaimTransaction.transaction_type == 'claim payment').group_by(ClaimTransaction.claim_id).subquery()
 
    claim_paid = session.query(
        claim_policy.c.claim_id,
        claim_policy.c.policy_id,
        func.ifnull(payment_query.c.paid_loss, 0).label('paid_loss')
    ).outerjoin(payment_query, claim_policy.c.claim_id == payment_query.c.claim_id).statement
 
    claim_payments = pd.read_sql(claim_paid, connection)
 
    connection.close()
 
    return claim_payments
 
 
def query_incurred_by_claim(company_name):
 
    case = query_case_by_claim(company_name)
    case = case.drop(columns=['policy_id'], axis=1)
 
    paid = query_paid_by_claim(company_name)
 
    incurred = paid.merge(case, on='claim_id', how='left')
 
    incurred['incurred_loss'] = incurred['paid_loss'] + incurred['case_reserve']
 
    return incurred
 
def query_pricing_model_data(company_name):
    session, connection = connect_company(company_name)
 
    policy_query = session.query(
        Policy.policy_id,
        Policy.person_id,
        Customer.age_class,
        Customer.profession,
        Customer.health_status,
        Customer.education_level,
        ).outerjoin(
            Customer,
            Policy.person_id == Customer.person_id
        ).statement
 
    policy = pd.read_sql(policy_query, connection)
 
    claim = query_incurred_by_claim(company_name)
 
    claim = claim.drop(columns=['claim_id', 'paid_loss', 'case_reserve'], axis=1)
 
    claim = claim.groupby(['policy_id'])['incurred_loss'].agg('sum')
 
    model_set = policy.merge(claim, on='policy_id', how='left')
 
    model_set['incurred_loss'] = model_set['incurred_loss'].fillna(0)
 
    return model_set

We mostly need to be concerned about the last one of these, query_pricing_model_data() which uses the first three to combine policy and claim information together, which can be priced with a GLM:

Python
1
query_pricing_model_data('company_1')

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Out[15]:
     policy_id  person_id age_class profession health_status education_level  \
0            1          1         E          A             P               H  
1            2          2         E          B             F               P  
2            3          3         Y          C             F               H  
3            4          4         M          C             F               H  
4            5          5         M          A             P               P  
..         ...        ...       ...        ...           ...             ...  
995        996        996         M          B             P               H  
996        997        997         E          A             P               H  
997        998        998         E          B             G               H  
998        999        999         M          A             G               P  
999       1000       1000         M          C             G               H  
     incurred_loss  
0              0.0  
1              0.0  
2              0.0  
3              0.0  
4              0.0  
..             ...  
995            0.0  
996            0.0  
997            0.0  
998            0.0  
999            0.0  
[1000 rows x 7 columns]

Since most people don’t have a claim, most incurred loss amounts are zero. This data set can then be used by the insurer to reprice with a GLM, and the new pricing algorithm is used by the broker to quote and place business:

1
2
3
4
5
6
7
company_1.price_book(company_1_formula)
 
rayon.place_business(
        pricing_date,
        blargo,
        company_1
    )

Further Improvements

Now that I’ve got transactions modeled, I can calculate the wealth for each entity in the simulation. This is key piece required to make further changes to the way consumer preferences work in MIES, which will soon incorporate risk tolerance and wealth.

Posted in: Actuarial, Mathematics, MIES

No. 140: MIES – Rate Changes – Slutsky and Hicks Decomposition

28 June, 2020 6:24 PM / Leave a Comment / Gene Dan

This entry is part of a series dedicated to MIES – a miniature insurance economic simulator. The source code for the project is available on GitHub.

Current Status

Last week, I revisited the MIES backend to split what was a single database into multiple databases – one per entity in the simulation. This enhances the level of realism and should make it easier to program transactions between the entities. This week, I’ll revisit consumer behavior by implementing the Slutsky and Hicks decomposition methods for analyzing price changes.

In the context of insurance, consumers frequently experience changes in price, typically during renewals when insurers apply updated rating algorithms against new information known about the insureds. For example, If you’ve made a claim or two during your policy period, and miss a rent payment, causing your credit score to go down, your premium is likely to increase upon renewal. This is because past claims and credit are frequently used as the underlying variables in a rating algorithm.

What happens when consumers face an increase in rates? Oftentimes, they reduce coverage, may try to shop around, or may do nothing at all and simply pay the new price. Today’s post discusses the first phenomenon, whereby an increase in rates causes a reduction in the amount of insurance that a consumer purchases. We can use the Slutsky and Hicks decomposition methods to break down this behavior into two components:

  1. The substitution effect
  2. The income effect

The substitution effect concerns the change in relative prices between two goods. Here, when a consumer faces a premium increase, the price of insurance increases relative to the price of all other goods, altering the rate at which the consumer would be willing to exchange a unit of insurance for a unit of all other goods.

The income effect concerns the change in the price of a good relative to consumer income. When a consumer faces a premium increase without a corresponding increase in income, their income affords them fewer units of insurance.

The content of this post roughly follows the content of chapter 8 in Varian. As usual, I focus on how this applies to my own project and will skip over much of the theoretical derivations that can be found in the text.

Slutsky Identity

The Slutsky identity has the following form:

    \[\frac{\Delta x_1}{\Delta p_1} = \frac{\Delta x_1^s}{\Delta p_1} - \frac{\Delta x_1^m}{\Delta m}x_1\]

Where the x_1 represents the quantity of good 1 (in this case insurance), p_1 represents the price of insurance (that is, the premium) and m represents the consumer’s income. The deltas are used to describe how the quantity of insurance purchased changes with premium, expressed on the left side of the identity. The first term after the equals sign represents the substitution effect, and the second term after the equals sign represents the income effect.

The Slutsky identity can also be expressed in terms of units instead of rates of change (\Delta x_1^m = -\Delta x_1^n):

    \[\Delta x_1 = \Delta x_1^s + \Delta x_1^n\]

While MIES can handle both forms, we’ll focus on the first form for the rest of the chapter.

Substitution Effect

Let’s examine the substitution effect. First, we’ll conduct all the steps manually, and then I’ll show how they are integrated into MIES. If we have some data already stored in a past simulation, we can extract a person from it:

Python
1
2
my_person = Person(1)
my_person.data

1
2
3
4
5
Out[12]:
   person_id age_class profession health_status education_level        income  \
0          1         E          B             F               U  89023.365436  
          wealth  cobb_c  cobb_d  
0  225300.272033     0.1     0.9  

Here, we have a person who makes 89k per year, with Cobb Douglas parameters c = 0.1 and d = 0.9. This means this person will spend 10% of their income on premium. For the sake of making the graphs in this post less cluttered and easier to read, let’s change c = d = .5 so the person spends 50% of their income on insurance:

Python
1
2
3
4
5
6
my_person.utility.c = .5
my_person.utility.d = .5
my_person.get_budget()
my_person.get_consumption()
my_person.get_consumption_figure()
my_person.show_consumption()

From the above figure, we can see that the person consumes about 45k worth of insurance. That’s about 11 units of exposure at a 4k premium per exposure, which is unrealistic in real life, but makes life easy for me because I haven’t bothered to update the margins of my website to accommodate wider figures. We’ll denote this bundle as ‘Old Bundle’ because we’ll compare it to the new consumption bundle after a rate change (note – if you are trying to replicate this in MIES, your figures may look a bit different, in reality I manually adjust the style of the plots so they can fit on my blog).

Now, let’s suppose the person faces a 100% increase in their rate so that their premium is 8k per unit of exposure, and construct a budget to reflect this:

Python
1
2
3
insurance = Good(8000, name='insurance')
all_other = Good(1, name='all other goods')
new_budget = Budget(insurance, all_other, income=my_person.income, name='new_budget')

Plotting this new budget, we see that the line tilts inward, since the person can only afford half as much insurance as before:

The first stage in Slutsky decomposition involves calculating the substitution effect. This is done by tilting the budget line to the slope of the new prices, but with income adjusted so that the person can still afford their old bundle. We then determine the new optimal bundle at this budget (denoted ‘substitution budget’) and call it the substitution bundle:

    \[\Delta m = x_1 \Delta p_1\]

1
2
3
sub_income = my_person.income + my_person.optimal_bundle[0] * 4000
substitution_budget = Budget(insurance, all_other, income=sub_income, name='Substitution<br /> Budget')
...more plotting code...

You can see that if the person were given enough money to purchase the same amount of insurance even after the rate increase, they would still reduce their consumption from 11.1 to 8.3 units of insurance. 8.3 – 11.1 = -2.8 is the substitution effect.

Income Effect

Now, to calculate the income effect, we then shift the substitution budget to the position of the new budget:

The person purchases 5.5 units of insurance at the new budget, so the income effect is 5.5 – 8.3 = -2.8 for the substitution effect. The total effect is -2.8 -2.8 = -5.6 units of insurance, the sum of the substitution and income effect. This is the same as the new consumption minus the old consumption 5.5 – 11.1 = -5.6

MIES Integration

The Slutsky decomposition process has been integrated into MIES as a class in econtools/slutsky.py:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import plotly.graph_objects as go
 
from plotly.offline import plot
from econtools.budget import Budget
from econtools.utility import CobbDouglas
 
 
class Slutsky:
    """
    Implementation of the Slutsky equation, accepts two budgets, a utility function, and calculates
    the income and substitution effects
    """
    def __init__(
            self,
            old_budget: Budget,
            new_budget: Budget,
            utility_function: CobbDouglas  # need to replace with utility superclass
    ):
        self.old_budget = old_budget
        self.old_budget.name = 'Old Budget'
        self.new_budget = new_budget
        self.new_budget.name = 'New Budget'
        self.utility = utility_function
 
        self.old_bundle = self.utility.optimal_bundle(
            self.old_budget.good_x.price,
            self.old_budget.good_y.price,
            self.old_budget.income
        )
 
        self.delta_p = self.new_budget.good_x.price - self.old_budget.good_x.price
        self.pivoted_budget = self.calculate_pivoted_budget()
        self.substitution_bundle = self.calculate_substitution_bundle()
        self.substitution_effect = self.calculate_substitution_effect()
        self.new_bundle = self.calculate_new_bundle()
        self.income_effect = self.calculate_income_effect()
        self.total_effect = self.substitution_effect + self.income_effect
        self.substitution_rate = self.calculate_substitution_rate()
        self.income_rate = self.calculate_income_rate()
        self.slutsky_rate = self.substitution_rate - self.income_rate
        self.plot = self.get_slutsky_plot()
 
    def calculate_pivoted_budget(self):
        """
        Pivot the budget line at the new price so the consumer can still afford their old bundle
        """
        delta_m = self.old_bundle[0] * self.delta_p
        pivoted_income = self.old_budget.income + delta_m
        pivoted_budget = Budget(
            self.new_budget.good_x,
            self.old_budget.good_y,
            pivoted_income,
            'Pivoted Budget'
        )
        return pivoted_budget
 
    def calculate_substitution_bundle(self):
        """
        Return the bundle consumed after pivoting the budget line
        """
        substitution_bundle = self.utility.optimal_bundle(
            self.pivoted_budget.good_x.price,
            self.pivoted_budget.good_y.price,
            self.pivoted_budget.income
        )
        return substitution_bundle
 
    def calculate_substitution_effect(self):
        substitution_effect = self.substitution_bundle[0] - self.old_bundle[0]
        return substitution_effect
 
    def calculate_new_bundle(self):
        """
        Shift the budget line outward
        """
        new_bundle = self.utility.optimal_bundle(
            self.new_budget.good_x.price,
            self.new_budget.good_y.price,
            self.new_budget.income
        )
        return new_bundle
 
    def calculate_income_effect(self):
        income_effect = self.new_bundle[0] - self.substitution_bundle[0]
        return income_effect
 
    def calculate_substitution_rate(self):
        delta_s = self.calculate_substitution_effect()
        delta_p = self.new_budget.good_x.price - self.old_budget.good_x.price
        substitution_rate = delta_s / delta_p
        return substitution_rate
 
    def calculate_income_rate(self):
        delta_p = self.new_budget.good_x.price - self.old_budget.good_x.price
        delta_m = self.old_bundle[0] * delta_p
        delta_x1m = -self.calculate_income_effect()
        income_rate = delta_x1m / delta_m * self.old_bundle[0]
        return income_rate
 
    def get_slutsky_plot(self):
        max_x_int = max(
            self.old_budget.income / self.old_budget.good_x.price,
            self.pivoted_budget.income / self.pivoted_budget.good_x.price,
            self.new_budget.income / self.new_budget.good_x.price
        ) * 1.2
 
        max_y_int = max(
            self.old_budget.income,
            self.pivoted_budget.income,
            self.new_budget.income,
        ) * 1.2
 
        # interval boundaries
        effect_boundaries = [
            self.new_bundle[0],
            self.substitution_bundle[0],
            self.old_bundle[0]
        ]
        effect_boundaries.sort()
 
        fig = go.Figure()
 
        # budget lines
        fig.add_trace(self.old_budget.get_line())
        fig.add_trace(self.pivoted_budget.get_line())
        fig.add_trace(self.new_budget.get_line())
 
        # utility curves
        fig.add_trace(
            self.utility.trace(
                k=self.old_bundle[2],
                m=max_x_int,
                name='Old Utility'
            )
        )
        fig.add_trace(
            self.utility.trace(
                k=self.substitution_bundle[2],
                m=max_x_int,
                name='Pivoted Utility'
            )
        )
        fig.add_trace(
            self.utility.trace(
                k=self.new_bundle[2],
                m=max_x_int,
                name='New Utility'
            )
        )
        # consumption bundles
 
        fig.add_trace(
            go.Scatter(
                x=[self.old_bundle[0]],
                y=[self.old_bundle[1]],
                mode='markers+text',
                text=['Old Bundle'],
                textposition='top center',
                marker=dict(
                    size=[15],
                    color=[1]
                ),
                showlegend=False
            )
        )
 
        fig.add_trace(
            go.Scatter(
                x=[self.substitution_bundle[0]],
                y=[self.substitution_bundle[1]],
                mode='markers+text',
                text=['Pivoted Bundle'],
                textposition='top center',
                marker=dict(
                    size=[15],
                    color=[2]
                ),
                showlegend=False
            )
        )
 
        fig.add_trace(
            go.Scatter(
                x=[self.new_bundle[0]],
                y=[self.new_bundle[1]],
                mode='markers+text',
                text=['New Bundle'],
                textposition='top center',
                marker=dict(
                    size=[15],
                    color=[3]
                ),
                showlegend=False
            )
        )
        # Substitution and income effect interval lines
        fig.add_shape(
            type='line',
            x0=self.substitution_bundle[0],
            y0=self.substitution_bundle[1],
            x1=self.substitution_bundle[0],
            y1=0,
            line=dict(
                color="grey",
                dash="dashdot",
                width=1
            )
        )
 
        fig.add_shape(
            type='line',
            x0=self.new_bundle[0],
            y0=self.new_bundle[1],
            x1=self.new_bundle[0],
            y1=0,
            line=dict(
                color="grey",
                dash="dashdot",
                width=1
            )
        )
 
        fig.add_shape(
            type='line',
            x0=self.old_bundle[0],
            y0=self.old_bundle[1],
            x1=self.old_bundle[0],
            y1=0,
            line=dict(
                color="grey",
                dash="dashdot",
                width=1
            )
        )
        fig.add_shape(
            type='line',
            xref='x',
            yref='y',
            x0=effect_boundaries[0],
            y0=max_y_int / 10,
            x1=effect_boundaries[1],
            y1=max_y_int / 10,
            line=dict(
                color='grey',
                dash='dashdot'
            )
        )
        fig.add_shape(
            type='line',
            xref='x',
            yref='y',
            x0=effect_boundaries[1],
            y0=max_y_int / 15,
            x1=effect_boundaries[2],
            y1=max_y_int / 15,
            line=dict(
                color='grey',
                dash='dashdot'
            )
        )
        fig.add_shape(
            type='line',
            xref='x',
            yref='y',
            x0=effect_boundaries[0],
            y0=max_y_int / 20,
            x1=effect_boundaries[2],
            y1=max_y_int / 20,
            line=dict(
                color='grey',
                dash='dashdot'
            )
        )
 
        fig.add_annotation(
            x=(self.substitution_bundle[0] + self.old_bundle[0]) / 2,
            y=max_y_int / 10,
            text='Substitution<br />Effect',
            xref='x',
            yref='y',
            showarrow=True,
            arrowhead=7,
            ax=5,
            ay=-40,
        )
 
        fig.add_annotation(
            x=(self.new_bundle[0] + self.substitution_bundle[0]) / 2,
            y=max_y_int / 15,
            text='Income Effect',
            xref='x',
            yref='y',
            showarrow=True,
            arrowhead=7,
            ax=50,
            ay=-20
        )
 
        fig.add_annotation(
            x=(effect_boundaries[2] + effect_boundaries[0]) / 2,
            y=max_y_int / 20,
            text='Total Effect',
            xref='x',
            yref='y',
            showarrow=True,
            arrowhead=7,
            ax=100,
            ay=20
        )
 
        fig['layout'].update({
            'title': 'Slutsky Decomposition',
            'title_x': 0.5,
            'xaxis': {
                'title': 'Amount of Insurance',
                'range': [0, max_x_int]
            },
            'yaxis': {
                'title': 'Amount of All Other Goods',
                'range': [0, max_y_int]
            }
        })
        return fig
 
    def show_plot(self):
        plot(self.plot)

This has been added to the Person class, so we can use its methods to get the substitution and income effects. This conveniently packages all the manual steps we took above together:

Python
1
2
3
my_person.calculate_slutsky(new_budget)
my_person.slutsky.income_effect
my_person.slutsky.substitution_effect

Which returns -2.8 for each effect, the same as above.

Hicks Decomposition

A similar decomposition method is called Hicks decomposition. Instead of pivoting the budget constraint so that the person can afford the same bundle as before, the budget constraint is shifted so that they have the same utility as before. Using algebra, we can solve this by fixing utility:

    \[m^\prime = \frac{\bar{u}}{\left(\frac{c}{c +d}\frac{1}{p_1^\prime}\right)^c\left(\frac{d}{c+d}\frac{1}{p_2}\right)^d}\]

where m^\prime is the adjusted income, and p_1^\prime is the new premium, and \bar{u} is the utility fixed at the original level.

You’ll notice there’s a subtle difference, the new bundle is the same as in the Slutsky case, but the substitution bundle in this case is on the original utility curve. This gives different answers for the substitution and income effects, which are -3.3 and -2.3, which add up to a total effect of -5.6, as before.

The code for the Hicks class is much the same as that for the Slutsky class, so I won’t post most of it here, the relevant change is in calculating the substitution budget:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Hicks:
...
 
    def calculate_pivoted_budget(self):
        """
        Pivot the budget line at the new price so the consumer still as the same utility
        """
        old_utility = self.old_bundle[2]
        c = self.utility.c
        d = self.utility.d
        p1_p = self.new_budget.good_x.price
        p2 = self.old_budget.good_y.price
        x1_no_m = (((c / (c + d)) * (1 / p1_p)) ** c)
        x2_no_m = (((d / (c + d)) * (1 / p2)) ** d)
        pivoted_income = old_utility / (x1_no_m * x2_no_m)
        pivoted_budget = Budget(
            self.new_budget.good_x,
            self.old_budget.good_y,
            pivoted_income,
            'Pivoted Budget'
        )
        return pivoted_budget
        ...

Further Improvements

Since Hicks substitution is mostly similar to Slutsky in terms of code, it makes sense for the Hicks class to inherit from the Slutsky class or for both of them to inherit from a superclass.

I’m currently working on implementing wealth into MIES, since as of today, none of the transactions actually impact wealth.

Posted in: Actuarial, Mathematics, MIES / Tagged: actuarial, insurance, MIES, simulator

No. 139: MIES – Schema Changes and Claims Reporting

21 June, 2020 4:13 PM / Leave a Comment / Gene Dan

This entry is part of a series dedicated to MIES – a miniature insurance economic simulator. The source code for the project is available on GitHub.

Current Status

When programming MIES, I face a trade-off between rolling out enough features for a weekly blog post and spending more time trying to learn Python in order to code them in the most elegant way possible. If I implement things too quickly, I face the problem of accumulating bugs and technical debt, but if I spend too much time trying learn the latest libraries, I’d never get anything done. Therefore, I’ve decided to just write what I can most weeks, but revisiting the code to make it better every 3rd or 4th week, so I can implement any new technical knowledge I’d gained during that time.

Last month, I demonstrated the phenomenon of adverse selection using MIES. However, the underlying database was not a realistic depiction of the real world:

There was only one database, shared by all entities involved in the simulation – firms, people, and the environment. In business, it’s more realistic for companies to have their own internal databases, without the ability to access any other company’s database. I’ve reached point where I’ve decided that it would be difficult to add insurance operations, specifically the reporting of claims, if all entities used the same database. Therefore, I’ve decided to spend time this week setting up separate schemas for each company.

The reason why I didn’t do this earlier is because I hadn’t gone far enough in the SQLAlchemy documentation to figure out how to establish multiple metadata bases to allow each company in the simulation to access its own database. The decision I was facing last month was to either write the adverse selection demo with the knowledge I had, or to keep reading further to set things up the right way and then write about how I built nothing, but read some documentation in the hope that I’d build something later. I’d gotten tired of doing the latter one too many times, so I decided to just go for it.

This week, I was confident that I’d be able to fix the single-schema problem and add a claims reporting facility as well, so that’s what I’ll go over today.

Splitting the Database

Splitting the database involved two separate challenges:

  1. Defining the schema for each entity
  2. Rewriting the code to accept the new backend structure

This second challenge was the main motivation for updating the schema now rather than later. The more features I added to MIES, the more of them I would have to rewrite when I inevitably split the database.

First, I envisioned the database structure I wanted for each firm:

Much of this was taken from the original schema, as seen at the top of this post. The policy table contains the same information as before, minus the company id. This is because a company does not need to have this field if all of its policies belong to itself.

The customer table is new. Here, I faced the challenge of whether I wanted to maintain data integrity between the information a company knows about its own customers, and their actual attributes as they exist outside the knowledge of the firm. For example, if a person switches insurers and then changes their profession, should the original insurer know about it?

In the real world, the answer is no. This also applies to things like credit scores which can change after the company does an initial credit check. Therefore, I’ve decided to discard data integrity between companies and the environment. I may want to rethink whether I want the all-knowing environment class to at least have a canonical record of all the information in the simulation (the answer is usually yes when I write these thoughts out in a blog post).

The customer table is analogous to the person table as before, but only contains information that the firm knows about each customer. This information typically costs money to acquire when quoting policies, and is considered an underwriting expense that I will need to add later.

Lastly, I’ve added a new table for claims. I mentioned last month that since we assumed each loss was fully covered, there was no need to distinguish between losses and claims. Now that we’re facing the challenge of assigning a losses to different companies as claims, I decided to lay the groundwork now so that we can loosen this assumption when we need to. Gradually, we will see the what the differences is between a loss and a claim. A loss is a fortuitous event that happens to a person, and an insurance claim is a financial claim that a person makes against their insurer for indemnification under their policy. There is also a distinction between the loss a person endures and the loss that a company needs to pay, due to things like coverage exclusions, limits, and deductibles. The claim table will take care of many of these distinctions.

Therefore, in a two-firm simulation, we will have a database for each firm. Below, we have two ER-diagrams side-by-side for each company:

This idea carries over to the case of an n-firm simulation, where we’ll have n databases. The code that defines the firm schema now resides in a new module:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Date, Float, String
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
 
 
Base = declarative_base()
 
 
class Customer(Base):
    __tablename__ = 'customer'
 
    person_id = Column(
        Integer,
        primary_key=True
    )
    age_class = Column(String)
    profession = Column(String)
    health_status = Column(String)
    education_level = Column(String)
 
    policy = relationship(
        "Policy",
        back_populates="customer"
    )
 
    def __repr__(self):
        return "<Customer(" \
               "age_class='%s', " \
               "profession='%s', " \
               "health_status='%s', " \
               "education_level='%s'" \
               ")>" % (
                   self.age_class,
                   self.profession,
                   self.health_status,
                   self.education_level,
               )
 
 
class Policy(Base):
    __tablename__ = 'policy'
 
    policy_id = Column(
        Integer,
        primary_key=True
    )
    person_id = Column(
        Integer,
        ForeignKey('customer.person_id')
    )
    effective_date = Column(Date)
    expiration_date = Column(Date)
    premium = Column(Float)
 
    customer = relationship(
        "Customer",
        back_populates="policy"
    )
 
    def __repr__(self):
        return "<Policy(person_id ='%s'," \
               "effective_date ='%s', " \
               "expiration_date='%s', " \
               "premium='%s')>" % (
                self.person_id,
                self.effective_date,
                self.expiration_date,
                self.premium
                )
 
 
class Claim(Base):
    __tablename__ = 'claim'
 
    claim_id = Column(
        Integer,
        primary_key=True
    )
    policy_id = Column(
        Integer,
        ForeignKey('policy.policy_id')
    )
    person_id = Column(Integer)
    event_id = Column(Integer)
    occurrence_date = Column(Date)
    report_date = Column(Date)
    incurred_loss = Column(Float)
 
    def __repr__(self):
        return "<Claim(policy_id ='%s'," \
               "person_id ='%s', " \
               "event_id='%s', " \
               "occurrence_date='%s'," \
               "report_date='%s'," \
               "incurred_loss='%s')>" % (
                self.policy_id,
                self.person_id,
                self.event_id,
                self.occurence_date,
                self.report_date,
                self.incurred_loss
                )

There is also a schema for the environment, which is similar to before but minus the policy-specific information:

Query Encapsulation

MIES relies on querying its databases to run the simulations. Up until now, many of these queries resided within the environment, broker, and insurer classes. The tricky thing about querying databases is that you should close the connections to them once you no longer need them. However, establishing and closing a database requires a few lines of code, and the queries themselves can be verbose as well. This can get repetitive and can lead to bugs if I forget to close a connection or make modifications to reused queries.

Therefore, I thought it would be a good idea to encapsulate them into functions. In order to do this, I created two modules in a folder called utilities that are used to connect to and then query the databases, respectively. For example, the connections file contains code to connect to either the universe (environment) database or the database for a particular company:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
import schema.universe as universe
 
 
def connect_universe():
    engine = sa.create_engine(
        'sqlite:///db/universe.db',
        echo=True
    )
    session = sessionmaker(bind=engine)()
    connection = engine.connect()
    return session, connection
 
 
def connect_company(company_name):
    engine = sa.create_engine(
        'sqlite:///db/companies/' + company_name + '.db',
        echo=True)
    session = sessionmaker(bind=engine)()
    connection = engine.connect()
    return session, connection

You can see that you need to import some things, and also define an engine, session, and connection. That’s quite a bit of code that I don’t want to repeat every time I need to access a database.

Moving onto the queries, we likewise have code that we don’t want to repeat. For example, the query_population() function in the queries file returns the PersonTable from the environment schema as a dataframe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import pandas as pd
 
from schema.universe import Company, PersonTable
from schema.insco import Customer, Policy
from utilities.connections import (
    connect_universe,
    connect_company)
 
def query_population():
    session, connection = connect_universe()
    query = session.query(PersonTable).statement
    population = pd.read_sql(query, connection)
    connection.close()
    return population

Now, rather than calling all the code that we see within these functions, we can just get the information in one line, that is, query_population(). Other queries within the file access other parts of the database and return information we might want to ask about frequently:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def query_company():
    session, connection = connect_universe()
    companies_query = session.query(Company).statement
    companies = pd.read_sql(
        companies_query,
        connection
    )
    connection.close()
    return companies
 
 
def query_all_policies():
    companies = query_company()
    policies = pd.DataFrame()
 
    for index, row in companies.iterrows():
        company_id = row['company_id']
        company_name = row['company_name']
        session, connection = connect_company(company_name)
        query = session.query(Policy).statement
        policy_c = pd.read_sql(
            query,
            connection
        )
        policy_c['company_id'] = company_id
        policy_c['company_name'] = company_name
        policies = policies.append(policy_c)
        connection.close()
 
    return policies
 
 
def query_in_force_policies(curr_date):
    companies = get_company_names()
    in_force = pd.DataFrame()
 
    for company in companies:
        session, connection = connect_company(company)
 
        exp_query = session.query(
            Policy
        ).filter(
            Policy.expiration_date == curr_date
        ).statement
 
        company_in_force = pd.read_sql(
            exp_query,
            connection)
 
        in_force = in_force.append(
            company_in_force,
            ignore_index=True
        )
 
        connection.close()
    return in_force
 
 
def get_company_names():
    session, connection = connect_universe()
    companies_query = session.query(Company.company_name).statement
    companies = pd.read_sql(
        companies_query,
        connection
    )
    connection.close()
    return list(companies['company_name'])
 
 
def get_company_ids():
    session, connection = connect_universe()
    companies_query = session.query(Company.company_id).statement
    companies = pd.read_sql(companies_query, connection)
    connection.close()
    return list(companies['company_id'])
 
 
def get_uninsured_ids(curr_date):
    population = query_population()
    in_force = query_in_force_policies(curr_date)
    uninsureds = population[~population['person_id'].isin(in_force['person_id'])]['person_id']
    return uninsureds
 
 
def get_customer_ids(company):
    session, connection = connect_company(company)
    id_query = session.query(Customer.person_id).statement
    ids = pd.read_sql(id_query, connection)
    return ids

Claims Reporting

If you looked carefully, you may have noticed an additional date field added since last month. In addition to the event date, synonymous with occurrence date, we now have the report date – that is, the date the claim is reported to the insurer. This distinction has important implications in actuarial science, since different types of policies exist that may only offer coverage on claims that either occur during or are reported during the period the policy is in effect.

For now, I have assumed that all policies are written on an occurrence basis, which means that a policy covers all claims that occur within the coverage period. This means that even if an insured reports a claim several years after is has occurred and can prove that its valid and occurred during the policy period, it is still covered under the terms of the policy.

I decided to add the claims reporting facility as a method in the broker class, which now resembles more of a place where transactions happen than an actual brokerage:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    def report_claims(self, report_date):
        # match events to policies in which they are covered
        session, connection = connect_universe()
 
        event_query = session.query(Event).\
            filter(Event.report_date == report_date).\
            statement
 
        events = pd.read_sql(event_query, connection)
        connection.close()
 
        policies = query_all_policies()
        claims = events.merge(
            policies,
            on='person_id',
            how='left'
        )
        claims = claims.query(
            'event_date >= effective_date '
            'and event_date <= expiration_date'
        )
 
        claims = claims.drop([
            'effective_date',
            'expiration_date',
            'premium',
            'company_id'
        ], axis=1)
 
        companies = get_company_names()
        for company in companies:
            reported_claims = claims[claims['company_name'] == company]
 
            reported_claims = reported_claims.rename(columns={
                'event_date': 'occurrence_date',
                'ground_up_loss': 'incurred_loss'
            })
 
            reported_claims = reported_claims.drop(['company_name'], axis=1)
 
            session, connection = connect_company(company)
            reported_claims.to_sql(
                'claim',
                connection,
                index=False,
                if_exists='append'
            )
            connection.close()

This function takes the report date as an argument, and then combs through both the event table from the environment and the policy table from each company to match the event to the appropriate company and policy, and then populate that company’s claim table.

The actual claims cycle is much more complex, involving adjusters, claimants, lawyers, agents, and other parties. These have been abstracted away for now, but I plan to add more detail later since claims modeling is itself an important part of an insurer’s strategy.

I’ve also made many changes to the other classes, but I wanted to highlight claims reporting since it’s a new feature. You can examine the other changes on GitHub.

Simulation

By encapsulating the queries, I’ve removed the number of arguments and lines of code needed to run the simulations. For example, we can now define an Insurer with just two arguments:

Python
1
2
3
company_1 = Insurer(4000000, 'company_1')
 
company_2 = Insurer(4000000, 'company_2')

Whereas previously, it took five aguments:

Python
1
2
3
company_1 = Insurer(gsession, engine, 4000000, Company, 'company_1')
 
company_2 = Insurer(gsession, engine, 4000000, Company, 'company_2')

The need to specify the connection, engine, and table was cumbersome and is now handled automatically by the Insurer class.

Finally, I tested out how the data flows from policy issuance to claims occurrence, and then to renewals, and MIES is still able to demonstrate the phenomenon of adverse selection:

Further Improvements

I’d like to get back to the subject of economics next week, so now I’m working on implementing the Slutsky Equation, which decomposes the motivations a consumer might have for changing their consumption as prices change.

Posted in: Actuarial, Mathematics, MIES

Post Navigation

1 2 3 Next »

Archives

  • September 2023
  • February 2023
  • January 2023
  • October 2022
  • March 2022
  • February 2022
  • December 2021
  • July 2020
  • June 2020
  • May 2020
  • May 2019
  • April 2019
  • November 2018
  • September 2018
  • August 2018
  • December 2017
  • July 2017
  • March 2017
  • November 2016
  • December 2014
  • November 2014
  • October 2014
  • August 2014
  • July 2014
  • June 2014
  • February 2014
  • December 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • March 2013
  • January 2013
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • January 2011
  • December 2010
  • October 2010
  • September 2010
  • August 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • September 2009
  • August 2009
  • May 2009
  • December 2008

Categories

  • Actuarial
  • Cycling
  • Logs
  • Mathematics
  • MIES
  • Music
  • Uncategorized

Links

Cyclingnews
Jason Lee
Knitted Together
Megan Turley
Shama Cycles
Shama Cycles Blog
South Central Collegiate Cycling Conference
Texas Bicycle Racing Association
Texbiker.net
Tiffany Chan
USA Cycling
VeloNews

Texas Cycling

Cameron Lindsay
Jacob Dodson
Ken Day
Texas Cycling
Texas Cycling Blog
Whitney Schultz
© Copyright 2025 - Gene Dan's Blog
Infinity Theme by DesignCoral / WordPress