matplotlib 用Python绘制期权定价的蒙特卡罗模拟

dbf7pr2w  于 2022-11-15  发布在  Python
关注(0)|答案(1)|浏览(153)

我试图在x轴上显示不同模拟次数的蒙特卡罗障碍价格。这是我目前为止尝试的,但我得到的错误-〉ValueError:x和y必须具有相同的第一维度,但具有形状(10,)和(5,)。我是Python的新手,尽管我很努力,但还是找不到错误

import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt

def mc_single_barrier_do(S0, K, T, H, r, vol, N, M):

    # Constants
    dt = T / N  # change in time
    nudt = (r - 0.5 * vol ** 2) * dt  # deterministic component
    volsdt = vol * np.sqrt(dt)  # diffusion coefficient
    erdt = np.exp(r * dt)  # discount factor

     # Standard Error Placeholders
    sum_CT = 0
    sum_CT2 = 0
     # Monte Carlo Method
    for i in range(M):

        # Barrier Crossed Flag
        BARRIER = False
        St = S0

        for j in range(N):
            epsilon = np.random.normal()
            Stn = St * np.exp(nudt + volsdt * epsilon)
            St = Stn
            Ptn = np.exp(-2. * (H - St) * (H - Stn) / (St ** 2. * volsdt ** 2.))
            Pt = Ptn
            if Pt >= npr.uniform():
                BARRIER = True
        if np.amin(St) > H and BARRIER == False:
            CT = np.maximum(St - K, 0)

        else:
            CT = 0.

        sum_CT = sum_CT + CT
        sum_CT2 = sum_CT2 + CT * CT

    C0_MC = np.exp(-r * T) * sum_CT / M
    return C0_MC

def sim_iterator(max_sample, N, S0, T, r, vol, K, H, method):

    assert (method in ['MC', 'AV', 'CV'])
    
    mean_payoffs = np.zeros(int(np.ceil(max_sample / 10)))
    
    if method == 'MC':
        for n_sample in range(10, max_sample + 1, 10):
            payoffs = mc_single_barrier_do(n_sample, S0, K, T, H, r, vol, N)
            mean_payoffs[int(n_sample / 10 - 1)] = np.mean(payoffs)
        
    return mean_payoffs

r = 0.1
vol = 0.2
T = 2
N = 20
dt = T / N
S0 = 50
K = 50
H = 45
max_sample = 100

MC_price_estimates = sim_iterator(S0, T, r, vol, K, H, max_sample, N, method='MC')
x_axis1 = range(10, max_sample + 1, 10)
plt.plot(x_axis1, MC_price_estimates)
plt.xlabel("No. of Simulations")
plt.ylabel("Estimated option price")
plt.title("Ordinary Monte Carlo Method")
plt.legend()
plt.show()
nr9pn0ug

nr9pn0ug1#

在函数定义中使用了:

def sim_iterator(max_sample, N, S0, T, r, vol, K, H, method):

当使用您使用的函数时:

MC_price_estimates = sim_iterator(S0, T, r, vol, K, H, max_sample, N, method='MC')

python有位置参数,这意味着参数是根据它们的位置而不是它们的名称来Map的,所以第一个位置的被Map到第一个参数,这意味着第二行的S0被Map到第一行的max_sample,只要固定参数的排列,或者使用关键字参数S0=S0

MC_price_estimates = sim_iterator(S0=S0, T=T, r=r, vol=vol, K=K, H=H, max_sample=max_sample, N=N, method='MC')

这就是将所有参数都固定为关键字参数时代码的外观。

def mc_single_barrier_do(S0, K, T, H, r, vol, N, M):
    # Constants
    dt = T / N  # change in time
    nudt = (r - 0.5 * vol ** 2) * dt  # deterministic component
    volsdt = vol * np.sqrt(dt)  # diffusion coefficient
    erdt = np.exp(r * dt)  # discount factor

    # Standard Error Placeholders
    sum_CT = 0
    sum_CT2 = 0
    # Monte Carlo Method
    for i in range(M):

        # Barrier Crossed Flag
        BARRIER = False
        St = S0

        for j in range(N):
            epsilon = np.random.normal()
            Stn = St * np.exp(nudt + volsdt * epsilon)
            St = Stn
            Ptn = np.exp(-2. * (H - St) * (H - Stn) / (St ** 2. * volsdt ** 2.))
            Pt = Ptn
            if Pt >= npr.uniform():
                BARRIER = True
        if np.amin(St) > H and BARRIER == False:
            CT = np.maximum(St - K, 0)

        else:
            CT = 0.

        sum_CT = sum_CT + CT
        sum_CT2 = sum_CT2 + CT * CT

    C0_MC = np.exp(-r * T) * sum_CT / M
    return C0_MC

def sim_iterator(max_sample, N, S0, T, r, vol, K, H, method):
    assert (method in ['MC', 'AV', 'CV'])

    mean_payoffs = np.zeros(int(np.ceil(max_sample / 10)))

    if method == 'MC':
        for n_sample in range(10, max_sample + 1, 10):
            payoffs = mc_single_barrier_do(M=n_sample,S0= S0, K=K, T=T, H=H, r=r, vol=vol, N=N)
            mean_payoffs[int(n_sample / 10 - 1)] = np.mean(payoffs)

    return mean_payoffs

r = 0.1
vol = 0.2
T = 2
N = 20
dt = T / N
S0 = 50
K = 50
H = 45
max_sample = 100

MC_price_estimates = sim_iterator(S0=S0, T=T, r=r, vol=vol, K=K, H=H, max_sample=max_sample, N=N, method='MC')
x_axis1 = range(10, max_sample + 1, 10)
plt.plot(x_axis1, MC_price_estimates)
plt.xlabel("No. of Simulations")
plt.ylabel("Estimated option price")
plt.title("Ordinary Monte Carlo Method")
plt.legend()
plt.show()

相关问题