Please, check the comments above. Here is a quick implementation in R.
x <- c(1,2,1,1,3,4,4,1,2,4,1,4,3,4,4,4,3,1,3,2,3,3,3,4,2,2,3)
p <- matrix(nrow = 4, ncol = 4, 0)
for (t in 1:(length(x) - 1)) p[x[t], x[t + 1]] <- p[x[t], x[t + 1]] + 1
for (i in 1:4) p[i, ] <- p[i, ] / sum(p[i, ])
Results:
> p
[,1] [,2] [,3] [,4]
[1,] 0.1666667 0.3333333 0.3333333 0.1666667
[2,] 0.2000000 0.2000000 0.4000000 0.2000000
[3,] 0.1428571 0.1428571 0.2857143 0.4285714
[4,] 0.2500000 0.1250000 0.2500000 0.3750000
A (probably dumb) implementation in MATLAB (which I have never used, so I don't know if this is going to work. I've just googled "declare vector matrix MATLAB" to get the syntax):
x = [ 1, 2, 1, 1, 3, 4, 4, 1, 2, 4, 1, 4, 3, 4, 4, 4, 3, 1, 3, 2, 3, 3, 3, 4, 2, 2, 3 ]
n = length(x) - 1
p = zeros(4,4)
for t = 1:n
p(x(t), x(t + 1)) = p(x(t), x(t + 1)) + 1
end
for i = 1:4
p(i, :) = p(i, :) / sum(p(i, :))
end
[...] but by having the initial Prob. of each state (P(t=1)), and Transition prob. i can calculate prob. of each state in any given time [...]
That sounds correct. You should be able to use induction and conditioning to deduce this. It has been a while since I've done anything with Markov Chains, so apologies in advance for any poor notation (also my first post ever :D ). The base case for induction should look something like this.
Suppose that we know the initial probabilities as above. Then we have :
$P_1[2] = P(X_2=1|X_1=1)P(X_1=1)+P(X_2=1|X_1=2)P(X_1=2)= S_1\epsilon_{11}[1]+S_2\epsilon_{21}[1],$
which are all known quantities.
I think a simple induction argument should finish it from there.
Best Answer
I am not immediately aware of a "built-in" function (e.g., in
base
or similar), but we can do this very easily and efficiently in a couple of lines of code.Here is a function that takes a matrix (not a data frame) as an input and produces either the transition counts (
prob=FALSE
) or, by default (prob=TRUE
), the estimated transition probabilities.If you need to call it on a data frame you can always do
If you're looking for some third-party package, then Rseek or the R search site may provide additional resources.