File:Em old faithful.gif

From testwiki
Jump to navigation Jump to search
    Original file (360 × 360 pixels, file size: 272 KB, MIME type: image/gif, looped, 30 frames, 6.0 s)

    This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

    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

    278,374 byte

    360 pixel

    360 pixel

    image/gif

    87e6e615b8dd28e7b9af1129f7ade0821664c49c

    File history

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

    Date/TimeThumbnailDimensionsUserComment
    current18:10, 23 March 2009Thumbnail for version as of 18:10, 23 March 2009360 × 360 (272 KB)wikimediacommons>3mta3A bit smaller

    There are no pages that use this file.