【发布时间】:2016-05-05 15:42:28
【问题描述】:
假设我有一个序列 x= 1,3,3,1,2,1,4,2,3,1,4,2,4,4,4,3,1,2,5,1 和它有五个状态1 3 2 4 5。我必须通过这个等式在MATLAB中获得转移概率矩阵,概率=(观察对x(t)和x(t + 1)的数量,x(t)处于状态i和 x(t+1) 在状态 j)/(观察对的数量 x(t) 和 x(t+1),其中 x(t) 在状态 i 和 x(t+1) 在任何一个状态1......秒)。 我尝试了这段代码,但它给出了错误
x=[1 3 3 1 2 1 4 2 3 1 4 2 4 4 4 3 1 2 5 1]
n = length(x)-1
p = zeros(5,5)
for t = 1:n
if x(t)=x(t+1);
a(t)=count (x(t)=x(t+1)) % Here i am trying to count how many number of times pair of that states occur in sequence.
q(t)=sum(x==x(t)) % (Here i am trying to count Number of observation pairs x(t) & x(t+1), with x(t) in state i and x(t+1) in any one of the states 1......s)
end
for i=1:5
p(i, :) = a(t)/q(t)
end
我手动计算的转移概率矩阵如下
1 3 2 4 5
1 0 1/5 2/5 2/5 0
3 3/4 1/4 0 0 0
2 1/4 1/4 0 1/4 1/4
4 0 1/5 2/5 2/5 0
5 1 0 0 0 0
【问题讨论】:
-
我想你可以自己解决这个问题;这样你会学到更多。用每对 i,j 的出现次数填充 p 矩阵,然后将行除以行和以获得概率。
标签: matlab matrix markov-chains