Estimating transition probabilities in a Markov chain with maximum likelihood reduces to counting the transitions from a given state to another divided by the total number of transitions. Since the QuantEcon package in Julia seems to lack a built-in routine to do this, would someone be able to recommend an efficient way to write this from scratch?
Playing around with it, it seems this Julia code estimates the Markov chain using proportions:
function estimate_markov(X::Vector, nstates::Int64)
P = zeros(nstates, nstates)
n = length(X) - 1
for t = 1:n
# add one each count is observed
P[X[t], X[t+1]] = P[X[t], X[t+1]] + 1
end
for i = 1:nstates
# divide by total number of counts
P[i, :] .= P[i, :]/sum(P[i, :])
end
return P
end
Appreciate any feedback if there are any errors or significant ways to improve form/efficiency.