[Depmix-commits] r320 - trunk/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jan 20 12:58:15 CET 2010


Author: ingmarvisser
Date: 2010-01-20 12:58:14 +0100 (Wed, 20 Jan 2010)
New Revision: 320

Added:
   trunk/R/stationary.R
Log:
Function for computing the stationary distribution of a transition probability matrix.

Added: trunk/R/stationary.R
===================================================================
--- trunk/R/stationary.R	                        (rev 0)
+++ trunk/R/stationary.R	2010-01-20 11:58:14 UTC (rev 320)
@@ -0,0 +1,15 @@
+# 
+# compute the stationary distribution of a transition matrix
+# 
+# Ingmar Visser, 09-2009
+# 
+
+stationary <- function(tpm) {
+	if(!(nrow(tpm)==ncol(tpm))) stop("stationary distribution only defined for square matrices.")
+	nr <- nrow(tpm)
+	rs <- all.equal(rowSums(tpm),rep(1,nr))
+	if(!(rs==TRUE)) stop("rows of the transition matrix should sum to unity")
+	e1 <- as.real(eigen(t(tpm))$vectors[,1])
+	e1 <- e1/sum(e1)
+	return(e1)
+}


Property changes on: trunk/R/stationary.R
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the depmix-commits mailing list