{"id":2508,"date":"2026-02-02T09:34:40","date_gmt":"2026-02-02T09:34:40","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2508"},"modified":"2026-02-02T09:34:40","modified_gmt":"2026-02-02T09:34:40","slug":"chapter-6-binomial-distribution","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-6-binomial-distribution\/","title":{"rendered":"Chapter 6: Binomial Distribution"},"content":{"rendered":"<h3 dir=\"auto\">1. What is the Binomial distribution really?<\/h3>\n<p dir=\"auto\">The <strong>binomial distribution<\/strong> answers a very simple but extremely common question:<\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">\u201cIf I repeat the same yes\/no (success\/failure) experiment <strong>n<\/strong> times independently, and each trial has the same probability <strong>p<\/strong> of success, what is the probability of getting exactly <strong>k<\/strong> successes?\u201d<\/p>\n<\/blockquote>\n<p dir=\"auto\">That\u2019s it.<\/p>\n<p dir=\"auto\"><strong>Key ingredients<\/strong> (you must remember these four):<\/p>\n<ul dir=\"auto\">\n<li><strong>n<\/strong> = number of independent trials \/ attempts \/ repetitions<\/li>\n<li><strong>p<\/strong> = probability of \u201csuccess\u201d on <strong>each<\/strong> trial (0 \u2264 p \u2264 1)<\/li>\n<li><strong>k<\/strong> = number of successes we are interested in (k = 0, 1, 2, &#8230;, n)<\/li>\n<li>Each trial must be <strong>independent<\/strong> and have <strong>exactly two possible outcomes<\/strong> (success \/ failure)<\/li>\n<\/ul>\n<h3 dir=\"auto\">2. Classic everyday examples<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"xl\">Example<\/th>\n<th data-col-size=\"sm\">n (trials)<\/th>\n<th data-col-size=\"sm\">p (success probability)<\/th>\n<th data-col-size=\"lg\">What k means<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"xl\">Flipping a fair coin 20 times<\/td>\n<td data-col-size=\"sm\">20<\/td>\n<td data-col-size=\"sm\">0.5<\/td>\n<td data-col-size=\"lg\">Number of heads<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xl\">Clicking \u201cbuy now\u201d on a website<\/td>\n<td data-col-size=\"sm\">1000<\/td>\n<td data-col-size=\"sm\">0.024<\/td>\n<td data-col-size=\"lg\">Number of purchases<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xl\">Testing 50 light bulbs<\/td>\n<td data-col-size=\"sm\">50<\/td>\n<td data-col-size=\"sm\">0.03<\/td>\n<td data-col-size=\"lg\">Number of defective bulbs<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xl\">Sending 200 emails in a campaign<\/td>\n<td data-col-size=\"sm\">200<\/td>\n<td data-col-size=\"sm\">0.18<\/td>\n<td data-col-size=\"lg\">Number of people who open the email<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xl\">Shooting 10 free throws<\/td>\n<td data-col-size=\"sm\">10<\/td>\n<td data-col-size=\"sm\">0.72<\/td>\n<td data-col-size=\"lg\">Number of successful shots<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Generating binomial data in NumPy<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># 10,000 simulations of flipping a fair coin 20 times\r\n# \u2192 how many heads each time?\r\nheads = np.random.binomial(n=20, p=0.5, size=10000)\r\n\r\nprint(\"First 15 experiments:\", heads[:15])\r\nprint(\"Average number of heads:\", heads.mean().round(3))\r\nprint(\"Theoretical expected value:\", 20 * 0.5)   # n \u00d7 p<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Visualizing binomial distributions (very important)<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>fig, axes = plt.subplots(2, 2, figsize=(14, 10), sharey=False)\r\n\r\n# Different combinations of n and p\r\nparams = [\r\n    (10, 0.5, \"Coin flips \u2013 n=10, p=0.5\", \"skyblue\"),\r\n    (40, 0.5, \"n=40, p=0.5\", \"teal\"),\r\n    (30, 0.1, \"Rare events \u2013 n=30, p=0.1\", \"coral\"),\r\n    (30, 0.9, \"Very likely events \u2013 n=30, p=0.9\", \"orchid\")\r\n]\r\n\r\nfor ax, (n, p, title, color) in zip(axes.flat, params):\r\n    data = np.random.binomial(n=n, p=p, size=40000)\r\n    \r\n    sns.histplot(data, bins=np.arange(-0.5, n+1.5, 1), stat=\"probability\",\r\n                 discrete=True, color=color, alpha=0.8, ax=ax)\r\n    \r\n    ax.set_title(title, fontsize=13, pad=12)\r\n    ax.set_xlabel(\"Number of successes (k)\", fontsize=11)\r\n    ax.set_ylabel(\"Probability\", fontsize=11)\r\n    ax.set_xticks(range(0, n+1, max(1, n\/\/10)))\r\n\r\nplt.tight_layout()\r\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>What you should notice<\/strong>:<\/p>\n<ul dir=\"auto\">\n<li>When <strong>p = 0.5<\/strong> \u2192 symmetric bell shape<\/li>\n<li>When <strong>p is small<\/strong> (e.g. 0.1) \u2192 skewed right (most values near 0)<\/li>\n<li>When <strong>p is large<\/strong> (e.g. 0.9) \u2192 skewed left<\/li>\n<li>As <strong>n<\/strong> increases \u2192 shape becomes more symmetric and bell-like (\u2192 approaches normal!)<\/li>\n<\/ul>\n<h3 dir=\"auto\">5. Expected value &amp; variance \u2013 very important formulas<\/h3>\n<p dir=\"auto\"><strong>Expected number of successes<\/strong> (mean): <strong>E[k] = n \u00d7 p<\/strong><\/p>\n<p dir=\"auto\"><strong>Variance<\/strong>: <strong>Var(k) = n \u00d7 p \u00d7 (1-p)<\/strong><\/p>\n<p dir=\"auto\"><strong>Standard deviation<\/strong>: <strong>\u03c3 = \u221a(n \u00d7 p \u00d7 (1-p))<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>n, p = 200, 0.04\r\n\r\nprint(f\"Expected conversions: {n * p:.1f}\")\r\nprint(f\"Standard deviation:   {np.sqrt(n * p * (1-p)):.2f}\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">\u2192 Most of the time you will see roughly <strong>8 \u00b1 3<\/strong> conversions (mean \u00b1 1 sd)<\/p>\n<h3 dir=\"auto\">6. Realistic examples you will actually use<\/h3>\n<p dir=\"auto\"><strong>Example 1 \u2013 A\/B test simulation<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Control group: 12,000 visitors, 3.2% conversion rate\r\ncontrol = np.random.binomial(n=12000, p=0.032, size=10000)\r\n\r\n# Variant B: 12,000 visitors, suppose true rate is 3.8%\r\nvariant = np.random.binomial(n=12000, p=0.038, size=10000)\r\n\r\ndiff = variant - control\r\n\r\nplt.hist(diff, bins=60, color=\"purple\", alpha=0.7)\r\nplt.axvline(0, color='red', linestyle='--', lw=2)\r\nplt.title(\"Difference in conversions (Variant B \u2013 Control)\\n10,000 simulated A\/B tests\")\r\nplt.xlabel(\"Extra conversions from Variant B\")\r\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Example 2 \u2013 Quality control<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Batch of 500 products, historical defect rate 1.8%\r\ndefects = np.random.binomial(500, 0.018, size=2000)\r\n\r\nprint(\"Probability of \u2265 15 defects:\", np.mean(defects &gt;= 15).round(4))<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Example 3 \u2013 Email campaign planning<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>sent = 25000\r\nopen_rate = 0.22\r\n\r\nopens = np.random.binomial(sent, open_rate, 5000)\r\n\r\nprint(f\"95% of campaigns will get between {np.percentile(opens, 2.5):.0f} and {np.percentile(opens, 97.5):.0f} opens\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Summary \u2013 Binomial Distribution Cheat Sheet<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Property<\/th>\n<th data-col-size=\"md\">Value \/ Formula<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">Number of trials<\/td>\n<td data-col-size=\"md\"><strong>n<\/strong> (fixed)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Success probability<\/td>\n<td data-col-size=\"md\"><strong>p<\/strong> (same for every trial)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Possible outcomes<\/td>\n<td data-col-size=\"md\">k = 0, 1, 2, &#8230;, n<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Expected value (mean)<\/td>\n<td data-col-size=\"md\"><strong>n \u00d7 p<\/strong><\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Variance<\/td>\n<td data-col-size=\"md\"><strong>n \u00d7 p \u00d7 (1-p)<\/strong><\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Standard deviation<\/td>\n<td data-col-size=\"md\"><strong>\u221a(n \u00d7 p \u00d7 (1-p))<\/strong><\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">NumPy function<\/td>\n<td data-col-size=\"md\">np.random.binomial(n, p, size=&#8230;)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Shape when p \u2248 0.5<\/td>\n<td data-col-size=\"md\">Symmetric (bell-like for large n)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Shape when p &lt;&lt; 0.5<\/td>\n<td data-col-size=\"md\">Right-skewed<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Shape when p &gt;&gt; 0.5<\/td>\n<td data-col-size=\"md\">Left-skewed<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Approximation for large n<\/td>\n<td data-col-size=\"md\">Normal distribution (Central Limit Theorem)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Final teacher messages<\/h3>\n<ol dir=\"auto\">\n<li><strong>Whenever you have \u201cnumber of successes in fixed number of yes\/no trials\u201d<\/strong> \u2192 think binomial.<\/li>\n<li><strong>When n is large and p is not too close to 0 or 1<\/strong> \u2192 binomial looks very much like normal \u2192 you can often use normal approximation.<\/li>\n<li><strong>Binomial + Poisson connection<\/strong> \u2014 when n is very large and p is very small (n\u00d7p = \u03bb fixed) \u2192 binomial \u2248 Poisson.<\/li>\n<\/ol>\n<p dir=\"auto\">Would you like to continue with any of these next?<\/p>\n<ul dir=\"auto\">\n<li>How binomial becomes Poisson (rare events limit)<\/li>\n<li>How binomial becomes normal (large n)<\/li>\n<li>Binomial confidence intervals (real A\/B testing)<\/li>\n<li>Comparing binomial simulations vs theoretical probabilities<\/li>\n<li>Realistic mini-project: simulate A\/B test + power analysis<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me what feels most interesting or useful right now! \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is the Binomial distribution really? The binomial distribution answers a very simple but extremely common question: \u201cIf I repeat the same yes\/no (success\/failure) experiment n times independently, and each trial has the&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-2508","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2508"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2508\/revisions"}],"predecessor-version":[{"id":2509,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2508\/revisions\/2509"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}