Skip to content

Commit c9d76b4

Browse files
authored
Merge pull request #99 from snowch-forks/main
updates
2 parents 4b4cd5d + fde0deb commit c9d76b4

1 file changed

Lines changed: 47 additions & 40 deletions

File tree

chapter_07.md

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -913,21 +913,21 @@ def draw_sequence(ax, cx, cy, label, k, prob_val):
913913
914914
ax.text(cx, cy, label, transform=ax.transAxes,
915915
ha="center", va="center",
916-
fontsize=22, weight="bold", family="monospace", zorder=4)
916+
fontsize=20, weight="bold", family="monospace", zorder=4)
917917
918918
# Formula showing the calculation
919919
if k == 1:
920920
formula_text = rf"${p:.1f}$"
921921
else:
922922
formula_text = rf"${q:.1f}^{{{k-1}}} \times {p:.1f}$"
923923
924-
ax.text(cx, cy - 0.065, formula_text,
924+
ax.text(cx, cy - 0.062, formula_text,
925925
transform=ax.transAxes, ha="center", va="top",
926-
fontsize=24, zorder=4)
926+
fontsize=20, zorder=4)
927927
928-
ax.text(cx, cy - 0.100, rf"$= {prob_val:.4f}$",
928+
ax.text(cx, cy - 0.095, rf"$= {prob_val:.4f}$",
929929
transform=ax.transAxes, ha="center", va="top",
930-
fontsize=24, weight="bold", zorder=4)
930+
fontsize=20, weight="bold", zorder=4)
931931
932932
return (cx - w/2, cy - h/2, cx + w/2, cy + h/2)
933933
@@ -938,11 +938,11 @@ ax.text(0.5, 0.96, rf"Geometric Distribution: Trials Until First Success ($p={p}
938938
939939
# Draw sequences at different positions
940940
sequences = [
941-
("S", 1, 0.20, 0.84), # Success on trial 1
942-
("FS", 2, 0.38, 0.84), # Fail then Success
943-
("FFS", 3, 0.56, 0.84), # Fail Fail then Success
944-
("FFFS", 4, 0.74, 0.84), # Fail Fail Fail then Success
945-
("FFFFS", 5, 0.92, 0.84), # Fail Fail Fail Fail then Success
941+
("S", 1, 0.16, 0.86), # Success on trial 1
942+
("FS", 2, 0.34, 0.86), # Fail then Success
943+
("FFS", 3, 0.52, 0.86), # Fail Fail then Success
944+
("FFFS", 4, 0.70, 0.86), # Fail Fail Fail then Success
945+
("FFFFS", 5, 0.88, 0.86), # Fail Fail Fail Fail then Success
946946
]
947947
948948
seq_boxes = {}
@@ -951,78 +951,85 @@ for label, k, x, y in sequences:
951951
seq_boxes[label] = draw_sequence(ax, x, y, label, k, prob_val)
952952
953953
# Explanation box
954-
expl_w, expl_h = 0.70, 0.10
955-
expl_xy = (0.5 - expl_w/2, 0.64 - expl_h/2)
954+
expl_w, expl_h = 0.70, 0.11
955+
expl_xy = (0.5 - expl_w/2, 0.66 - expl_h/2)
956956
rounded_box(ax, expl_xy, expl_w, expl_h,
957957
fc="lightyellow", ec="orange", lw=2.2, r=0.02)
958958
959-
ax.text(0.5, 0.67, "Each sequence shows trials until first success",
959+
ax.text(0.5, 0.695, "Each sequence shows trials until first success",
960960
transform=ax.transAxes, ha="center", va="center",
961961
fontsize=18, weight="bold", zorder=4)
962-
ax.text(0.5, 0.625, "Probability decreases exponentially with more failures",
962+
ax.text(0.5, 0.635, "Probability decreases exponentially with more failures",
963963
transform=ax.transAxes, ha="center", va="center",
964-
fontsize=24, style="italic", zorder=4)
964+
fontsize=22, style="italic", zorder=4)
965965
966966
# Formula block
967-
ax.text(0.5, 0.50, "General Formula:", transform=ax.transAxes,
967+
ax.text(0.5, 0.545, "General Formula:", transform=ax.transAxes,
968968
ha="center", va="center", fontsize=20, weight="bold", zorder=4)
969969
970-
ax.text(0.5, 0.445, r"$P(X=k) = (1-p)^{k-1} \cdot p$",
970+
ax.text(0.5, 0.495, r"$P(X=k) = (1-p)^{k-1} \cdot p$",
971971
transform=ax.transAxes, ha="center", va="center", fontsize=20, zorder=4)
972972
973-
ax.text(0.5, 0.39, r"$P(X=k) = (\text{fail})^{k-1} \times \text{succeed}$",
973+
ax.text(0.5, 0.45, r"$P(X=k) = (\text{fail})^{k-1} \times \text{succeed}$",
974974
transform=ax.transAxes, ha="center", va="center", fontsize=18, zorder=4)
975975
976+
ax.text(0.5, 0.405, "where $k$ is the trial number of first success",
977+
transform=ax.transAxes, ha="center", va="center", fontsize=16, zorder=4)
978+
976979
# Key insight box
977-
key_w, key_h = 0.60, 0.12
978-
key_xy = (0.5 - key_w/2, 0.22 - key_h/2)
980+
key_w, key_h = 0.68, 0.15
981+
key_xy = (0.5 - key_w/2, 0.25 - key_h/2)
979982
rounded_box(ax, key_xy, key_w, key_h,
980983
fc="lightgreen", ec="green", lw=2.2, r=0.02)
981984
982-
ax.text(0.5, 0.255, "Key Insight:",
985+
ax.text(0.5, 0.305, "Key Insight:",
983986
transform=ax.transAxes, ha="center", va="center",
984987
fontsize=18, weight="bold", zorder=4)
985-
ax.text(0.5, 0.215, rf"All trials before the $k$-th must fail: $(1-p)^{{k-1}}$",
988+
ax.text(0.5, 0.260, rf"All trials before the $k$-th must fail: $(1-p)^{{k-1}}$",
989+
transform=ax.transAxes, ha="center", va="center",
990+
fontsize=20, zorder=4)
991+
ax.text(0.5, 0.220, rf"The $k$-th trial must succeed: $p$",
986992
transform=ax.transAxes, ha="center", va="center",
987-
fontsize=24, zorder=4)
988-
ax.text(0.5, 0.180, rf"The $k$-th trial must succeed: $p$",
993+
fontsize=20, zorder=4)
994+
ax.text(0.5, 0.185, r"(Each trial is independent)",
989995
transform=ax.transAxes, ha="center", va="center",
990-
fontsize=24, zorder=4)
996+
fontsize=16, style="italic", zorder=4)
991997
992998
# ---- Callouts ----
993999
# Arrow pointing to decreasing probabilities
9941000
x0, y0, x1, y1 = seq_boxes["S"]
9951001
ax.annotate("Most likely:\nsucceed early",
996-
xy=((x0 + x1)/2, y0), xycoords=ax.transAxes,
997-
xytext=(0.08, 0.75), textcoords=ax.transAxes,
1002+
xy=(x0, y1), xycoords=ax.transAxes,
1003+
xytext=(0.05, 0.66), textcoords=ax.transAxes,
9981004
arrowprops=dict(arrowstyle="->",
999-
connectionstyle="arc3,rad=-0.15",
1005+
connectionstyle="arc3,rad=-0.3",
10001006
lw=2.5, color="green",
10011007
shrinkA=6, shrinkB=8),
1002-
fontsize=22, color="green", weight="bold",
1008+
fontsize=18, color="green", weight="bold",
10031009
ha="center", va="center", zorder=5)
10041010
10051011
# Arrow pointing to later trials
10061012
x0, y0, x1, y1 = seq_boxes["FFFFS"]
10071013
ax.annotate("Less likely:\nmany failures",
1008-
xy=((x0 + x1)/2, y0), xycoords=ax.transAxes,
1009-
xytext=(0.92, 0.70), textcoords=ax.transAxes,
1014+
xy=(x1, y1), xycoords=ax.transAxes,
1015+
xytext=(0.95, 0.66), textcoords=ax.transAxes,
10101016
arrowprops=dict(arrowstyle="->",
1011-
connectionstyle="arc3,rad=0.15",
1017+
connectionstyle="arc3,rad=0.3",
10121018
lw=2.5, color="red",
10131019
shrinkA=6, shrinkB=8),
1014-
fontsize=22, color="red", weight="bold",
1020+
fontsize=18, color="red", weight="bold",
10151021
ha="center", va="center", zorder=5)
10161022
10171023
# Bottom explanation
1018-
why = (
1019-
f"Example: P(X=3) means getting your first success on the 3rd trial. "
1020-
f"This requires exactly 2 failures followed by 1 success: "
1021-
f"P(X=3) = (0.6)² × 0.4 = 0.36 × 0.4 = 0.144"
1022-
)
1023-
ax.text(0.5, 0.06, why,
1024+
ax.text(0.5, 0.10, "Example: P(X=3) means getting your first success on the 3rd trial.",
1025+
transform=ax.transAxes, ha="center", va="center",
1026+
fontsize=19, style="italic", zorder=4)
1027+
ax.text(0.5, 0.068, "This requires exactly 2 failures followed by 1 success:",
1028+
transform=ax.transAxes, ha="center", va="center",
1029+
fontsize=19, style="italic", zorder=4)
1030+
ax.text(0.5, 0.036, r"$P(X=3) = (0.6)^2 \times 0.4 = 0.36 \times 0.4 = 0.144$",
10241031
transform=ax.transAxes, ha="center", va="center",
1025-
fontsize=21, style="italic", wrap=True, zorder=4)
1032+
fontsize=19, zorder=4)
10261033
10271034
plt.savefig('ch07_geometric_formula_breakdown.svg', format='svg', bbox_inches='tight')
10281035
plt.show()

0 commit comments

Comments
 (0)