Coin toss simulation of Bayesian update
Coin toss simulation of Bayesian update
Simulate the process of repeatedly tossing a coin and use Bayes' formula to update the belief distribution of "whether the coin is rigged" in real-time.
After learning this case study, you will understand the core of the Bayesian method: updating your belief once for every new piece of data observed using the likelihood function.
Introduction from life
**Is the new colleague reliable?
On the first day, you don't know the new colleague (prior: they might be reliable or unreliable, 50% each). In the first week, they complete every task on time (new evidence), and you start to feel that "they are likely reliable." In the second week, they complete on time again (more evidence), and your judgment becomes more certain.
Your judgment is the posterior probability—continuously correcting the initial judgment with new evidence. This is the intuition of Bayesian updating.
Intuitive understanding
We have a coin, but we don't know what its probability of landing on heads is. Prior: Before tossing, we believe any probability of heads (from 0 to 1) is possible. Likelihood: With each result seen, we update using "the probability of seeing this result if the probability of heads is theta." Posterior: The updated belief distribution—the more data, the more concentrated the distribution around the true value.
Mathematical definition
[ P(\theta \mid \text{data}) = \frac{P(\text{data} \mid \theta) \cdot P(\theta)}{P(\text{data})} ] [ Posteriort \propto \text{Likelihood}(\text{t-th result} \mid \theta) \times \text{Posterior}{t-1} ]
Python hands-on practice
Instance
import
numpy
as
np
np.
random
.
seed
(
1
)
true_p
=
0.75
The true probability of a coin landing heads (pretending not to know)
theta_grid
=
np.
linspace
(
0
,
1
,
200
)
Prior: treat all theta equally
prior
=
np.
ones_like
(
theta_grid
)
prior /
=
prior.
sum
(
)
Simulate 40 coin flips
flips
=
np.
random
.
binomial
(
1
,
true_p
,
size
=
40
)
posterior
=
prior.
copy
(
)
(
"RUNOOB Bayesian update process (true theta=0.75):\n"
)
for
i
,
outcome
in
enumerate
(
flips
,
start
=
1
)
:
likelihood
=
theta_grid
if
outcome
==
1
else
(
1
- theta_grid
)
posterior
=
posterior * likelihood
posterior /
=
posterior.
sum
(
)
if
i %
10
==
0
:
est
=
theta_grid
[
np.
argmax
(
posterior
)
]
(
f
"After {i:2d} throws, the most likely theta is={est:.3f}"
)
Uncertainty Comparison
prior_std
=
np.
sqrt
(
np.
sum
(
theta_grid**
2
- prior
)
- np.
sum
(
theta_grid * prior
)
**
2
)
post_std
=
np.
sqrt
(
np.
sum
(
theta_grid**
2
- posterior
)
- np.
sum
(
theta_grid * posterior
)
**
2
)
(
f
"\nRUNOOB Prior standard deviation: {prior_std:.4f} -> Posterior standard deviation: {post_std:.4f} (reduced by {prior_std/post_std:.1f} times)"
)
Original prompt (Chinese, left untranslated)
RUNOOB 贝叶斯更新过程 (真实 theta=0.75):
抛 10 次后,最可能的 theta=0.754
抛 20 次后,最可能的 theta=0.769
抛 30 次后,最可能的 theta=0.759
抛 40 次后,最可能的 theta=0.774
RUNOOB 先验标准差: 0.2890 -> 后验标准差: 0.0642 (缩小了 4.5 倍)
Application scenarios in AI
| Scenarios | The role of Bayesian methods |
|---|---|
| Naive Bayes classification | Bayes' theorem + feature independence assumption $\rightarrow$ classic classifier |
| Bayesian optimization | Using prior + observed data $\rightarrow$ posterior Gaussian process $\rightarrow$ guided hyperparameter search |
| Variational inference | Mathematical foundation of VAE: approximating complex posterior with a simple distribution |