← Simulation & Statistics

Simulation & Statistics · Part 6 of 12

Discrete Variables

In order to verify the distribution's characteristics inside of the statistical processing of discrete random variables RV we have the following distribution:

Table of a discrete distribution: values x1, x2, …, xm with probabilities p1, p2, …, pm

The variance and expected value are calculated with the following formulas:

E x = Σ pi xi, and Var x = D x = Σ pi (xi − E x)² = Σ pi xi² − (E x)²: the expected value and variance

With this set is possible to evaluate the empiric distribution, as we already cover with a Group of events.

p̂i = ni / N, the empirical probability: how often value i came up out of N trials

By using this values we can calculate the empiric expected value and relative variance of values of Xi with the following formulas:

Ê x = Σ p̂i xi and D̂ x = Σ p̂i xi² − (Ê x)²: the empirical expected value and variance

With the resulting values we can verify the empiric results with the theoretical ones based on absolute errors.

Absolute errors: ΔE = |Ê x − E x| and ΔD = |D̂ x − D x|

and relative errors

Relative errors: δE = ΔE / |E x| and δD = ΔD / |D x|

Generally relative errors are presented by percentage, so the bigger the expected value's result is and the variance as well, we can expect that the bigger the simulation's error will be. Depending on the experiment, values of 1%, 5% or 10% are acceptable, and if expected value's the theoretical values and the variance are not known we use the empirical ones as approximation.

If the margin of error is smaller is possible to calculate the distribution with Chi-squared test.

Chi-squared statistic X̂²N = Σ ni² / (N pi) − N, comparing observed counts with the expected ones

By fixing the value of alfa (∝) that will define the precision of the test method, given by the following hypothesis:

Reject the hypothesis that the empirical distribution fits the theory if X̂²N > χ²(1−α, m−1), with a table of chi-squared critical values

This way the empirical estimation of the distribution corresponds to the theoretical one in a big number of events N, in which is possible to compare the values and reach a conclusion.

Result

The simulation window before running: four probabilities entered (0.264, 0.128, 0.228, 0.207), 100 experiments, and an empty chart

After Calculate: a bar chart of the five probabilities, with average 2.897 (error 4%) and variance 2.072 (error 3%)

Implementation

C#
Random rand = new Random();
int i = 0;
double sum, p5 = 0;

int n = int.Parse(textBox6.Text) //Experiments

double p1 = double.Parse(textBox1.Text); // Prob 1
double p2 = double.Parse(textBox2.Text); // Prob 2
double p3 = double.Parse(textBox3.Text); // Prob 3
double p4 = double.Parse(textBox4.Text); // Prob 4

sum = p1 + p2 + p3 + p4;
p5 = 1 - sum;

if (p5 < 0) p5 = 0;
textBox5.Text = p5.ToString("0.000");

double[] experiments = new double[5];
double[] frequency = new double[5];

this.chart1.Series["Series1"].Points.AddXY("1", p1);
this.chart1.Series["Series1"].Points.AddXY("2", p2);
this.chart1.Series["Series1"].Points.AddXY("3", p3);
this.chart1.Series["Series1"].Points.AddXY("4", p4);
this.chart1.Series["Series1"].Points.AddXY("5", p5);

//Expected Value - µ
double xp1 = 1 * p1;
double xp2 = 2 * p2;
double xp3 = 3 * p3;
double xp4 = 4 * p4;
double xp5 = 5 * p5;
double miu = xp1 + xp2 + xp3 + xp4 + xp5;

//Variance - µ
double x2p1 = (1*1) * p1;
double x2p2 = (2*2) * p2;
double x2p3 = (3*3) * p3;
double x2p4 = (4*4) * p4;
double x2p5 = (5*5) * p5;
double variance = x2p1 + x2p2 + x2p3 + x2p4 + x2p5;

variance -= (miu * miu);

experiments[0] = 0;
experiments[1] = 0;
experiments[2] = 0;
experiments[3] = 0;
experiments[4] = 0;

while (i < n)
{
    int r = rand.Next(1, 6);

    if (r == 1) experiments[0]++;
    else if (r == 2) experiments[1]++;
    else if (r == 3) experiments[2]++;
    else if (r == 4) experiments[3]++;
    else if (r == 5) experiments[4]++;

    i++;
}

i = 0;
foreach (var item in experiments)
{
    frequency[i++] = item / n;
}

double e_miu = 1 * frequency[0] +
               2 * frequency[1] +
               3 * frequency[2] +
               4 * frequency[3] +
               5 * frequency[4];

double e_variance = (1 * 1) * frequency[0] +
                    (2 * 2) * frequency[1] +
                    (3 * 3) * frequency[2] +
                    (4 * 4) * frequency[3] +
                    (5 * 5) * frequency[4];

e_variance -= (e_miu * e_miu);

//Errors
double miuError = Math.Abs(e_miu - miu) / Math.Abs(miu);
double varianceError = Math.Abs(e_variance - variance) / Math.Abs(variance);