Skip to content

Commit

Permalink
ENH: Update CashValue_ME_EX4
Browse files Browse the repository at this point in the history
  • Loading branch information
fumitoh committed Mar 7, 2023
1 parent 6d5c734 commit f8a62f0
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 109 deletions.
20 changes: 10 additions & 10 deletions lifelib/libraries/savings/CashValue_ME_EX4/Projection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ def mort_rate(t):
# return mort_table_reindexed().reindex(
# mi, fill_value=0).set_axis(model_point().index, inplace=False)

return pd.Series(0, index=model_point().index).values
return np.zeros(len(model_point().index))


def mort_rate_mth(t):
Expand Down Expand Up @@ -1431,7 +1431,7 @@ def pols_new_biz(t):
* :func:`model_point`
"""
return model_point()['policy_count'].where(duration_mth(t) == 0, other=0).values
return model_point()['policy_count'].values * (duration_mth(t) == 0)


def prem_to_av(t):
Expand Down Expand Up @@ -1491,11 +1491,8 @@ def premium_pp(t):
# model_point().index, inplace=False)
# return np.around(sum_assured() * prem_rates, 2)

sp = model_point()['premium_pp'].where((premium_type() == 'SINGLE') & (duration_mth(t) == 0), other=0).values
lp = model_point()['premium_pp'].where(
(premium_type() == 'LEVEL') & (duration_mth(t) < 12 * policy_term()),
other=0).values
return sp + lp
return model_point()['premium_pp'].values * ((premium_type() == 'SINGLE') & (duration_mth(t) == 0) |
(premium_type() == 'LEVEL') & (duration_mth(t) < 12 * policy_term()))


def premium_type():
Expand Down Expand Up @@ -1900,9 +1897,12 @@ def surr_charge_rate(t):
* :func:`surr_charge_max_idx`
* :func:`surr_charge_table_stacked`
"""
ind_row = surr_charge_table.index.searchsorted(np.minimum(duration(t), surr_charge_max_idx()), side='right') - 1
ind_col = surr_charge_table.columns.searchsorted(surr_charge_id(), side='right') - 1 # TODO: not clear how it should work with has_surr_charge()
return surr_charge_table.values.flat[ind_col + ind_row * len(surr_charge_table.columns)]
ind_row = np.minimum(duration(t), surr_charge_max_idx())
return surr_charge_table.values.flat[surr_charge_table_column() + ind_row * len(surr_charge_table.columns)]


def surr_charge_table_column():
return surr_charge_table.columns.searchsorted(surr_charge_id(), side='right') - 1


def surr_charge_table_stacked():
Expand Down
204 changes: 105 additions & 99 deletions lifelib/libraries/savings/savings_example4.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
{
"data": {
"text/plain": [
"1.2689610000234097"
"1.2499775000000004"
]
},
"execution_count": 3,
Expand Down Expand Up @@ -82,7 +82,7 @@
{
"data": {
"text/plain": [
"0.526920499978587"
"0.2862044000000008"
]
},
"execution_count": 6,
Expand Down Expand Up @@ -149,7 +149,7 @@
{
"data": {
"text/plain": [
"7.84547259984538"
"8.0889741"
]
},
"execution_count": 9,
Expand Down Expand Up @@ -190,7 +190,7 @@
{
"data": {
"text/plain": [
"3.8828028000425547"
"2.3929682999999997"
]
},
"execution_count": 12,
Expand Down Expand Up @@ -295,70 +295,70 @@
" <tbody>\n",
" <tr>\n",
" <th>surr_charge_rate(t)</th>\n",
" <td>0.626154</td>\n",
" <td>36.491786</td>\n",
" <td>0.697034</td>\n",
" <td>39.238824</td>\n",
" </tr>\n",
" <tr>\n",
" <th>inv_return_mth(t)</th>\n",
" <td>0.291798</td>\n",
" <td>17.005771</td>\n",
" <td>0.338032</td>\n",
" <td>19.029207</td>\n",
" </tr>\n",
" <tr>\n",
" <th>premium_pp(t)</th>\n",
" <td>0.206414</td>\n",
" <td>12.029663</td>\n",
" <td>0.146663</td>\n",
" <td>8.256274</td>\n",
" </tr>\n",
" <tr>\n",
" <th>claim_pp(t, kind)</th>\n",
" <td>0.077510</td>\n",
" <td>4.517216</td>\n",
" <th>pols_new_biz(t)</th>\n",
" <td>0.069975</td>\n",
" <td>3.939195</td>\n",
" </tr>\n",
" <tr>\n",
" <th>claims(t, kind)</th>\n",
" <td>0.063502</td>\n",
" <td>3.700866</td>\n",
" <th>inv_income_pp(t)</th>\n",
" <td>0.069556</td>\n",
" <td>3.915600</td>\n",
" </tr>\n",
" <tr>\n",
" <th>pols_if_at(t, timing)</th>\n",
" <td>0.038705</td>\n",
" <td>2.255718</td>\n",
" <th>claims(t, kind)</th>\n",
" <td>0.064405</td>\n",
" <td>3.625641</td>\n",
" </tr>\n",
" <tr>\n",
" <th>av_pp_at(t, timing)</th>\n",
" <td>0.034805</td>\n",
" <td>2.028384</td>\n",
" <th>expenses(t)</th>\n",
" <td>0.035827</td>\n",
" <td>2.016841</td>\n",
" </tr>\n",
" <tr>\n",
" <th>inv_return_table()</th>\n",
" <td>0.031250</td>\n",
" <td>1.821226</td>\n",
" <th>pols_maturity(t)</th>\n",
" <td>0.032865</td>\n",
" <td>1.850092</td>\n",
" </tr>\n",
" <tr>\n",
" <th>expenses(t)</th>\n",
" <td>0.029133</td>\n",
" <td>1.697868</td>\n",
" <th>claim_pp(t, kind)</th>\n",
" <td>0.032804</td>\n",
" <td>1.846696</td>\n",
" </tr>\n",
" <tr>\n",
" <th>inv_income(t)</th>\n",
" <td>0.027631</td>\n",
" <td>1.610330</td>\n",
" <th>prem_to_av_pp(t)</th>\n",
" <td>0.031291</td>\n",
" <td>1.761523</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" duration dur_perc\n",
"surr_charge_rate(t) 0.626154 36.491786\n",
"inv_return_mth(t) 0.291798 17.005771\n",
"premium_pp(t) 0.206414 12.029663\n",
"claim_pp(t, kind) 0.077510 4.517216\n",
"claims(t, kind) 0.063502 3.700866\n",
"pols_if_at(t, timing) 0.038705 2.255718\n",
"av_pp_at(t, timing) 0.034805 2.028384\n",
"inv_return_table() 0.031250 1.821226\n",
"expenses(t) 0.029133 1.697868\n",
"inv_income(t) 0.027631 1.610330"
" duration dur_perc\n",
"surr_charge_rate(t) 0.697034 39.238824\n",
"inv_return_mth(t) 0.338032 19.029207\n",
"premium_pp(t) 0.146663 8.256274\n",
"pols_new_biz(t) 0.069975 3.939195\n",
"inv_income_pp(t) 0.069556 3.915600\n",
"claims(t, kind) 0.064405 3.625641\n",
"expenses(t) 0.035827 2.016841\n",
"pols_maturity(t) 0.032865 1.850092\n",
"claim_pp(t, kind) 0.032804 1.846696\n",
"prem_to_av_pp(t) 0.031291 1.761523"
]
},
"execution_count": 15,
Expand Down Expand Up @@ -415,71 +415,71 @@
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>surr_charge_rate(t)</th>\n",
" <td>0.204015</td>\n",
" <td>37.341396</td>\n",
" <th>premium_pp(t)</th>\n",
" <td>0.033878</td>\n",
" <td>10.744304</td>\n",
" </tr>\n",
" <tr>\n",
" <th>premium_pp(t)</th>\n",
" <td>0.115471</td>\n",
" <td>21.134984</td>\n",
" <th>mort_rate_mth(t)</th>\n",
" <td>0.032263</td>\n",
" <td>10.231875</td>\n",
" </tr>\n",
" <tr>\n",
" <th>pv_claims(kind)</th>\n",
" <td>0.031336</td>\n",
" <td>5.735535</td>\n",
" <th>surr_charge_rate(t)</th>\n",
" <td>0.024496</td>\n",
" <td>7.768857</td>\n",
" </tr>\n",
" <tr>\n",
" <th>inv_return_table()</th>\n",
" <td>0.023608</td>\n",
" <td>4.320994</td>\n",
" <th>duration(t)</th>\n",
" <td>0.019624</td>\n",
" <td>6.223781</td>\n",
" </tr>\n",
" <tr>\n",
" <th>lapse_rate(t)</th>\n",
" <td>0.022686</td>\n",
" <td>4.152201</td>\n",
" <th>inv_return_table()</th>\n",
" <td>0.018503</td>\n",
" <td>5.868249</td>\n",
" </tr>\n",
" <tr>\n",
" <th>prem_to_av_pp(t)</th>\n",
" <td>0.018686</td>\n",
" <td>3.420122</td>\n",
" <th>pv_claims(kind)</th>\n",
" <td>0.018216</td>\n",
" <td>5.776984</td>\n",
" </tr>\n",
" <tr>\n",
" <th>av_change(t)</th>\n",
" <td>0.016086</td>\n",
" <td>2.944244</td>\n",
" <th>claims(t, kind)</th>\n",
" <td>0.018125</td>\n",
" <td>5.748327</td>\n",
" </tr>\n",
" <tr>\n",
" <th>commissions(t)</th>\n",
" <td>0.015623</td>\n",
" <td>2.859585</td>\n",
" <th>av_at(t, timing)</th>\n",
" <td>0.016349</td>\n",
" <td>5.185010</td>\n",
" </tr>\n",
" <tr>\n",
" <th>premiums(t)</th>\n",
" <td>0.015507</td>\n",
" <td>2.838290</td>\n",
" <th>prem_to_av_pp(t)</th>\n",
" <td>0.015626</td>\n",
" <td>4.955751</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std_norm_rand()</th>\n",
" <td>0.012620</td>\n",
" <td>2.309915</td>\n",
" <th>pols_new_biz(t)</th>\n",
" <td>0.015588</td>\n",
" <td>4.943653</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" duration dur_perc\n",
"surr_charge_rate(t) 0.204015 37.341396\n",
"premium_pp(t) 0.115471 21.134984\n",
"pv_claims(kind) 0.031336 5.735535\n",
"inv_return_table() 0.023608 4.320994\n",
"lapse_rate(t) 0.022686 4.152201\n",
"prem_to_av_pp(t) 0.018686 3.420122\n",
"av_change(t) 0.016086 2.944244\n",
"commissions(t) 0.015623 2.859585\n",
"premiums(t) 0.015507 2.838290\n",
"std_norm_rand() 0.012620 2.309915"
"premium_pp(t) 0.033878 10.744304\n",
"mort_rate_mth(t) 0.032263 10.231875\n",
"surr_charge_rate(t) 0.024496 7.768857\n",
"duration(t) 0.019624 6.223781\n",
"inv_return_table() 0.018503 5.868249\n",
"pv_claims(kind) 0.018216 5.776984\n",
"claims(t, kind) 0.018125 5.748327\n",
"av_at(t, timing) 0.016349 5.185010\n",
"prem_to_av_pp(t) 0.015626 4.955751\n",
"pols_new_biz(t) 0.015588 4.943653"
]
},
"execution_count": 16,
Expand Down Expand Up @@ -529,40 +529,46 @@
" <tbody>\n",
" <tr>\n",
" <th>surr_charge_rate(t)</th>\n",
" <td>0.626154</td>\n",
" <td>0.204015</td>\n",
" <td>0.697034</td>\n",
" <td>0.024496</td>\n",
" </tr>\n",
" <tr>\n",
" <th>inv_return_mth(t)</th>\n",
" <td>0.291798</td>\n",
" <td>0.002037</td>\n",
" <td>0.338032</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>premium_pp(t)</th>\n",
" <td>0.206414</td>\n",
" <td>0.115471</td>\n",
" <td>0.146663</td>\n",
" <td>0.033878</td>\n",
" </tr>\n",
" <tr>\n",
" <th>claim_pp(t, kind)</th>\n",
" <td>0.077510</td>\n",
" <td>0.002008</td>\n",
" <th>pols_new_biz(t)</th>\n",
" <td>0.069975</td>\n",
" <td>0.015588</td>\n",
" </tr>\n",
" <tr>\n",
" <th>inv_income_pp(t)</th>\n",
" <td>0.069556</td>\n",
" <td>0.015507</td>\n",
" </tr>\n",
" <tr>\n",
" <th>claims(t, kind)</th>\n",
" <td>0.063502</td>\n",
" <td>0.000000</td>\n",
" <td>0.064405</td>\n",
" <td>0.018125</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" df1 df4\n",
"surr_charge_rate(t) 0.626154 0.204015\n",
"inv_return_mth(t) 0.291798 0.002037\n",
"premium_pp(t) 0.206414 0.115471\n",
"claim_pp(t, kind) 0.077510 0.002008\n",
"claims(t, kind) 0.063502 0.000000"
"surr_charge_rate(t) 0.697034 0.024496\n",
"inv_return_mth(t) 0.338032 0.000000\n",
"premium_pp(t) 0.146663 0.033878\n",
"pols_new_biz(t) 0.069975 0.015588\n",
"inv_income_pp(t) 0.069556 0.015507\n",
"claims(t, kind) 0.064405 0.018125"
]
},
"execution_count": 17,
Expand Down Expand Up @@ -593,7 +599,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down

0 comments on commit f8a62f0

Please sign in to comment.