-
Notifications
You must be signed in to change notification settings - Fork 1
/
random_dice.jl
219 lines (178 loc) · 7.02 KB
/
random_dice.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
### A Pluto.jl notebook ###
# v0.12.20
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 1dfd0480-6b0d-11eb-1f62-dbdc1a677571
begin
import Pkg; Pkg.add("Plotly")
import Pkg; Pkg.add("PyPlot")
import Pkg; Pkg.add("PlutoUI")
using PlutoUI
using Plots
using LinearAlgebra
using SparseArrays
end
# ╔═╡ e6375832-6b29-11eb-38b2-7582cac61e64
md"""
# **The strange dice** - _The central limit theorem_
**Bernoulli and Laplace** invented this interesting dice game with colored faces (red for odd numbers and green for even numbers of pips).
The side facing the gambler works as a bonus / malus factor.
In the case of a green side facing the gambler, the points get doubled, in case of a red face the points get subtracted by minus 1.
At the trinautic tournament the whole crew is rolling the strange dice and the points are summed up.
Look at the distribution below that shows the probabilities for the different number of points.
**Use the slider to add crew members to play and look how the average distribution of points (total points divided by the number of gamblers) changes**
"""
# ╔═╡ 86b1d820-6b2b-11eb-19cf-6390ab5fccc8
HTML("<div><br></div>")
# ╔═╡ 9f155e80-6be6-11eb-22c1-1b4f6e30ec72
md"Choose number of 👉 $(@bind iterations Slider(1:60, show_value = true)) crew members that roll the strange dice."
# ╔═╡ 7ecb15e0-6b26-11eb-1d2b-37dd4e07ef05
HTML("<div><br><br><br><br><br><br><br><br><br><br><br><br><br></div>")
# ╔═╡ 185ef260-6b0b-11eb-1fd8-b5c70ec616d7
p = [1/6 1/6 1/6 1/6 1/6 1/6]
# ╔═╡ 943dea30-6b0b-11eb-19f0-75227e40a492
A = [ 3/4 1/4; 1/2 1/2; 3/4 1/4; 1/4 3/4; 1/2 1/2; 1/4 3/4]
# ╔═╡ 036a9bb0-6b0c-11eb-0ece-d54174c4d7a0
probability_table = A.*p'
# ╔═╡ 3eb465e0-6b14-11eb-19a4-df75db84f12d
sum(probability_table)
# ╔═╡ 427d9fb0-6b10-11eb-043c-653dbc050130
begin
B = zeros(13,2)
B[(1:6)',2] = probability_table[:,2]
B[:,1] = (0:12)'
index = collect(1:6)*2
B[index.+1,2] = B[index.+1,2] + probability_table[:,1]
end
# ╔═╡ 29969590-6b12-11eb-11d7-9d831132fe4f
begin
plot(
B[:,1], B[:,2],
line = (1.0, 0.0, :bar),
normalize = false,
bins = 10,
bar_width = 0.1,
marker = (6, 0.5, :x),
markerstrokewidth = 5.,
color = [:steelblue],
fill = 0.9,
orientation = :v,
title = "The strange dice distribution",
ylabel = "probability mass function",
xlabel = "Points",
label = :none,
)
plot!( B[:,1], B[:,2],
line = (1.0, 0, :path),
normalize = false,
bins = 10,
bar_width = 0.2,
marker = (4, 0.5, :o),
markerstrokewidth = 5.,
color = [:steelblue],
fill = 0.9,
orientation = :v,
title = "The strange dice distribution",
ylabel = "probability mass function",
xlabel = "Points",
label = :none,
)
end
# ╔═╡ 6df0d2c0-6b10-11eb-04d1-1b71b887b8bb
B,sum(B[:,2])
# ╔═╡ 462e27c0-6b14-11eb-2b2f-3bd578fb8712
dice_mean = sum(B[:,2].*B[:,1])
# ╔═╡ 691d5aa0-6b71-11eb-2f53-db4a2217c8e0
dice_std = sqrt(sum(B[:,2].* (B[:,1].-dice_mean).^2))
# ╔═╡ 18e26640-6be7-11eb-20b0-5542c5620d8d
iter = iterations-1
# ╔═╡ 7dbbfe90-6b20-11eb-39c8-c99fd55ccf05
sparse_vector = repeat(B[:,2]',iter*12+1)
# ╔═╡ 80f0ce50-6b21-11eb-2b31-6982188b8a41
sparse_vector[:,1]
# ╔═╡ e107cae0-6b1d-11eb-3ff5-b7861639d659
P = Array(spdiagm(0 => sparse_vector[:,1], 1 => sparse_vector[:,2], 2 => sparse_vector[:,3], 3 => sparse_vector[:,4], 4 => sparse_vector[:,5], 5 => sparse_vector[:,6], 6 => sparse_vector[:,7], 7 => sparse_vector[:,8], 8 => sparse_vector[:,9], 9 => sparse_vector[:,10], 10=> sparse_vector[:,11], 11 => sparse_vector[:,12], 12 => sparse_vector[:,13]))
# ╔═╡ 58fe1a00-6b22-11eb-207c-095c2b30a0e6
pp = append!(B[:,2], zeros((iter)*12))
# ╔═╡ b3279d30-6b22-11eb-0658-dbe663f09558
p_final = Array(pp'*P^(iter))
# ╔═╡ 6f072a20-6b23-11eb-0add-8b62821b708a
r = Array(LinRange(0,12,(iter-1)*12+13*2-1))
# ╔═╡ b7a9ad70-6b23-11eb-295b-4324ef257047
begin
plot(
[B[:,1], r, [dice_mean, dice_mean]], [B[:,2]./0.9,p_final[1,:]./((r[2]-r[1])*0.9), [0,1]],
line = ([1 0.3 1], [0 0.0 3], [:bar :bar :line]),
linestyle = [:auto :auto :dash],
normalize = true,
marker = (6, 0.8, [:none :none]),
markerstrokewidth = 5.,
color = [:steelblue :orangered :black],
fill = [1. 1.],
bar_width = [1*0.9 (r[2]-r[1])*0.9],
orientation = :v,
title = string("Average distribution of \nstrange dice with ", string(iter+1), " gamblers"),
ylabel = "probability / width of bar",
xlabel = "Points",
usetex = true,
label = [:none :none "Mean"],
ylim = [0,1],
)
plot!([dice_mean - dice_std, dice_mean + dice_std, 3, dice_mean+dice_std, dice_mean-dice_std], 0.5.*[1, 1,NaN, 1, 1],
line = (2., 2., :path),
color = :magenta,
arrow = true,
label = "Std",
)
plot!([dice_mean - dice_std/sqrt(iter), dice_mean + dice_std/sqrt(iter), 3, dice_mean+dice_std/sqrt(iter), dice_mean-dice_std/sqrt(iter)], 0.08*[1, 1,NaN, 1, 1]/sqrt(((r[2]-r[1]))),
line = (2., 2., :path),
color = :green,
arrow = true,
label = string("Std/sqrt(",string(iter+1), ")"),
)
end
# ╔═╡ 89f8f100-6b24-11eb-3292-c15016c60a5b
p_final[1,:]
# ╔═╡ cec01d8e-6be2-11eb-1465-ad0f3ffcc2f5
function pretty(M::Matrix{T} where T<:String)
max_length = maximum(length.(M))
dv="<div style='display:flex;flex-direction:row'>"
HTML(dv*join([join("<div style='width:40px; text-align:center'>".*M[i,:].*"</div>", " ") for i in 1:size(M,1)]
, "</div>$dv")*"</div>")
end
# ╔═╡ e7e029f0-6b9c-11eb-179d-1d2fca9eb8af
[string.(rand("🧔👩👨👧🧑", 1, iterations));string.(rand("🎲", 1, iterations))] |> pretty
# ╔═╡ Cell order:
# ╠═1dfd0480-6b0d-11eb-1f62-dbdc1a677571
# ╟─e6375832-6b29-11eb-38b2-7582cac61e64
# ╠═86b1d820-6b2b-11eb-19cf-6390ab5fccc8
# ╠═29969590-6b12-11eb-11d7-9d831132fe4f
# ╟─e7e029f0-6b9c-11eb-179d-1d2fca9eb8af
# ╟─9f155e80-6be6-11eb-22c1-1b4f6e30ec72
# ╠═b7a9ad70-6b23-11eb-295b-4324ef257047
# ╟─7ecb15e0-6b26-11eb-1d2b-37dd4e07ef05
# ╟─185ef260-6b0b-11eb-1fd8-b5c70ec616d7
# ╟─943dea30-6b0b-11eb-19f0-75227e40a492
# ╟─036a9bb0-6b0c-11eb-0ece-d54174c4d7a0
# ╟─3eb465e0-6b14-11eb-19a4-df75db84f12d
# ╟─427d9fb0-6b10-11eb-043c-653dbc050130
# ╟─6df0d2c0-6b10-11eb-04d1-1b71b887b8bb
# ╟─691d5aa0-6b71-11eb-2f53-db4a2217c8e0
# ╟─462e27c0-6b14-11eb-2b2f-3bd578fb8712
# ╟─18e26640-6be7-11eb-20b0-5542c5620d8d
# ╟─7dbbfe90-6b20-11eb-39c8-c99fd55ccf05
# ╟─80f0ce50-6b21-11eb-2b31-6982188b8a41
# ╟─e107cae0-6b1d-11eb-3ff5-b7861639d659
# ╟─58fe1a00-6b22-11eb-207c-095c2b30a0e6
# ╟─b3279d30-6b22-11eb-0658-dbe663f09558
# ╟─6f072a20-6b23-11eb-0add-8b62821b708a
# ╟─89f8f100-6b24-11eb-3292-c15016c60a5b
# ╟─cec01d8e-6be2-11eb-1465-ad0f3ffcc2f5