如何获得矩阵中的转移概率矩阵? [英] How to obtain transition probability matrix in matrix?
问题描述
假设我有一个序列x = 1,3,3,1,2,1,4,2,3,1,4,2,4,4,4,3,1,2,5,1和它具有5个状态1 3 2 45.我必须通过此方程在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 ...... s). 我尝试过此代码,但它给出了错误
Suppose I have a sequence x= 1,3,3,1,2,1,4,2,3,1,4,2,4,4,4,3,1,2,5,1 and it has five states 1 3 2 4 5. I have to obtain transition probability matrix in MATLAB by this equation, probability= (Number of observation pairs x(t) & x(t+1), with x(t) in state i and x(t+1) in state j)/(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). I tried by this code but it giving error
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
由我手动计算的转移概率矩阵如下
Transition probability matrix calculated manually by me as follows
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
推荐答案
由于已经有一段时间了,所以我认为现在对此提供答案是安全的.以下两种方法均不需要工具箱.假设具有
Since it has been a while, I think it is safe to provide an answer to this now. There is no toolbox required for either approach below. Assumes basic knowledge of a transition probability matrix of a Discrete Time Markov Chain (DTMC).
这两种方法都使用 unique()
函数来查找状态空间.请注意,顺序是不同的,例如您的[1 3 2 4 5]
与我的[1 2 3 4 5]
相比,但这不是限制性的问题.我将获取过渡计数与过渡概率分开了,以说明一些技巧.
Both approaches use the unique()
function to find the statespace. Note that the order is different, e.g. your [1 3 2 4 5]
vs. my [1 2 3 4 5]
but that isn't a limiting issue. I've separated getting the transition counts from the transition probabilities to illustrate some techniques.
方法1:矢量化方法
这种方法使用 unique()
和
Approach 1: Vectorized Approach
This approach uses the unique()
and accumarray()
functions.
% MATLAB 2018b
X =[1 3 3 1 2 1 4 2 3 1 4 2 4 4 4 3 1 2 5 1];
[u,~,n] = unique(X);
NumU = length(u); % Number of Unique Observations
Counts = accumarray([n(1:end-1),n(2:end)],1,[NumU,NumU]);
P = Counts./sum(Counts,2); % Probability transition matrix
验证:您可以验证sum(sum(Counts)) == length(X)-1
和P
的行总和为1(sum(P,2)
).
Verification: You can verify that sum(sum(Counts)) == length(X)-1
and the rows of P
sum to one (sum(P,2)
).
请注意,计数矩阵使用1步偏移量对转换进行计数.输出是NumU x NumU
数组,该数组按索引的形式表示过渡的数量,如n
-从unique()
的输出中给出的那样.
Notice that the counts matrix uses a 1-step offset to count the transitions. The output is a NumU x NumU
array of the number of transitions in terms of indices as given in the n
-output from unique()
.
方法2:单个for
循环
这是一种直接方法,可以使用状态空间的任何顺序(请参见下文).
Approach 2: Single for
loop
This is a direct approach that can use any ordering of the statespace (see below).
States = unique(X);
Counts = zeros(length(States));
for k = 2:length(X)
Counts(find(X(k-1) == States),find(X(k) == States)) = ...
Counts(find(X(k-1) == States),find(X(k) == States)) + 1;
end
P = Counts./sum(Counts,2); % Probability transition matrix
使用状态空间排序:如果将方法2 与States = [1 3 2 4 5];
结合使用,则结果概率转换矩阵P
与您手动计算的矩阵匹配.
Using your statespace ordering: If you use Approach 2 with States = [1 3 2 4 5];
, the resulting probability transition matrix, P
, matches the one you manually calculated.
这篇关于如何获得矩阵中的转移概率矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!