import numpy as np
import pandas as pd
import yfinance as yf
import scipy.stats as stats
import matplotlib.pyplot as plt
from statsmodels.stats.stattools import jarque_bera
1a 人工分布偏度判断
分析Gamma分布的偏度特征:
xs2 = np.linspace(stats.gamma.ppf(0.01, 0.7, loc=-1), stats.gamma.ppf(0.99, 0.7, loc=-1), 150) + 1
X = stats.gamma.pdf(xs2, 1.5)
# 计算偏度
skew = _____
# plt.plot(xs2, X)
# print(f"偏度: {skew:.3f}")
# if _____:
# print("正偏态分布")
1b 实际数据偏度分析
分析Netflix股票收益偏度:
nflx = yf.download('NFLX', start='2015-01-01', end='2016-01-01')['Close']
returns = _____
# 计算偏度
# skew = _____
# plt.hist(returns, bins=30)
# print(f"偏度系数: {skew:.2f}")
2a 余弦分布峰度判断
分析修正余弦分布的峰度特征:
xs = np.linspace(-6,6,300) + 2
Y = stats.cosine.pdf(xs)
# 计算超额峰度
# excess_kurt = _____
# plt.plot(xs, Y)
# print(f"超额峰度: {excess_kurt:.2f}")
2b 实际数据峰度分析
分析Netflix股票收益峰度:
returns = _____.pct_change().dropna()
# kurt = _____
# print(f"超额峰度: {kurt:.2f}")
3a 混合分布偏度
分析双峰混合分布的偏度:
xs2 = np.linspace(stats.lognorm.ppf(0.01, 0.7), stats.lognorm.ppf(0.99, 0.7), 150)
lognorm = stats.lognorm.pdf(xs2, 0.4)
Z = lognorm/2 + lognorm[::-1]
# 计算偏度
# skew = _____
# print(f"偏度: {skew:.2f}")
3b Jarque-Bera校准验证
验证正态性检验方法的准确性:
np.random.seed(42)
N = 1000
significant_count = 0
for _ in range(N):
sample = _____
_, pval, _, _ = jarque_bera(sample)
if _____:
significant_count += 1
# print(f"第一类错误率: {significant_count/N:.3f}")
3c 混合分布正态性检验
对双峰分布进行JB检验:
# _, pval, _, _ = _____
# print(f"P值: {pval:.2e}")
# if _____:
# print("服从正态分布")
3d 偏度误导性验证
可视化展示偏度指标的局限性:
# plt.plot(Z)
# plt.title("双峰分布示例")
# plt.show()
4a AMC股票正态性检验
分析AMC娱乐控股历史收益分布:
amc = yf.download('AMC', start='2014-01-01', end='2016-01-01')['Close']
returns = _____
# 绘制直方图
# plt.hist(_____, bins=30)
4b 样本内偏度计算
计算2014-2016年偏度:
# print(f"样本内偏度: {stats.skew(returns):.2f}")
4c 样本外验证
分析2016上半年偏度变化:
out_sample = yf.download('AMC', start='2016-01-01', end='2016-07-01')['Close']
out_returns = _____
# print(f"样本外偏度: {stats.skew(out_returns):.2f}")
4d 滚动偏度分析
可视化60日滚动偏度变化:
amc_full = yf.download('AMC', start='2015-01-01', end='2017-01-01')['Close']
rolling_skew = _____
# plt.plot(rolling_skew)
# plt.title("60日滚动偏度")
提示与工具:
.rolling(window).skew()
计算滚动偏度np.log(price/price.shift(1)).dropna()
pd.concat()
对齐时间序列完成练习后建议:
通过完成这些练习,您将掌握: