问题1:
import tushare as ts
import numpy as np
import pandas as pd
import warnings
warnings.simplefilter("ignore")
from matplotlib import pyplot as plt
import seaborn
%matplotlib inline
dd=ts.get_k_data('002466',start='2010-01-01',end='2021-07-18')['close']
dd.sort_index(inplace=True)
dd_returns=dd.pct_change()
dd_returns.dropna(inplace=True)
d_mu_1=dd_returns.mean()
d_sigma_1=dd_returns.std()
# VaR_1=d_mu_1-2.33*d_sigma_1
ds_0=dd[-1]
为什么最后一行位置函数使用-1不行
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/438498095626177.png)
改成1又可以
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/445589095626179.png)
问题2:
def ZQ(ds_0,d_mu_1,d_sigma_1,T,n):
delta_1=T/n
sim_price=[ds_0]
for i in range(n):
start_price=sim_price[i]
epsilon=np.random.normal()
end_price=start_price+start_price*(d_mu_1*delta_1+d_sigma_1*epsilon*np.sqrt(delta_1))
end_price = max(0,end_price)
sim_price.append(end_price)
return sim_price
zq1=ZQ(ds_0,d_mu_1,d_sigma_1,1000,1000)
# zq1=np.array(zq1)
zq1=pd.Series(zq1)
zq2=[ZQ(ds_0,d_mu_1,d_sigma_1,1000,1000) for iu in range(100)]
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cmx
import numpy as np
import matplotlib
import seaborn as sns
# 设置图像风格
sns.set_style('whitegrid')
# 设置绘图参数
# for ii in range(len(zq2)):
fig = plt.figure(figsize=(9,9))
ax = fig.add_subplot(111)
lines = []
for ii in range(len(zq2)):
line = zq2[ii]
retLine, = ax.plot(line,
color='cornflowerblue',
alpha=0.8,
linewidth=0.6)
plt.xlabel('stock_price', fontsize='large')
plt.ylabel('steps', fontsize='large')
plt.title('Monte Carlo Simulation', fontsize='large')
plt.show()
plt.plot(zq1)
为什么在定义zq2时引用zq1和不引用zq1画出的效果不同?
zq2=[zq1 for iu in range(100)]
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/818303195626188.png)
zq2=[ZQ(ds_0,d_mu_1,d_sigma_1,1000,1000) for iu in range(100)]
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/853014195626184.png)