4
$\begingroup$

I want to fit a mixture of two gaussian densities to my financial data. The data can be found here: http://uploadeasy.net/upload/2a7mw.rar the variable is called dat.

The probability density of a mixture is given by: \begin{align} f(l)=\pi \phi(l;\mu_1,\sigma^2_1)+(1-\pi)\phi(l;\mu_2,\sigma^2_2) \end{align}

The quantile can be computed by using a numerical algorithm to solve the following: \begin{align} \alpha=P(L \leq VaR_\alpha) = \pi F_1(Quantile_\alpha;\mu_1,\sigma^2_1)+(1-\pi) F_2(Quantile_\alpha;\mu_2,\sigma^2_2) \end{align} I use mixtools in R:

install.packages("mixtools")
library(mixtools)
mixture<-normalmixEM(dat,k=2,fast=TRUE)

This uses the EM algorithm.

I now want to calculate the 0,95 quantile of the mixture distribution. I do a loop, a kind of a grid search, I assume, that the quantile (due to the characteristics of my data) will be below 0.3. So the loop ends at 0.3

pi<-mixture$lambda[1]
mu1<-mixture$mu[1]
mu2<-mixture$mu[2]
sigma1<-mixture$sigma[1]
sigma2<-mixture$sigma[2]

quantile<-0
probabilitylevel<-0.95
dummy1<-0

# the loop lasts for about 20-40 seconds
for (i in 1:100000){
quantile[i]<-i/(1000000/3)
}
dummy1<- probabilitylevel - ( pi * pnorm(quantile,mean=mu1,sd=sigma1) + (1-pi) * pnorm(quantile,mean=mu2,sd=sigma2))

min(abs(dummy1))
which.min(abs(dummy1))
quantileresult<-which.min(abs(dummy1))/(1000000/3)

the result

quantileresult

is 0.025371

which seems to be correct, if control it with:

pi * pnorm(quantileresult,mean=mu1,sd=sigma1) + (1-pi) * pnorm(quantileresult,mean=mu2,sd=sigma2)

I look at the plot:

plot(density(dat),col="red")
curve(expr=pi*dnorm(x,mu1,sigma1)+(1-pi)*dnorm(x,mu2,sigma2),lwd=2,col="black",add=TRUE)
curve(dnorm(x,mean(dat),sd(dat)),add=TRUE,lty=3,col="orange",lwd=2)

which gives

quantilecorrect

It looks like, that the mixture normal (black) is fitting the data way better. The dashed orange line is the univariate normal distribution fitted to the data set. It is fitting the data not as good as the mixture density, is this correct interpreted?

Finally, we look at the single densities and compare it to the mixture:

plot(density(dat),col="red")
curve(dnorm(x,mu1,sigma1),add=TRUE,lty=2,col="darkgreen")
curve(dnorm(x,mu2,sigma2),add=TRUE,lty=2,col="blue")
curve(expr=pi*dnorm(x,mu1,sigma1)+(1-pi)*dnorm(x,mu2,sigma2),lwd=2,col="black",add=TRUE)

which gives the following plot:

densitiescompared

The first density has a higher peak, the second density is shifted to the left and has a lower peak, higher variance.

Are my calculations and interpretations correct?

$\endgroup$
  • 2
    $\begingroup$ You could speed up the calculation of the quantile by about a thousand fold by using a root-finding routine. $\endgroup$ – whuber Mar 29 '13 at 16:42
  • 2
    $\begingroup$ "* the second density is shifted to the left*"... I'm pretty sure that being shifted that way $\rightarrow$ is 'to the right' $\endgroup$ – Glen_b Mar 30 '13 at 1:16
  • $\begingroup$ @Glen_b yes, you are right. Any other comments are welcome, also: Is this correct what I am doing? Can I do this? I think yes? $\endgroup$ – Stat Tistician Mar 30 '13 at 8:21
  • 3
    $\begingroup$ The interpretations look okay to me. $\endgroup$ – Glen_b Mar 30 '13 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.