Skip to content

Commit 3a70bce

Browse files
authored
Merge pull request #76 from snowch/claude/binomial-distribution-calculator-QJG1z
Claude/binomial distribution calculator qjg1z
2 parents db09f58 + 58f8260 commit 3a70bce

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

chapter_07.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,37 @@ print(f"Mean (Expected Value): {bernoulli_rv.mean():.2f}")
223223
print(f"Variance: {bernoulli_rv.var():.2f}")
224224
```
225225

226+
:::{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+
226257
```{code-cell} ipython3
227258
# Generate random samples
228259
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 $$
367398

368399
where $\binom{n}{k} = \frac{n!}{k!(n-k)!}$ is the binomial coefficient (number of ways to choose $k$ successes from $n$ trials).
369400

401+
:::{admonition} Connection to Bernoulli Trials
402+
:class: note
403+
404+
The Binomial PMF formula directly reflects $n$ independent **Bernoulli trials**:
405+
406+
$$P(X=k) = \binom{n}{k} \cdot p^k \cdot (1-p)^{n-k}$$
407+
408+
Breaking this down:
409+
- **$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.
416+
:::
417+
370418
:::{admonition} Connection to Counting Techniques
371419
:class: note
372420

0 commit comments

Comments
 (0)