Fitting t distribution to data
A Student t distribution is generally a much better fit to daily, weekly, and monthly returns than a normal distribution.
You can create one by using the fit.st() function in the QRM package. The resulting fitted model has a parameter estimates component par.ests which can be assigned to a list tpars in order to store its values of nu, mu, and sigma for later use:
> tfit <- fit.st(ftse)
> tpars <- tfit$par.ests
> tpars
nu mu sigma
2.949514e+00 4.429863e-05 1.216422e-02
In this exercise, you will fit a Student t distribution to the daily log-returns of the Dow Jones index from 2008-2011 contained in djx. Then, you will plot a histogram of the data and superimpose a red line to the plot showing the fitted t density. The djx data and QRM package have been loaded for you.
This exercise is part of the course
Quantitative Risk Management in R
Exercise instructions
- Use
fit.st()to fit a Student t distribution to the data indjxand assign the results totfit. - Assign the
par.estscomponent of the fitted model totparsand the elements oftparstonu,mu, andsigma, respectively. - Fill in
hist()to plot a histogram ofdjx. - Fill in
dt()to compute the fitted t density at the valuesdjxand assign toyvals. Refer to the video for this equation. - Fill in
lines()to add a red line to the histogram ofdjxshowing the fitted t density.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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")