-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMainLoop.py
More file actions
563 lines (496 loc) · 19.5 KB
/
Copy pathMainLoop.py
File metadata and controls
563 lines (496 loc) · 19.5 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import numpy as np
import pandas as pd
import datetime
import datetime as dt
import random
from typing import Dict, Optional
from LiabilityClasses import Liability, UnitLinkedFund, UnitLinkedPolicy
from SocietyClass import Society
def create_cashflow_dataframe(cf_dict: dict[int, dict[datetime.date, float]], unique_dates: list[datetime.date]) -> pd.DataFrame:
"""
Create a dataframe with dates as columns and equity shares as rows. If a cell has a non zero value, that
means that there is a cash flow from that particular share at that time.
Parameters
----------
:type cf_dates: pd.DataFrame
Dictionary of date/cash-flow pairs for each security
:type unique_dates: list
List of all relevant dates for the modelling run
Returns
-------
:type: pd.DataFrame
Dataframe matrix with cash flows in a matrix form
"""
cash_flows = pd.DataFrame(data=np.zeros((len(cf_dict), len(unique_dates))),
columns=unique_dates, index=cf_dict.keys()) # Dataframe of cashflows (columns are dates, rows, assets)
for asset_id in cf_dict.keys():
keys = cf_dict[asset_id]
for key in keys:
cash_flows.loc[asset_id, key] = keys[key]
return cash_flows
def calculate_expired_dates(list_of_dates: list[datetime.date], deadline: dt.date) -> list[datetime.date]:
"""
Returns all dates before the deadline date.
Parameters
----------
:type list_of_dates: list
List of all the dates considered
:type deadline: date
Last date considered
Returns
-------
:type: list
List of dates that occur before the deadline date
"""
return list(a_date for a_date in list_of_dates if a_date <= deadline)
def set_dates_of_interest(modelling_date: dt.date, end_date: dt.date, days_interval: int = 365) -> pd.Series:
"""
Calculates all dates at which the modelling run will run.
Parameters
----------
:type modelling_date: date
The starting modelling date
:type end_date: date
The end of the modelling window
:type days_interval: int
Time difference between two modelling dates of interest
Returns
-------
:type: pd.Series
Series of dates at which the modell will run
"""
next_date_of_interest: dt.date = modelling_date
dates_of_interest: list[dt.date] = []
while next_date_of_interest <= end_date:
next_date_of_interest += datetime.timedelta(days=days_interval)
dates_of_interest.append(next_date_of_interest)
return pd.Series(dates_of_interest, name="Dates of interest")
def create_liabilities_df(liabilities: Liability) -> pd.DataFrame:
"""
Create a liability DataFrame with dates as columns and individual positions as rows.
Parameters
----------
:type modelling_date: date
The starting modelling date
Returns
-------
:type: pd.DataFrame
The DataFrame with liability cash flows
"""
cash_flows = pd.DataFrame(columns=liabilities.cash_flow_dates)
cash_flows.loc[-1] = liabilities.cash_flow_series
cash_flows.index = [liabilities.liability_id]
return cash_flows
def portfolio_market_value(
eq_price: pd.DataFrame,
eq_units: pd.DataFrame,
bd_price: pd.DataFrame,
bd_units: pd.DataFrame,
as_of: dt.date,
) -> float:
"""
Calculate total market value of the equity and bond portfolios at a given date.
Parameters
----------
eq_price : pd.DataFrame
Per-asset equity prices indexed by asset_id with date columns.
eq_units : pd.DataFrame
Per-asset equity units indexed by asset_id with date columns.
bd_price : pd.DataFrame
Per-asset bond prices indexed by asset_id with date columns.
bd_units : pd.DataFrame
Per-asset bond units indexed by asset_id with date columns.
as_of : date
Valuation date (column name in the price and units DataFrames).
Returns
-------
float
Combined market value of equities and bonds at as_of.
"""
return float(
sum(eq_units[as_of] * eq_price[as_of])
+ sum(bd_units[as_of] * bd_price[as_of])
)
def process_expired_cf(unique_dates: list[datetime.date], expiration_date: dt.date, cash_flows: pd.DataFrame, units: pd.DataFrame) -> tuple[float, pd.DataFrame, list[datetime.date]]:
"""
Remove columns with expired dates from the cash-flow DataFrame and sum
per-unit expired flows into cash.
Parameters
----------
unique_dates : list
Dates at which cash flows may occur (not yet expired).
expiration_date : date
Period-end date; flows on or before this date are treated as expired.
cash_flows : DataFrame
Per-unit cash flows (rows = asset_id, columns = dates).
units : DataFrame
Holdings per asset_id; the expiration_date column is used for unit counts.
Returns
-------
tuple[float, pd.DataFrame, list]
Expired cash total, remaining cash-flow DataFrame, and remaining dates.
"""
expired_dates = calculate_expired_dates(list_of_dates = unique_dates,
deadline = expiration_date)
cash = 0.0
for expired_date in expired_dates:
cash += sum(units[expiration_date] * cash_flows[expired_date])
if expired_dates:
cash_flows = cash_flows.drop(columns=expired_dates)
unique_dates = [d for d in unique_dates if d not in expired_dates]
return cash, cash_flows, unique_dates
def process_expired_liab(unique_dates: list[datetime.date], date_of_interest: dt.date, cash_flows: pd.DataFrame) -> tuple[float, pd.DataFrame, list[datetime.date]]:
"""
Remove columns with expired dates from dataframe and sum cashflows within those columns into cash.
The cash flows are aggregated liabilities without any units
Parameters
----------
:type unique_dates: list
The list of unique dates at which cash flows occur
:type date_of_interest: date
The period-end date; cash flows on or before this date are treated as expired
:type cash_flows: DataFrame
The dataframe of aggregated liability cash flows (absolute amounts, not per unit)
Returns
-------
:type: list
List with the DataFrame with remaining (non-expired) cash flow columns and the expired cashflows summed into cash
"""
expired_dates = calculate_expired_dates(unique_dates, date_of_interest)
cash = 0.0
for expired_date in expired_dates:
cash += sum(cash_flows[expired_date])
if expired_dates:
cash_flows = cash_flows.drop(columns=expired_dates)
unique_dates = [d for d in unique_dates if d not in expired_dates]
return cash, cash_flows, unique_dates
def trade(current_date: dt.date, bank_account: pd.DataFrame, eq_units: pd.DataFrame, eq_price: pd.DataFrame, bd_units: pd.DataFrame, bd_price: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
"""
Proportionally buy or sell equities and bonds to drive the bank account toward zero.
Parameters
----------
current_date : date
The modelling date for this trading step.
bank_account : DataFrame
Cash balance at each modelling date (single row).
eq_units : DataFrame
Equity units per asset_id at each modelling date.
eq_price : DataFrame
Equity prices per asset_id at each modelling date.
bd_units : DataFrame
Bond units per asset_id at each modelling date.
bd_price : DataFrame
Bond prices per asset_id at each modelling date.
Returns
-------
tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]
Updated eq_units, bd_units, and bank_account DataFrames.
"""
total_market_value = portfolio_market_value(eq_price, eq_units, bd_price, bd_units, current_date)
if total_market_value <= 0:
pass
elif bank_account[current_date][0] < 0: # Sell assets
percent_to_sell = min(1, -bank_account[current_date][0] / total_market_value) # How much of the portfolio needs to be sold
eq_units[current_date] = eq_units[current_date] * (1 - percent_to_sell)
bd_units[current_date] = bd_units[current_date] * (1 - percent_to_sell)
bank_account[current_date] += total_market_value - portfolio_market_value(
eq_price, eq_units, bd_price, bd_units, current_date
)
elif bank_account[current_date][0] > 0: # Buy assets
percent_to_buy = bank_account[current_date][0] / total_market_value # What % of the portfolio is the excess cash
eq_units[current_date] = eq_units[current_date] * (1 + percent_to_buy)
bd_units[current_date] = bd_units[current_date] * (1 + percent_to_buy)
bank_account[current_date] += total_market_value - portfolio_market_value(
eq_price, eq_units, bd_price, bd_units, current_date
)
else: # Remaining cash flow is equal to 0 so no trading needed
pass
return eq_units, bd_units, bank_account
def capitalize_policies(
mv_df: pd.DataFrame,
gv_df: pd.DataFrame,
active_df: pd.DataFrame,
policies: Dict[int, UnitLinkedPolicy],
current_date: dt.date,
portfolio_return: float) -> tuple[pd.DataFrame, pd.DataFrame]:
"""
Capitalize active policy MV (and GV if guaranteed) by the period portfolio return.
Parameters
----------
:type mv_df: pd.DataFrame
Market-value state matrix.
:type gv_df: pd.DataFrame
Guaranteed-value state matrix.
:type active_df: pd.DataFrame
Active flags (1 = in force).
:type policies: dict[int, UnitLinkedPolicy]
Static policy metadata including is_guaranteed.
:type current_date: date
Current modelling date column.
:type portfolio_return: float
Period portfolio return from the asset MTM step.
Returns
-------
:rtype: tuple[pd.DataFrame, pd.DataFrame]
Updated mv_df and gv_df.
"""
factor = 1.0 + portfolio_return
for policy_id in sorted(mv_df.index):
if active_df.loc[policy_id, current_date] <= 0:
continue
mv_df.loc[policy_id, current_date] = mv_df.loc[policy_id, current_date] * factor
if policies[policy_id].is_guaranteed:
gv_df.loc[policy_id, current_date] = gv_df.loc[policy_id, current_date] * factor
return mv_df, gv_df
def apply_premiums(
mv_df: pd.DataFrame,
premium_df: pd.DataFrame,
active_df: pd.DataFrame,
fund: UnitLinkedFund,
current_date: dt.date,
time: float,
) -> tuple[pd.DataFrame, pd.DataFrame, float, float]:
"""
Grow premiums, allocate net to MV, and record gross premium and entry fee cash.
Parameters
----------
:type mv_df: pd.DataFrame
Market-value state matrix.
:type premium_df: pd.DataFrame
Premium state matrix.
:type active_df: pd.DataFrame
Active flags (1 = in force).
:type fund: UnitLinkedFund
Fund parameters (premium_growth, entry_fee).
:type current_date: date
Current modelling date column.
:type time: float
Elapsed year fraction for the period.
Returns
-------
:rtype: tuple[pd.DataFrame, pd.DataFrame, float, float]
Updated mv_df, premium_df, total gross premium, and total entry fee.
"""
gross_total = 0.0
entry_total = 0.0
for policy_id in sorted(mv_df.index):
if active_df.loc[policy_id, current_date] <= 0:
continue
prev_premium = float(premium_df.loc[policy_id, current_date])
gross = prev_premium * ((1.0 + fund.premium_growth) ** time)
entry = gross * fund.entry_fee
net = gross - entry
premium_df.loc[policy_id, current_date] = gross
mv_df.loc[policy_id, current_date] = float(mv_df.loc[policy_id, current_date]) + net
gross_total += gross
entry_total += entry
return mv_df, premium_df, gross_total, entry_total
def apply_admin_fees(
mv_df: pd.DataFrame,
active_df: pd.DataFrame,
fund: UnitLinkedFund,
current_date: dt.date,
time: float,
) -> tuple[pd.DataFrame, float]:
"""
Deduct period-scaled admin fees from MV of active policies.
Parameters
----------
:type mv_df: pd.DataFrame
Market-value state matrix.
:type active_df: pd.DataFrame
Active flags (1 = in force).
:type fund: UnitLinkedFund
Fund parameters (admin_fee).
:type current_date: date
Current modelling date column.
:type time: float
Elapsed year fraction for the period.
Returns
-------
:rtype: tuple[pd.DataFrame, float]
Updated mv_df and total admin fee cash.
"""
fee_factor = 1.0 - ((1.0 - fund.admin_fee) ** time)
admin_total = 0.0
for policy_id in sorted(mv_df.index):
if active_df.loc[policy_id, current_date] <= 0:
continue
mv = float(mv_df.loc[policy_id, current_date])
fee = mv * fee_factor
mv_df.loc[policy_id, current_date] = mv - fee
admin_total += fee
return mv_df, admin_total
def apply_mortality(
mv_df: pd.DataFrame,
active_df: pd.DataFrame,
policies: Dict[int, UnitLinkedPolicy],
society: Society,
current_date: dt.date,
time: float,
rng: random.Random,
) -> tuple[pd.DataFrame, pd.DataFrame, float, int]:
"""
Stochastic mortality sampling: liquidate full MV if drawn against period-scaled q.
Parameters
----------
:type mv_df: pd.DataFrame
Market-value state matrix.
:type active_df: pd.DataFrame
Active flags (1 = in force).
:type policies: dict[int, UnitLinkedPolicy]
Static policy metadata for age and sex.
:type society: Society
Mortality tables.
:type current_date: date
Current modelling date column.
:type time: float
Elapsed year fraction.
:type rng: random.Random
Seeded random number generator.
Returns
-------
:rtype: tuple[pd.DataFrame, pd.DataFrame, float, int]
Updated mv_df, active_df, death benefit total, and death count.
"""
death_total = 0.0
death_count = 0
for policy_id in sorted(mv_df.index):
if active_df.loc[policy_id, current_date] <= 0:
continue
policy = policies[policy_id]
q_annual = society.mortality_rate(policy.age_at(current_date), policy.is_female)
q_period = 1.0 - ((1.0 - q_annual) ** time)
if rng.random() < q_period:
mv = float(mv_df.loc[policy_id, current_date])
death_total += mv
death_count += 1
active_df.loc[policy_id, current_date] = 0.0
mv_df.loc[policy_id, current_date] = 0.0
return mv_df, active_df, death_total, death_count
def apply_lapse(
mv_df: pd.DataFrame,
active_df: pd.DataFrame,
fund: UnitLinkedFund,
current_date: dt.date,
time: float,
rng: random.Random,
) -> tuple[pd.DataFrame, pd.DataFrame, float, int]:
"""
Stochastic lapse sampling on survivors: liquidate full MV if drawn against period-scaled lapse.
Parameters
----------
:type mv_df: pd.DataFrame
Market-value state matrix.
:type active_df: pd.DataFrame
Active flags (1 = in force).
:type fund: UnitLinkedFund
Fund parameters (lapse_rate).
:type current_date: date
Current modelling date column.
:type time: float
Elapsed year fraction.
:type rng: random.Random
Seeded random number generator.
Returns
-------
:rtype: tuple[pd.DataFrame, pd.DataFrame, float, int]
Updated mv_df, active_df, surrender total, and lapse count.
"""
lapse_period = 1.0 - ((1.0 - fund.lapse_rate) ** time)
surrender_total = 0.0
lapse_count = 0
for policy_id in sorted(mv_df.index):
if active_df.loc[policy_id, current_date] <= 0:
continue
if rng.random() < lapse_period:
mv = float(mv_df.loc[policy_id, current_date])
surrender_total += mv
lapse_count += 1
active_df.loc[policy_id, current_date] = 0.0
mv_df.loc[policy_id, current_date] = 0.0
return mv_df, active_df, surrender_total, lapse_count
def process_unit_linked_period(
current_date: dt.date,
previous_date: dt.date,
portfolio_return: float,
time: float,
mv_df: pd.DataFrame,
gv_df: pd.DataFrame,
premium_df: pd.DataFrame,
active_df: pd.DataFrame,
policies: Dict[int, UnitLinkedPolicy],
fund: UnitLinkedFund,
society: Society,
random_seed: int,
proj_period: int,
rng: Optional[random.Random] = None
) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame, Dict[str, float]]:
"""
Run one unit-linked period: carry forward, capitalize, premiums, fees, mortality, lapse.
Parameters
----------
:type current_date: date
New modelling date.
:type previous_date: date
Previous modelling date to carry forward from.
:type portfolio_return: float
Period portfolio return from asset MTM.
:type time: float
Elapsed year fraction.
:type mv_df: pd.DataFrame
Market-value state matrix.
:type gv_df: pd.DataFrame
Guaranteed-value state matrix.
:type premium_df: pd.DataFrame
Premium state matrix.
:type active_df: pd.DataFrame
Active flags.
:type policies: dict[int, UnitLinkedPolicy]
Static policy metadata.
:type fund: UnitLinkedFund
Fund parameters.
:type society: Society
Mortality tables.
:type random_seed: int
Base seed for reproducible draws.
:type proj_period: int
Projection period index used with random_seed.
:type rng: random.Random, optional
Optional override RNG (for unit tests).
Returns
-------
:rtype: tuple
Updated mv_df, gv_df, premium_df, active_df, and a cashflow dict with absolute amounts.
"""
mv_df[current_date] = mv_df[previous_date]
gv_df[current_date] = gv_df[previous_date]
premium_df[current_date] = premium_df[previous_date]
active_df[current_date] = active_df[previous_date]
if rng is None:
rng = random.Random(random_seed + proj_period)
mv_df, gv_df = capitalize_policies(
mv_df, gv_df, active_df, policies, current_date, portfolio_return
)
mv_df, premium_df, gross_premium, entry_fee = apply_premiums(
mv_df, premium_df, active_df, fund, current_date, time
)
mv_df, admin_fee = apply_admin_fees(mv_df, active_df, fund, current_date, time)
mv_df, active_df, death, deaths = apply_mortality(
mv_df, active_df, policies, society, current_date, time, rng
)
mv_df, active_df, surrender, lapses = apply_lapse(
mv_df, active_df, fund, current_date, time, rng
)
in_force = int(active_df[current_date].sum())
cashflows: Dict[str, float] = {
"gross_premium": float(gross_premium),
"entry_fee": float(entry_fee),
"admin_fee": float(admin_fee),
"death": float(death),
"surrender": float(surrender),
"deaths": float(deaths),
"lapses": float(lapses),
"in_force": float(in_force),
}
return mv_df, gv_df, premium_df, active_df, cashflows