Skip to content

Commit a166823

Browse files
committed
Add standard deviation formula and enhance PMF/CDF visualizations
- Add SD formula: SD(X) = √(np(1-p)) - PMF: Add mean line (red dashed) and mean±1SD shaded region (orange) - CDF: Add mean line (red dashed) - Include legends with calculated values - Update descriptions to explain visual elements - Increase figure size to 10x5 for better readability This helps readers understand the distribution's center and spread visually.
1 parent 4509ecf commit a166823

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

chapter_07.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ $$
599599

600600
**Variance:** $Var(X) = np(1-p)$
601601

602+
**Standard Deviation:** $SD(X) = \sqrt{np(1-p)}$
603+
602604
**Visualizing the Distribution**
603605

604606
Let's visualize a Binomial distribution with $n = 10$ and $p = 0.5$ (our coin flip example):
@@ -611,43 +613,61 @@ n_viz = 10
611613
p_viz = 0.5
612614
binomial_viz = stats.binom(n=n_viz, p=p_viz)
613615
616+
# Calculate mean and std
617+
mean_viz = binomial_viz.mean()
618+
std_viz = binomial_viz.std()
619+
614620
# Plotting the PMF
615621
k_values_viz = np.arange(0, n_viz + 1)
616622
pmf_values_viz = binomial_viz.pmf(k_values_viz)
617623
618-
plt.figure(figsize=(8, 4))
624+
plt.figure(figsize=(10, 5))
619625
plt.bar(k_values_viz, pmf_values_viz, color='skyblue', edgecolor='black', alpha=0.7)
626+
627+
# Add mean line
628+
plt.axvline(mean_viz, color='red', linestyle='--', linewidth=2, label=f'Mean = {mean_viz:.1f}')
629+
630+
# Add mean ± 1 std region
631+
plt.axvspan(mean_viz - std_viz, mean_viz + std_viz, alpha=0.2, color='orange',
632+
label=f'Mean ± 1 SD = [{mean_viz - std_viz:.1f}, {mean_viz + std_viz:.1f}]')
633+
620634
plt.title(f"Binomial PMF (n={n_viz}, p={p_viz})")
621635
plt.xlabel("Number of Successes (k)")
622636
plt.ylabel("Probability")
637+
plt.legend(loc='upper right', fontsize=10)
623638
plt.grid(axis='y', linestyle='--', alpha=0.6)
624639
plt.savefig('ch07_binomial_pmf_generic.svg', format='svg', bbox_inches='tight')
625640
plt.show()
626641
```
627642

628643
![Binomial PMF](ch07_binomial_pmf_generic.svg)
629644

630-
The PMF shows the probability distribution for the number of heads in 10 coin flips. The distribution is symmetric around the mean (np = 5) since p = 0.5.
645+
The PMF shows the probability distribution for the number of heads in 10 coin flips. The distribution is symmetric around the mean ($np = 5$) since $p = 0.5$. The shaded region shows mean ± 1 standard deviation ($\sqrt{np(1-p)} = \sqrt{2.5} \approx 1.58$).
631646

632647
```{code-cell} ipython3
633648
:tags: [remove-input, remove-output]
634649
635650
# Plotting the CDF
636651
cdf_values_viz = binomial_viz.cdf(k_values_viz)
637652
638-
plt.figure(figsize=(8, 4))
653+
plt.figure(figsize=(10, 5))
639654
plt.step(k_values_viz, cdf_values_viz, where='post', color='darkgreen', linewidth=2)
655+
656+
# Add mean line
657+
plt.axvline(mean_viz, color='red', linestyle='--', linewidth=2, label=f'Mean = {mean_viz:.1f}')
658+
640659
plt.title(f"Binomial CDF (n={n_viz}, p={p_viz})")
641660
plt.xlabel("Number of Successes (k)")
642661
plt.ylabel("Cumulative Probability P(X <= k)")
662+
plt.legend(loc='lower right', fontsize=10)
643663
plt.grid(True, which='both', linestyle='--', linewidth=0.5, alpha=0.6)
644664
plt.savefig('ch07_binomial_cdf_generic.svg', format='svg', bbox_inches='tight')
645665
plt.show()
646666
```
647667

648668
![Binomial CDF](ch07_binomial_cdf_generic.svg)
649669

650-
The CDF shows P(X ≤ k), the cumulative probability of getting k or fewer heads.
670+
The CDF shows P(X ≤ k), the cumulative probability of getting k or fewer heads. The red dashed line marks the mean.
651671

652672
:::{admonition} Example: Sales Calls with n = 20, p = 0.15
653673
:class: tip

0 commit comments

Comments
 (0)