Skip to content

Commit

Permalink
add objectivity example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Burn authored and Ryan Burn committed Mar 29, 2024
1 parent be8261e commit cb85df1
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions example/14-p-value-objectivity.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d94dbb54-d25a-41dc-a62d-f6b69ac62bec",
"metadata": {},
"source": [
"Reproduce an example from James Berger and Donald Berry's paper\n",
"[Statistical Analysis and the Illusion of Objectivity](https://si.biostat.washington.edu/sites/default/files/modules/BergerBerry.pdf).\n",
"\n",
"The example shows how a p-value depends on not just the data but also the intentions of the investigator."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "29ca528a-12ef-4a2a-a42b-4b9123ff5c4a",
"metadata": {},
"outputs": [],
"source": [
"# Imagine an investigator is studying a coin for bias. The investigator flips \n",
"# the coin 17 time and observes 13 heads.\n",
"n = 17\n",
"num_heads = 13"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "81ce0295-f667-4092-86a1-667f770995f1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p-value 1: 0.049041748046875\n"
]
}
],
"source": [
"# Here's the p-value the investigator would compute, if their intention\n",
"# with their experiment was to flip the coin 17 times and then do their analysis\n",
"import math\n",
"def p_value1(n, k):\n",
" res = 0\n",
" k = min(k, n - k)\n",
" for j in range(k+1):\n",
" t = math.comb(n, j) * 0.5 ** n\n",
" if j != n - j:\n",
" t *= 2\n",
" res += t\n",
" return res\n",
"print('p-value 1:', p_value1(n, num_heads))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e264e4af-905a-4f45-a5b4-809268c3b787",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p-value 2: 0.021270751953125\n"
]
}
],
"source": [
"# Here's the p-value the investigator would compute if their intention\n",
"# was to keep flipping the coin until the got at least 4 heads and 4 tails\n",
"def p_value2(n, k):\n",
" t = 0\n",
" k = min(k, n - k)\n",
" for j in range(2 * k, n):\n",
" t += 2 * math.comb(j - 1, k - 1) * 0.5 ** j\n",
" return 1 - t\n",
"print('p-value 2:', p_value2(n, num_heads))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "304ebda6-ee8e-4e48-9af9-9855972193f2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit cb85df1

Please sign in to comment.