程式

使用 Heaviside 函式和 QuantLib Python 定價二元期權的問題

  • September 4, 2020

我正在嘗試使用 MC Simulation 和 Python QuantLib 庫為二元期權定價。期權的價格與分析引擎相匹配。但是,我不確定如何結合 Heaviside 函式來計算收益(如果 St > K 則為 1;否則為 0)。這是相同的程式碼:

import QuantLib as ql
today = ql.Date().todaysDate()
initialValue = 40
riskFreeTS = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.01, ql.Actual365Fixed()))
dividendTS = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.02, ql.Actual365Fixed()))
volatility = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(today, ql.NullCalendar(), 0.2, ql.Actual365Fixed()))

process = ql.BlackScholesMertonProcess(ql.QuoteHandle(ql.SimpleQuote(40)), riskFreeTS, dividendTS, volatility)
steps = 2
rng = "pseudorandom" # could use "lowdiscrepancy"
numPaths = 500000
option_type = ql.Option.Call
strike_price = 40

maturity_date = ql.Date(2, 4, 2021)
exercise = ql.EuropeanExercise(maturity_date)
payoff=ql.PlainVanillaPayoff(ql.Option.Call, strike_price)
binary_option = ql.VanillaOption(payoff, exercise)

engine = ql.MCEuropeanEngine(process, rng, steps, requiredSamples=numPaths)

與蒙地卡羅一起奔跑

binary_option.setPricingEngine(engine)
price = binary_option.NPV()
print("Monte Carlo Price: {}".format(price))

使用分析引擎執行

engine = ql.AnalyticEuropeanEngine(process)
binary_option.setPricingEngine(engine)
print("Analytic Price: {}".format(binary_option.NPV()))

輸出是:

Monte Carlo Price: 2.5199444258975885

Analytic Price: 2.5135333959120034

您需要將收益更改為:

payoff = ql.PlainVanillaPayoff(ql.Option.Call, strike_price)

payoff = ql.CashOrNothingPayoff(ql.Option.Call, strike_price, 1)
import QuantLib as ql

today = ql.Date().todaysDate()

initialValue = 40

riskFreeTS = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.01, ql.Actual365Fixed()))

dividendTS = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.02, ql.Actual365Fixed()))

volatility = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(today, ql.NullCalendar(), 0.2, ql.Actual365Fixed()))


process = ql.BlackScholesMertonProcess(ql.QuoteHandle(ql.SimpleQuote(40)), riskFreeTS, dividendTS, volatility)

steps = 2

rng = "pseudorandom" # could use "lowdiscrepancy"

numPaths = 500000

option_type = ql.Option.Call

strike_price = 40

maturity_date = ql.Date(2, 4, 2021)

exercise = ql.EuropeanExercise(maturity_date)

payoff = ql.CashOrNothingPayoff(ql.Option.Call, strike_price, 1)

binary_option = ql.VanillaOption(payoff, exercise)

engine = ql.MCEuropeanEngine(process, rng, steps, requiredSamples=numPaths)

#Run with Monte Carlo

binary_option.setPricingEngine(engine)

price = binary_option.NPV()

print("Monte Carlo Price: {}".format(price))

# Run with Analytic Engine

engine = ql.AnalyticEuropeanEngine(process)

binary_option.setPricingEngine(engine)

print("Analytic Price: {}".format(binary_option.NPV()))

引用自:https://quant.stackexchange.com/questions/57780