Optimizing Mathematica Code
Here is an optimized version of your function
ForwardProcedure[TM_, EM_, P_, N_, M_, Seq_] := Module[
{DP, DPPart},
DP = ConstantArray[0, {N, Length@Seq}];
DP[[All, 1]] = P*EM[[All, Part[Seq, 1]]];
Do[
DPPart = DP[[All, j - 1]].TM;
DP[[All, j]] = DPPart*EM[[All, Seq[[j]]]];
DP[[All, j]] = DP[[All, j]]*1/Total[DP[[All, j]]];
, {j, 2, Length@Seq}
];
DP]
The general idea is to avoid explicit looping constructs, and try to have operations work on lists/arrays as a whole. On my machine this accelerated evaluation by about a factor 3. Feel free to ask about some of the changes.
Update: A version of Simon Woods's code using compile:
norm = Compile[{{x, _Real, 1}}, x/Total[x]]
ForwardProcedure =
Compile[
{
{TM, _Real, 2},
{EM, _Real, 2},
{P, _Real, 1},
{n, _Integer},
{m2, _Integer},
{Seq, _Integer, 1}
},
With[
{m = Transpose[EM][[Seq]]},
Transpose@FoldList[norm[#1.TM #2] &, P First[m], Rest[m]]
],
CompilationOptions -> {"InlineCompiledFunctions" -> True}
]
A version of mmeent's code in a more functional style:
norm[x_] := x / Total[x]
ForwardProcedure[TM_, EM_, P_, N_, M_, Seq_] := Module[{DP, m},
m = Transpose[EM][[Seq]];
DP = FoldList[norm[#1.TM #2] &, P First[m], Rest[m]];
Transpose @ DP]