Exercise - Variance sample size dependence
Table of Contents
Introduction
In statistics it is always helpful to have a fairly large sample size and a great number of experiments to sharpen your statistical predictions. The following simple coin toss experiment will show how sample size and the number of experiments you do decrease the variance.
Requirements
Knowledge
To complete this exercise notebook you should possess knowledge about the following topics:
- Variance
Python Modules
import matplotlib.pyplot as plt
import numpy as np
import scipy as sc
import scipy.stats as st
%matplotlib inline
np.random.seed(42)
Coin toss experiment
To simulate a coin toss we use the binomal distribution with probability 0.5 for a fair coin. The following expample shows 30 experiments with 10 throws each. You will see that often you get a number which is not equal to 5, which means that this specific experiment would suggest that the coin is not fair. So in order to test if the coin is fair or not you have to do some more throws and experiments and take the mean of all outcomes.
print()
print('Example')
print('--------')
print('Do 30 experiments with 10 throws each and count the number of successes')
print()
CoinExp = np.random.binomial(10,0.5,30)
print(CoinExp)
Exercise
Complete the function cointossexp which simulates a coin toss experiment for a given setup. The function takes three parameters:
- successprob - The probability of heads (0.5)
- thrownumb - The number of throws in each experiments
- expnumb - The number of experiments
Your functon should create three separate plots. On the x-axis, plot the number of throws. On the y-axis, plot:
1. the number of heads
2. the variance
3. the variance using plt.loglog
scaling.
The values for number of heads and variance are to be taken as the mean across all experiments.
Comment on your results.
Hint: What meaning does the slope of your loglog plot have?
Your plots may look like the following:
def cointossexp(successprob,thrownumb,expnumb):
p = successprob #set success probability (e.x. head)
n = np.arange(1,thrownumb,1) #number n of throws
N = expnumb #number N of experiments with n throws
StrN = str(expnumb) #used for Plot title
CoinToss = []
MeanHeads = []
VarHeads = []
#insert your code here
return n,MeanHeads,VarHeads
MaxThrows = 100
exp1 = cointossexp(0.5,MaxThrows,2)
exp2 = cointossexp(0.5,MaxThrows,3)
exp3 = cointossexp(0.5,MaxThrows,8)
exp5 = cointossexp(0.5,MaxThrows,20)
exp6 = cointossexp(0.5,MaxThrows,30)
exp7 = cointossexp(0.5,MaxThrows,50)
Licenses
Notebook License (CC-BY-SA 4.0)
The following license applies to the complete notebook, including code cells. It does however not apply to any referenced external media (e.g., images).
exercise-variance-sample-size-dependence-new
by Oliver Fischer
is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://gitlab.com/deep.TEACHING.
Code License (MIT)
The following license only applies to code cells of the notebook.
Copyright 2018 Oliver Fischer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.