# 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 is a part of the course

## “Quantitative Risk Management in R”

### Exercise instructions

- Use
`fit.st()`

to fit a Student t distribution to the data in`djx`

and assign the results to`tfit`

. - Assign the
`par.ests`

component of the fitted model to`tpars`

and the elements of`tpars`

to`nu`

,`mu`

, and`sigma`

, respectively. - Fill in
`hist()`

to plot a histogram of`djx`

. - Fill in
`dt()`

to compute the fitted t density at the values`djx`

and assign to`yvals`

. Refer to the video for this equation. - Fill in
`lines()`

to add a red line to the histogram of`djx`

showing 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")
```

This exercise is part of the course

## Quantitative Risk Management in R

Work with risk-factor return series, study their empirical properties, and make estimates of value-at-risk.

In this chapter, you will learn about graphical and numerical tests of normality, apply them to different datasets, and consider the alternative Student t model.

Exercise 1: The normal distributionExercise 2: Graphical methods for assessing normalityExercise 3: Testing for normalityExercise 4: Q-Q plots for assessing normalityExercise 5: Skewness, kurtosis and the Jarque-Bera testExercise 6: Numerical tests of normalityExercise 7: Testing normality for longer time horizonsExercise 8: Overlapping returnsExercise 9: Reviewing knowledge of normal distributions and returnsExercise 10: The Student t distributionExercise 11: Fitting t distribution to dataExercise 12: Testing FX returns for normalityExercise 13: Testing interest-rate returns for normalityExercise 14: Testing gold price returns for normality### What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.