Skip to content

Commit bfcce1f

Browse files
authored
Merge pull request #52 from snowch/claude/review-chapter-7-consistency-R0Nup
Claude/review chapter 7 consistency r0 nup
2 parents 53a80b0 + a162af5 commit bfcce1f

1 file changed

Lines changed: 59 additions & 2 deletions

File tree

chapter_07.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,62 @@ Let's verify this compact formula works for our example where $p = 0.3$:
7070

7171
**Variance:** $Var(X) = p(1-p)$
7272

73-
**Example:** Modeling the outcome of a single customer purchase where the probability of purchase ($p$) is 0.1.
73+
**Visualizing the Distribution**
74+
75+
Let's visualize a Bernoulli distribution with $p = 0.3$ (our customer purchase example from above):
76+
77+
```{code-cell} ipython3
78+
:tags: [remove-input, remove-output]
79+
80+
# Create Bernoulli distribution for visualization (p=0.3)
81+
p_viz = 0.3
82+
bernoulli_viz = stats.bernoulli(p=p_viz)
83+
84+
# Plotting the PMF
85+
k_values_viz = [0, 1]
86+
pmf_values_viz = bernoulli_viz.pmf(k_values_viz)
87+
88+
plt.figure(figsize=(8, 4))
89+
plt.bar(k_values_viz, pmf_values_viz, tick_label=["Failure (0)", "Success (1)"], color='skyblue', edgecolor='black', alpha=0.7)
90+
plt.title(f"Bernoulli PMF (p={p_viz})")
91+
plt.xlabel("Outcome")
92+
plt.ylabel("Probability")
93+
plt.ylim(0, 1)
94+
plt.grid(axis='y', linestyle='--', alpha=0.6)
95+
plt.savefig('ch07_bernoulli_pmf_generic.svg', format='svg', bbox_inches='tight')
96+
plt.show()
97+
```
98+
99+
![Bernoulli PMF](ch07_bernoulli_pmf_generic.svg)
100+
101+
The PMF shows two bars: P(X=0) = 0.7 for failure and P(X=1) = 0.3 for success.
102+
103+
```{code-cell} ipython3
104+
:tags: [remove-input, remove-output]
105+
106+
# Plotting the CDF
107+
cdf_values_viz = bernoulli_viz.cdf(k_values_viz)
108+
109+
plt.figure(figsize=(8, 4))
110+
plt.step(k_values_viz, cdf_values_viz, where='post', color='darkgreen', linewidth=2)
111+
plt.title(f"Bernoulli CDF (p={p_viz})")
112+
plt.xlabel("Outcome")
113+
plt.ylabel("Cumulative Probability P(X <= k)")
114+
plt.ylim(0, 1.1)
115+
plt.xticks([0, 1])
116+
plt.grid(True, which='both', linestyle='--', linewidth=0.5, alpha=0.6)
117+
plt.savefig('ch07_bernoulli_cdf_generic.svg', format='svg', bbox_inches='tight')
118+
plt.show()
119+
```
120+
121+
![Bernoulli CDF](ch07_bernoulli_cdf_generic.svg)
122+
123+
The CDF shows the cumulative probability: P(X ≤ 0) = 0.7 (just the failure outcome) and P(X ≤ 1) = 1.0 (both outcomes).
124+
125+
:::{admonition} Example: Customer Purchase with p = 0.1
126+
:class: tip
127+
128+
Modeling the outcome of a single customer purchase where the probability of purchase is 0.1.
74129

75130
Let's use `scipy.stats.bernoulli` to calculate probabilities, compute the mean and variance, and generate random samples.
76131

@@ -120,7 +175,7 @@ plt.show()
120175

121176
![Bernoulli PMF](ch07_bernoulli_pmf.svg)
122177

123-
The PMF shows the probability of each outcome. With p = 0.1 (from our example), "No Purchase" has probability 1 - p = 0.9 and "Purchase" has probability p = 0.1.
178+
The PMF shows the probability of each outcome. With p = 0.1, "No Purchase" has probability 0.9 and "Purchase" has probability 0.1.
124179

125180
```{code-cell} ipython3
126181
:tags: [remove-input, remove-output]
@@ -144,6 +199,8 @@ plt.show()
144199

145200
The CDF shows cumulative probabilities: P(X ≤ 0) = 0.9 and P(X ≤ 1) = 1.0.
146201

202+
:::
203+
147204
**Quick Check Questions**
148205

149206
1. A quality control inspector checks a single product. It's either defective or not defective. Which distribution models this?

0 commit comments

Comments
 (0)