简体   繁体   中英

How to calculate the transition probability matrix of a second order Markov Chain

I have data like in form of this

Broker.Position

IP BP SP IP IP ..

I would like to calculate the second order transition matrix like in this form

             BP IP SP

BPBP

SPSP

IPIP

BPSP

SPBP

IPSP

SPIP

BPIP

IPBP

You can use embed to generate the pairs of consecutive transitions, table to count them, apply to compute the totals and convert the counts to probabilities, dcast and melt to convert the array to a data.frame.

# Sample data
states <- sample(LETTERS[1:3], 1e5, replace=TRUE)

# Pairs of transitions
d <- embed( states, 3 )
colnames(d) <- c("today", "yesterday", "day before yesterday")
head(d)

# Count the transitions
counts <- table( as.data.frame( d ) )

# Divide by the total number of transitions, to have probabilities
probabilities <- counts
probabilities[] <- as.vector(counts) / rep( as.vector(apply( counts, 2:3, sum )), each=dim(counts)[1] )

# Check that the probabilities sum up to 1
apply( probabilities, 2:3, sum )

# Convert the 3-dimensional array to a data.frame
library(reshape2)
dcast( melt( probabilities ), yesterday + `day before yesterday` ~ today )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM