编程代考 GR5260 Class3 – Jupyter Notebook

2/5/22, 10:22 AM GR5260 Class3 – Jupyter Notebook
import numpy as np
import scipy.stats as stats
def BS_option_value(S0, K, T, r, q, v):

Copyright By PowCoder代写 加微信 powcoder

d1 = (np.log(S0/K)+(r-q+0.5*v**2)*T)/(v*np.sqrt(T))
d2 = d1 – v*np.sqrt(T)
price = S0*np.exp(-q*T)*stats.norm.cdf(d1) – K*np.exp(-r*T)*stats.norm.cdf(d2) return price
Out[4]: 22.80986759122129 In [6]:
Out[14]: 22.871249565132807 In [ ]:
S0, K, T, r, q, v = 180, 160, 180/365, 0.02, 0.015, 0.20 BS_option_value(S0, K, T, r, q, v)
def sim_option_value(S0, K, T, r, q, v, n):
z = np.random.normal(size=n)
S = S0*np.exp((r-q-0.5*v**2)*T + v*np.sqrt(T)*z) disc_payoff = np.exp(-r*T)*np.maximum(S-K, 0) price = np.mean(disc_payoff)
return price
sim_option_value(S0, K, T, r, q, v, n)
# generate 1000 simulated values
# plot the distribution as a histogram
# n = # samples in computing the simulated option value # m = # simulated values
def sim_values(S0, K, T, r, q, v, n, m):
outputs = np.zeros(m) for i in range(m):
outputs[i] = sim_option_value(S0, K, T, r, q, v, n) return outputs
m, n = 1000, 10000
values = sim_values(S0, K, T, r, q, v, n, m) n = 50000
values2 = sim_values(S0, K, T, r, q, v, n, m)
localhost:8889/notebooks/2022_GR5260/GR5260 Class3.ipynb

2/5/22, 10:22 AM GR5260 Class3 – Jupyter Notebook
import matplotlib.pyplot as plt plt.hist(values, bins=50, label=’10000′) plt.hist(values2, bins=50, label=’50000′) plt.legend()
Out[22]:
localhost:8889/notebooks/2022_GR5260/GR5260 Class3.ipynb

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com