将 t 分布拟合到数据
与正态分布相比,Student t 分布通常更适合刻画日度、周度和月度收益。
您可以使用 QRM 包中的 fit.st() 函数来拟合。拟合结果的参数估计组件为 par.ests,可将其赋给列表 tpars,以便保存 nu、mu 和 sigma,供后续使用:
> tfit <- fit.st(ftse)
> tpars <- tfit$par.ests
> tpars
nu mu sigma
2.949514e+00 4.429863e-05 1.216422e-02
本练习中,您将把 Student t 分布拟合到 djx 中包含的 2008–2011 年道琼斯指数的日对数收益。随后,您将绘制数据的直方图,并叠加一条红色曲线来显示拟合的 t 密度。djx 数据与 QRM 包已为您加载。
本练习是课程的一部分
R 中的定量风险管理
练习说明
- 使用
fit.st()将 Student t 分布拟合到djx的数据上,并将结果赋给tfit。 - 将拟合模型的
par.ests组件赋给tpars,并将tpars的各元素分别赋给nu、mu和sigma。 - 补全
hist(),绘制djx的直方图。 - 补全
dt(),在djx的取值处计算拟合的 t 密度并赋给yvals。该公式可参考视频。 - 补全
lines(),在djx的直方图上添加一条红色曲线来显示拟合的 t 密度。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Fit a Student t distribution to djx
tfit <- ___(___)
# Define tpars, nu, mu, and sigma
tpars <- ___
nu <- ___
mu <- ___
sigma <- ___
# Plot a histogram of djx
hist(___, nclass = 20, probability = TRUE, ylim = range(0, 40))
# Compute the fitted t density at the values djx
yvals <- dt((___ - ___)/___, df = ___)/___
# Superimpose a red line to show the fitted t density
lines(___, yvals, col = "red")