Friday, January 11, 2013

Calculate mean and variance from PDF via R

It is easy to get the mean and variance from a series data in R. You can use mean and var to get them very easily.

Actually that is only a discrete version of mean and variance calculation. What if we only know the probability density function? How to calculate mean and variance given a pdf function.

In this case, we have to use integral, which seems difficult in R. But if the pdf is a discrete function, we can solve the problem as follows:

rm(list=ls())
pdf=matrix(c(1/3,1/3,1/3,10,-3,-1),nrow=3,byrow=F,dimnames=list(c('case1','case2','case3'),c('freq','val')))
mean_=sum((pdf[,2])*pdf[,1])
variance=sum((pdf[,2]-mean_)^2*pdf[,1])

No comments: