问题描述
我有一个问题,我正在绘制随机游走线,我希望获得所有模拟的平均线。我的代码如下:
<div class="modal-body">
<select class="custom-select">
<option selected>Customers</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6+</option>
</select>
<p>Pick a date: <input type="text" id="datepicker"></p>
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
我遇到的问题是我已经能够(可能不正确)得出的平均线似乎在情节中途停止。我敢肯定这是一个简单的解决方法,但这会让我发疯!
谢谢!
解决方法
之所以失败是因为您运行了100次模拟,因此len(avarage)
将为100,但是len(price_list)
始终为252 +1。最简单的解决方法是使这两个相同。但这不会解决另一个巨大的问题:您每次计算平均价格为252 + 1天,因此这就是开始时平均价格错误的原因。您应该按天计算。更好的解决方案是:
import numpy as np
import matplotlib.pyplot as plt
S = 100
T = 10
mu = 0.061
stdev = 0.165
SIMULATIONS = 100
if __name__ == '__main__':
# the array to store simulation results
full_array = np.empty(shape=(SIMULATIONS,T + 1))
for i in range(SIMULATIONS):
daily_returns = np.random.normal((mu/T),stdev/np.sqrt(T),T) + 1
# A more efficient way of calculating the same as you have
# It's a simple geometric series.
price_list = np.empty(shape=len(daily_returns) + 1)
price_list[0] = S
price_list[1:] = daily_returns
price_list = np.cumprod(price_list)
plt.plot(price_list,color="gray")
# save that simulation run
full_array[i,:] = price_list
# plot the mean by days
plt.plot(np.mean(full_array,axis=0),color="red")