You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:::{admonition} Working with Frozen Random Variables in scipy.stats
227
+
:class: note
228
+
229
+
When we write `bernoulli_rv = stats.bernoulli(p=p_positive)`, we're creating a **frozen random variable** — a distribution object with parameters locked in.
230
+
231
+
**Two ways to use scipy.stats:**
232
+
233
+
1.**Non-frozen** (pass parameters every time):
234
+
```python
235
+
stats.bernoulli.pmf(1, p=0.1)
236
+
stats.bernoulli.cdf(0, p=0.1)
237
+
stats.bernoulli.mean(p=0.1)
238
+
```
239
+
240
+
2.**Frozen** (set parameters once, reuse):
241
+
```python
242
+
rv = stats.bernoulli(p=0.1) # Create frozen RV
243
+
rv.pmf(1) # Use it multiple times
244
+
rv.cdf(0)
245
+
rv.mean()
246
+
```
247
+
248
+
**Benefits of frozen RVs:**
249
+
- Cleaner, more readable code
250
+
- More efficient (parameters validated once)
251
+
- Easier to pass distributions to functions
252
+
- Matches the pattern in scipy documentation
253
+
254
+
Throughout this chapter, we use frozen RVs for all examples. This is the recommended approach when working with the same distribution parameters multiple times.
255
+
:::
256
+
226
257
```{code-cell} ipython3
227
258
# Generate random samples
228
259
n_samples = 10
@@ -367,6 +398,23 @@ $$ P(X=k) = \binom{n}{k} p^k (1-p)^{n-k} \quad \text{for } k = 0, 1, \dots, n $$
367
398
368
399
where $\binom{n}{k} = \frac{n!}{k!(n-k)!}$ is the binomial coefficient (number of ways to choose $k$ successes from $n$ trials).
369
400
401
+
:::{admonition} Connection to Bernoulli Trials
402
+
:class: note
403
+
404
+
The Binomial PMF formula directly reflects $n$ independent **Bernoulli trials**:
-**$p^k$**: Probability of $k$ successes — each of the $k$ successes is an independent Bernoulli trial with probability $p$
410
+
-**$(1-p)^{n-k}$**: Probability of $(n-k)$ failures — each failure is an independent Bernoulli trial with probability $1-p$
411
+
-**$\binom{n}{k}$**: Number of ways to arrange $k$ successes among $n$ trial positions
412
+
413
+
**Why this works:** Any specific sequence of $k$ successes and $(n-k)$ failures has probability $p^k(1-p)^{n-k}$ (by independence). Since there are $\binom{n}{k}$ such sequences, we multiply by the binomial coefficient.
414
+
415
+
This shows why Binomial "counts successes in repeated Bernoulli trials": it's built from the ground up using the Bernoulli probability $p$ for each trial.
0 commit comments