HW12

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 8

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 价格。

S0 - 标的资产初始价格 , X - 执行价格, r - 无风险利率, T - 到期时间(年化), sigma - 波动率, NPoints - 模拟点



返回 :
Price - 欧式看涨期权的 Monte Carlo 价格
"""
nuT = (r - 0.5 * sigma ** 2) * T
siT = sigma * np.sqrt(T)
# 使用 Box-Muller 生成标准正态随机数
U1 = np.random.rand(int(np.ceil(NPoints / 2)))
U2 = np.random.rand(int(np.ceil(NPoints / 2)))
VLog = np.sqrt(-2 * np.log(U1))
Norm1 = VLog * np.cos(2 * np.pi * U2)
Norm2 = VLog * np.sin(2 * np.pi * U2)
Norm = np.concatenate([Norm1, Norm2])
DiscPayoff = np.exp(-r * T) * np.maximum(0, S0 * np.exp(nuT + siT * Norm) - X)
Price = np.mean(DiscPayoff)

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)

print("Black-Scholes 看涨期权价格 :", call_price)


print("Black-Scholes 看跌期权价格 :", put_price)
print("Monte Carlo 看涨期权价格 ( 使用 Rand + Box-Muller):", rand_price)
Halton + Box Muller: European
Put
import numpy as np

def get_halton(how_many, base):


seq = np.zeros(how_many)
num_bits = 1 + int(np.ceil(np.log(how_many) / np.log(base)))
vet_base = base ** (-(np.arange(1, num_bits + 1).astype(float)))
work_vet = np.zeros(num_bits)

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)

# Use Box-Muller to generate standard normals


H1 = get_halton(int(np.ceil(NPoints / 2)), Base1)
H2 = get_halton(int(np.ceil(NPoints / 2)), Base2)

VLog = np.sqrt(-2 * np.log(H1))


Norm1 = VLog * np.cos(2 * np.pi * H2)
Norm2 = VLog * np.sin(2 * np.pi * H2)
Norm = np.concatenate([Norm1, Norm2])

DiscPayoff = np.exp(-r * T) * np.maximum(0, X - S0 * np.exp(nuT + siT * Norm))


Price = np.mean(DiscPayoff)

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

Halton27 = Ep_halton(S0, X, r, T, sigma, NRepl, Base11, Base12)


Halton117 = Ep_halton(S0, X, r, T, sigma, NRepl, Base21, Base22)
Halton24 = Ep_halton(S0, X, r, T, sigma, NRepl, Base31, Base32)

print("Halton27:", Halton27)
print("Halton117:", Halton117)
print("Halton24:", Halton24)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy