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 \(X\) as a random variable in a distribution, and \(X_1 , \cdots , X_m\) as independent samples of \(X\), define \(Y\) as follows.

\[ Y = \frac{1}{m} \sum _{k=1}^{m} X_k\]

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

\begin{eqnarray*} E(Y) & = & E(X) \\ V(Y) & = & \frac{1}{m} E(X) \end{eqnarray*}

Explanation

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

\(X_i\) are independent, then \(E(Y) = \frac{1}{m} \sum_{k=1}^{m} E(X_k) = \frac{1}{m} E(X)\) .

The variance meets \(V(A+B) = V(A)+V(B)\) when the two random variables, \(A\) and \(B\), are independent. Therefour, \(V(Y) = m V \left( \frac{1}{m} X \right) = \frac{1}{m} V(X) \) .

Test in Binomial distribution

Test the real value in Binomial distribution with computer.

When \(X \sim \mathrm{Binomial}(n, p)\), \(E(X) = np\), \(V(X) = np(1-p)\) .

Suppose \(Y\) as a mean value of \(m\) independent samples. Then, \(E(Y) = \frac{1}{m} np\) and \(V(Y) = \frac{1}{m} np(1-p) \) .

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.