I recently discovered the Markov process.
To simplify my work, I use the following code taken from the DiscreteMarkovProcess
documentation:
K = DiscreteMarkovProcess[{1, 0, 0}, ({
{0, 1/2, 1/2},
{1/2, 0, 1/2},
{1/2, 1/2, 0}
})
];
data = RandomFunction[K, {0, 10}]
However, I am facing a case where the code does not seem to work.
In fact, when I use the following matrix RandomFunction
does not return a result:
{{0.666667, 0.333333, 0., 0., 0., 0., 0., 0., 0.}, {0., 0., 0., 1.,
0., 0., 0., 0., 0.}, {0.0263158, 0., 0.315789, 0.105263, 0.526316,
0.0263158, 0., 0., 0.}, {0., 0., 0., 0., 0.655172, 0.344828, 0., 0.,
0.}, {0., 0., 0.0481928, 0.060241, 0.678715, 0.176707, 0., 0.,
0.0361446}, {0., 0., 0.210526, 0.105263, 0.684211, 0., 0., 0.,
0.}, {0., 0., 0., 1., 0., 0., 0., 0., 0.}, {0., 0., 1., 0., 0., 0.,
0., 0., 0.}, {0., 0., 0., 0., 0.181818, 0.181818, 0.272727,
0.181818, 0.181818}}
To investigate the problem I wrote the following code:
TheV = RandomReal[{0, 1}, {4, 4}];
no = Total /@ TheV;
TheVno = Table[TheV[[i]]/no[[i]], {i, 1, Length@no, 1}];
K = DiscreteMarkovProcess[1, TheVno];
data = RandomFunctionK, {0, 10}]
This code seems to show that in some cases RandomFunction
does not work. What is happening in these cases ? Do I need to replace the command RandomFunction
with something else ?
Edit n°1
To better investigate the problem I wrote the following code :
MarkovTest[x_, y_] :=
Block[
{step1, step2, step31, step311, step32, step322, step4,
step41, step5, step51, step6, step61, res},
step1 = Table[
RandomReal[{0, 1}, {BlockRandom[RandomInteger[{1, 18}]],
RandomInteger[{1, 18}]}],
{i, 1, x, 1}
];
step2 = Table[
Total /@ step1[[i]],
{i, 1, x, 1}
];
step31 = Table[
N[step1[[i, j]]/step2[[i, j]], y],
{i, 1, Length@step1,1}, {j, 1, Length@step1[[i]],1}
];
step311 = Table[
Rationalize@(step1[[i, j]]/step2[[i, j]]),
{i, 1, Length@step1,1}, {j, 1, Length@step1[[i]],1}
];
step32 = Table[
DiscreteMarkovProcess[1, step31[[i]]],
{i, 1, x, 1}
];
step322 = Table[
DiscreteMarkovProcess[1, step311[[i]]],
{i, 1, x, 1}
];
step4 = Table[
RandomFunction[step32[[i]], {0, 5}],
{i, 1, x, 1}
];
step41 = Table[
RandomFunction[step322[[i]], {0, 5}],
{i, 1, x, 1}
];
step5 = Head /@ step4;
step51 = Head /@ step41;
step6 = Count[step5, TemporalData];
step61 = Count[step51, TemporalData];
res = {step6, step61}
];
With this function, you can see the different cases where RandomFunction
does not work.
As mentioned by ubpdqn the main problem seems to be the arithmetic precision.
I continue to work on the problem.
PS : Obviously, I checked that the given matrix is stochastic....