Tuesday, January 1, 2013

Normal VS Lognormal Distribution using R


Objective:
The main aim of this topic is to study and observe the difference between the normal distribution and lognormal distribution using R commands. In financial time series analysis it is more appropriate to use the lognormal distribution than the normal distribution.

Introduction:
The moments of a normal distribution and lognormal distribution are not exactly the same and hence we observe a shift in log normal from a normal distribution.


The above formulae and further characteristics are also available on wikipedia using the following link - http://en.wikipedia.org/wiki/Log-normal_distribution.The wiki article also elaborates on the PDF and CDF which are covered in brief in this article. The mean of the distribution shifts by 0.5*variance, hence you can observe that the graph would shift with shift in variance from .5 to 1. This is clear from the image below.

R functionality:
The above plot is created in R using the following commands. Also, in R the plot may get cut while plotting. The best way to plot all the images correctly is plot the image with the maximum y value first using the PLOT command and subsequent images using the lines command in R.


 x<- seq(0,3,length = 100)
 plot(x, dlnorm(x,0, 0.25), type= "l", col ="blue")
 lines(x,dlnorm(x,0,0.5), type ="l", col = "red")
 lines(x,dlnorm(x,0,1), type ="l", col = "green")


dlnorm(n, mean, standard deviation) = computes the density of a lognormal distribution
type ="l" = allows r to plot a line graph
col = is used to color the line
lwd = 2   = can also be used in the plot to increase the width of the plot


Cumulative distribution function of a log normal in R:




x<- seq(0,3,length = 100)
plot(x, plnorm(x,0, 0.25), type ="l", col ="blue", lwd = 2)
lines(x,plnorm(x,0,1), type ="l", col = "green")
lines(x,plnorm(x,0,0.5), type ="l", col = "red")

In order to calculate the CDF of a log normal simply use the command PLNORM in r.

PLNORM(n, mean, standard dev.) = computes CDF for a log normal distribution.

RLNORM(n, mean , standard dev) = generates a data set with a given mean and Standard deviation.

qlnorm(p, mean, standard deviation) = can be used to generate p quantiles of the log normal distribution 




Normal Distribution using simple R command


Introduction:
The main objective of this study is to learn to plot the cumulative distribution function, quantiles and probability mass function using simple R commands.
R Commands:
·         seq – is used to generate a sequence of values.
·         pnorm – is used to calculate the cumulative distribution function.
·         dnorm – is used to calculate the probability mass function
·         qnorm- is used to generate quantiles or inverse of the normal distribution.
R commands and images:

Using Pnorm:

·         x<- seq(-5,5, length = 250)
·         plot(x,pnorm(x), type ="l", lwd = 2, col ="blue", xlab =" x values", ylab = "CDF")

Using dnorm:
·         x<- seq(-5,5, length = 250)
·         plot(x,dnorm(x), type ="l", lwd = 2, col ="blue", xlab =" x values", ylab = "PDF")

The primary goal is to understand the commands in R that help in creating basic statistical images. Readers can use Wikipedia in order to learn more about normal distribution and its usefulness in statistics and economics.