def test_open_model_close_old(savetestmodel, name, newname): """Test open_model API with/without name args.""" model, file = savetestmodel model.close() newmodel = mx.open_model(file, name) assert newmodel.name == newname
def unpickled_model(request, testmodel, tmpdir_factory): model = testmodel if request.param: file = str(tmpdir_factory.mktemp("data").join("testmodel.mx")) model.save(file) model.close() model = mx.open_model(file) yield model model.close()
def sample_dynamic_model(request, build_sample_dynamic_model, tmpdir_factory): model = build_sample_dynamic_model if request.param: file = str(tmpdir_factory.mktemp("data").join(model.name + ".mx")) model.save(file) model.close() model = mx.open_model(file) yield model model.close()
def test_open_model_leave_old(savetestmodel, name, newname, renamed): """Test open_model API with/without name args when old model exists. Args: name: Name passed to open_model newname: Name the new model should have renamed: True if the old model is renamed """ model, file = savetestmodel oldname = model.name newmodel = mx.open_model(file, name) assert newmodel.name == newname assert model.name[:len(oldname)] == oldname assert renamed != (len(model.name) == len(oldname))
def build(load_saved=False): """Build a model and return it. Read input data from `input.xlsx`, create `Input` space and its subspace and cells and populate them with the data. Args: load_saved: If ``True``, input data is read from `solvency2.mx` file instead of `input.xlsx`, which is saved when :py:func:`build_input <solvency2.build_input.build_input>` is executed last time. Defaults to ``False`` """ # Make sure the current directory is this folder os.chdir(os.path.abspath(os.path.dirname(__file__))) # ------------------------------------------------------------------------ # Build Input space from build_input import build_input from build_input_scr import build_input_scr if load_saved: model = mx.open_model('solvency2.mx') input = model.Input else: model = mx.new_model(name='solvency2') input = build_input(model, 'input.xlsx') build_input_scr(input, 'input_scr.xlsx') model.save('solvency2.mx') # ------------------------------------------------------------------------ # Build CommFunc space lifetable_refs = {'Input': input} def lifetable_params(Sex, IntRate, TableID): refs={'MortalityTable': Input.MortalityTables(TableID).MortalityTable} return {'refs': refs} lifetable = model.import_module( module='lifetable', name='LifeTable', formula=lifetable_params, refs=lifetable_refs) # ------------------------------------------------------------------------ # Build Policy space from policy import policy_attrs policy_refs = {'PolicyData': input.PolicyData, 'ProductSpec': input.ProductSpec, 'LifeTable': lifetable, 'PolicyAttrs': policy_attrs} def policy_params(PolicyID): refs = {attr: PolicyData[PolicyID].cells[attr] for attr in PolicyAttrs} alias = {'PremTerm': refs['PolicyTerm'], 'x': refs['IssueAge'], 'm': refs['PolicyTerm'], 'n': refs['PolicyTerm']} refs.update(alias) return {'refs': refs} policy = model.import_module( module='policy', name='Policy', formula=policy_params, refs=policy_refs) # ------------------------------------------------------------------------ # Build Assumption space asmp_refs = {'Policy': policy, 'ProductSpec': input.ProductSpec, 'MortalityTables': input.MortalityTables, 'asmp': input.Assumption, 'asmp_tbl': input.AssumptionTables} def asmp_params(PolicyID): refs = {'pol': Policy[PolicyID]} alias = {'prod': refs['pol'].Product, 'polt': refs['pol'].PolicyType, 'gen': refs['pol'].Gen} refs.update(alias) return {'refs': refs} asmp = model.import_module( module='assumption', name='Assumption', formula=asmp_params, refs=asmp_refs) asmp.allow_none = True # ------------------------------------------------------------------------ # Build Assumption space def econ_params(ScenID): refs = {'Scenario': Input.Scenarios[ScenID]} return {'refs': refs} economic = model.import_module( module='economic', name='Economic', formula=econ_params, refs={'asmp': asmp, 'Input': input}) # ------------------------------------------------------------------------ # Build SCR_life space def scrlife_params(t0, PolicyID, ScenID=1): pass scr_life = model.import_module( module='scr_life', name='SCR_life', formula=scrlife_params, refs={'Corr': input.CorrLife}) # ------------------------------------------------------------------------ # Build Projection space projbase = model.import_module( module='projection', name='BaseProj') pvmixin = model.import_module( module='present_value', name='PV') override = model.new_space(name='Override') override.import_module( module='override.mortality', name='Mortality') override.import_module( module='override.lapse', name='Lapse') override.import_module( module='override.lapse_mass', name='LapseMass') override.import_module( module='override.expense', name='Expense') proj_refs = {'Policy': policy, 'Assumption': asmp, 'Economic': economic} def proj_params(Risk='base', Shock=None, Scope=None): if Risk == 'mort' or Risk == 'longev': bases = [_space.model.Override.Mortality, _space] elif Risk == 'lapse': if Shock == 'mass': bases = [_space.model.Override.LapseMass, _space] else: bases = [_space.model.Override.Lapse, _space] elif Risk == 'exps': bases = [_space.model.Override.Expense, _space] else: bases = [_space] refs = {'pol': Policy[PolicyID], 'asmp': Assumption[PolicyID], 'scen': Economic[ScenID], 'DiscRate': Economic[ScenID].DiscRate, 'Factor': _space.model.Input.Factor} return {'bases':bases, 'refs': refs} proj = scr_life.new_space( name='Projection', bases=[projbase, pvmixin], formula=proj_params, refs=proj_refs) return model
def build(load_saved=False): """Build a model and return it. Read input data from `input.xlsm`, create `Input` space and its subspace and cells and populate them with the data. Args: load_saved: If ``True``, input data is read from `lifelib.mx` file instead of `input.xlsm`, which is saved when :py:func:`build_input <simplelife.build_input.build_input>` is executed last time. Defaults to ``False`` """ # ------------------------------------------------------------------------ # Build Input space from build_input import build_input if load_saved: model = mx.open_model(proj_dir + '/nestedlife.mx') input = model.Input else: model = mx.new_model(name='nestedlife') input = build_input(model, proj_dir + '/input.xlsm') model.save(proj_dir + '/nestedlife.mx') # ------------------------------------------------------------------------ # Build CommFunc space lifetable_refs = {'Input': input} def lifetable_params(Sex, IntRate, TableID): refs = { 'MortalityTable': Input.MortalityTables(TableID).MortalityTable } return {'bases': _self, 'refs': refs} lifetable = model.import_module(module_='lifetable', name='LifeTable', formula=lifetable_params, refs=lifetable_refs) # ------------------------------------------------------------------------ # Build Policy space from policy import policy_attrs policy_refs = { 'PolicyData': input.PolicyData, 'ProductSpec': input.ProductSpec, 'LifeTable': lifetable, 'PolicyAttrs': policy_attrs } def policy_params(PolicyID): refs = {attr: PolicyData[PolicyID].cells[attr] for attr in PolicyAttrs} alias = { 'PremTerm': refs['PolicyTerm'], 'x': refs['IssueAge'], 'm': refs['PolicyTerm'], 'n': refs['PolicyTerm'] } refs.update(alias) return {'bases': _self, 'refs': refs} policy = model.import_module(module_='policy', name='Policy', formula=policy_params, refs=policy_refs) # ------------------------------------------------------------------------ # Build Assumptions space asmp_refs = { 'Policy': policy, 'ProductSpec': input.ProductSpec, 'MortalityTables': input.MortalityTables, 'asmp': input.Assumptions, 'asmp_tbl': input.AssumptionTables } def asmp_params(PolicyID): refs = {'pol': Policy[PolicyID]} alias = { 'prd': refs['pol'].Product, 'polt': refs['pol'].PolicyType, 'gen': refs['pol'].Gen } refs.update(alias) return {'bases': _self, 'refs': refs} asmp = model.import_module(module_='assumptions', name='Assumptions', formula=asmp_params, refs=asmp_refs) asmp.allow_none = True # ------------------------------------------------------------------------ # Build Assumptions space def econ_params(ScenID): refs = {'Scenario': Input.Scenarios[ScenID]} return {'bases': _self, 'refs': refs} economic = model.import_module(module_='economic', name='Economic', formula=econ_params, refs={ 'asmp': asmp, 'Input': input }) # ------------------------------------------------------------------------ # Build Projection space # Model tree structure # # lifelib --+ # +--BaseProjection # +--OuterProjection[PolicyID] <--- BaseProjection # +--InnerProjection[t] <-- BaseProjection proj_refs = {'Pol': policy, 'Asmp': asmp, 'Scen': economic} def proj_params(PolicyID, ScenID=1): refs = { 'pol': Pol[PolicyID], 'asmp': Asmp[PolicyID], 'scen': Scen[ScenID] } return {'bases': _self, 'refs': refs} baseproj = model.import_module(module_='projection', name='BaseProjection') outerproj = model.new_space(bases=baseproj, name='OuterProjection', formula=proj_params, refs=proj_refs) def innerproj_params(t0): refs = { 'pol': _self.parent.pol, 'asmp': _self.parent.asmp, 'scen': _self.parent.scen, 'outer': _self.parent } return {'bases': _self, 'refs': refs} innerproj = outerproj.new_space(bases=baseproj, name='InnerProjection', formula=innerproj_params) return model
def build(load_saved=False): """Build a model and return it. Read input data from `input.xlsx`, create `Input` space and its subspace and cells and populate them with the data. Args: load_saved: If ``True``, input data is read from `ifrs17sim.mx` file instead of `input.xlsx`, which is saved when :py:func:`build_input <simplelife.build_input.build_input>` is executed last time. Defaults to ``False`` """ # Make sure the current directory is this folder os.chdir(os.path.abspath(os.path.dirname(__file__))) # ------------------------------------------------------------------------ # Build Input space from build_input import build_input if load_saved: model = mx.open_model('ifrs17sim.mx') input = model.Input else: model = mx.new_model(name='ifrs17sim') input = build_input(model, 'input.xlsx') model.save('ifrs17sim.mx') # ------------------------------------------------------------------------ # Build CommFunc space lifetable_refs = {'Input': input} def lifetable_params(Sex, IntRate, TableID): refs={'MortalityTable': Input.MortalityTables(TableID).MortalityTable} return {'refs': refs} lifetable = model.import_module( module='lifetable', name='LifeTable', formula=lifetable_params, refs=lifetable_refs) # ------------------------------------------------------------------------ # Build Policy space from policy import policy_attrs policy_refs = {'PolicyData': input.PolicyData, 'ProductSpec': input.ProductSpec, 'LifeTable': lifetable, 'PolicyAttrs': policy_attrs} def policy_params(PolicyID): refs = {attr: PolicyData[PolicyID].cells[attr]() for attr in PolicyAttrs} alias = {'PremTerm': refs['PolicyTerm'], 'x': refs['IssueAge'], 'm': refs['PolicyTerm'], 'n': refs['PolicyTerm']} refs.update(alias) return {'refs': refs} policy = model.import_module( module='policy', name='Policy', formula=policy_params, refs=policy_refs) # ------------------------------------------------------------------------ # Build Assumption space asmp_refs = {'Policy': policy, 'ProductSpec': input.ProductSpec, 'MortalityTables': input.MortalityTables, 'asmp': input.Assumption, 'asmp_tbl': input.AssumptionTables} def asmp_params(PolicyID): refs = {'pol': Policy[PolicyID]} alias = {'prod': refs['pol'].Product, 'polt': refs['pol'].PolicyType, 'gen': refs['pol'].Gen} refs.update(alias) return {'refs': refs} asmp = model.import_module( module='assumption', name='Assumption', formula=asmp_params, refs=asmp_refs) asmp.allow_none = True # ------------------------------------------------------------------------ # Build Assumption space def econ_params(ScenID): refs = {'Scenario': Input.Scenarios[ScenID]} return {'refs': refs} economic = model.import_module( module='economic', name='Economic', formula=econ_params, refs={'asmp': asmp, 'Input': input}) # ------------------------------------------------------------------------ # Build Projection space # Model tree structure # # lifelib --+ # +--BaseProj # +--OuterProj[PolicyID] <--- BaseProj # +--InnerProj[t] <-- BaseProj proj_refs = {'Pol': policy, 'Asmp': asmp, 'Scen': economic} def proj_params(PolicyID, ScenID=1): refs = {'pol': Pol[PolicyID], 'asmp': Asmp[PolicyID], 'scen': Scen[ScenID]} return {'refs': refs} baseproj = model.import_module( module='projection', name='BaseProj') ifrs = model.import_module( module='ifrs', name='IFRS') outerproj = model.new_space( bases=[ifrs, baseproj], name='OuterProj', formula=proj_params, refs=proj_refs) def innerproj_params(t0): refs = {'pol': _space.parent.pol, 'asmp': _space.parent.asmp, 'scen': _space.parent.scen, 'outer': _space.parent} return {'refs': refs} innerproj = outerproj.new_space( bases=baseproj, name='InnerProj', formula=innerproj_params) pvs = innerproj.import_module( module='present_value', name='PresentValue') def pvs_params(t_rate): refs = {'last_t': _space.parent.last_t, 'InsurIF_Beg1': _space.parent.InsurIF_Beg1, 'InsurIF_End': _space.parent.InsurIF_End, 'PremIncome': _space.parent.PremIncome, 'BenefitSurr': _space.parent.BenefitSurr, 'BenefitDeath': _space.parent.BenefitDeath, 'BenefitTotal': _space.parent.BenefitTotal, 'ExpsCommTotal': _space.parent.ExpsCommTotal, 'ExpsAcq': _space.parent.ExpsAcq, 'ExpsMaint': _space.parent.ExpsMaint, 'ExpsTotal': _space.parent.ExpsTotal, 'DiscRate': _space.parent.parent[t_rate].DiscRate} return {'refs': refs} pvs.set_formula(pvs_params) # Add or override functions. baseproj.new_cells(name='SurrRateMult', formula=SurrRateMult_outer) baseproj.PolsSurr.set_formula(PolsSurr) outerproj.IntAccumCF.set_formula(IntAccumCF_outer) outerproj.new_cells(name='DiscRate', formula=DiscRate_outer) outerproj.new_cells(formula=DiscRateAdj) innerproj.new_cells(name='DiscRate', formula=DiscRate_inner) innerproj.SurrRateMult.set_formula(SurrRateMult_inner) innerproj.IntAccumCF.set_formula(IntAccumCF_inner) innerproj.PolsIF_End.set_formula(PolsIF_End_inner) return model
def build(load_saved=False, mrt_mult=1, int_adj=0, sur_mult=1, mrt_i=0, int_i=0, sur_i=0): """Build a model and return it. Read input data from `input.xlsm`, create `Input` space and its subspace and cells and populate them with the data. Args: load_saved: If ``True``, input data is read from `simplelife.mx` file instead of `input.xlsm`, which is saved when :py:func:`build_input <simplelife.build_input.build_input>` is executed last time. Defaults to ``False`` """ # Make sure the current directory is this folder os.chdir(os.path.abspath(os.path.dirname(__file__))) # ------------------------------------------------------------------------ # Build Input space from build_input import build_input if load_saved: model = mx.open_model('simplelife.mx') model.rename('simplelife' + str(mrt_i) + str(int_i) + str(sur_i)) input = model.Input else: model = mx.new_model(name='simplelife') input = build_input(model, 'input.xlsm') model.save('simplelife.mx') # ------------------------------------------------------------------------ # Build CommFunc space lifetable_refs = {'Input': input} def lifetable_params(Sex, IntRate, TableID): refs = { 'MortalityTable': Input.MortalityTables(TableID).MortalityTable } return {'refs': refs} lifetable = model.import_module(module_='lifetable', name='LifeTable', formula=lifetable_params, refs=lifetable_refs) # ------------------------------------------------------------------------ # Build Policy space from policy import policy_attrs policy_refs = { 'PolicyData': input.PolicyData, 'ProductSpec': input.ProductSpec, 'LifeTable': lifetable, 'PolicyAttrs': policy_attrs } def policy_params(PolicyID): refs = {attr: PolicyData[PolicyID].cells[attr] for attr in PolicyAttrs} alias = { 'PremTerm': refs['PolicyTerm'], 'x': refs['IssueAge'], 'm': refs['PolicyTerm'], 'n': refs['PolicyTerm'] } refs.update(alias) return {'refs': refs} policy = model.import_module(module_='policy', name='Policy', formula=policy_params, refs=policy_refs) # ------------------------------------------------------------------------ # Build Assumption space asmp_refs = { 'Policy': policy, 'ProductSpec': input.ProductSpec, 'MortalityTables': input.MortalityTables, 'asmp': input.Assumption, 'asmp_tbl': input.AssumptionTables } def asmp_params(PolicyID): refs = {'pol': Policy[PolicyID]} alias = { 'prod': refs['pol'].Product, 'polt': refs['pol'].PolicyType, 'gen': refs['pol'].Gen } refs.update(alias) return {'refs': refs} asmp = model.import_module(module_='assumption', name='Assumption', formula=asmp_params, refs=asmp_refs) asmp.allow_none = True # ------------------------------------------------------------------------ # Build Assumption space def econ_params(ScenID): refs = {'Scenario': Input.Scenarios[ScenID]} return {'refs': refs} economic = model.import_module(module_='economic', name='Economic', formula=econ_params, refs={ 'asmp': asmp, 'Input': input, 'int_adj': int_adj }) # ------------------------------------------------------------------------ # Build Projection space projbase = model.import_module(module_='projection', name='BaseProj', refs={ 'mrt_mult': mrt_mult, 'sur_mult': sur_mult, 'int_adj': int_adj }) pvmixin = model.import_module(module_='present_value', name='PV') proj_refs = {'Policy': policy, 'Assumption': asmp, 'Economic': economic} def proj_params(PolicyID, ScenID=1): refs = { 'pol': Policy[PolicyID], 'asmp': Assumption[PolicyID], 'scen': Economic[ScenID], 'DiscRate': Economic[ScenID].DiscRate } return {'refs': refs} proj = model.new_space(name='Projection', bases=[projbase, pvmixin], formula=proj_params, refs=proj_refs) """ def PolsSurr(t): return PolsIF_Beg1(t) * asmp.SurrRate(t) * PolsSurrMult() def PolsSurrMult(): return sur_mult def PolsDeath(t): return PolsIF_Beg1(t) * asmp.BaseMortRate(AttAge(t)) * asmp.MortFactor(t) * mrt_mult def DiscRate(t): return Scenario.IntRate(t) + int_adj projbase.new_cells(name='PolsSurrMult', formula=PolsSurrMult) projbase.PolsSurr.set_formula(PolsSurr) #ob_ml projbase.PolsDeath.set_formula(PolsDeath) #ob_ml #projbase.DiscRate.set_formula(DiscRate) #ob_ml """ return model