I want to understand how to use the method of Monte Carlo to search for the limit probabilities of the some system S.
For example:
S0 S1 S2 S3
S0 0.1 0.9 0 0
S1 0 0.2 0.3 0.5
S2 0.2 0.1 0.5 0.2
S3 0.5 0 0.4 0.1
As I understand the method we need to generate a some number (x) and then compare the probability:
if x
0 <= x < 0.1 => S0 -> S0
0.1 <= x < 0.9 => S0 -> S1
0.9 <= x < 0.9 => S0 -> S2
0.9 <= x < 0.9 => S0 -> S3
0.9 <= x < 1 => S0 -> S4
when S4 - limit (border)
Similarly for other states.
Following this approach I can count the number of transitions:
static double[] SimpleMonte(double[][] a, int iter = 1)
{
var n = a.GetLength(0);
var p =
a
.Select(x => x.Select((_, i) => x.Take(i + 1).Sum()).ToArray())
.ToArray();
Random rand = new Random();
double[] X = new double[n];
for (int x = 0; x < n; x++)
{
double count = 0;
for (int i = 0; i < iter; i++)
{
int row = x;
bool notG = true;
Console.Write("{0} -> ", row);
while (notG)
{
var e = rand.NextDouble();
Console.Write("({0})", Math.Round(e, 2));
bool ch = false;
for (int j = 0; j < n - 1; j++)
{
if (p[row][j] <= e && e < p[row][j + 1])
{
row = j + 1;
ch = true;
break;
}
}
if (!ch)
notG = false;
else
{
Console.Write("{0} -> ", row);
count++;
}
}
Console.WriteLine();
}
X[x] = count / iter;
}
return X;
}
https://dotnetfiddle.net/nJF5sm
I will be glad to hear a hint on what to do to solve this problem.