本文介绍了具有不同状态序列长度的MATLAB中的马尔可夫链转移矩阵估计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MATLAB中建立马尔可夫链的转移矩阵;我有几个不同的观察序列(所有长度都不同),我需要使用这些观察序列来生成转换矩阵.

I'm trying to build the transition matrix for a Markov Chain in MATLAB; I have several different observation sequences (all of varying lengths) and I need to generate the transition matrix using those.

构造多阶马尔可夫链转移矩阵Matlab中的向我展示了如何使用单个观察序列构建过渡矩阵.

Constructing a multi-order Markov chain transition matrix in Matlab shows me how to build a transition matrix with a single observation sequence.

如何使用不同长度的观测值构造一个?一个示例可以是一个序列是1,2,3,4,而另一个序列是4,5,6.有什么方法可以不必循环遍历所有序列和计算计数吗?

How can I construct one using observations of different length? One example can be that one sequence is 1,2,3,4 and another is 4,5,6. Is there any way to do this without having to for loop through all sequences and computing counts?

推荐答案

因此对于Markov链,我假设您只对状态转换感兴趣.您可以将所有状态转换分组到单个Nx2矩阵中,然后计算行出现的次数.

So for Markov chains, I assume you're only interested in the state transitions. You could group all state transitions into a single Nx2 matrix and then count the number of times a row appears.

在此示例中,我使用了三个长度分别为4、3和3的观察值.我可以使用cellfun通过以下方式将所有状态转换归为一个矩阵:

For this example I'm using three observations of length 4, 3, and 3. I can use cellfun to group all the state transitions together in a single matrix in the following way:

obs = cell(1, 3);

obs(1) = {[1 2 3 4]};
obs(2) = {[4 5 6]};
obs(3) = {[3 4 5]};

transitions = cellfun(@(x)([x(1:length(x)-1); x(2:length(x))]), obs, 'UniformOutput', false);

alltransitions = cell2mat(transitions)';

这给了我观察到的过渡(1->2, 2->3, 3->4 ...):

Which gives me my observed transitions (1->2, 2->3, 3->4 ...):

alltransitions =

     1     2
     2     3
     3     4
     4     5
     5     6
     3     4
     4     5

要设置转换矩阵,您可以采用此处列出的建议,并计算所有转换的行数:

To set up the transition matrix, you could take the advice listed here, and count the rows of all of your transitions:

http://www.mathworks.it/matlabcentral/answers/75009-i-ve-a-matrix-of-6x4-并且我想计算出矩阵多次出现行的次数

[uniqueTransitions, ~, i]=unique(alltransitions,'rows','stable');
v=arrayfun(@(x) sum(i==x),1:size(uniqueTransitions,1))';
p = v/sum(v);

我的向量p包含我的转移概率,因此我可以继续构建稀疏矩阵

My vector p contains my transition probability, so I can then go ahead and construct a sparse matrix

transitionMatrix = sparse(uniqueTransitions(:,1), uniqueTransitions(:,2), p, 6,6)

其结果是:

transitionMatrix =

   (1,2)       0.1429
   (2,3)       0.1429
   (3,4)       0.2857
   (4,5)       0.2857
   (5,6)       0.1429

这篇关于具有不同状态序列长度的MATLAB中的马尔可夫链转移矩阵估计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 20:33