about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/KPMstats/est_transmat.m
blob: 7654d9ab57f45f9217060bbabc8e744f3a431cf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function [A,C] = est_transmat(seq)
% ESTIMATE_TRANSMAT Max likelihood of a Markov chain transition matrix
% [A,C] = estimate_transmat(seq)
%
% seq is a vector of positive integers
%
% e.g., seq = [1 2 1 2 3], C(1,2)=2, C(2,1)=1, C(2,3)=1, so
% A(1,:)=[0 1 0], A(2,:) = [0.5 0 0.5],
% all other entries are 0

% Use a trick with sparse matrices to count the number of each transition.
% From http://www.mathworks.com/company/newsletter/may03/dna.shtml

C = full(sparse(seq(1:end-1), seq(2:end),1));
A = mk_stochastic(C);