rubythinking 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bc180980ba6248f4c8a4fd3b17de9f3160a7174c7ded011170631d4cbe1569e9
4
+ data.tar.gz: e2f798b0a2a1134ada1273a4a2a17bf3dd5172b0629533e40ce8a29f5ef988b4
5
+ SHA512:
6
+ metadata.gz: c130be20e595081fcfb7c9c4e73b587c747399594d668f84a5dcbbf191dccf5e239644b1600c9c9bef5f4f071cc4d043a6f7a41f2ccdf08a2929db274de4d674
7
+ data.tar.gz: cf9e1f535799c02f95445958b2549b8e7dcb7e070ad9afd3b2687b6efa77f8d0d142aa1ede00aa86acc15615824bdf6f59d40eab8799bd5e5a745c730e76a375
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .ipynb_checkpoints/*
3
+ notebooks/.ipynb_checkpoints
@@ -0,0 +1,242 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# 2H1: The panda twin problem\n",
8
+ "\n",
9
+ "_Suppose that there are two species of panda bear. Both are equally common in the wild and live in the same places. They look exactly alike and eat the same food, and there is yet no genetic assay capable of telling them apart. They differ however in their family sizes. Species A gives birth to twins 10% of the time, otherwise birthing a single infant. Species B birth twins 20% of the time, otherwise birthing singleton infants. Assume that these numbers are known with certainty, from many years of field research.\n",
10
+ "Now suppose you are managing a captive panda breeding program. You have a new female panda of unknown species, and the has just given birth to twins. What is the probability that her next birth will also be twins?_"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "markdown",
15
+ "metadata": {},
16
+ "source": [
17
+ "### Terminology\n",
18
+ "\n",
19
+ "Event $T_0$ means that the _first_ birth that a specific panda bear gives are twins. Consequently, event $T_i$ means that the $i$-th birth that the specific panda bear gives are twins. \n",
20
+ "<br>\n",
21
+ "Event $A$ is short for the event that the specific panda bear is of species $A$. Conversely, $B$ means the event that the specific panda bear is of species $B$."
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "markdown",
26
+ "metadata": {},
27
+ "source": [
28
+ "### Our assumptions\n",
29
+ "\n",
30
+ "(1) $P(A) = P(B) = 0.5$"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "markdown",
35
+ "metadata": {},
36
+ "source": [
37
+ "(2) $P(T_0 | A) = 0.1$\n"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "markdown",
42
+ "metadata": {},
43
+ "source": [
44
+ "(3) $P(T_0 | B) = 0.2$"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "markdown",
49
+ "metadata": {},
50
+ "source": [
51
+ "(4) $P(T_{i+1} | S) = P(T_i | S) \\quad \\forall S \\in \\{A,B\\}$"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "markdown",
56
+ "metadata": {},
57
+ "source": [
58
+ "(5) $P(A|B) = \\frac{P(B|A)P(A)}{P(B)}$"
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "markdown",
63
+ "metadata": {},
64
+ "source": [
65
+ "(6) $P(C) = P(C|A)P(A) + P(C|B)P(B)$ if $A \\cap B = \\emptyset$ and $P(A \\cup B) = 1$"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "markdown",
70
+ "metadata": {},
71
+ "source": [
72
+ "### Some intermediate results we will use"
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "markdown",
77
+ "metadata": {},
78
+ "source": [
79
+ "We will employ Bayes theorem (5) to compute this:\n",
80
+ "\n",
81
+ "$$P(A | T_0) = \\frac{P(T_0 | A) P(A)}{P(T_0)}$$\n",
82
+ "$$P(B | T_0) = \\frac{P(T_0 | B) P(B)}{P(T_0)}$$\n",
83
+ "\n",
84
+ "From all terms in the equatuions for $P(A | T_0)$ and $P(B | T_0$ we know everything but $P(T_0)$, so let's solve this and compute $P(T_0)$ first. This indeed is quite easy where we use the rule of total probability (6) because $A$ and $B$ are partitioning the whole event space:\n",
85
+ "\n",
86
+ "$$P(T_0) = P(T_0 | A) P(A) + P(T_0 | B) P(B) = 0.1 \\cdot 0.5 + 0.2\\cdot 0.5 = 0.15$$\n",
87
+ "\n",
88
+ "Plugging this into the formula above we obtain \n",
89
+ "$$P(A | T_0) = \\frac{0.1 \\cdot 0.5}{0.15} = \\frac{1}{3}$$\n",
90
+ "$$P(B | T_0) = \\frac{0.2 \\cdot 0.5}{0.15} = \\frac{2}{3}$$\n",
91
+ "\n"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "markdown",
96
+ "metadata": {},
97
+ "source": [
98
+ "### Final solution"
99
+ ]
100
+ },
101
+ {
102
+ "cell_type": "markdown",
103
+ "metadata": {},
104
+ "source": [
105
+ "Having the above computations we will tackle the final part. We are interested in the probability of a twin birth given the fact that the first birth also was a twin. That is, we want to know $P(T_1 | T_0)$ in our terminology."
106
+ ]
107
+ },
108
+ {
109
+ "cell_type": "markdown",
110
+ "metadata": {},
111
+ "source": [
112
+ "The term we are looking for naturally occurrs in the formula for conditional probability:\n",
113
+ "<br>\n",
114
+ "(7) $$P(T_1 \\cap T_0) = P(T_1 | T_0)P(T_0)$$. So to continue, let us deconstruct $P(T_1 \\cap T_0)$ and return to this point here!"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "markdown",
119
+ "metadata": {},
120
+ "source": [
121
+ "Again we will exploit the fact that (6) applies to $A$ and $B$. We can then compute: \n",
122
+ "$$P(T_1 \\cap T_0) = P((T_1 \\cap T_0 \\cap A) \\cup (T_1 \\cap T_0 \\cap B))\n",
123
+ "$$"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "markdown",
128
+ "metadata": {},
129
+ "source": [
130
+ "To the latter we apply again the defintion of conditional probabilities to get:\n",
131
+ "$$P(T_1 \\cap T_0) = P(T_1 | A \\cap T_0)P(A \\cap T_0) + P(T_1 | B \\cap T_0)P(B \\cap T_0)$$ "
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "markdown",
136
+ "metadata": {},
137
+ "source": [
138
+ "We do this again for $P(A \\cap T_0)$ and $P(B \\cap T_0$ and obtain:"
139
+ ]
140
+ },
141
+ {
142
+ "cell_type": "markdown",
143
+ "metadata": {},
144
+ "source": [
145
+ "$$P(T_1 \\cap T_0) = P(T_1 | A \\cap T_0)P(A | T_0)P(T_0) + P(T_1 | B \\cap T_0)P(B | T_0)P(T_0)$$"
146
+ ]
147
+ },
148
+ {
149
+ "cell_type": "markdown",
150
+ "metadata": {},
151
+ "source": [
152
+ "Now, we plug in the knowledge that $P(T_1 | A \\cap T_0) = P(T_1 | A) = P(T_0 | A)$:\n"
153
+ ]
154
+ },
155
+ {
156
+ "cell_type": "markdown",
157
+ "metadata": {},
158
+ "source": [
159
+ "$$P(T_1 \\cap T_0) = P(T_0 | A)P(A | T_0)P(T_0) + P(T_0 | B)P(B | T_0)P(T_0)$$"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "markdown",
164
+ "metadata": {},
165
+ "source": [
166
+ "Having this, remember that we also had (7) stating $$P(T_1 \\cap T_0) = P(T_1 | T_0)P(T_0)$$"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "markdown",
171
+ "metadata": {},
172
+ "source": [
173
+ "Plugging this together and eliminating $P(T_0)$ because it occurrs on both sides we get:\n",
174
+ "$$P(T_1 | T_0) = P(T_0 | A)P(A | T_0) + P(T_0 | B)P(B | T_0)$$"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "markdown",
179
+ "metadata": {},
180
+ "source": [
181
+ "And *drumroll* this is what we we need. We can plug in everything:"
182
+ ]
183
+ },
184
+ {
185
+ "cell_type": "markdown",
186
+ "metadata": {},
187
+ "source": [
188
+ "$$P(T_1 | T_0) = 0.1 \\cdot \\frac{1}{3} + 0.2 \\cdot \\frac{2}{3} = 0.1\\overline{6}$$"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type": "markdown",
193
+ "metadata": {},
194
+ "source": [
195
+ "# 2H2"
196
+ ]
197
+ },
198
+ {
199
+ "cell_type": "markdown",
200
+ "metadata": {},
201
+ "source": [
202
+ "On our way we already have answered what 2H2 was aksing for:\n",
203
+ "$$P(A | T_0) = \\frac{0.1 \\cdot 0.5}{0.15} = \\frac{1}{3}$$"
204
+ ]
205
+ },
206
+ {
207
+ "cell_type": "markdown",
208
+ "metadata": {},
209
+ "source": [
210
+ "So the probability that the panda is of species $A$ given the fact that we observed the first birth being twins is $\\frac{1}{3}$."
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": null,
216
+ "metadata": {},
217
+ "outputs": [],
218
+ "source": []
219
+ }
220
+ ],
221
+ "metadata": {
222
+ "kernelspec": {
223
+ "display_name": "Python 3",
224
+ "language": "python",
225
+ "name": "python3"
226
+ },
227
+ "language_info": {
228
+ "codemirror_mode": {
229
+ "name": "ipython",
230
+ "version": 3
231
+ },
232
+ "file_extension": ".py",
233
+ "mimetype": "text/x-python",
234
+ "name": "python",
235
+ "nbconvert_exporter": "python",
236
+ "pygments_lexer": "ipython3",
237
+ "version": "3.8.5"
238
+ }
239
+ },
240
+ "nbformat": 4,
241
+ "nbformat_minor": 4
242
+ }