我试图对Callpayoffs
中的值求和,因为它们表示基于在先路径资产价格循环中生成的最后价格的收益。如果我运行10个模拟,则基于每个模拟路径的最后价格(应具有252个价格点)应有10个Callpayoffs
。不幸的是,我无法将Callpayoffs
列表中的值相加。非常感谢您的帮助-以下是print(sum(Callpayoffs)
4.620174500863143
22.762337253759725
0
51.97221078945353
根据我的代码
import numpy as np
import pandas as pd
from math import *
import matplotlib.pyplot as plt
from matplotlib import *
def Generate_asset_price(S,v,r,dt):
return (1 + r * dt + v * sqrt(dt) * np.random.normal(0,1))
# initial values
S = 100
v = 0.2
r = 0.05
T = 1
N = 252 # number of steps
dt = 0.00396825
simulations = 4
for x in range(simulations):
stream = [100]
Callpayoffs = []
t = 0
for n in range(N):
s = stream[t] * Generate_asset_price(S,v,r,dt)
stream.append(s)
t += 1
Callpayoffs.append(max(stream[-1] - S,0))
print(sum(Callpayoffs))
plt.plot(stream)
答案 0 :(得分:0)
您需要在Cardpayoffs
循环之外初始化for
,并在遍历列表后调用sum
。以下应该可以解决问题:
Callpayoffs = []
for x in range(simulations):
stream = [100]
t = 0
for n in range(N):
s = stream[t] * Generate_asset_price(S,v,r,dt)
stream.append(s)
t += 1
Callpayoffs.append(max(stream[-1] - S,0))
print(sum(Callpayoffs))
plt.plot(stream)