rubythinking 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +3 -0
- data/.ipynb_checkpoints/2H1_2H2-checkpoint.ipynb +242 -0
- data/.ipynb_checkpoints/2M1-checkpoint.ipynb +559 -0
- data/.ipynb_checkpoints/2M2-checkpoint.ipynb +269 -0
- data/.ipynb_checkpoints/2M3-checkpoint.ipynb +134 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +42 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rubythinking/distributions/binomial.rb +29 -0
- data/lib/rubythinking/distributions.rb +4 -0
- data/lib/rubythinking/version.rb +3 -0
- data/lib/rubythinking.rb +17 -0
- data/local_reinstall.sh +1 -0
- data/r/0_all.R +4600 -0
- data/r/2_6.R +11 -0
- data/r/2_7.R +5 -0
- data/r/4.R +2 -0
- data/rubythinking.gemspec +32 -0
- metadata +129 -0
@@ -0,0 +1,269 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "markdown",
|
5
|
+
"metadata": {},
|
6
|
+
"source": [
|
7
|
+
"# Statistical Rethinking: Excercise 2M2"
|
8
|
+
]
|
9
|
+
},
|
10
|
+
{
|
11
|
+
"cell_type": "markdown",
|
12
|
+
"metadata": {},
|
13
|
+
"source": [
|
14
|
+
"Like in the previous notebook for 2M1, we have the following globe tossing results:\n",
|
15
|
+
"\n",
|
16
|
+
"(1) W, W, W <br>\n",
|
17
|
+
"(2) W, W, W, L <br>\n",
|
18
|
+
"(3) L, W, W, L, W, W, W <br>\n",
|
19
|
+
"\n",
|
20
|
+
"<br>\n",
|
21
|
+
"We want to estimate the probability of Water $p$ as parameter of the model. The parameter is at least 0 and at most 1.\n",
|
22
|
+
"<br>\n",
|
23
|
+
"<br>\n",
|
24
|
+
"The key point for this exercise is that we employ a different prior:\n",
|
25
|
+
"$$ \\text{prior}(p) =\n",
|
26
|
+
" \\begin{cases}\n",
|
27
|
+
" 0, & \\text{if}\\ p < 0.5 \\\\\n",
|
28
|
+
" 1, & \\text{otherwise}\n",
|
29
|
+
" \\end{cases}$$"
|
30
|
+
]
|
31
|
+
},
|
32
|
+
{
|
33
|
+
"cell_type": "code",
|
34
|
+
"execution_count": 17,
|
35
|
+
"metadata": {},
|
36
|
+
"outputs": [
|
37
|
+
{
|
38
|
+
"data": {
|
39
|
+
"text/plain": [
|
40
|
+
"[0.0, 0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7000000000000001, 0.72, 0.74, 0.76, 0.78, 0.8, 0.8200000000000001, 0.84, 0.86, 0.88, 0.9, 0.92, 0.9400000000000001, 0.96, 0.98, 1.0]"
|
41
|
+
]
|
42
|
+
},
|
43
|
+
"execution_count": 17,
|
44
|
+
"metadata": {},
|
45
|
+
"output_type": "execute_result"
|
46
|
+
}
|
47
|
+
],
|
48
|
+
"source": [
|
49
|
+
"require 'iruby/chartkick'\n",
|
50
|
+
"include IRuby::Chartkick\n",
|
51
|
+
"grid_size = 50\n",
|
52
|
+
"step_size = 1.0 / grid_size.to_f\n",
|
53
|
+
"grid = 0.step(by: step_size, to: 1).to_a"
|
54
|
+
]
|
55
|
+
},
|
56
|
+
{
|
57
|
+
"cell_type": "code",
|
58
|
+
"execution_count": 18,
|
59
|
+
"metadata": {},
|
60
|
+
"outputs": [
|
61
|
+
{
|
62
|
+
"data": {
|
63
|
+
"text/html": [
|
64
|
+
"<div id=\"iruby-chartkick-areachart-53045\" style=\"height: 300px; width: 100%; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\">Loading...</div><script type=\"text/javascript\">\n",
|
65
|
+
" new Chartkick[\"AreaChart\"](\"iruby-chartkick-areachart-53045\", [[0.0,0],[0.02,0],[0.04,0],[0.06,0],[0.08,0],[0.1,0],[0.12,0],[0.14,0],[0.16,0],[0.18,0],[0.2,0],[0.22,0],[0.24,0],[0.26,0],[0.28,0],[0.3,0],[0.32,0],[0.34,0],[0.36,0],[0.38,0],[0.4,0],[0.42,0],[0.44,0],[0.46,0],[0.48,0],[0.5,1],[0.52,1],[0.54,1],[0.56,1],[0.58,1],[0.6,1],[0.62,1],[0.64,1],[0.66,1],[0.68,1],[0.7000000000000001,1],[0.72,1],[0.74,1],[0.76,1],[0.78,1],[0.8,1],[0.8200000000000001,1],[0.84,1],[0.86,1],[0.88,1],[0.9,1],[0.92,1],[0.9400000000000001,1],[0.96,1],[0.98,1],[1.0,1]], {});\n",
|
66
|
+
"</script>\n"
|
67
|
+
],
|
68
|
+
"text/plain": [
|
69
|
+
"\"<div id=\\\"iruby-chartkick-areachart-53045\\\" style=\\\"height: 300px; width: 100%; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\\\">Loading...</div><script type=\\\"text/javascript\\\">\\n new Chartkick[\\\"AreaChart\\\"](\\\"iruby-chartkick-areachart-53045\\\", [[0.0,0],[0.02,0],[0.04,0],[0.06,0],[0.08,0],[0.1,0],[0.12,0],[0.14,0],[0.16,0],[0.18,0],[0.2,0],[0.22,0],[0.24,0],[0.26,0],[0.28,0],[0.3,0],[0.32,0],[0.34,0],[0.36,0],[0.38,0],[0.4,0],[0.42,0],[0.44,0],[0.46,0],[0.48,0],[0.5,1],[0.52,1],[0.54,1],[0.56,1],[0.58,1],[0.6,1],[0.62,1],[0.64,1],[0.66,1],[0.68,1],[0.7000000000000001,1],[0.72,1],[0.74,1],[0.76,1],[0.78,1],[0.8,1],[0.8200000000000001,1],[0.84,1],[0.86,1],[0.88,1],[0.9,1],[0.92,1],[0.9400000000000001,1],[0.96,1],[0.98,1],[1.0,1]], {});\\n</script>\\n\""
|
70
|
+
]
|
71
|
+
},
|
72
|
+
"execution_count": 18,
|
73
|
+
"metadata": {},
|
74
|
+
"output_type": "execute_result"
|
75
|
+
}
|
76
|
+
],
|
77
|
+
"source": [
|
78
|
+
"prior = grid.each_with_object({}) do |x, prior| \n",
|
79
|
+
" prior[x] =\n",
|
80
|
+
" if x < 0.5\n",
|
81
|
+
" 0\n",
|
82
|
+
" else\n",
|
83
|
+
" 1\n",
|
84
|
+
" end\n",
|
85
|
+
"end\n",
|
86
|
+
"\n",
|
87
|
+
"area_chart(prior)"
|
88
|
+
]
|
89
|
+
},
|
90
|
+
{
|
91
|
+
"cell_type": "code",
|
92
|
+
"execution_count": 19,
|
93
|
+
"metadata": {},
|
94
|
+
"outputs": [
|
95
|
+
{
|
96
|
+
"data": {
|
97
|
+
"text/plain": [
|
98
|
+
"#<Proc:0x00007f98e9bed1b8@(pry):193 (lambda)>"
|
99
|
+
]
|
100
|
+
},
|
101
|
+
"execution_count": 19,
|
102
|
+
"metadata": {},
|
103
|
+
"output_type": "execute_result"
|
104
|
+
}
|
105
|
+
],
|
106
|
+
"source": [
|
107
|
+
"factorial = ->(n) do\n",
|
108
|
+
" return 1 if n < 1\n",
|
109
|
+
" n.to_i.downto(1).inject(:*)\n",
|
110
|
+
"end\n",
|
111
|
+
"\n",
|
112
|
+
"\n",
|
113
|
+
"likelihood = ->(w, l, p) do\n",
|
114
|
+
" (factorial[w+l].to_f / (factorial[w] * factorial[l])).to_f * (p**w) * ((1-p)**l)\n",
|
115
|
+
"end"
|
116
|
+
]
|
117
|
+
},
|
118
|
+
{
|
119
|
+
"cell_type": "markdown",
|
120
|
+
"metadata": {},
|
121
|
+
"source": [
|
122
|
+
"Now, let's compute the grid aprroximation of the posterior for each of the cases. The difference is only the data input we give in terms of \"count of Water\" versus \"count of Land\" of our tossing result given in the exercise."
|
123
|
+
]
|
124
|
+
},
|
125
|
+
{
|
126
|
+
"cell_type": "code",
|
127
|
+
"execution_count": 20,
|
128
|
+
"metadata": {},
|
129
|
+
"outputs": [
|
130
|
+
{
|
131
|
+
"data": {
|
132
|
+
"text/html": [
|
133
|
+
"<div id=\"iruby-chartkick-areachart-47755\" style=\"height: 300px; width: 100%; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\">Loading...</div><script type=\"text/javascript\">\n",
|
134
|
+
" new Chartkick[\"AreaChart\"](\"iruby-chartkick-areachart-47755\", [[0.0,0.0],[0.02,0.0],[0.04,0.0],[0.06,0.0],[0.08,0.0],[0.1,0.0],[0.12,0.0],[0.14,0.0],[0.16,0.0],[0.18,0.0],[0.2,0.0],[0.22,0.0],[0.24,0.0],[0.26,0.0],[0.28,0.0],[0.3,0.0],[0.32,0.0],[0.34,0.0],[0.36,0.0],[0.38,0.0],[0.4,0.0],[0.42,0.0],[0.44,0.0],[0.46,0.0],[0.48,0.0],[0.5,0.010175010175010175],[0.52,0.011445502645502646],[0.54,0.01281758241758242],[0.56,0.0142951566951567],[0.58,0.015882132682132677],[0.6,0.01758241758241758],[0.62,0.0193999185999186],[0.64,0.021338542938542943],[0.66,0.023402197802197804],[0.68,0.025594790394790397],[0.7000000000000001,0.027920227920227927],[0.72,0.030382417582417578],[0.74,0.032985266585266584],[0.76,0.03573268213268214],[0.78,0.03862857142857143],[0.8,0.041676841676841686],[0.8200000000000001,0.04488140008140009],[0.84,0.048246153846153834],[0.86,0.05177501017501017],[0.88,0.05547187627187627],[0.9,0.059340659340659345],[0.92,0.0633852665852666],[0.9400000000000001,0.06760960520960521],[0.96,0.07201758241758241],[0.98,0.07661310541310541],[1.0,0.0814000814000814]], {});\n",
|
135
|
+
"</script>\n"
|
136
|
+
],
|
137
|
+
"text/plain": [
|
138
|
+
"\"<div id=\\\"iruby-chartkick-areachart-47755\\\" style=\\\"height: 300px; width: 100%; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\\\">Loading...</div><script type=\\\"text/javascript\\\">\\n new Chartkick[\\\"AreaChart\\\"](\\\"iruby-chartkick-areachart-47755\\\", [[0.0,0.0],[0.02,0.0],[0.04,0.0],[0.06,0.0],[0.08,0.0],[0.1,0.0],[0.12,0.0],[0.14,0.0],[0.16,0.0],[0.18,0.0],[0.2,0.0],[0.22,0.0],[0.24,0.0],[0.26,0.0],[0.28,0.0],[0.3,0.0],[0.32,0.0],[0.34,0.0],[0.36,0.0],[0.38,0.0],[0.4,0.0],[0.42,0.0],[0.44,0.0],[0.46,0.0],[0.48,0.0],[0.5,0.010175010175010175],[0.52,0.011445502645502646],[0.54,0.01281758241758242],[0.56,0.0142951566951567],[0.58,0.015882132682132677],[0.6,0.01758241758241758],[0.62,0.0193999185999186],[0.64,0.021338542938542943],[0.66,0.023402197802197804],[0.68,0.025594790394790397],[0.7000000000000001,0.027920227920227927],[0.72,0.030382417582417578],[0.74,0.032985266585266584],[0.76,0.03573268213268214],[0.78,0.03862857142857143],[0.8,0.041676841676841686],[0.8200000000000001,0.04488140008140009],[0.84,0.048246153846153834],[0.86,0.05177501017501017],[0.88,0.05547187627187627],[0.9,0.059340659340659345],[0.92,0.0633852665852666],[0.9400000000000001,0.06760960520960521],[0.96,0.07201758241758241],[0.98,0.07661310541310541],[1.0,0.0814000814000814]], {});\\n</script>\\n\""
|
139
|
+
]
|
140
|
+
},
|
141
|
+
"execution_count": 20,
|
142
|
+
"metadata": {},
|
143
|
+
"output_type": "execute_result"
|
144
|
+
}
|
145
|
+
],
|
146
|
+
"source": [
|
147
|
+
"# For case (1)\n",
|
148
|
+
"w = 3\n",
|
149
|
+
"l = 0\n",
|
150
|
+
"\n",
|
151
|
+
"\n",
|
152
|
+
"u_posterior = grid.each_with_object({}) do |x, u_posterior| \n",
|
153
|
+
" u_posterior[x] = prior[x] * likelihood[w, l, x] \n",
|
154
|
+
"end\n",
|
155
|
+
"\n",
|
156
|
+
"posterior = u_posterior.each_with_object({}) do |(x,y), posterior| \n",
|
157
|
+
" posterior[x] = y.to_f / u_posterior.values.sum.to_f\n",
|
158
|
+
"end\n",
|
159
|
+
"\n",
|
160
|
+
"area_chart(posterior)"
|
161
|
+
]
|
162
|
+
},
|
163
|
+
{
|
164
|
+
"cell_type": "code",
|
165
|
+
"execution_count": 21,
|
166
|
+
"metadata": {},
|
167
|
+
"outputs": [
|
168
|
+
{
|
169
|
+
"data": {
|
170
|
+
"text/html": [
|
171
|
+
"<div id=\"iruby-chartkick-areachart-68133\" style=\"height: 300px; width: 100%; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\">Loading...</div><script type=\"text/javascript\">\n",
|
172
|
+
" new Chartkick[\"AreaChart\"](\"iruby-chartkick-areachart-68133\", [[0.0,0.0],[0.02,0.0],[0.04,0.0],[0.06,0.0],[0.08,0.0],[0.1,0.0],[0.12,0.0],[0.14,0.0],[0.16,0.0],[0.18,0.0],[0.2,0.0],[0.22,0.0],[0.24,0.0],[0.26,0.0],[0.28,0.0],[0.3,0.0],[0.32,0.0],[0.34,0.0],[0.36,0.0],[0.38,0.0],[0.4,0.0],[0.42,0.0],[0.44,0.0],[0.46,0.0],[0.48,0.0],[0.5,0.03033366841116807],[0.52,0.032756401520313755],[0.54,0.0351547512134438],[0.56,0.03750262568233768],[0.58,0.03977206941818761],[0.6,0.04193326321159874],[0.62,0.04395452415258893],[0.64,0.04580230563058892],[0.66,0.047441197334442235],[0.68,0.048833925252405236],[0.7000000000000001,0.04994135167214711],[0.72,0.050722475180749835],[0.74,0.05113443066470822],[0.76,0.05113248930992991],[0.78,0.05067005860173534],[0.8,0.04969868232485777],[0.8200000000000001,0.04816804056344327],[0.84,0.046025949701050776],[0.86,0.043218362420651975],[0.88,0.03968936770463141],[0.9,0.035381190834786434],[0.92,0.03023419339232721],[0.9400000000000001,0.024186873257876735],[0.96,0.01717586461147086],[0.98,0.00913593793255812],[1.0,0.0]], {});\n",
|
173
|
+
"</script>\n"
|
174
|
+
],
|
175
|
+
"text/plain": [
|
176
|
+
"\"<div id=\\\"iruby-chartkick-areachart-68133\\\" style=\\\"height: 300px; width: 100%; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\\\">Loading...</div><script type=\\\"text/javascript\\\">\\n new Chartkick[\\\"AreaChart\\\"](\\\"iruby-chartkick-areachart-68133\\\", [[0.0,0.0],[0.02,0.0],[0.04,0.0],[0.06,0.0],[0.08,0.0],[0.1,0.0],[0.12,0.0],[0.14,0.0],[0.16,0.0],[0.18,0.0],[0.2,0.0],[0.22,0.0],[0.24,0.0],[0.26,0.0],[0.28,0.0],[0.3,0.0],[0.32,0.0],[0.34,0.0],[0.36,0.0],[0.38,0.0],[0.4,0.0],[0.42,0.0],[0.44,0.0],[0.46,0.0],[0.48,0.0],[0.5,0.03033366841116807],[0.52,0.032756401520313755],[0.54,0.0351547512134438],[0.56,0.03750262568233768],[0.58,0.03977206941818761],[0.6,0.04193326321159874],[0.62,0.04395452415258893],[0.64,0.04580230563058892],[0.66,0.047441197334442235],[0.68,0.048833925252405236],[0.7000000000000001,0.04994135167214711],[0.72,0.050722475180749835],[0.74,0.05113443066470822],[0.76,0.05113248930992991],[0.78,0.05067005860173534],[0.8,0.04969868232485777],[0.8200000000000001,0.04816804056344327],[0.84,0.046025949701050776],[0.86,0.043218362420651975],[0.88,0.03968936770463141],[0.9,0.035381190834786434],[0.92,0.03023419339232721],[0.9400000000000001,0.024186873257876735],[0.96,0.01717586461147086],[0.98,0.00913593793255812],[1.0,0.0]], {});\\n</script>\\n\""
|
177
|
+
]
|
178
|
+
},
|
179
|
+
"execution_count": 21,
|
180
|
+
"metadata": {},
|
181
|
+
"output_type": "execute_result"
|
182
|
+
}
|
183
|
+
],
|
184
|
+
"source": [
|
185
|
+
"# For case (2)\n",
|
186
|
+
"w = 3\n",
|
187
|
+
"l = 1\n",
|
188
|
+
"\n",
|
189
|
+
"\n",
|
190
|
+
"u_posterior = grid.each_with_object({}) do |x, u_posterior| \n",
|
191
|
+
" u_posterior[x] = prior[x] * likelihood[w, l, x] \n",
|
192
|
+
"end\n",
|
193
|
+
"\n",
|
194
|
+
"posterior = u_posterior.each_with_object({}) do |(x,y), posterior| \n",
|
195
|
+
" posterior[x] = y.to_f / u_posterior.values.sum.to_f\n",
|
196
|
+
"end\n",
|
197
|
+
"\n",
|
198
|
+
"area_chart(posterior)"
|
199
|
+
]
|
200
|
+
},
|
201
|
+
{
|
202
|
+
"cell_type": "code",
|
203
|
+
"execution_count": 22,
|
204
|
+
"metadata": {},
|
205
|
+
"outputs": [
|
206
|
+
{
|
207
|
+
"data": {
|
208
|
+
"text/html": [
|
209
|
+
"<div id=\"iruby-chartkick-areachart-70746\" style=\"height: 300px; width: 100%; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\">Loading...</div><script type=\"text/javascript\">\n",
|
210
|
+
" new Chartkick[\"AreaChart\"](\"iruby-chartkick-areachart-70746\", [[0.0,0.0],[0.02,0.0],[0.04,0.0],[0.06,0.0],[0.08,0.0],[0.1,0.0],[0.12,0.0],[0.14,0.0],[0.16,0.0],[0.18,0.0],[0.2,0.0],[0.22,0.0],[0.24,0.0],[0.26,0.0],[0.28,0.0],[0.3,0.0],[0.32,0.0],[0.34,0.0],[0.36,0.0],[0.38,0.0],[0.4,0.0],[0.42,0.0],[0.44,0.0],[0.46,0.0],[0.48,0.0],[0.5,0.0302304402399983],[0.52,0.03389640455469191],[0.54,0.037595763061228776],[0.56,0.041257217006775745],[0.58,0.0448015448911555],[0.6,0.04814272579711524],[0.62,0.051189405068867475],[0.64,0.05384672730179831],[0.66,0.05601856160623747],[0.68,0.05761014410818552],[0.7000000000000001,0.05853116264989264],[0.72,0.058699308653184123],[0.74,0.058044321108427326],[0.76,0.05651254765203497],[0.78,0.05407204769539964],[0.8,0.050718262568154315],[0.8200000000000001,0.04648027763865389],[0.84,0.04142770137457234],[0.86,0.03567818630651054],[0.88,0.02940561685750976],[0.9,0.022848989001365234],[0.92,0.016322006712635093],[0.9400000000000001,0.010223420171239311],[0.96,0.005048130684543599],[0.98,0.0013990872898229294],[1.0,0.0]], {});\n",
|
211
|
+
"</script>\n"
|
212
|
+
],
|
213
|
+
"text/plain": [
|
214
|
+
"\"<div id=\\\"iruby-chartkick-areachart-70746\\\" style=\\\"height: 300px; width: 100%; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\\\">Loading...</div><script type=\\\"text/javascript\\\">\\n new Chartkick[\\\"AreaChart\\\"](\\\"iruby-chartkick-areachart-70746\\\", [[0.0,0.0],[0.02,0.0],[0.04,0.0],[0.06,0.0],[0.08,0.0],[0.1,0.0],[0.12,0.0],[0.14,0.0],[0.16,0.0],[0.18,0.0],[0.2,0.0],[0.22,0.0],[0.24,0.0],[0.26,0.0],[0.28,0.0],[0.3,0.0],[0.32,0.0],[0.34,0.0],[0.36,0.0],[0.38,0.0],[0.4,0.0],[0.42,0.0],[0.44,0.0],[0.46,0.0],[0.48,0.0],[0.5,0.0302304402399983],[0.52,0.03389640455469191],[0.54,0.037595763061228776],[0.56,0.041257217006775745],[0.58,0.0448015448911555],[0.6,0.04814272579711524],[0.62,0.051189405068867475],[0.64,0.05384672730179831],[0.66,0.05601856160623747],[0.68,0.05761014410818552],[0.7000000000000001,0.05853116264989264],[0.72,0.058699308653184123],[0.74,0.058044321108427326],[0.76,0.05651254765203497],[0.78,0.05407204769539964],[0.8,0.050718262568154315],[0.8200000000000001,0.04648027763865389],[0.84,0.04142770137457234],[0.86,0.03567818630651054],[0.88,0.02940561685750976],[0.9,0.022848989001365234],[0.92,0.016322006712635093],[0.9400000000000001,0.010223420171239311],[0.96,0.005048130684543599],[0.98,0.0013990872898229294],[1.0,0.0]], {});\\n</script>\\n\""
|
215
|
+
]
|
216
|
+
},
|
217
|
+
"execution_count": 22,
|
218
|
+
"metadata": {},
|
219
|
+
"output_type": "execute_result"
|
220
|
+
}
|
221
|
+
],
|
222
|
+
"source": [
|
223
|
+
"# For case (3)\n",
|
224
|
+
"w = 5\n",
|
225
|
+
"l = 2\n",
|
226
|
+
"\n",
|
227
|
+
"\n",
|
228
|
+
"u_posterior = grid.each_with_object({}) do |x, u_posterior| \n",
|
229
|
+
" u_posterior[x] = prior[x] * likelihood[w, l, x] \n",
|
230
|
+
"end\n",
|
231
|
+
"\n",
|
232
|
+
"posterior = u_posterior.each_with_object({}) do |(x,y), posterior| \n",
|
233
|
+
" posterior[x] = y.to_f / u_posterior.values.sum.to_f\n",
|
234
|
+
"end\n",
|
235
|
+
"\n",
|
236
|
+
"area_chart(posterior)"
|
237
|
+
]
|
238
|
+
},
|
239
|
+
{
|
240
|
+
"cell_type": "code",
|
241
|
+
"execution_count": null,
|
242
|
+
"metadata": {},
|
243
|
+
"outputs": [],
|
244
|
+
"source": []
|
245
|
+
},
|
246
|
+
{
|
247
|
+
"cell_type": "code",
|
248
|
+
"execution_count": null,
|
249
|
+
"metadata": {},
|
250
|
+
"outputs": [],
|
251
|
+
"source": []
|
252
|
+
}
|
253
|
+
],
|
254
|
+
"metadata": {
|
255
|
+
"kernelspec": {
|
256
|
+
"display_name": "Ruby 2.6.5",
|
257
|
+
"language": "ruby",
|
258
|
+
"name": "ruby"
|
259
|
+
},
|
260
|
+
"language_info": {
|
261
|
+
"file_extension": ".rb",
|
262
|
+
"mimetype": "application/x-ruby",
|
263
|
+
"name": "ruby",
|
264
|
+
"version": "2.6.5"
|
265
|
+
}
|
266
|
+
},
|
267
|
+
"nbformat": 4,
|
268
|
+
"nbformat_minor": 4
|
269
|
+
}
|
@@ -0,0 +1,134 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "markdown",
|
5
|
+
"metadata": {},
|
6
|
+
"source": [
|
7
|
+
"# Statistical Rethinking: Excercise 2M3"
|
8
|
+
]
|
9
|
+
},
|
10
|
+
{
|
11
|
+
"cell_type": "markdown",
|
12
|
+
"metadata": {},
|
13
|
+
"source": [
|
14
|
+
"A globe is being tossed and the result is \"Land\". With same probability, this globe was either a \n",
|
15
|
+
"globe of our Earth or the globe of Mars. We know that the earth is covered 70% by water and the Mars is covered 100% by land.\n",
|
16
|
+
"\n",
|
17
|
+
"Here are our facts:\n",
|
18
|
+
"\n",
|
19
|
+
"\n",
|
20
|
+
"$$\\text{Pr}[Earth] = \\text{Pr}[Mars] = 0.5$$"
|
21
|
+
]
|
22
|
+
},
|
23
|
+
{
|
24
|
+
"cell_type": "markdown",
|
25
|
+
"metadata": {},
|
26
|
+
"source": [
|
27
|
+
"\n",
|
28
|
+
"$$\\text{Pr}[Land | Earth] = 0.3$$"
|
29
|
+
]
|
30
|
+
},
|
31
|
+
{
|
32
|
+
"cell_type": "markdown",
|
33
|
+
"metadata": {},
|
34
|
+
"source": [
|
35
|
+
"$$\\text{Pr}[Land | Mars] = 1.0$$"
|
36
|
+
]
|
37
|
+
},
|
38
|
+
{
|
39
|
+
"cell_type": "markdown",
|
40
|
+
"metadata": {},
|
41
|
+
"source": [
|
42
|
+
"We want to show that\n",
|
43
|
+
"$$\\text{Pr}[Earth|Land] = 0.23$$"
|
44
|
+
]
|
45
|
+
},
|
46
|
+
{
|
47
|
+
"cell_type": "markdown",
|
48
|
+
"metadata": {},
|
49
|
+
"source": [
|
50
|
+
"### Solution\n",
|
51
|
+
"\n",
|
52
|
+
"Let's abbreviate Earth by $E$ and Mars by $M$ as well as water by $W$ and Land by $L$. \n",
|
53
|
+
"Straight-forward application of Bayes theorem gives us:\n",
|
54
|
+
"\n",
|
55
|
+
"$$ \\text{Pr}[E|L] = \\frac{\\text{Pr}[L|E] \\cdot \\text{Pr}[E]}{\\text{Pr}[L]} $$\n"
|
56
|
+
]
|
57
|
+
},
|
58
|
+
{
|
59
|
+
"cell_type": "markdown",
|
60
|
+
"metadata": {},
|
61
|
+
"source": [
|
62
|
+
"We already have all concrete numbers for the numerator, as given in our \"fact list\" above. So we need to concentrate on computing the denominator $\\text{Pr}[L]$. \n",
|
63
|
+
"<br><br>\n",
|
64
|
+
"For that we exploit the fact that we know our planet is either Earth or Mars as two distinct events and no other possibility. By applicaion of the marginalizaion principle we get:"
|
65
|
+
]
|
66
|
+
},
|
67
|
+
{
|
68
|
+
"cell_type": "markdown",
|
69
|
+
"metadata": {},
|
70
|
+
"source": [
|
71
|
+
"$$\\text{Pr}[L] = \\text{E}[\\text{Pr}[L|planet]]\n",
|
72
|
+
"= \\sum\\limits_{p \\in \\{E, M\\}}\\text{Pr}[L|p] \\text{Pr}[p] = (0.5 \\cdot 0.3) + (0.5 \\cdot 1.0) = 0.65$$"
|
73
|
+
]
|
74
|
+
},
|
75
|
+
{
|
76
|
+
"cell_type": "markdown",
|
77
|
+
"metadata": {},
|
78
|
+
"source": [
|
79
|
+
"With the latter info that $\\text{Pr}[L]$ is 0.65, we have all the pieces together:\n",
|
80
|
+
"\n",
|
81
|
+
"$$ \\text{Pr}[E|L] = \\frac{\\text{Pr}[L|E] \\cdot \\text{Pr}[E]}{\\text{Pr}[L]} = \\frac{0.3 \\cdot 0.5}{0.65}$$\n"
|
82
|
+
]
|
83
|
+
},
|
84
|
+
{
|
85
|
+
"cell_type": "code",
|
86
|
+
"execution_count": 11,
|
87
|
+
"metadata": {},
|
88
|
+
"outputs": [
|
89
|
+
{
|
90
|
+
"data": {
|
91
|
+
"text/plain": [
|
92
|
+
"0.23076923076923075"
|
93
|
+
]
|
94
|
+
},
|
95
|
+
"execution_count": 11,
|
96
|
+
"metadata": {},
|
97
|
+
"output_type": "execute_result"
|
98
|
+
}
|
99
|
+
],
|
100
|
+
"source": [
|
101
|
+
"0.3*0.5 / 0.65\n"
|
102
|
+
]
|
103
|
+
},
|
104
|
+
{
|
105
|
+
"cell_type": "markdown",
|
106
|
+
"metadata": {},
|
107
|
+
"source": [
|
108
|
+
" "
|
109
|
+
]
|
110
|
+
},
|
111
|
+
{
|
112
|
+
"cell_type": "code",
|
113
|
+
"execution_count": null,
|
114
|
+
"metadata": {},
|
115
|
+
"outputs": [],
|
116
|
+
"source": []
|
117
|
+
}
|
118
|
+
],
|
119
|
+
"metadata": {
|
120
|
+
"kernelspec": {
|
121
|
+
"display_name": "Ruby 2.6.5",
|
122
|
+
"language": "ruby",
|
123
|
+
"name": "ruby"
|
124
|
+
},
|
125
|
+
"language_info": {
|
126
|
+
"file_extension": ".rb",
|
127
|
+
"mimetype": "application/x-ruby",
|
128
|
+
"name": "ruby",
|
129
|
+
"version": "2.6.5"
|
130
|
+
}
|
131
|
+
},
|
132
|
+
"nbformat": 4,
|
133
|
+
"nbformat_minor": 4
|
134
|
+
}
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.2
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at robin.stammer@posteo.de. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rubythinking (0.1.0)
|
5
|
+
cmd_stan_rb
|
6
|
+
croupier
|
7
|
+
distribution
|
8
|
+
vega
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
cmd_stan_rb (0.4.0)
|
14
|
+
croupier (1.6.0)
|
15
|
+
diff-lcs (1.4.4)
|
16
|
+
distribution (0.8.0)
|
17
|
+
rake (12.3.3)
|
18
|
+
rspec (3.10.0)
|
19
|
+
rspec-core (~> 3.10.0)
|
20
|
+
rspec-expectations (~> 3.10.0)
|
21
|
+
rspec-mocks (~> 3.10.0)
|
22
|
+
rspec-core (3.10.0)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-expectations (3.10.0)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.10.0)
|
27
|
+
rspec-mocks (3.10.0)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.10.0)
|
30
|
+
rspec-support (3.10.0)
|
31
|
+
vega (0.1.2)
|
32
|
+
|
33
|
+
PLATFORMS
|
34
|
+
ruby
|
35
|
+
|
36
|
+
DEPENDENCIES
|
37
|
+
rake (~> 12.0)
|
38
|
+
rspec (~> 3.0)
|
39
|
+
rubythinking!
|
40
|
+
|
41
|
+
BUNDLED WITH
|
42
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Robin Stammer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# rubythinking
|
2
|
+
|
3
|
+
Let's do the _Statistical Rethinking_ journey in Ruby!
|
4
|
+
|
5
|
+
## Links to compiled web versions of the notebooks
|
6
|
+
|
7
|
+
#### Chapter 2
|
8
|
+
|
9
|
+
* [2M1](https://www.robinstammer.de/rubythinking/solutions/2m1)
|
10
|
+
* [2M2](https://www.robinstammer.de/rubythinking/solutions/2m2)
|
11
|
+
* [2H1 and 2H2](https://www.robinstammer.de/rubythinking/solutions/2h1_2h2)
|
12
|
+
|
13
|
+
#### Chapter 3
|
14
|
+
|
15
|
+
* [Fun content: Sampling from a Binomial](https://www.robinstammer.de/rubythinking/binomial)
|
16
|
+
|
17
|
+
## Ruby ported code snippets from the chapters
|
18
|
+
|
19
|
+
* [Chapter 0](https://github.com/rstammer/rubythinking/blob/master/notebooks/0_chapter_code_snippets.ipynb)
|
20
|
+
* [Chapter 2](https://github.com/rstammer/rubythinking/blob/master/notebooks/2_chapter_code_snippets.ipynb)
|
21
|
+
|
22
|
+
## FAQ
|
23
|
+
|
24
|
+
### Q: What is the purpose of this?
|
25
|
+
|
26
|
+
The book "Statistical Rethinking" by Richard McElreath is a famous textbook
|
27
|
+
to learn Bayesian Statistics. There is a variety of additional material on
|
28
|
+
the internet available for Python, R and probably other programming languages.
|
29
|
+
|
30
|
+
This repository offers some material of following the book and its excercises
|
31
|
+
in Ruby. On the one hand, this repository offers Ruby-based Jupyter notebooks
|
32
|
+
using [iruby](https://github.com/SciRuby/iruby), on the other hand it is
|
33
|
+
packaged as Ruby gem to easily install some dependencies like charting libraries
|
34
|
+
to run the notebooks.
|
35
|
+
|
36
|
+
### **Q**: Which version of the book are you using?
|
37
|
+
|
38
|
+
The second edition!
|
39
|
+
|
40
|
+
|
41
|
+
### Q: You are often using this in Ruby based notebooks. Can use it without them?
|
42
|
+
|
43
|
+
Sure. Just install the gem and play with it in any other Ruby environment you use!
|
44
|
+
|
45
|
+
### Q: I'm new to Ruby but want to run your Ruby based notebooks. What should I do?
|
46
|
+
|
47
|
+
First: You need to have Jupyter installed on your machine! Check [https://jupyter.org/install](https://jupyter.org/install) for
|
48
|
+
installation information!
|
49
|
+
|
50
|
+
The structure of this repository forms a Ruby gem. But before you can install
|
51
|
+
it, you need to make sure [iruby](https://github.com/SciRuby/iruby) is working on
|
52
|
+
your machine. In case of doubt, check latest `iruby` documentation!
|
53
|
+
|
54
|
+
Then you just do
|
55
|
+
|
56
|
+
```bash
|
57
|
+
gem install rubythinking && iruby register --force
|
58
|
+
```
|
59
|
+
|
60
|
+
After this, you have installed this little Ruby gem and told iruby kernel to take
|
61
|
+
it into account. Now you can start a Jupyter notebook and inspect the jupyter
|
62
|
+
notebooks in the `/notebooks` folder of this repository. To do so, download the repo,
|
63
|
+
`cd` into the directory and
|
64
|
+
|
65
|
+
```
|
66
|
+
jupyter notebook
|
67
|
+
```
|
68
|
+
|
69
|
+
By installing the gem, some other dependency gems will get installed for you that are
|
70
|
+
convenient helpers in the notebooks.
|
71
|
+
|
72
|
+
If you are creating a new Jupyter notebook with iruby, then you can load all code
|
73
|
+
used by `rubythinking` with
|
74
|
+
|
75
|
+
```Ruby
|
76
|
+
require "rubythinking"
|
77
|
+
include Rubythinking
|
78
|
+
|
79
|
+
dbinom(6, prob: 0.5, size: 9) # => 0.1640625
|
80
|
+
rbinom(10, prob: 0.5, size: 5) # => [2, 2, 1, 0, 4, 1, 3, 1, 3, 1]
|
81
|
+
…
|
82
|
+
```
|
83
|
+
|
84
|
+
### Q: Can I participate?
|
85
|
+
|
86
|
+
I'd appreciate this very much! ☀️ This project is very incomplete and there are tons of excercises and snippets of the book
|
87
|
+
that await being ported to Ruby by someone. Unfortunately, I can only do it from time to time and I'd love having people joining
|
88
|
+
this initiative.
|
89
|
+
|
90
|
+
If you are interested, feel free to pick up any part of the book you are interested in, try replicating or adopting it with Ruby and
|
91
|
+
then either support an iruby notebook to [the notebooks folder](https://github.com/rstammer/rubythinking/tree/master/notebooks) or _anything else you like_ to this repository.
|
92
|
+
Supplementary Ruby code can go somewhere in the [the lib folder](https://github.com/rstammer/rubythinking/tree/master/lib).
|
93
|
+
|
94
|
+
Even tiny contributions are rewarded with extra positive karma 😉
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rubythinking"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|