🔴
入学要求
💯
能力测试
🛣️
课程安排
🕹️
研究资源

练习


准备工作

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

练习1:偏度分析

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}")

练习2:峰度分析

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}")

练习3:综合应用

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()

练习4:样本外检验

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日滚动偏度")

提示与工具:

  1. 使用.rolling(window).skew()计算滚动偏度
  1. Jarque-Bera检验需从statsmodels导入
  1. 对数收益率计算推荐:np.log(price/price.shift(1)).dropna()
  1. 样本外验证时注意数据频率一致性
  1. 处理多期数据时使用pd.concat()对齐时间序列

完成练习后建议:

  1. 对比不同行业股票的矩特征
  1. 测试不同滚动窗口对结果的影响
  1. 分析市场极端事件的统计矩变化
  1. 探索加密货币市场的非正态性特征

通过完成这些练习,您将掌握:

  1. 偏度与峰度的实际计算方法
  1. 正态性检验的验证与应用
  1. 滚动窗口分析技巧
  1. 样本外测试的重要性
  1. 统计矩在风险管理中的应用