コード例 #1
0
def test_FinBinomialTree():

    stock_price = 50.0
    risk_free_rate = 0.06
    dividend_yield = 0.04
    volatility = 0.40

    valuation_date = Date(1, 1, 2016)
    expiry_date = Date(1, 1, 2017)

    model = BlackScholes(volatility)
    discount_curve = DiscountCurveFlat(valuation_date, risk_free_rate)
    dividend_curve = DiscountCurveFlat(valuation_date, dividend_yield)

    num_steps_list = [100, 500, 1000, 2000, 5000]

    strike_price = 50.0

    testCases.banner("================== EUROPEAN PUT =======================")

    put_option = EquityVanillaOption(expiry_date, strike_price,
                                     OptionTypes.EUROPEAN_PUT)
    value = put_option.value(valuation_date, stock_price, discount_curve,
                             dividend_curve, model)
    delta = put_option.delta(valuation_date, stock_price, discount_curve,
                             dividend_curve, model)
    gamma = put_option.gamma(valuation_date, stock_price, discount_curve,
                             dividend_curve, model)
    theta = put_option.theta(valuation_date, stock_price, discount_curve,
                             dividend_curve, model)
    testCases.header("BS Value", "BS Delta", "BS Gamma", "BS Theta")
    testCases.print(value, delta, gamma, theta)

    payoff = EquityTreePayoffTypes.VANILLA_OPTION
    exercise = EquityTreeExerciseTypes.EUROPEAN
    params = np.array([-1, strike_price])

    testCases.header("NumSteps", "Results", "TIME")

    for num_steps in num_steps_list:
        start = time.time()
        tree = EquityBinomialTree()
        results = tree.value(stock_price, discount_curve, dividend_curve,
                             volatility, num_steps, valuation_date, payoff,
                             expiry_date, payoff, exercise, params)
        end = time.time()
        duration = end - start
        testCases.print(num_steps, results, duration)

    testCases.banner("================== AMERICAN PUT =======================")

    payoff = EquityTreePayoffTypes.VANILLA_OPTION
    exercise = EquityTreeExerciseTypes.AMERICAN
    params = np.array([-1, strike_price])

    testCases.header("NumSteps", "Results", "TIME")

    for num_steps in num_steps_list:
        start = time.time()
        tree = EquityBinomialTree()
        results = tree.value(stock_price, discount_curve, dividend_curve,
                             volatility, num_steps, valuation_date, payoff,
                             expiry_date, payoff, exercise, params)
        end = time.time()
        duration = end - start
        testCases.print(num_steps, results, duration)

    testCases.banner(
        "================== EUROPEAN CALL =======================")

    call_option = EquityVanillaOption(expiry_date, strike_price,
                                      OptionTypes.EUROPEAN_CALL)
    value = call_option.value(valuation_date, stock_price, discount_curve,
                              dividend_curve, model)
    delta = call_option.delta(valuation_date, stock_price, discount_curve,
                              dividend_curve, model)
    gamma = call_option.gamma(valuation_date, stock_price, discount_curve,
                              dividend_curve, model)
    theta = call_option.theta(valuation_date, stock_price, discount_curve,
                              dividend_curve, model)
    testCases.header("BS Value", "BS Delta", "BS Gamma", "BS Theta")
    testCases.print(value, delta, gamma, theta)

    payoff = EquityTreePayoffTypes.VANILLA_OPTION
    exercise = EquityTreeExerciseTypes.EUROPEAN
    params = np.array([1.0, strike_price])

    testCases.header("NumSteps", "Results", "TIME")
    for num_steps in num_steps_list:
        start = time.time()
        tree = EquityBinomialTree()

        results = tree.value(stock_price, discount_curve, dividend_curve,
                             volatility, num_steps, valuation_date, payoff,
                             expiry_date, payoff, exercise, params)

        end = time.time()
        duration = end - start
        testCases.print(num_steps, results, duration)

    testCases.banner(
        "================== AMERICAN CALL =======================")

    payoff = EquityTreePayoffTypes.VANILLA_OPTION
    exercise = EquityTreeExerciseTypes.AMERICAN
    params = np.array([1.0, strike_price])

    testCases.header("NumSteps", "Results", "TIME")
    for num_steps in num_steps_list:
        start = time.time()
        tree = EquityBinomialTree()

        results = tree.value(stock_price, discount_curve, dividend_curve,
                             volatility, num_steps, valuation_date, payoff,
                             expiry_date, payoff, exercise, params)

        end = time.time()
        duration = end - start
        testCases.print(num_steps, results, duration)
コード例 #2
0
def test_EquityVanillaOption():

    valuation_date = Date(1, 1, 2015)
    expiry_date = Date(1, 7, 2015)
    stock_price = 100
    volatility = 0.30
    interest_rate = 0.05
    dividend_yield = 0.01
    model = BlackScholes(volatility)
    discount_curve = DiscountCurveFlat(valuation_date, interest_rate)
    dividend_curve = DiscountCurveFlat(valuation_date, dividend_yield)

    num_paths_list = [10000, 20000, 40000, 80000, 160000, 320000]

    testCases.header("NUMPATHS", "VALUE_BS", "VALUE_MC", "TIME")

    for num_paths in num_paths_list:

        call_option = EquityVanillaOption(expiry_date, 100.0,
                                          FinOptionTypes.EUROPEAN_CALL)
        value = call_option.value(valuation_date, stock_price, discount_curve,
                                  dividend_curve, model)
        start = time.time()
        value_mc = call_option.value_mc(valuation_date, stock_price,
                                        discount_curve, dividend_curve, model,
                                        num_paths)
        end = time.time()
        duration = end - start
        testCases.print(num_paths, value, value_mc, duration)

###############################################################################

    stock_prices = range(80, 120, 10)
    num_paths = 100000

    testCases.header("NUMPATHS", "CALL_VALUE_BS", "CALL_VALUE_MC",
                     "CALL_VALUE_MC_SOBOL", "TIME")
    useSobol = True

    for stock_price in stock_prices:

        call_option = EquityVanillaOption(expiry_date, 100.0,
                                          FinOptionTypes.EUROPEAN_CALL)

        value = call_option.value(valuation_date, stock_price, discount_curve,
                                  dividend_curve, model)

        start = time.time()

        useSobol = False
        value_mc1 = call_option.value_mc(valuation_date, stock_price,
                                         discount_curve, dividend_curve, model,
                                         num_paths, useSobol)

        useSobol = True
        value_mc2 = call_option.value_mc(valuation_date, stock_price,
                                         discount_curve, dividend_curve, model,
                                         num_paths, useSobol)

        end = time.time()
        duration = end - start
        testCases.print(num_paths, value, value_mc1, value_mc2, duration)

###############################################################################

    stock_prices = range(80, 120, 10)
    num_paths = 100000

    testCases.header("NUMPATHS", "PUT_VALUE_BS", "PUT_VALUE_MC",
                     "PUT_VALUE_MC_SOBOL", "TIME")

    for stock_price in stock_prices:

        put_option = EquityVanillaOption(expiry_date, 100.0,
                                         FinOptionTypes.EUROPEAN_PUT)

        value = put_option.value(valuation_date, stock_price, discount_curve,
                                 dividend_curve, model)

        start = time.time()

        useSobol = False
        value_mc1 = put_option.value_mc(valuation_date, stock_price,
                                        discount_curve, dividend_curve, model,
                                        num_paths, useSobol)

        useSobol = True
        value_mc2 = put_option.value_mc(valuation_date, stock_price,
                                        discount_curve, dividend_curve, model,
                                        num_paths, useSobol)

        end = time.time()
        duration = end - start
        testCases.print(num_paths, value, value_mc1, value_mc2, duration)


###############################################################################

    stock_prices = range(80, 120, 10)

    testCases.header("STOCK PRICE", "CALL_VALUE_BS", "CALL_DELTA_BS",
                     "CALL_VEGA_BS", "CALL_THETA_BS", "CALL_RHO_BS",
                     "CALL_VANNA_BS")

    for stock_price in stock_prices:

        call_option = EquityVanillaOption(expiry_date, 100.0,
                                          FinOptionTypes.EUROPEAN_CALL)
        value = call_option.value(valuation_date, stock_price, discount_curve,
                                  dividend_curve, model)
        delta = call_option.delta(valuation_date, stock_price, discount_curve,
                                  dividend_curve, model)
        vega = call_option.vega(valuation_date, stock_price, discount_curve,
                                dividend_curve, model)
        theta = call_option.theta(valuation_date, stock_price, discount_curve,
                                  dividend_curve, model)
        rho = call_option.rho(valuation_date, stock_price, discount_curve,
                              dividend_curve, model)
        vanna = call_option.vanna(valuation_date, stock_price, discount_curve,
                                  dividend_curve, model)
        testCases.print(stock_price, value, delta, vega, theta, rho, vanna)

    ###########################################################################

    testCases.header("STOCK PRICE", "PUT_VALUE_BS", "PUT_DELTA_BS",
                     "PUT_VEGA_BS", "PUT_THETA_BS", "PUT_RHO_BS",
                     "PUT_VANNA_BS")

    for stock_price in stock_prices:

        put_option = EquityVanillaOption(expiry_date, 100.0,
                                         FinOptionTypes.EUROPEAN_PUT)

        value = put_option.value(valuation_date, stock_price, discount_curve,
                                 dividend_curve, model)
        delta = put_option.delta(valuation_date, stock_price, discount_curve,
                                 dividend_curve, model)
        vega = put_option.vega(valuation_date, stock_price, discount_curve,
                               dividend_curve, model)
        theta = put_option.theta(valuation_date, stock_price, discount_curve,
                                 dividend_curve, model)
        rho = put_option.rho(valuation_date, stock_price, discount_curve,
                             dividend_curve, model)
        vanna = put_option.vanna(valuation_date, stock_price, discount_curve,
                                 dividend_curve, model)
        testCases.print(stock_price, value, delta, vega, theta, rho, vanna)