"If you have never used a Jupyter notebook before, don't worry!\n",
"\n",
"This is an interactive notebook that allows you evaluate the cells as you go along. To evaluate a cell, select it and hit the play button on the left or press \"shift+return\". "
"To do so, simply replace my data, i.e. the result of my coin flips, with yours and re-evaluate the cell. Remember that $F=0$ means tail and $F=1$ means head, so each time you flipped a head, enter a 1 and each time you had a tail, enter a 0.\n",
"\n",
" * The black dashed line indicates $F=0.5$ -- the fair coin.\n",
" * The blue solid curve is the calculated posterior probability.\n",
" * The blue vertical line indicates the maximum posterior probability of the fairness of your coin.\n",
"\n",
"Let's go!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# EVALUATE ME!\n",
"\n",
"# These are the results from my first coin flip.\n",
"# Simply replace my sequence of 0s and 1s with yours and find out if your coin is fair!\n",
"\n",
"my_results1 = [0,1,1,1,0,0,1,0,1,0]\n",
"\n",
"coin.calculate_fairness(my_results1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# EVALUATE ME!\n",
"\n",
"# These are the results from my second coin flip, after I attached some Blu Tack ton one side of the coin.\n",
"# Let's find out whether or not this biased my coin!\n",
"\n",
"my_results2 = [1,1,1,0,0,1,0,1,1,0]\n",
"\n",
"coin.calculate_fairness(my_results2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, while my 100 Yen coin is fair, it definitely was biased towards tails with a fairness of $F \\sim 0.6$ after I attached some Blu Tack. \n",
"\n",
"What about yours? You could also try different types of coins or vary the number of tosses."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Activity 2: Does the result depend on the number of coin flips?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have only done 10 tosses. Is this a large enough number of tosses aka \"sample size\" or do we need to do more flips to be sure? Could we falsely mistake a truly fair coin for a biased one or vice versa when only doing a small number of tosses? Let's see if the result changes as we vary the number of tosses -- simply evaluate the cell below.\n",
"\n",
"In this activity we assume that our coin is unbiased, i.e. the true fairness value of our coin $F$ is $0.5$. \n",
"You can repeat this experiment assuming that your coin is biased by passing the argument \"pheads=\" a value between 0 and 1 as in the second example. Test this option and note down what you observe.\n",
"\n",
" * The black dashed line indicates $F=0.5$ -- the fair coin.\n",
" * The blue solid curve is the calculated posterior probability for each number of tosses.\n",
" * The blue vertical line indicates the maximum posterior probability of the fairness of your coin."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# EVALUATE ME!\n",
"\n",
"# N in the corner of each plot indicates the number of coin flips\n",
"\n",
"coin.vary_tosses()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# EVALUATE ME!\n",
"\n",
"# N in the corner of each plot indicates the number of coin flips\n",
"\n",
"coin.vary_tosses(pheads=0.8)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Activity 3: What about the prior?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far we have used a uniform prior, i.e. we assumed a priori that all values of fairness $F \\in [0.1]$ are equally probable. How would our inference about the fairness of the coin have changed if we had chosen a different prior? Let's find out!\n",
"\n",
"Instead of a uniform prior, we could for example have chosen a normal (Gaussian) distribution peaked at $F=0.5$ with a variance of 0.05, which reflects our background information that most coins are fair.\n",
"\n",
"Does the result change?\n",
"\n",
" * The black dashed line indicates $F=0.5$ -- the fair coin.\n",
" * The blue solid curve is the calculated posterior probability for each number of tosses.\n",
" * The blue vertical line indicates the maximum posterior probability of the fairness of your coin."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# EVALUATE ME!\n",
"\n",
"# Using the measurements from your coin flip (see Activity 1), \n",
"# let's find out how fair your coin is if we assume a Gaussian prior instead of a uniform one!\n",
"Is the outcome the same as in Activity 1 or do you get a different result? \n",
"\n",
"Previously, we have already seen that the number of tosses $N$ plays an important role and that our results may not give the true answer if our sample size is too small. So let's increase the number of tosses as we did in Activity 2 but let's now use the Gaussian prior. Do you observe any differences?\n",
"\n",
" * The black dashed line indicates $F=0.5$ -- the fair coin.\n",
" * The blue solid curve is the calculated posterior probability assuming a uniform prior on $F$.\n",
" * The red dashed curve is the calculated posterior probability assuming a Gaussian prior peaked at $F=0.5$ on $F$.\n",
" * The vertical lines indicate the maximum posterior probabilities for the corresponding posterior distributions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's see what we get if our true coin is unbiased:\n",
"\n",
"coin.vary_tosses_Gaussian()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's see what happens if our coin is biased heavily towards tails.\n",
"# In our example the true rate of heads is 1 head in 10 tosses.\n",
"\n",
"coin.vary_tosses_Gaussian(pheads=0.1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you compare the results, what do you notice about the impact of the prior? Does one prior allow you to reach the correct result quicker (i.e. a lower number of tosses) than the other? Test this also for differently biased coins by varying \"pheads=\". "