Пример #1
0
def test():
    print('interpolation test...')

    # 1 dimension
    print('1 dim ---------')
    data = [(1.0, 6.0), (2.0, 5.0), (3.0, 8.0), (4.0, 6.0), (5.0, 4.0),
            (6.0, 1.0), (7.0, 2.0), (8.0, 3.0), (9.0, 6.0), (10.0, 5.0),
            (11.0, 4.0), (12.0, 2.0)]

    x = [v[0] for v in data]
    y = [v[1] for v in data]

    interpolation1d = mx.Interpolation1D(mx.Interpolator1D.ForwardFlat, x, y)

    print(interpolation1d.interpolate(1.17))
    print(interpolation1d.interpolate([1.17, 2, 23]))

    # 2 dimension
    print('2 dim ---------')
    z = mx.Matrix(len(x), len(y))

    for i in range(len(x)):
        for j in range(len(y)):
            z[i][j] = i * (pow(j, 0.5))

    interpolation2d = mx.Interpolation2D(mx.Interpolator2D.Bilinear, x, y, z)

    print(interpolation2d.interpolate(1.17, 2.33))
    print(interpolation2d.interpolate([1.17, 2.23], [4.3, 3.3]))
Пример #2
0
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 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))
Пример #6
0
def correlation_test():
    # (date, SPX Index,	NKY Index, SHCOMP Index, US0003M Index ) - bloomberg ticker
    mrk_data = [['2020-01-06', 3246.28, 23204.86, 3083.408, 1.87225],
                ['2020-01-07', 3237.18, 23575.72, 3104.802, 1.878],
                ['2020-01-08', 3253.05, 23204.76, 3066.893, 1.834],
                ['2020-01-09', 3274.7, 23739.87, 3094.882, 1.84788],
                ['2020-01-10', 3265.35, 23850.57, 3092.291, 1.83775],
                ['2020-01-14', 3283.15, 24025.17, 3106.82, 1.84263],
                ['2020-01-15', 3289.29, 23916.58, 3090.038, 1.83613],
                ['2020-01-16', 3316.81, 23933.13, 3074.081, 1.82663],
                ['2020-01-17', 3329.62, 24041.26, 3075.496, 1.81913],
                ['2020-01-21', 3320.79, 23864.56, 3052.142, 1.80625],
                ['2020-01-22', 3321.75, 24031.35, 3060.754, 1.80088],
                ['2020-01-23', 3325.54, 23795.44, 2976.528, 1.79413],
                ['2020-02-03', 3248.92, 22971.94, 2746.606, 1.741],
                ['2020-02-04', 3297.59, 23084.59, 2783.288, 1.73738],
                ['2020-02-05', 3334.69, 23319.56, 2818.088, 1.74163],
                ['2020-02-06', 3345.78, 23873.59, 2866.51, 1.73413],
                ['2020-02-07', 3327.71, 23827.98, 2875.964, 1.73088],
                ['2020-02-10', 3352.09, 23685.98, 2890.488, 1.71313],
                ['2020-02-12', 3379.45, 23861.21, 2926.899, 1.70375],
                ['2020-02-13', 3373.94, 23827.73, 2906.073, 1.69163],
                ['2020-02-14', 3380.16, 23687.59, 2917.008, 1.69175],
                ['2020-02-18', 3370.29, 23193.8, 2984.972, 1.69463],
                ['2020-02-19', 3386.15, 23400.7, 2975.402, 1.696],
                ['2020-02-20', 3373.23, 23479.15, 3030.154, 1.68275],
                ['2020-02-21', 3337.75, 23386.74, 3039.669, 1.67925],
                ['2020-02-25', 3128.21, 22605.41, 3013.05, 1.63763],
                ['2020-02-26', 3116.39, 22426.19, 2987.929, 1.61325],
                ['2020-02-27', 2978.76, 21948.23, 2991.329, 1.58038],
                ['2020-02-28', 2954.22, 21142.96, 2880.304, 1.46275],
                ['2020-03-02', 3090.23, 21344.08, 2970.931, 1.25375],
                ['2020-03-03', 3003.37, 21082.73, 2992.897, 1.31425],
                ['2020-03-04', 3130.12, 21100.06, 3011.666, 1.00063],
                ['2020-03-05', 3023.94, 21329.12, 3071.677, 0.99888],
                ['2020-03-06', 2972.37, 20749.75, 3034.511, 0.896],
                ['2020-03-09', 2746.56, 19698.76, 2943.291, 0.76813],
                ['2020-03-10', 2882.23, 19867.12, 2996.762, 0.78413],
                ['2020-03-11', 2741.38, 19416.06, 2968.517, 0.7725],
                ['2020-03-12', 2480.64, 18559.63, 2923.486, 0.7405],
                ['2020-03-13', 2711.02, 17431.05, 2887.427, 0.84313],
                ['2020-03-16', 2386.13, 17002.04, 2789.254, 0.88938],
                ['2020-03-17', 2529.19, 17011.53, 2779.641, 1.05188],
                ['2020-03-18', 2398.1, 16726.55, 2728.756, 1.11575],
                ['2020-03-19', 2409.39, 16552.83, 2702.13, 1.19513],
                ['2020-03-23', 2237.4, 16887.78, 2660.167, 1.21563],
                ['2020-03-24', 2447.33, 18092.35, 2722.438, 1.23238],
                ['2020-03-25', 2475.56, 19546.63, 2781.591, 1.267],
                ['2020-03-26', 2630.07, 18664.6, 2764.911, 1.37463],
                ['2020-03-27', 2541.47, 19389.43, 2772.203, 1.45013],
                ['2020-03-30', 2626.65, 19084.97, 2747.214, 1.43338],
                ['2020-03-31', 2584.59, 18917.01, 2750.296, 1.4505]]

    mrk_data_return = []

    for row, shift_row in zip(mrk_data[:-1], mrk_data[1:]):
        _row = row[1:]  # except date
        _shift_row = shift_row[1:]  # except date
        mrk_data_return.append(
            [math.log(v[1] / v[0]) for v in zip(_row, _shift_row)])

    corr_arr = np.corrcoef(np.transpose(mrk_data_return))

    corr = mx.Matrix(corr_arr)
    print(corr)
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))