def test(): print('gtwoext test...', filename) ref_date = mx.Date(2018, 9, 9) tenor_rates = [('3M', 0.0151), ('6M', 0.0152), ('9M', 0.0153), ('1Y', 0.0154), ('2Y', 0.0155), ('3Y', 0.0156), ('4Y', 0.0157), ('5Y', 0.0158), ('7Y', 0.0159), ('10Y', 0.016), ('15Y', 0.0161), ('20Y', 0.0162)] tenors = [] zerorates = [] interpolator1DType = mx.Interpolator1D.Linear extrapolator1DType = mx.Extrapolator1D.FlatForward for tr in tenor_rates: tenors.append(tr[0]) zerorates.append(tr[1]) fittingCurve = mx.ZeroYieldCurveExt(ref_date, tenors, zerorates, interpolator1DType, extrapolator1DType, 'irskrw_krccp') alpha1 = 0.1 sigma1 = 0.01 alpha2 = 0.2 sigma2 = 0.02 corr = 0.5 g2ext = mx.GTwoExtModel('g2ext', fittingCurve, alpha1, sigma1, alpha2, sigma2, corr) models = [g2ext] corrMatrix = mx.Matrix([[1.0]]) timeGrid = mx.TimeEqualGrid(ref_date, 3, 365) # random scenario_num = 5000 dimension = (len(timeGrid) - 1) * len(models) seed = 100 skip = 0 isMomentMatching = False randomType = "crude" subType = "mersennetwister" randomTransformType = "invnormal" rsg = mx.Rsg(scenario_num, dimension, seed, skip, isMomentMatching, randomType, subType, randomTransformType) scen = mx.ScenarioGenerator(models, corrMatrix, timeGrid, rsg, False, filename, False) scen.generate() results = mx.ScenarioResult(filename) print(results.multiPath(scenCount=10))
def pricing(): results = mx.ScenarioResult(filename) payoff = build_stepdown() payoff.initialize_timeGrid(results.timeGrid()) simulNum = results.simulNum() discount_curve = mx.FlatForward(refDate, 0.0307, mx.Actual365Fixed()) v = 0 for i in range(simulNum): path = results.multiPath(i) v += payoff.value(path, discount_curve) if i != 0 and i % 5000 == 0: print(i, v / i) print(simulNum, v / simulNum)
def build_scenario(overwrite=True): print('stepdown test...', filename) if not overwrite: return initialValues = [387833, 27450] dividends = [0.0247, 0.0181] volatilities = [0.2809, 0.5795] gbmconst1 = mx.GBMConstModel('gbmconst', initialValues[0], riskFree, dividends[0], volatilities[0]) gbmconst2 = mx.GBMConstModel('gbmconst', initialValues[1], riskFree, dividends[1], volatilities[1]) models = [gbmconst1, gbmconst2] corr = 0.6031 corrMatrix = mx.Matrix([[1.0, corr], [corr, 1.0]]) timeGrid = mx.TimeEqualGrid(refDate, 3, 365) # random scenario_num = 30000 dimension = (len(timeGrid) - 1) * len(models) seed = 100 skip = 0 isMomentMatching = False randomType = "crude" subType = "mersennetwister" randomTransformType = "invnormal" rsg = mx.Rsg(scenario_num, dimension, seed, skip, isMomentMatching, randomType, subType, randomTransformType) scen = mx.ScenarioGenerator(models, corrMatrix, timeGrid, rsg, False, filename, False) scen.generate() results = mx.ScenarioResult(filename)
def test(): print('multiplemodels test...', filename) ref_date = mx.Date(2018, 9, 9) initialValue = 10000 riskFree = 0.032 dividend = 0.01 volatility = 0.15 gbmconst1 = mx.GBMConstModel('gbmconst', initialValue, riskFree, dividend, volatility) gbmconst2 = mx.GBMConstModel('gbmconst', initialValue, riskFree, dividend, volatility) models = [gbmconst1, gbmconst2] corrMatrix = mx.Matrix([[1.0, 0.0], [0.0, 1.0]]) timeGrid = mx.TimeEqualGrid(ref_date, 3, 365) # random scenario_num = 5000 dimension = (len(timeGrid) - 1) * len(models) seed = 100 skip = 0 isMomentMatching = False randomType = "crude" subType = "mersennetwister" randomTransformType = "invnormal" rsg = mx.Rsg(scenario_num, dimension, seed, skip, isMomentMatching, randomType, subType, randomTransformType) scen = mx.ScenarioGenerator(models, corrMatrix, timeGrid, rsg, False, filename, False) scen.generate() results = mx.ScenarioResult(filename) print(results.multiPath(scenCount=10))
def test(): print('cir1f test...', filename) ref_date = mx.Date(2018, 10, 10) initialValue = 0.02 meanReverting = 0.1 longTermRate = 0.042 volatility = 0.03 cir1f = mx.CIR1FModel('cir1f', initialValue, meanReverting, longTermRate, volatility) models = [cir1f] corrMatrix = mx.Matrix([[1.0]]) timeGrid = mx.TimeEqualGrid(ref_date, 3, 365) # random scenario_num = 5000 dimension = (len(timeGrid) - 1) * len(models) seed = 100 skip = 0 isMomentMatching = False randomType = "crude" subType = "mersennetwister" randomTransformType = "invnormal" rsg = mx.Rsg(scenario_num, dimension, seed, skip, isMomentMatching, randomType, subType, randomTransformType) scen = mx.ScenarioGenerator(models, corrMatrix, timeGrid, rsg, False, filename, False) scen.generate() results = mx.ScenarioResult(filename) print(results.multiPath(scenCount=10))
def test(): print('gbm test...', filename) ref_date = mx.Date(2018, 9, 9) initialValue = 10000 # (period, rf, div) tenor_rates = [('3M', 0.0151, 0.01), ('6M', 0.0152, 0.01), ('9M', 0.0153, 0.01), ('1Y', 0.0154, 0.01), ('2Y', 0.0155, 0.01), ('3Y', 0.0156, 0.01), ('4Y', 0.0157, 0.01), ('5Y', 0.0158, 0.01), ('7Y', 0.0159, 0.01), ('10Y', 0.016, 0.01), ('15Y', 0.0161, 0.01), ('20Y', 0.0162, 0.01)] tenors = [] rf_rates = [] div_rates = [] vols = [] interpolator1DType = mx.Interpolator1D.Linear extrapolator1DType = mx.Extrapolator1D.FlatForward for tr in tenor_rates: tenors.append(tr[0]) rf_rates.append(tr[1]) div_rates.append(tr[2]) rfCurve = mx.ZeroYieldCurveExt(ref_date, tenors, rf_rates, interpolator1DType, extrapolator1DType, 'irskrw_krccp') divCurve = mx.ZeroYieldCurveExt(ref_date, tenors, div_rates, interpolator1DType, extrapolator1DType, 'irskrw_krccp') volCurve = mx.BlackConstantVol(ref_date, 0.2, 'irskrw_krccp') gbm = mx.GBMModel('gbm', initialValue, rfCurve, divCurve, volCurve) models = [gbm] corrMatrix = mx.Matrix([[1.0]]) timeGrid = mx.TimeEqualGrid(ref_date, 3, 365) # random scenario_num = 5000 dimension = (len(timeGrid) - 1) * len(models) seed = 100 skip = 0 isMomentMatching = False randomType = "crude" subType = "mersennetwister" randomTransformType = "invnormal" rsg = mx.Rsg(scenario_num, dimension, seed, skip, isMomentMatching, randomType, subType, randomTransformType) scen = mx.ScenarioGenerator(models, corrMatrix, timeGrid, rsg, False, filename, False) scen.generate() results = mx.ScenarioResult(filename) print(results.multiPath(scenCount=10))