Test in the Expectation and the Variance in a Sampling Distribution


Test the expectation and the variance in a sampling distribution with a program.

The expectation and variance in sampling distribution

Suppose XX as a random variable in a distribution, and X1,,XmX1,,Xm as independent samples of XX, define YY as follows.

Y=1mmk=1XkY=1mmk=1Xk

Then, E(Y)E(Y), V(Y)V(Y) is written as follows.

E(Y)=E(X)V(Y)=1mE(X)

Explanation

Let’s review the calculation of the expectation and the variance in a sampling distribution.

Xi are independent, then E(Y)=1mmk=1E(Xk)=1mE(X) .

The variance meets V(A+B)=V(A)+V(B) when the two random variables, A and B, are independent. Therefour, V(Y)=mV(1mX)=1mV(X) .

Test in Binomial distribution

Test the real value in Binomial distribution with computer.

When XBinomial(n,p), E(X)=np, V(X)=np(1p) .

Suppose Y as a mean value of m independent samples. Then, E(Y)=1mnp and V(Y)=1mnp(1p) .

Now, let’s suppose n=3, p=0.5, m=10 and calculate them. E(X)=E(Y)=0.5, V(X)=0.75, V(Y)=0.075 .

Code in Kotlin

At first, define n, p and m.

Build a method to simulate each X value for each Bernoulli trial. To generate random value which is not less than 0 and less than 1 with Math.random, compare it to p, return 1 if it is not less than p. Do it n times.

Define the method to calculate the mean value of m trials. This correspontds to Y value.

Calculate Y 10,000 times and calculate the expectation and the variance.

以上をまとめると次のようになります。

The code outputs as follows, and you can see E(Y) and V(Y) are as we reviewed before.