来自 numpy Monte Carlo 投资模拟的错误结果 returns
Incorrect results from a numpy Monte Carlo simulation of investment returns
How much money will you get by investing ,000 periodically at an average rate of 8% annually for 30 years and using monte carlo simulations?
我正在尝试通过对 python 中的利率使用 Monte Carlo 模拟来解决上述问题。我想出了下面的代码,它似乎是正确的,但它非常歪斜,我怀疑我做错了什么。下面代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def sni(i,n):
sni = round(((1+i)**n-1)/i,2)
return sni
df = pd.DataFrame()
investment = 10000
for p in range(1000):
i = np.random.normal(0.08,0.18)
lst = []
for n in range(30):
final = investment * sni(i,n)
lst.append(final)
df[p]=lst
我不记得我袖手旁观的方程式,但我怀疑你无论如何都有多年来的利率变化,所以这个指数的东西是行不通的。
然后我就这样做了:
import numpy as np
import pylab
years = 30
investment = 10000.0
def one_run():
account = 0
for n in range(years):
interest = np.random.normal(0.08, 0.018)
account = account * (1 + interest) + investment
return account
df = [one_run() for _ in range(10000)]
# ****** everything below here is just plotting
p, b = np.histogram(df,50, density=True)
pylab.plot(b[:-1], p)
pylab.grid()
pylab.xlabel("Return [$]")
pylab.ylabel("Probability density [1/$]")
pylab.show()
此外,您将利率以 0.18 的比例在 0.08 左右变化,即很多时候它会是负数。我自由地在此处插入另一个零(值为 0.08 +- 0.018)。
How much money will you get by investing ,000 periodically at an average rate of 8% annually for 30 years and using monte carlo simulations?
我正在尝试通过对 python 中的利率使用 Monte Carlo 模拟来解决上述问题。我想出了下面的代码,它似乎是正确的,但它非常歪斜,我怀疑我做错了什么。下面代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def sni(i,n):
sni = round(((1+i)**n-1)/i,2)
return sni
df = pd.DataFrame()
investment = 10000
for p in range(1000):
i = np.random.normal(0.08,0.18)
lst = []
for n in range(30):
final = investment * sni(i,n)
lst.append(final)
df[p]=lst
我不记得我袖手旁观的方程式,但我怀疑你无论如何都有多年来的利率变化,所以这个指数的东西是行不通的。
然后我就这样做了:
import numpy as np
import pylab
years = 30
investment = 10000.0
def one_run():
account = 0
for n in range(years):
interest = np.random.normal(0.08, 0.018)
account = account * (1 + interest) + investment
return account
df = [one_run() for _ in range(10000)]
# ****** everything below here is just plotting
p, b = np.histogram(df,50, density=True)
pylab.plot(b[:-1], p)
pylab.grid()
pylab.xlabel("Return [$]")
pylab.ylabel("Probability density [1/$]")
pylab.show()
此外,您将利率以 0.18 的比例在 0.08 左右变化,即很多时候它会是负数。我自由地在此处插入另一个零(值为 0.08 +- 0.018)。