Table of Contents
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
.
1 2 3 |
val n = 3 val p = 0.5 val m = 10 |
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.
1 2 3 4 5 |
fun exec(n: Int, p: Double): Int { var result = 0 for (i in 1..n) if (Math.random() >= p) result++; return result } |
Define the method to calculate the mean value of \(m\) trials. This correspontds to \(Y\) value.
1 2 3 4 5 |
fun exec(m: Int, n: Int, p: Double): Double { var result = 0.0 for (i in 1..m) result += exec(n, p) return result / m } |
Calculate \(Y\) 10,000 times and calculate the expectation and the variance.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
val timeCount = 100000 var mean = 0.0 var variance = 0.0 repeat(timeCount) { val result = exec(m, n, p) mean += result variance += Math.pow(exec(m, n, p) - 1.5, 2.0) } // np = 1.5 println(mean / timeCount) // np(1-p)/m = 0.075 println(variance / timeCount) |
以上をまとめると次のようになります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.improve_future.sample object Main { @JvmStatic fun main(args: Array<String>) { val n = 3 val p = 0.5 val m = 10 val timeCount = 100000 var mean = 0.0 var variance = 0.0 repeat(timeCount) { val result = exec(m, n, p) mean += result variance += Math.pow(exec(m, n, p) - 1.5, 2.0) } // np = 1.5 print("Expectation: ") println(mean / timeCount) // np(1-p)/m = 0.075 print("Variance : ") println(variance / timeCount) } fun exec(n: Int, p: Double): Int { var result = 0 for (i in 1..n) if (Math.random() >= p) result++; return result } fun exec(m: Int, n: Int, p: Double): Double { var result = 0.0 for (i in 1..m) result += exec(n, p) return result / m } } |
The code outputs as follows, and you can see \(E(Y)\) and \(V(Y)\) are as we reviewed before.
1 2 |
Expectation: 1.4999001999999781 Variance : 0.00756705900000417 |