以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文
主要内容包括:
几何布朗运动生成蒙特卡洛路径以及对数收益率
检验时间序列的正态性
检验 4 个真实时间序列数据的正态性
熟悉均值-方差投资组合理论(MPT)基本原理
使用 scipy 求解投资组合优化问题
一、正态检验
1、基准案例
- import numpy as np
- np.random.seed(1000)
- import scipy.stats as scs
- import statsmodels.api as sm
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- %matplotlib inline
- def gen_paths(S0, r, sigma, T, M, I):
- ''' Generate Monte Carlo paths for geometric Brownian motion.
-
- Parameters
- ==========
- S0 : float
- initial stock/index value
- r : float
- constant short rate
- sigma : float
- constant volatility
- T : float
- final time horizon
- M : int
- number of time steps/intervals
- I : int
- number of paths to be simulated
-
- Returns
- =======
- paths : ndarray, shape (M + 1, I)
- simulated paths given the parameters
- '''
- dt = float(T) / M
- paths = np.zeros((M + 1, I), np.float64)
- paths[0] = S0
- for t in range(1, M + 1):
- rand = np.random.standard_normal(I)
- rand = (rand - rand.mean()) / rand.std()
- paths[t] = paths[t - 1] * np.exp((r - 0.5 * sigma ** 2) * dt +
- sigma * np.sqrt(dt) * rand)
- return paths
- S0 = 100.
- r = 0.05
- sigma = 0.2
- T = 1.0
- M = 50
- I = 250000
- paths = gen_paths(S0, r, sigma, T, M, I)
- # 几何布朗运动的10条模拟路径
- # 标签: normal_sim_1
- plt.plot(paths[:, :10])
- plt.grid(True)
- plt.xlabel('time steps')
- plt.ylabel('index level')
- log_returns = np.log(paths[1:] / paths[0:-1])
- paths[:, 0].round(4)
- log_returns[:, 0].round(4)
以上内容转自 数析学院,如需完整内容可以直接查看原文