Source code for fastlife.model.Projection
"""Space for cashflow projection.
This Space is for projecting cashflows.
Most Cells in this Spaces return calculation results for all model points as
pandas Series objects indexed by Policy ID.
This Space has child Spaces,
:mod:`~fastlife.model.Projection.Policy` and :mod:`~fastlife.model.Projection.Assumptions`.
The :mod:`~fastlife.model.Projection.Policy` Space contains Cells representing policy attributes, such as
product type, issue age, sum assured, etc.
It also contains Cells for calculating policy values such as premium rates and
cash surrender value rates.
This Space has a base Space, :mod:`~fastlife.model.PV`, in which
Cells for taking present values of cashflows are defined.
.. rubric:: Composition Structure
.. blockdiag::
blockdiag {
default_node_color="#D5E8D4";
default_linecolor="#628E47";
node_width=150;
Projection <- Assumptions [hstyle=composition];
Projection <- Policy [hstyle=composition];
}
.. rubric:: Inheritance Structure
.. blockdiag::
blockdiag {
default_node_color="#D5E8D4";
default_linecolor="#628E47";
PV[style=dotted]
PV<- Projection [hstyle=generalization];
}
.. rubric:: References
The following attributes defined in this Space.
Attributes:
pol: Alias for :mod:`~fastlife.model.Projection.Policy` child Space
asmp: Alias for :mod:`~fastlife.model.Projection.Assumptions` child Space
scen: Alias for :mod:`~fastlife.model.Economic` Space
ScenID: Scenario ID (``1`` by default)
"""
from modelx.serialize.jsonvalues import *
_formula = lambda PolicyID, ScenID=1: None
_bases = [
".PV"
]
_allow_none = None
_spaces = [
"Policy",
"Assumptions"
]
# ---------------------------------------------------------------------------
# Cells
[docs]def DiscRate(t):
"""Rates for discount cashflows
Refers to :func:`Economic[ScenID].DiscRate<simplelife.model.Economic.DiscRate>`
"""
return scen[ScenID].DiscRate(t)
[docs]def InflFactor(t):
"""Inflation factors to adjust expense cashflows
Refers to :func:`Economic[ScenID].InflFactor<simplelife.model.Economic.InflFactor>`
"""
return scen[ScenID].InflFactor(t)
[docs]def InvstRetRate(t):
"""Rate of investment return
Refers to :func:`Economic[ScenID].InvstRetRate<simplelife.model.Economic.InvstRetRate>`
"""
return scen[ScenID].InvstRetRate(t)
[docs]def BaseMortRate(t):
"""Base mortality rate"""
exist = (t <= last_t())
keys = pd.concat([asmp.MortTableID(), pol.Sex(), AttAge(t), exist],
axis=1, keys=["ID", "Sex", "Age", "exist"])
def find_rate(key):
if key["exist"]:
return asmp.MortalityTables()[key["ID"], key["Sex"]][key["Age"]]
else:
return 0
result = keys.apply(find_rate, axis=1)
result.name = "BaseMortRate"
return result
def last_t():
result = np.minimum(asmp.LastAge() - pol.IssueAge(), pol.PolicyTerm())
result.name = "last_t"
return result
[docs]def AccumCF(t):
"""Accumulated cashflows"""
if t == 0:
return 0
else:
return (AccumCF(t-1)
+ IntAccumCF(t-1)
+ NetInsurCF(t-1))
[docs]def AttAge(t):
"""Attained age at time ``t``"""
return pol.IssueAge + t
[docs]def BenefitAccDth(t):
"""Accidental death benefits"""
return SizeBenefitAccDth(t) * PolsAccDeath(t)
[docs]def BenefitAccHosp(t):
"""Accidental hospitalization benefits"""
return SizeBenefitAccHosp(t) * PolsAccHosp(t)
[docs]def BenefitAnn(t):
"""Annuity benefits"""
return SizeBenefitAnn(t) * PolsAnnuity(t)
[docs]def BenefitDeath(t):
"""Death benefits"""
return SizeBenefitDeath(t) * PolsDeath(t)
[docs]def BenefitLiving(t):
"""Living benefits"""
return SizeBenefitLiving(t) * PolsLiving(t)
[docs]def BenefitMat(t):
"""Matuirty benefits"""
return SizeBenefitMat(t) * PolsMaturity(t)
[docs]def BenefitOther(t):
"""Other benefits"""
return SizeBenefitOther(t) * PolsOther(t)
[docs]def BenefitSickHosp(t):
"""Sickness hospitalization benefits"""
return SizeBenefitSickHosp(t) * PolsSickHosp(t)
[docs]def BenefitSurg(t):
"""Surgery benefits"""
return SizeBenefitSurg(t) * PolsSurg(t)
[docs]def BenefitSurr(t):
"""Surrender benefits"""
return SizeBenefitSurr(t) * PolsSurr(t)
[docs]def BenefitTotal(t):
"""Benefit Total"""
return (BenefitMat(t)
+ BenefitDeath(t)
+ BenefitAccDth(t)
+ BenefitSurr(t)
+ BenefitAnn(t)
+ BenefitAccHosp(t)
+ BenefitSickHosp(t)
+ BenefitSurg(t)
+ BenefitLiving(t)
+ BenefitOther(t))
[docs]def ChangeRsrv(t):
"""Change in reserve"""
return ReserveTotal_End(t+1) - ReserveTotal_End(t)
[docs]def ExpsAcq(t):
"""Acquisition expenses"""
return SizeExpsAcq(t) * (PolsNewBiz(t) + PolsRenewal(t))
[docs]def ExpsAcqTotal(t):
"""Commissions and acquisition expenses"""
return ExpsCommTotal(t) + ExpsAcq(t)
[docs]def ExpsCommInit(t):
"""Initial commissions"""
return SizeExpsCommInit(t) * PolsIF_Beg1(t)
[docs]def ExpsCommRen(t):
"""Renewal commissions"""
return SizeExpsCommRen(t) * PolsIF_Beg1(t)
[docs]def ExpsCommTotal(t):
"""Commissions Total"""
return ExpsCommInit(t) + ExpsCommRen(t)
[docs]def ExpsMaint(t):
"""Maintenance expenses"""
return SizeExpsMaint(t) * PolsIF_Beg1(t)
[docs]def ExpsMaintTotal(t):
"""Total maintenance expenses including other expenses"""
return ExpsMaint(t) + ExpsOther(t)
[docs]def ExpsOther(t):
"""Other expenses"""
return 0
[docs]def ExpsTotal(t):
"""Total expenses"""
return (ExpsCommInit(t)
+ ExpsCommRen(t)
+ ExpsAcq(t)
+ ExpsMaint(t)
+ ExpsOther(t))
[docs]def IncomeTotal(t):
"""Income Total"""
return PremIncome(t) + InvstIncome(t)
[docs]def InsurIF_Beg1(t):
"""Insurance in-force: Beginning of period 1"""
return PolsIF_Beg1(t) * SizeSumAssured(t)
[docs]def InsurIF_End(t):
"""Insurance in-force: End of period"""
return PolsIF_End(t) * SizeSumAssured(t)
[docs]def IntAccumCF(t):
"""Intrest on accumulated cashflows"""
return (AccumCF(t)
+ PremIncome(t)
- ExpsTotal(t)) * DiscRate(t)
[docs]def InvstIncome(t):
"""Investment income"""
return SizeInvstIncome(t) * PolsIF_Beg1(t)
[docs]def NetInsurCF(t):
"""Net liability cashflow"""
return (PremIncome(t)
- BenefitTotal(t)
- ExpsTotal(t))
[docs]def PolsAccDeath(t):
"""Number of policies: Accidental death"""
return 0
[docs]def PolsAccHosp(t):
"""Number of policies: Accidental Hospitalization"""
return 0
[docs]def PolsAnnuity(t):
"""Number of policies: Annuity"""
return 0
[docs]def PolsDeath(t):
"""Number of policies: Death"""
return PolsIF_Beg1(t) * BaseMortRate(t) * asmp.MortFactor(t)
[docs]def PolsIF_AftMat(t):
"""Number of policies: Maturity"""
return PolsIF_End(t) - PolsMaturity(t)
[docs]def PolsIF_Beg(t):
"""Number of policies: Beginning of period"""
return PolsIF_AftMat(t)
[docs]def PolsIF_Beg1(t):
"""Number of policies: Beginning of period 1"""
return PolsIF_Beg(t) + PolsRenewal(t) + PolsNewBiz(t)
[docs]def PolsIF_End(t):
"""Number of policies: End of period"""
if t == 0:
return 0 # pol.PolicyCount()
else:
return PolsIF_Beg1(t-1) - PolsDeath(t-1) - PolsSurr(t-1)
[docs]def PolsLiving(t):
"""Number of policies: Living benefits"""
return 0
[docs]def PolsMaturity(t):
"""Number of policies: Maturity"""
return (pol.PolicyTerm == t) * PolsIF_End(t)
[docs]def PolsNewBiz(t):
"""Number of policies: New business"""
return pol.PolicyCount() if t == 0 else 0
[docs]def PolsOther(t):
"""Number of policies: Other benefits"""
return 0
[docs]def PolsRenewal(t):
"""Number of policies: Renewal policies"""
return 0
[docs]def PolsSickHosp(t):
"""Number of policies: Sickness Hospitalization"""
return 0
[docs]def PolsSurg(t):
"""Number of policies: Surgery"""
return 0
[docs]def PolsSurr(t):
"""Number of policies: Surrender"""
return PolsIF_Beg1(t) * asmp.SurrRate(t)
[docs]def PremIncome(t):
"""Premium income"""
return SizePremium(t) * PolsIF_Beg1(t)
[docs]def ProfitBefTax(t):
"""Profit before Tax"""
return (PremIncome(t)
+ InvstIncome(t)
- BenefitTotal(t)
- ExpsTotal(t)
- ChangeRsrv(t))
[docs]def ReserveHospRsrvEnd(t):
"""Hospitalization reserve: End of period"""
return 0
[docs]def ReservePremRsrvEnd(t):
"""Premium reserve: End of period"""
return SizeReservePremRsrvEnd(t) * PolsIF_End(t)
[docs]def ReserveTotal_End(t):
"""Total reserve: End of period"""
return (ReservePremRsrvEnd(t)
+ ReserveUernPremEnd(t)
+ ReserveHospRsrvEnd(t))
[docs]def ReserveUernPremEnd(t):
"""Unearned Premium: End of period"""
return 0
[docs]def SizeAnnPrem(t):
"""Annualized premium per policy at time ``t``"""
return SizeSumAssured(t) * pol.AnnPremRate()
[docs]def SizeBenefitAccDth(t):
"""Accidental death benefit per policy"""
return 0
[docs]def SizeBenefitAccHosp(t):
"""Accidental hospitalization benefit per policy"""
return 0
[docs]def SizeBenefitAnn(t):
"""Annuity benefit per policy"""
return 0
[docs]def SizeBenefitDeath(t):
"""Death benefit per policy"""
return SizeSumAssured(t)
[docs]def SizeBenefitLiving(t):
"""Living benefit per policy"""
return 0
[docs]def SizeBenefitMat(t):
"""Maturity benefit per policy"""
return 0
[docs]def SizeBenefitOther(t):
"""Other benefit per policy"""
return 0
[docs]def SizeBenefitSickHosp(t):
"""Sickness hospitalization benefit per policy"""
return 0
[docs]def SizeBenefitSurg(t):
"""Surgery benefit per policy"""
return 0
[docs]def SizeBenefitSurr(t):
"""Surrender benefit per policy"""
return SizeSumAssured(t) * (pol.CashValueRate(t)
+ pol.CashValueRate(t+1)) / 2
[docs]def SizeExpsAcq(t):
"""Acquisition expense per policy at time t"""
if t == 0:
return (SizeAnnPrem(t) * asmp.ExpsAcqAnnPrem()
+ (SizeSumAssured(t) * asmp.ExpsAcqSA() + asmp.ExpsAcqPol())
* InflFactor(t) / InflFactor(0))
else:
return 0
[docs]def SizeExpsCommInit(t):
"""Initial commission per policy at time t"""
if t == 0:
return SizePremium(t) * asmp.CommInitPrem() * (1 + asmp.CnsmpTax)
else:
return 0
[docs]def SizeExpsCommRen(t):
"""Renewal commission per policy at time t"""
if t == 0:
return 0
elif (t < asmp.CommRenTerm()).any():
return (t < asmp.CommRenTerm()) * SizePremium(t) * asmp.CommRenPrem() * (1 + asmp.CnsmpTax())
else:
return 0
[docs]def SizeExpsMaint(t):
"""Maintenance expense per policy at time t"""
return (SizeAnnPrem(t) * asmp.ExpsMaintAnnPrem()
+ (SizeSumAssured(t) * asmp.ExpsMaintSA() + asmp.ExpsMaintPol())
* InflFactor(t))
[docs]def SizeExpsOther(t):
"""Other expenses per policy at time t"""
return 0
[docs]def SizeInvstIncome(t):
"""Investment Income per policy from t to t+1"""
return (SizeReserveTotalAftMat(t) + SizePremium(t)) * InvstRetRate(t)
[docs]def SizePremium(t):
"""Premium income per policy from t to t+1"""
return SizeSumAssured(t) * pol.GrossPremRate() * pol.PremFreq()
[docs]def SizeReservePremRsrvAftMat(t):
"""Premium reserve per policy: After maturity"""
return SizeSumAssured(t) * pol.ReserveNLP_Rate('VAL', t)
[docs]def SizeReservePremRsrvEnd(t):
"""Premium reserve per policy: End of period"""
return SizeSumAssured(t) * pol.ReserveNLP_Rate('VAL', t)
[docs]def SizeReserveTotalAftMat(t):
"""Total reserve per policy: After maturity"""
return (SizeReservePremRsrvAftMat(t)
+ SizeReserveUernPremAftMat(t))
[docs]def SizeReserveUernPremAftMat(t):
"""Unearned premium: After maturity"""
return 0 # SizeSumAssured(t) * polset.UnernPremRate(polset, tt, True)
[docs]def SizeReserveUernPremEnd(t):
"""Unearned reserve per policy: End of period"""
return 0 # SizeSumAssured(t) * pol.UnernPremRate(polset, tt)
[docs]def SizeSumAssured(t):
"""Sum assured per policy at time ``t``"""
return pol.SumAssured()
[docs]def InterestNetCF(t):
"""Interest accreted on pv of net cashflows"""
if t > last_t():
return 0
else:
return (PV_NetCashflow(t)
- PremIncome(t)
+ ExpsTotal(t)) * DiscRate(t)
[docs]def PV_BenefitDeath(t):
"""Present value of death benefits"""
if t > last_t():
return 0
else:
return (-BenefitDeath(t) + PV_BenefitDeath(t+1)) / (1 + DiscRate(t))
[docs]def PV_BenefitMat(t):
"""Present value of matuirty benefits"""
if t > last_t():
return 0
else:
return (-BenefitMat(t) + PV_BenefitMat(t+1)) / (1 + DiscRate(t))
[docs]def PV_BenefitSurr(t):
"""Present value of surrender benefits"""
if t > last_t():
return 0
else:
return (-BenefitSurr(t) + PV_BenefitSurr(t+1)) / (1 + DiscRate(t))
[docs]def PV_ExpsAcq(t):
"""Present value of acquisition expenses"""
if t > last_t():
return 0
else:
return - ExpsAcq(t) + PV_ExpsAcq(t+1) / (1 + DiscRate(t))
[docs]def PV_ExpsCommTotal(t):
"""Present value of commission expenses"""
if t > last_t():
return 0
else:
return - ExpsCommTotal(t) + PV_ExpsCommTotal(t+1) / (1 + DiscRate(t))
[docs]def PV_ExpsMaint(t):
"""Present value of maintenance expenses"""
if t > last_t():
return 0
else:
return - ExpsMaint(t) + PV_ExpsMaint(t+1) / (1 + DiscRate(t))
[docs]def PV_NetCashflowForCheck(t):
"""Present value of net cashflow"""
if t > last_t():
return 0
else:
return (PremIncome(t)
- ExpsTotal(t)
- BenefitTotal(t) / (1 + DiscRate(t))
+ PV_NetCashflow(t+1) / (1 + DiscRate(t)))
[docs]def PV_SumInsurIF(t):
"""Present value of insurance in-force"""
if t > last_t():
return 0
else:
return InsurIF_Beg1(t) + PV_SumInsurIF(t+1) / (1 + DiscRate(t))
# ---------------------------------------------------------------------------
# References
pol = ("Interface", (".", "Policy"), "auto")
asmp = ("Interface", (".", "Assumptions"), "auto")
scen = ("Interface", ("..", "Economic"), "auto")
ScenID = 1