將 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()對djx的資料配適 Student t 分配,並把結果指定給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")