Graphical methods for assessing normality
In the video, you learned how to create a histogram with 20 buckets that represents the probability density of the FTSE data, as well as how to add a normal distribution to the existing plot as a red line:
> hist(ftse, nclass = 20, probability = TRUE)
> lines(ftse, dnorm(ftse, mean = mu, sd = sigma), col = "red")
As you can see, dnorm(x, mean, sd)
calculates the probability density function (PDF) of the data x
with the calculated sample mean and standard deviation; this is known as the method-of-moments.
Finally, to calculate an estimate of the density of data x
, use density(x)
. This creates a so-called kernel-density estimate (KDE) using a non-parametric method that makes no assumptions about the underlying distribution.
The various plots suggest that the data are heavier tailed than normal, although you will learn about better graphical and numerical tests in future exercises.
In this exercise, you will fit a normal distribution to the log-returns of the Dow Jones index for 2008-2009 and compare the data with the fitted distribution using a histogram and a density plot. The object djx
containing Dow Jones data is loaded into your workspace.
This exercise is part of the course
Quantitative Risk Management in R
Exercise instructions
- Calculate the average and standard deviation (
sd()
) of thedjx
data and assign tomu
andsigma
, respectively. - Plot a histogram of
djx
with 20 buckets that represents a probability density of the data. - Fill in the
lines()
anddnorm()
functions to add the normal density curve fordjx
as a red line to the histogram. - Plot a kernel-density estimate for
djx
usingdensity()
. - Use the same
lines()
command as above to add the normal density curve fordjx
as a red line to the KDE.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Calculate average and standard deviation of djx
mu <- ___
sigma <- ___
# Plot histogram of djx
___(___)
# Add the normal density as a red line to histogram
lines(___, dnorm(___), col = ___)
# Plot non-parametric KDE of djx
___(___)
# Add the normal density as red line to KDE
lines(___, dnorm(___), col = ___)