Skip to content

Commit

Permalink
some experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewCaseres committed Apr 4, 2024
1 parent aaf9a2e commit a483316
Show file tree
Hide file tree
Showing 5 changed files with 637 additions and 484 deletions.
29 changes: 18 additions & 11 deletions Python/basicterm_me_heavylight_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@
premium_table = pd.read_excel("BasicTerm_ME/premium_table.xlsx", index_col=[0,1])

class ModelPoints:
def __init__(self, model_point_table: pd.DataFrame, premium_table: pd.DataFrame):
def __init__(self, model_point_table: pd.DataFrame, premium_table: pd.DataFrame, size_multiplier: int = 1):
self.table = model_point_table.merge(premium_table, left_on=["age_at_entry", "policy_term"], right_index=True)
self.table.sort_values(by="policy_id", inplace=True)
self.table["premium_pp"] = np.around(self.table["sum_assured"] * self.table["premium_rate"],2)
self.premium_pp = self.table["premium_pp"].to_numpy()
self.duration_mth = self.table["duration_mth"].to_numpy()
self.age_at_entry = self.table["age_at_entry"].to_numpy()
self.sum_assured = self.table["sum_assured"].to_numpy()
self.policy_count = self.table["policy_count"].to_numpy()
self.policy_term = self.table["policy_term"].to_numpy()
self.premium_pp = np.tile(self.table["premium_pp"].to_numpy(), size_multiplier)
self.duration_mth = np.tile(self.table["duration_mth"].to_numpy(), size_multiplier)
self.age_at_entry = np.tile(self.table["age_at_entry"].to_numpy(), size_multiplier)
self.sum_assured = np.tile(self.table["sum_assured"].to_numpy(), size_multiplier)
self.policy_count = np.tile(self.table["policy_count"].to_numpy(), size_multiplier)
self.policy_term = np.tile(self.table["policy_term"].to_numpy(), size_multiplier)
self.max_proj_len: int = np.max(12 * self.policy_term - self.duration_mth) + 1

class Assumptions:
def __init__(self, disc_rate_ann: pd.DataFrame, mort_table: pd.DataFrame):
self.disc_rate_ann = disc_rate_ann["zero_spot"].values
self.disc_rate_ann = disc_rate_ann["zero_spot"].to_numpy()
self.mort_table = mort_table.to_numpy()

def get_mortality(self, age, duration):
return self.mort_table[age-18, np.minimum(duration, 5)]

class TermME(LightModel):
def __init__(self, mp: ModelPoints, assume: Assumptions):
super().__init__()
super().__init__(storage_function=lambda x: np.sum(x))
self.mp = mp
self.assume = assume

Expand Down Expand Up @@ -97,6 +97,14 @@ def mort_rate_mth(self, t):
def net_cf(self, t):
return self.premiums(t) - self.claims(t) - self.expenses(t) - self.commissions(t)

def aggregated_discounted_net_cf(self, t):
return np.sum(self.net_cf(t)) * self.discount(t)

def accumulated_discounted_net_cf(self, t):
if t < 0:
return 0
return self.accumulated_discounted_net_cf(t-1) + self.aggregated_discounted_net_cf(t)

def pols_death(self, t):
return self.pols_if_at(t, "BEF_DECR") * self.mort_rate_mth(t)

Expand Down Expand Up @@ -137,8 +145,7 @@ def premiums(self, t):

def basicterm_me_heavylight_numpy():
model.ResetCache()
tot = sum(np.sum(model.premiums(t) - model.claims(t) - model.expenses(t) - model.commissions(t)) \
* model.discount(t) for t in range(model.mp.max_proj_len))
tot = sum(np.sum(model.net_cf(t)) * model.discount(t) for t in range(model.mp.max_proj_len))
return float(tot)

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion Python/basicterm_me_recursive_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def disc_rate_mth():

@cash
def duration(t):
return duration_mth(t) //12
return duration_mth(t) // 12

@cash
def duration_mth(t):
Expand Down
41 changes: 22 additions & 19 deletions Python/benchmark_results.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
basic_term_benchmark:
Python array numpy basic_term_m:
minimum time: 79.0311409999731 milliseconds
result: 14489630.534603368
minimum time: 95.96395771950483 milliseconds
result: 14489630.534603955
Python array pytorch basic_term_m:
minimum time: 45.24396900001193 milliseconds
result: 14489630.534603368
minimum time: 86.49199921637774 milliseconds
result: 14489630.534603959
Python lifelib basic_term_m:
minimum time: 614.4032699999684 milliseconds
result: 14489630.534601536
minimum time: 528.3367084339261 milliseconds
result: 14489630.534602122
Python recursive numpy basic_term_m:
minimum time: 46.281483000029766 milliseconds
result: 14489630.534603368
minimum time: 52.718209102749825 milliseconds
result: 14489630.534603957
Python recursive pytorch basic_term_m:
minimum time: 72.29064599999901 milliseconds
result: 14489630.53460337
minimum time: 85.5019586160779 milliseconds
result: 14489630.534603959
basic_term_me_benchmark:
Python heavylight numpy basic_term_me:
minimum time: 343.96580999998605 milliseconds
result: 215146132.0684811
minimum time: 230.15966545790434 milliseconds
result: 215146132.06850433
Python lifelib basic_term_me:
minimum time: 1146.6455289999544 milliseconds
result: 215146132.06848112
minimum time: 1459.7130427137017 milliseconds
result: 215146132.068504
Python recursive numpy basic_term_me:
minimum time: 320.24258900003133 milliseconds
result: 215146132.0684814
minimum time: 178.3146671950817 milliseconds
result: 215146132.06850427
mortality:
Python PyMort:
minimum time: 9.000889000020607 milliseconds
minimum time: 5.112958140671253 milliseconds
result: 1904.4865526636793
savings_benchmark:
Python lifelib cashvalue_me_ex4:
minimum time: 585.2152809999893 milliseconds
result: 3507113709040.141
minimum time: 1246.7584162950516 milliseconds
result: 3507113709040.142
Python recursive numpy cashvalue_me_ex4:
minimum time: 350.6229165941477 milliseconds
result: 3507113709040.124
Loading

0 comments on commit a483316

Please sign in to comment.