从蒙特卡洛中添加多个模拟值

时间:2019-05-05 08:04:49

标签: python

我试图对Callpayoffs中的值求和,因为它们表示基于在先路径资产价格循环中生成的最后价格的收益。如果我运行10个仿真,则基于每个仿真路径的最新价格(具有252个价格点)应该有10个Callpayoffs。不幸的是,我无法以任何方式将Callpayoffs列表中的值相加,因此我可以将10个模拟中的平均Callpayoff取值。非常感谢您的帮助-以下是print(sum(Callpayoffs)的示例。如您所见,代码仅将最后一个值除以10,即模拟次数

[0]
[0]
[0]
[16.651081469090343]
[14.076846993975735]
[9.483857458061152]
[5.357562042338017]
[6.09266787737144]
[0]
[27.85935401436157]

2.785935401436157 # this is the last value divided by no of 
simulations, but should be the sum of all values above divided by 
simulations


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 = 10


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)) 
    plt.plot(stream)
    print(Callpayoffs)

print(sum(Callpayoffs))
(sum(Callpayoffs)) / float(simulations)

0 个答案:

没有答案