def plot_all(source, seperate=True): res = None if isinstance(source, xen.ScenarioSet): for scen in source.scenario_list: res = xen_r.ResultObj(source.set_name, scen.get_scen_id(), scen.get_result_id()) plot_all_merge(res) elif isinstance(source, xen.Scenario): res = xen_r.ResultObj(source.owner_set_name, source.get_scen_id(), source.get_result_id()) elif isinstance(source, xen_r.ResultObj): res = source else: raise Exception('unknown source') if seperate: plot_all_seperate(res) else: plot_all_merge(res)
def equity(model, scenario_num=1000, maxyear=3, moment_match=False, frequency=xen.TimeGridFrequency.Day): if not isinstance(model, xen.Eq1FModel): raise Exception('Eq1FModel Type is needed') scenSetID = 'test_set' scenID = 'equity_test_scen' resultID = 'equity_test_result' scenSet = xen.ScenarioSet(scenSetID) scen = xen.Scenario(scenID, resultID) scen.general.maxyear = maxyear scen.general.scenario_num = scenario_num scen.general.moment_match = moment_match scen.general.frequency = frequency # setting additional calculation for martinagle test model.clear_calc() calc_randomZ = xen_c.RandomZ() model.add_calc(calc_randomZ) calc_uncon_expectation = xen_c.UnconditionalExpectation() model.add_calc(calc_uncon_expectation) scen.add_model(model) scenSet.add_scenario(scen) scenSet.generate() res = xen_r.ResultObj(scenSetID, scenID, resultID) equity = res.get_resultModel(model=model) equity_uncon_expectation = res.get_resultModel(model=model, calc=calc_uncon_expectation) rnd_z = res.get_resultModel(model=model, calc=calc_randomZ) plt.figure(1) plt.plot(res.timegrid.data['T'], equity.average(), label='equity_aver') plt.plot(res.timegrid.data['T'], equity_uncon_expectation.data[0], label='equity_analytic') plt.title('equity_aver vs equity_analytic') plt.xlabel('time (year)') plt.ylabel('value') plt.legend() plt.figure(2) plt.title('rand_z') plt.plot(rnd_z.average()) plt.show()
# scenario set set_name = 'set1' scenSet = xen.ScenarioSet(set_name=set_name) # scenario scen_id = 'scen1' result_id = 'res1' scen1 = xen.Scenario(scen_id=scen_id, result_id=result_id) # generation setting scen1.general.scenario_num = 100 scen1.general.maxyear = 10 # model add scen1.add_model(xen_s.gbm('kospi200')) scen1.add_model(xen_s.gbmconst('kospi')) scen1.add_model(xen_s.hw1f('irskrw')) scenSet.add_scenario(scen1) scenSet.generate() # get result from current repository res = xen_r.ResultObj(set_name, scen_id, result_id) # export csv for i, model in enumerate(scen1.models): filename = model + '_' + str(i) + '.csv' res.get_resultModel_by_index(i).export_csv(filename) # export npz res.export_npz(scenSet.set_name + '.npz')
# coding=euc-kr import xenarix as xen import xenarix.results as results repository_dir = 'C:\\Users\\09928829\\Source\\Workspaces\\InsuranceHedgeProject\\InsruranceHedge_sln\\Project\\Scenario\\xenarixPy\\test_repository' xen.set_repository(repository_dir) set_name = 'setname_1' scen_id = 'testallmodel_1' result_id = 'testresult_1' result = results.ResultObj(set_name, scen_id, result_id) print(result.res_models[0].data)
# coding=utf-8 import xenarix as xen import xenarix.results as xen_r xen.set_repository('c:\\xenarix') res = xen_r.ResultObj('set1','SCEN1','RES1') timeseries = [] ts_result_model = xen_r.TimeSeriesResultModel(timeseries=timeseries, timegrid=res.timegrid) for t_row in res.timegrid: ts_result_model.interpolated_value(t_row)
def shortrate(model, scenario_num=1000, maxyear=3, moment_match=False, frequency=xen.TimeGridFrequency.Day): if not isinstance(model, xen.Ir1FModel): raise Exception('Ir1FModel Type is needed') scenSetID = 'test_shortratecalc_set' scenID = 'test_shortratecalc_scen' resultID = 'test_shortratecalc_result' scenSet = xen.ScenarioSet(scenSetID) scen = xen.Scenario(scenID, resultID) scen.general.maxyear = maxyear scen.general.scenario_num = scenario_num scen.general.moment_match = moment_match scen.general.frequency = frequency model.clear_calc() # spot ---------------------------------------------- calc_spot3M = xen_c.Spot('test_spot3m') calc_spot3M.maturity = '3M' model.add_calc(calc_spot3M) calc_spot6M = xen_c.Spot('test_spot6m') calc_spot6M.maturity = '6M' model.add_calc(calc_spot6M) calc_spot24M = xen_c.Spot('test_spot24m') calc_spot24M.maturity = '24M' model.add_calc(calc_spot24M) # forward ---------------------------------------------- calc_forward_1M = xen_c.Forward('test_forward1m') calc_forward_1M.maturity = '1M' model.add_calc(calc_forward_1M) calc_forward_1Y_1M = xen_c.Forward('test_forward1y_1m') calc_forward_1Y_1M.forward_peoriod = '1Y' calc_forward_1Y_1M.maturity = '1M' model.add_calc(calc_forward_1Y_1M) calc_forward_1Y = xen_c.Forward('test_forward1y') calc_forward_1Y.maturity = '1Y' model.add_calc(calc_forward_1Y) calc_forward_1Y_1Y = xen_c.Forward('test_forward1y_1y') calc_forward_1Y_1Y.forward_peoriod = '1Y' calc_forward_1Y_1Y.maturity = '1Y' model.add_calc(calc_forward_1Y_1Y) # calc_cmt = xen_c.CMT('test_cmt') # model.add_calc(calc_cmt) # # calc_zerobond = xen_c.ZeroBond('test_zb') # model.add_calc(calc_zerobond) scen.add_model(model) scenSet.add_scenario(scen) scenSet.generate() res = xen_r.ResultObj(scenSetID, scenID, resultID) shortrate = res.get_resultModel(model=model) rm_calc_spot3M = res.get_resultModel(model=model, calc=calc_spot3M) rm_calc_spot6M = res.get_resultModel(model=model, calc=calc_spot6M) rm_calc_spot24M = res.get_resultModel(model=model, calc=calc_spot24M) # rm_calc_ = res.get_resultModel(model=model, calc=calc_zerobond) # rm_calc_cmt = res.get_resultModel(model=model, calc=calc_cmt) # rm_calc_zerobond = res.get_resultModel(model=model, calc=calc_zerobond) # plt.plot(shortrate_aver) # plt.plot(shortrate_uncon_expectation[0]) plt.figure(1) plt.plot(res.timegrid.data['T'], shortrate.average(), label='shor_aver') plt.plot(res.timegrid.data['T'], rm_calc_spot3M.average(), label='spot_3m') plt.plot(res.timegrid.data['T'], rm_calc_spot6M.average(), label='spot_6m') plt.plot(res.timegrid.data['T'], rm_calc_spot24M.average(), label='spot_24m') plt.title('short_aver vs cmt_aver vs zerobond_aver') plt.xlabel('time (year)') plt.ylabel('rate (%)') plt.show()
def mc_shortrate(model, scenario_num=200, maxyear=3, moment_match=False, frequency=xen.TimeGridFrequency.Day): if not isinstance(model, xen.Ir1FModel): raise Exception('Ir1FModel Type is needed') scenSetID = 'test_set' scenID = 'test_scen' resultID = 'test_result_' + model.model_name scenSet = xen.ScenarioSet(scenSetID) scen = xen.Scenario(scenID, resultID) scen.general.maxyear = maxyear scen.general.scenario_num = scenario_num scen.general.moment_match = moment_match scen.general.frequency = frequency # setting additional calculation for martinagle test model.clear_calc() # calc_randomZ = xen_c.RandomZ() # model.add_calc(calc_randomZ) calc_uncon_expectation = xen_c.UnconditionalExpectation() model.add_calc(calc_uncon_expectation) calc_mrk_discount = xen_c.FittingDiscount() model.add_calc(calc_mrk_discount) calc_model_discount = xen_c.ModelDiscount() model.add_calc(calc_model_discount) scen.add_model(model) scenSet.add_scenario(scen) scenSet.generate() res = xen_r.ResultObj(scenSetID, scenID, resultID) shortrate = res.get_resultModel(model=model) shortrate_uncon_expectation = res.get_resultModel( model=model, calc=calc_uncon_expectation) mrk_discount = res.get_resultModel(model=model, calc=calc_mrk_discount) model_discount = res.get_resultModel(model=model, calc=calc_model_discount) #rnd_z = res.get_resultModel(model=model, calc=calc_randomZ) print(mrk_discount.data[0]) # result_list['BASE_FITTINGDISCOUNT'].load(1, 1) # # shortrate_aver = result_list['BASE_nan'].averave() # shortrate = result_list['BASE_nan'].load() # shortrate_uncon_expectation = result_list['BASE_UNCONDITIONALEXPECTATION'].load() # mrk_discount = result_list['BASE_FITTINGDISCOUNT'].load() # discount = result_list['BASE_MODEL_DISCOUNTBOND'].averave() # mrk_spot = result_list['BASE_FITTINGSPOT'].averave() # mrk_forward = result_list['BASE_FITTINGFORWARD'].averave() # rnd_z = result_list['BASE_RANDOMZ'].averave() # print(shortrate_aver) # print(shortrate[0]) # print(shortrate[1]) # plt.plot(discount) # plt.plot(mrk_discount) # plt.plot(mrk_spot) i = 0 # for z, short_u_exp, short, spot, forward, mrk_disc, disc in zip(rnd_z, shortrate_uncon_expectation[0], # shortrate_aver, mrk_spot, mrk_forward, # mrk_discount[0], discount): # print ( # str(i) + ' : ' + str(z) + ' , ' + str(short_u_exp) + ' , ' + str(short) + ' , ' + str(spot) + ' , ' + str( # forward) + ' , ' + str(mrk_disc) + ' , ' + str(disc) + ' , ' + str(mrk_disc - disc)) # i += 1 # plt.plot(shortrate_aver) # plt.plot(shortrate_uncon_expectation[0]) plt.figure(1) plt.plot(res.timegrid.data['T'], shortrate.average(), label='shor_aver') plt.plot(res.timegrid.data['T'], shortrate_uncon_expectation.data[0], label='short_analytic') plt.title('short_aver vs short_analytic') plt.xlabel('time (year)') plt.ylabel('rate (%)') plt.legend() plt.figure(2) plt.plot(res.timegrid.data['T'], shortrate.average() - shortrate_uncon_expectation.data[0], label='diff') plt.title('short_aver - short_analytic') plt.xlabel('time (year)') plt.ylabel('rate (%)') plt.legend() # plt.plot(mrk_forward) # pos = 360 # print(mrk_discount[0][pos]) # print(discount[pos]) # # print(mrk_discount[0][pos] - discount[pos]) # # print('disc diff ' + str(pos) + ' : ' + str(mrk_discount[0][pos] - discount[pos])) plt.figure(3) plt.title('discount - mrk_discount') # plt.plot(mrk_discount[0]) # plt.plot(discount) plt.plot(mrk_discount.data[0] - model_discount.average()) # plt.figure(4) # plt.title('rand_z') # plt.plot(rnd_z.average()) #print('aver Z : ' + rnd_z)) plt.show()
def rw_shortrate(model, scenario_num=200, maxyear=3, moment_match=False, frequency=xen.TimeGridFrequency.Day): if not isinstance(model, xen.Ir1FModel): raise Exception('Ir1FModel Type is needed') scenSetID = 'test_set' scenID = 'test_scen' resultID = 'test_result_' + model.model_name scenSet = xen.ScenarioSet(scenSetID) scen = xen.Scenario(scenID, resultID) scen.general.maxyear = maxyear scen.general.scenario_num = scenario_num scen.general.moment_match = moment_match scen.general.frequency = frequency # setting additional calculation for martinagle test model.clear_calc() # calc_randomZ = xen_c.RandomZ() # model.add_calc(calc_randomZ) calc_uncon_expectation = xen_c.UnconditionalExpectation() model.add_calc(calc_uncon_expectation) scen.add_model(model) scenSet.add_scenario(scen) scenSet.generate() res = xen_r.ResultObj(scenSetID, scenID, resultID) shortrate = res.get_resultModel(model=model) shortrate_uncon_expectation = res.get_resultModel( model=model, calc=calc_uncon_expectation) #rnd_z = res.get_resultModel(model=model, calc=calc_randomZ) i = 0 plt.figure(1) plt.plot(res.timegrid.data['T'], shortrate.average(), label='shor_aver') plt.plot(res.timegrid.data['T'], shortrate_uncon_expectation.data[0], label='short_analytic') plt.title('short_aver vs short_analytic') plt.xlabel('time (year)') plt.ylabel('rate (%)') plt.legend() plt.figure(2) plt.plot(res.timegrid.data['T'], shortrate.average() - shortrate_uncon_expectation.data[0], label='diff') plt.title('short_aver - short_analytic') plt.xlabel('time (year)') plt.ylabel('rate (%)') plt.legend() # plt.figure(3) # plt.title('rand_z') # plt.plot(rnd_z.average()) #print('aver Z : ' + rnd_z)) plt.show()
import xenarix as xen import xenarix.results as xen_r #import model_generate as mg scenSetID = 'set1' scenID = 'scen1' resultID = 'res1' # mg.generate(scenSetID=scenSetID, # scenID=scenID, # resultID=resultID) result = xen_r.ResultObj(scenSetID, scenID, resultID) multipath = result.get_multipath(scen_count=0) modelpath = result.get_modelpath(model_count=0) calculated_result = [] print(result.names) # print (multipath) # print (modelpath) # for t in result.timegrid: # print(multipath[:,t[0]]) # calculated_result.append([]) # for t in result.timegrid: # print(modelpath[:, t[0]])