HW12
HW12
HW12
鮑欣禾 112071469
筆記
Rand + Box Muller
import numpy as np
from scipy.stats import norm
def blsprice(S0, X, r, T, sigma):
"""
计算欧式看涨和看跌期权的 Black-Scholes 价格。
参数 :
S0 - 标的资产初始价格
X - 执行价格
r - 无风险利率
T - 到期时间(年化)
sigma - 波动率
返回 :
call_price - 看涨期权价格
put_price - 看跌期权价格
"""
d1 = (np.log(S0 / X) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
call_price = S0 * norm.cdf(d1) - X * np.exp(-r * T) * norm.cdf(d2)
put_price = X * np.exp(-r * T) * norm.cdf(-d2) - S0 * norm.cdf(-d1)
return call_price, put_price
Rand + Box Muller
def bls_rand(S0, X, r, T, sigma, NPoints):
"""
使用 Box-Muller 变换和随机数生成欧式看涨期权的 Monte Carlo 价格。
return Price
Rand + Box Muller
# 参数
S0 = 50
X = 52
r = 0.1
T = 5 / 12
sigma = 0.4
NRepl = 5000
# 计算期权价格
call_price, put_price = blsprice(S0, X, r, T, sigma)
rand_price = bls_rand(S0, X, r, T, sigma, NRepl)
for i in range(how_many):
# Increment last bit and carry over if necessary
j=0
while True:
work_vet[j] += 1
if work_vet[j] < base:
break
work_vet[j] = 0
j += 1
seq[i] = np.dot(work_vet, vet_base)
return seq
Halton + Box Muller: European
Put
def Ep_halton(S0, X, r, T, sigma, NPoints, Base1, Base2):
nuT = (r - 0.5 * sigma ** 2) * T
siT = sigma * np.sqrt(T)
return Price
Halton + Box Muller: European
Put
S0 = 50
X = 52
r = 0.1
T = 5 / 12
sigma = 0.4
NRepl = 5000
Base11, Base12 = 2, 7
Base21, Base22 = 11, 7
Base31, Base32 = 2, 4
print("Halton27:", Halton27)
print("Halton117:", Halton117)
print("Halton24:", Halton24)