Jump to content

File:Em old faithful.gif

Page contents not supported in other languages.
This is a file from the Wikimedia Commons
From Wikipedia, the free encyclopedia

Em_old_faithful.gif (360 × 360 pixels, file size: 272 KB, MIME type: image/gif, looped, 30 frames, 6.0 s)

Summary

Description An animation of the EM algorithm to a 2 component Gaussian mixture model on the Old Faithful dataset
Date
Source Own work
Author 3mta3 (talk) 16:55, 23 March 2009 (UTC)

Created in R with code below (released under same license as image), combined into animated gif using ImageMagick.

There should be a way to make this file smaller (e.g. using less colours or an animated PNG). If you can, feel free to upload a new copy.

#load library for multivariate normal
library(mvtnorm)
#load Old Faithful data frame
data(faithful)

#setup grid for plotting
xpts <- seq(from=1,to=6,length.out=100)
ypts <- seq(from=40,to=100,length.out=100)

#initial parameter estimates (chosen to be deliberately bad)
theta <- list(
             tau=c(0.5,0.5),
             mu1=c(2.8,75),
             mu2=c(3.6,58),
             sigma1=matrix(c(0.8,7,7,70),ncol=2),
             sigma2=matrix(c(0.8,7,7,70),ncol=2)
             )

#E step: calculates conditional probabilities for latent variables
E.step <- function(theta)
 t(apply(cbind(
     theta$tau[1] * dmvnorm(faithful,mean=theta$mu1,sigma=theta$sigma1),
     theta$tau[2] * dmvnorm(faithful,mean=theta$mu2,sigma=theta$sigma2)
     ),1,function(x) x/sum(x)))
#M step: calculates the parameter estimates which maximise Q
M.step <- function(T) list(
 tau= apply(T,2,mean),
 mu1= apply(faithful,2,weighted.mean,T[,1]),
 mu2= apply(faithful,2,weighted.mean,T[,2]),
 sigma1= cov.wt(faithful,T[,1])$cov,
 sigma2= cov.wt(faithful,T[,2])$cov)

#function to plot current data
plot.em <- function(theta){
 mixture.contour <- outer(xpts,ypts,function(x,y) {
   theta$tau[1]*dmvnorm(cbind(x,y),mean=theta$mu1,sigma=theta$sigma1) + theta$tau[2]*dmvnorm(cbind(x,y),mean=theta$mu2,sigma=theta$sigma2)
   })
 contour(xpts,ypts,mixture.contour,nlevels=5,drawlabel=FALSE,col="red",xlab="Eruption time (mins)",ylab="Waiting time (mins)",main="Waiting time vs Eruption time of the Old Faithful geyser")
 points(faithful)
}

#plot initial contours
iter <- 1
png(filename=paste("em",formatC(iter,width=4,flag="0"),".png",sep=""))
plot.em(theta)
dev.off()

#run EM and plot
for (iter in 2:30){
 T <- E.step(theta)
 theta <- M.step(T)
 png(filename=paste("em",formatC(iter,width=4,flag="0"),".png",sep=""))
 plot.em(theta)
 dev.off()
}

Licensing

I, the copyright holder of this work, hereby publish it under the following licenses:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.
You may select the license of your choice.
 
This chart was created with R.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

23 March 2009

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current17:10, 23 March 2009Thumbnail for version as of 17:10, 23 March 2009360 × 360 (272 KB)3mta3A bit smaller
16:55, 23 March 2009Thumbnail for version as of 16:55, 23 March 2009480 × 480 (365 KB)3mta3{{Information |Description=An animation of the EM algorithm to a 2 component Gaussian mixture model on the [http://stat.ethz.ch/R-manual/R-patched/library/datasets/html/faithful.html Old Faithful dataset] |Source=self-made |Date=2009-03-23 |Author= ~~~~ |

The following 2 pages use this file:

Global file usage

The following other wikis use this file: