Files
skills/public/math-probability-statistics.html
zeekay 747dafba10 Register WebMCP tools on every page, and name each page's twin from site.py
Every page now loads /webmcp.js, scripts/webmcp.ts with its types
stripped (npm run webmcp). Where the browser has document.modelContext
it registers two read-only tools: search_skills over the published
skills index, and get_skill, which returns a skill's markdown checked
against the index digest. The markdown alternate link moves from
build.py to site.py, so CI holds every page to it.

The API catalog gains the site itself (llms.txt and the skills index)
and hanzo.ai/agents; the live discovery check now fetches every
catalog link. tests/site.mjs drives the tools through a recording
modelContext, and checks a browser without one loads clean.
2026-10-26 20:57:05 -07:00

747 lines
32 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>math-probability-statistics</title>
<meta name="color-scheme" content="dark">
<style>
/* Committed to one visual world: a dark instrument panel. No light variant —
the dot field and the hairline rules only read on black, and a naive
inversion would wash both out. Every colour is painted explicitly so the page
never borrows a host background. */
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
:root{
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
--font-zen-pixel-square:'Zen Pixel Square',monospace;
--mono:var(--font-zen-mono);
--sans:var(--font-zen-sans);
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
--r:999px; --r-soft:14px;
}
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
/* The dot field. A halftone made of one repeating radial-gradient, faded out
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
/* No overflow clip here: the dot field is inset to the box already, and a clip
would cut off the copy-prompt menu. */
.hero{position:relative;border-bottom:1px solid var(--line)}
.hero::before{content:"";position:absolute;inset:0;
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
background-size:7px 7px;
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
pointer-events:none}
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
nav{border-bottom:1px solid var(--line)}
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
justify-content:space-between;align-items:center;gap:1rem}
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--ink);text-decoration:none;
display:inline-flex;align-items:center;gap:.6rem}
.brand i{font-style:normal;color:var(--faint)}
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--soft);text-decoration:none}
nav a.up:hover{color:var(--ink)}
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
h1 em{font-style:normal;color:var(--faint)}
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
line-height:1.2;font-weight:400}
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
.btn.solid:hover{background:#d8d8d4}
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
/* Copy-prompt selector. Each row copies the prompt for its own target. */
.copy{position:relative;display:inline-flex}
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
padding:.4rem;display:flex;flex-direction:column;
box-shadow:0 12px 32px rgba(0,0,0,.75)}
.menu[hidden]{display:none}
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
white-space:nowrap}
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
.mi:hover .mark{color:var(--ink)}
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
letter-spacing:.1em;text-transform:uppercase}
/* Works-with bar: one scrolling row, name and mark per agent. */
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
display:flex;align-items:center}
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
-webkit-overflow-scrolling:touch;
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
.bar::-webkit-scrollbar{display:none}
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
text-decoration:none;background:var(--ground)}
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
.agent .mark{width:.95rem;height:.95rem}
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
p{margin:0 0 1rem;max-width:70ch}
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
li{margin:.22rem 0}
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
text-decoration-color:var(--line-hi)}
a:hover{text-decoration-color:var(--ink)}
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
display:block;overflow-x:auto}
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
.curl{display:block;background:var(--panel);border:1px solid var(--line);
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
white-space:pre;line-height:1.9}
.curl b{color:var(--ink);font-weight:400}
.curl i{font-style:normal;color:var(--faint)}
/* index */
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
#q:focus{outline:none;border-color:var(--soft)}
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
text-transform:uppercase;margin:0 0 2rem}
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
padding-bottom:.45rem}
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
border-radius:var(--r-soft);text-decoration:none}
.row:hover{background:#0a0a0a}
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
color:var(--faint);font-size:.85rem}
footer a{color:var(--soft)}
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
margin:0 0 2rem;font-size:.85rem}
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
border-bottom:1px solid var(--line);padding-bottom:.4rem}
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
.fmap a:hover{text-decoration:underline}
.fmap div{color:var(--faint);line-height:1.5}
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
@media (max-width:640px){
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
.btns{gap:.5rem}
.btn{padding:.75rem 1.15rem;font-size:.68rem}
.copy,.copy .btn{width:100%;justify-content:center}
.menu{left:0;right:0;min-width:0}
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
.strip b{border-right:0;padding-right:0}
.bar{width:100%}
}
</style>
<link rel="icon" href="/favicon.svg">
<link rel="canonical" href="https://hanzoskills.com/math-probability-statistics">
<link rel="alternate" type="text/markdown" href="/math-probability-statistics.md">
<script type="module" src="/webmcp.js"></script>
</head>
<!-- Reading this with a machine? Every page here is also plain markdown.
This one: https://hanzoskills.com/math-probability-statistics.md
Index: https://hanzoskills.com/llms.txt
Corpus: https://hanzoskills.com/all.md
Append .md to any path for the markdown twin. -->
<body>
<nav><div class="in">
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
<a class="up" href="/">All skills</a>
</div></nav>
<div class="wrap">
<h1>math-probability-statistics</h1><p class="lede">Statistical hypothesis testing (t-test, chi-square, ANOVA)</p><h1>Probability and Statistics</h1>
<h2>Metadata</h2>
<ul><li><strong>Skill ID</strong>: probability-statistics</li><li><strong>Category</strong>: Mathematics/Statistics</li><li><strong>Complexity</strong>: Advanced</li><li><strong>Last Updated</strong>: 2025-10-18</li></ul>
<h2>Overview</h2>
<p>Computational statistics and probability using SciPy, NumPy, and statsmodels. Covers distributions, hypothesis testing, confidence intervals, Bayesian inference, and statistical modeling for data analysis and decision making.</p>
<h2>When to Use This Skill</h2>
<ul><li>Statistical hypothesis testing (t-test, chi-square, ANOVA)</li><li>A/B testing and experiment analysis</li><li>Probability distributions and random sampling</li><li>Confidence intervals and parameter estimation</li><li>Bayesian inference and MCMC</li><li>Time series analysis</li><li>Regression and statistical modeling</li><li>Monte Carlo simulation</li></ul>
<h2>When NOT to Use This Skill</h2>
<ul><li>Machine learning model training (use scikit-learn)</li><li>Deep learning (use PyTorch/TensorFlow)</li><li>Causal inference without domain knowledge</li><li>When sample size is too small for statistical validity</li><li>Pure probability theory (use SymPy for symbolic)</li></ul>
<h2>Prerequisites</h2>
<ul><li>Probability basics (distributions, expectation, variance)</li><li>Statistics fundamentals (hypothesis testing, p-values)</li><li>NumPy array operations</li><li>Basic data manipulation (pandas helpful)</li></ul>
<h2>Core Concepts</h2>
<h3>1. Probability Distributions</h3>
<p><strong>Common Continuous Distributions</strong></p>
<pre><code class="lang-python">import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
# Normal (Gaussian) distribution
mu, sigma = 0, 1
normal = stats.norm(loc=mu, scale=sigma)
# PDF (probability density function)
x = np.linspace(-4, 4, 1000)
pdf = normal.pdf(x)
# CDF (cumulative distribution function)
cdf = normal.cdf(x)
# Inverse CDF (quantile function)
p = 0.95
quantile = normal.ppf(p) # 95th percentile
print(f&quot;95th percentile: {quantile:.4f}&quot;) # ~1.645
# Random sampling
samples = normal.rvs(size=10000, random_state=42)
print(f&quot;Sample mean: {samples.mean():.4f}&quot;)
print(f&quot;Sample std: {samples.std():.4f}&quot;)
# Other continuous distributions
exponential = stats.expon(scale=2.0) # λ = 1/2
gamma = stats.gamma(a=2.0, scale=2.0) # shape, scale
beta = stats.beta(a=2.0, b=5.0)
uniform = stats.uniform(loc=0, scale=10) # [0, 10]
lognormal = stats.lognorm(s=1.0) # s is shape parameter</code></pre>
<p><strong>Common Discrete Distributions</strong></p>
<pre><code class="lang-python"># Binomial: n trials, probability p
n, p = 10, 0.3
binomial = stats.binom(n=n, p=p)
# PMF (probability mass function)
k = np.arange(0, n+1)
pmf = binomial.pmf(k)
# Probability of exactly 3 successes
prob_3 = binomial.pmf(3)
print(f&quot;P(X=3) = {prob_3:.4f}&quot;)
# Probability of at most 5 successes
prob_le_5 = binomial.cdf(5)
print(f&quot;P(X≤5) = {prob_le_5:.4f}&quot;)
# Poisson distribution (events in fixed interval)
lambda_rate = 5.0
poisson = stats.poisson(mu=lambda_rate)
# Geometric (number of trials until first success)
geometric = stats.geom(p=0.3)
# Negative binomial (trials until r successes)
neg_binomial = stats.nbinom(n=5, p=0.3)</code></pre>
<p><strong>Multivariate Distributions</strong></p>
<pre><code class="lang-python"># Multivariate normal
mean = [0, 0]
cov = [[1, 0.5], [0.5, 2]] # Covariance matrix
mvn = stats.multivariate_normal(mean=mean, cov=cov)
# Sample
samples = mvn.rvs(size=1000, random_state=42)
# PDF at a point
point = [0.5, 1.0]
density = mvn.pdf(point)
# Marginal distributions
# X ~ N(0, 1), Y ~ N(0, 2)
# Conditional: X|Y=y ~ N(ρσ_x/σ_y * y, σ_x^2(1-ρ^2))</code></pre>
<h3>2. Hypothesis Testing</h3>
<p><strong>t-tests</strong></p>
<pre><code class="lang-python">from scipy.stats import ttest_1samp, ttest_ind, ttest_rel
# One-sample t-test: test if mean equals hypothesized value
data = np.random.randn(100) + 0.5
t_stat, p_value = ttest_1samp(data, 0) # H0: μ = 0
print(f&quot;t-statistic: {t_stat:.4f}&quot;)
print(f&quot;p-value: {p_value:.4f}&quot;)
if p_value &lt; 0.05:
print(&quot;Reject H0: mean is significantly different from 0&quot;)
else:
print(&quot;Fail to reject H0&quot;)
# Two-sample t-test (independent samples)
group1 = np.random.randn(50) + 0.5
group2 = np.random.randn(60)
# Equal variance assumed
t_stat, p_value = ttest_ind(group1, group2)
# Unequal variance (Welch&#x27;s t-test)
t_stat, p_value = ttest_ind(group1, group2, equal_var=False)
# Paired t-test (dependent samples)
before = np.random.randn(30) + 5
after = before + np.random.randn(30) * 0.5 + 0.3
t_stat, p_value = ttest_rel(before, after)
print(f&quot;Paired t-test p-value: {p_value:.4f}&quot;)</code></pre>
<p><strong>Chi-Square Tests</strong></p>
<pre><code class="lang-python">from scipy.stats import chi2_contingency, chisquare
# Chi-square goodness-of-fit test
# H0: observed frequencies match expected distribution
observed = np.array([50, 45, 55])
expected = np.array([50, 50, 50])
chi2_stat, p_value = chisquare(observed, expected)
print(f&quot;Chi-square goodness-of-fit p-value: {p_value:.4f}&quot;)
# Chi-square test of independence (contingency table)
# Example: relationship between gender and preference
observed = np.array([
[30, 20, 10], # Male: A, B, C
[15, 25, 35] # Female: A, B, C
])
chi2, p_value, dof, expected = chi2_contingency(observed)
print(f&quot;Chi-square independence test:&quot;)
print(f&quot;χ² = {chi2:.4f}, p = {p_value:.4f}, dof = {dof}&quot;)
if p_value &lt; 0.05:
print(&quot;Reject H0: variables are not independent&quot;)</code></pre>
<p><strong>ANOVA (Analysis of Variance)</strong></p>
<pre><code class="lang-python">from scipy.stats import f_oneway, kruskal
# One-way ANOVA: compare means of 3+ groups
group1 = np.random.randn(30) + 0
group2 = np.random.randn(30) + 0.5
group3 = np.random.randn(30) + 1.0
f_stat, p_value = f_oneway(group1, group2, group3)
print(f&quot;ANOVA F-statistic: {f_stat:.4f}, p-value: {p_value:.4f}&quot;)
# Kruskal-Wallis test (non-parametric alternative)
h_stat, p_value = kruskal(group1, group2, group3)
print(f&quot;Kruskal-Wallis H: {h_stat:.4f}, p-value: {p_value:.4f}&quot;)</code></pre>
<p><strong>Non-Parametric Tests</strong></p>
<pre><code class="lang-python">from scipy.stats import mannwhitneyu, wilcoxon, kstest
# Mann-Whitney U test (independent samples, non-parametric)
u_stat, p_value = mannwhitneyu(group1, group2, alternative=&#x27;two-sided&#x27;)
# Wilcoxon signed-rank test (paired samples, non-parametric)
w_stat, p_value = wilcoxon(before, after)
# Kolmogorov-Smirnov test (goodness-of-fit)
# Test if data comes from standard normal
data = np.random.randn(100)
ks_stat, p_value = kstest(data, &#x27;norm&#x27;)
print(f&quot;KS test p-value: {p_value:.4f}&quot;)</code></pre>
<h3>3. Confidence Intervals</h3>
<p><strong>Mean Confidence Intervals</strong></p>
<pre><code class="lang-python">from scipy import stats
def confidence_interval_mean(data, confidence=0.95):
&quot;&quot;&quot;
Compute confidence interval for mean.
&quot;&quot;&quot;
n = len(data)
mean = np.mean(data)
sem = stats.sem(data) # Standard error of mean
# t-distribution (for unknown population variance)
ci = stats.t.interval(confidence, df=n-1, loc=mean, scale=sem)
return ci
data = np.random.randn(50) + 5
ci = confidence_interval_mean(data, confidence=0.95)
print(f&quot;95% CI for mean: [{ci[0]:.4f}, {ci[1]:.4f}]&quot;)
# Proportion confidence interval (Wilson score interval)
def confidence_interval_proportion(successes, n, confidence=0.95):
&quot;&quot;&quot;Confidence interval for proportion.&quot;&quot;&quot;
p_hat = successes / n
z = stats.norm.ppf((1 + confidence) / 2)
# Wilson score interval (better for small samples)
denominator = 1 + z**2 / n
center = (p_hat + z**2 / (2*n)) / denominator
margin = z * np.sqrt((p_hat * (1 - p_hat) / n + z**2 / (4*n**2))) / denominator
return (center - margin, center + margin)
ci_prop = confidence_interval_proportion(60, 100, confidence=0.95)
print(f&quot;95% CI for proportion: [{ci_prop[0]:.4f}, {ci_prop[1]:.4f}]&quot;)</code></pre>
<p><strong>Bootstrap Confidence Intervals</strong></p>
<pre><code class="lang-python">from scipy.stats import bootstrap
def median_statistic(data, axis):
&quot;&quot;&quot;Statistic function for bootstrap&quot;&quot;&quot;
return np.median(data, axis=axis)
data = np.random.lognormal(size=100)
# Bootstrap resampling
result = bootstrap(
(data,),
median_statistic,
n_resamples=10000,
confidence_level=0.95,
random_state=42
)
print(f&quot;95% CI for median: [{result.confidence_interval.low:.4f}, &quot;
f&quot;{result.confidence_interval.high:.4f}]&quot;)
# Manual bootstrap
def bootstrap_ci(data, statistic, n_bootstrap=10000, confidence=0.95):
&quot;&quot;&quot;Manual bootstrap confidence interval&quot;&quot;&quot;
bootstrap_stats = []
n = len(data)
for _ in range(n_bootstrap):
sample = np.random.choice(data, size=n, replace=True)
bootstrap_stats.append(statistic(sample))
bootstrap_stats = np.array(bootstrap_stats)
alpha = 1 - confidence
lower = np.percentile(bootstrap_stats, 100 * alpha / 2)
upper = np.percentile(bootstrap_stats, 100 * (1 - alpha / 2))
return lower, upper
ci_boot = bootstrap_ci(data, np.median, n_bootstrap=10000)</code></pre>
<h3>4. A/B Testing</h3>
<p><strong>Two-Proportion Z-Test</strong></p>
<pre><code class="lang-python">from statsmodels.stats.proportion import proportions_ztest
def ab_test(conversions_a, visitors_a, conversions_b, visitors_b, alpha=0.05):
&quot;&quot;&quot;
A/B test for conversion rates.
&quot;&quot;&quot;
# Conversion rates
rate_a = conversions_a / visitors_a
rate_b = conversions_b / visitors_b
# Z-test
count = np.array([conversions_a, conversions_b])
nobs = np.array([visitors_a, visitors_b])
z_stat, p_value = proportions_ztest(count, nobs)
# Relative improvement
relative_improvement = (rate_b - rate_a) / rate_a * 100
# Effect size (Cohen&#x27;s h)
h = 2 * (np.arcsin(np.sqrt(rate_b)) - np.arcsin(np.sqrt(rate_a)))
print(f&quot;Control rate: {rate_a:.4f}&quot;)
print(f&quot;Treatment rate: {rate_b:.4f}&quot;)
print(f&quot;Relative improvement: {relative_improvement:.2f}%&quot;)
print(f&quot;p-value: {p_value:.4f}&quot;)
print(f&quot;Effect size (h): {h:.4f}&quot;)
if p_value &lt; alpha:
print(&quot;Result is statistically significant&quot;)
else:
print(&quot;Result is not statistically significant&quot;)
return {
&#x27;rate_a&#x27;: rate_a,
&#x27;rate_b&#x27;: rate_b,
&#x27;p_value&#x27;: p_value,
&#x27;relative_improvement&#x27;: relative_improvement,
&#x27;effect_size&#x27;: h
}
# Example
result = ab_test(conversions_a=120, visitors_a=1000,
conversions_b=150, visitors_b=1000)
# Sample size calculation for A/B test
from statsmodels.stats.power import zt_ind_solve_power
def calculate_sample_size(baseline_rate, mde, alpha=0.05, power=0.8):
&quot;&quot;&quot;
Calculate required sample size per group.
Args:
baseline_rate: Current conversion rate
mde: Minimum detectable effect (absolute, e.g., 0.01 for 1pp increase)
alpha: Significance level
power: Statistical power
&quot;&quot;&quot;
effect_size = 2 * (np.arcsin(np.sqrt(baseline_rate + mde)) -
np.arcsin(np.sqrt(baseline_rate)))
n = zt_ind_solve_power(effect_size=effect_size, alpha=alpha,
power=power, alternative=&#x27;two-sided&#x27;)
return int(np.ceil(n))
n_required = calculate_sample_size(baseline_rate=0.10, mde=0.02)
print(f&quot;Required sample size per group: {n_required}&quot;)</code></pre>
<h3>5. Bayesian Inference</h3>
<p><strong>Bayesian Parameter Estimation</strong></p>
<pre><code class="lang-python"># Example: Estimate success probability from binomial data
# Prior: Beta(alpha, beta)
# Likelihood: Binomial(n, p)
# Posterior: Beta(alpha + successes, beta + failures)
def bayesian_binomial(successes, trials, prior_alpha=1, prior_beta=1):
&quot;&quot;&quot;
Bayesian inference for binomial proportion.
&quot;&quot;&quot;
failures = trials - successes
# Posterior parameters
post_alpha = prior_alpha + successes
post_beta = prior_beta + failures
# Posterior distribution
posterior = stats.beta(post_alpha, post_beta)
# Point estimates
mean = posterior.mean()
mode = (post_alpha - 1) / (post_alpha + post_beta - 2) if post_alpha &gt; 1 else 0
# Credible interval (Bayesian CI)
credible_interval = posterior.interval(0.95)
print(f&quot;Posterior mean: {mean:.4f}&quot;)
print(f&quot;Posterior mode: {mode:.4f}&quot;)
print(f&quot;95% credible interval: [{credible_interval[0]:.4f}, {credible_interval[1]:.4f}]&quot;)
return posterior
# Weak prior (uniform)
posterior = bayesian_binomial(successes=60, trials=100, prior_alpha=1, prior_beta=1)
# Informative prior
posterior_informed = bayesian_binomial(successes=60, trials=100,
prior_alpha=30, prior_beta=70)</code></pre>
<p><strong>Bayesian A/B Testing</strong></p>
<pre><code class="lang-python">def bayesian_ab_test(conversions_a, visitors_a, conversions_b, visitors_b,
prior_alpha=1, prior_beta=1):
&quot;&quot;&quot;
Bayesian A/B test: probability that B is better than A.
&quot;&quot;&quot;
# Posterior distributions
post_a = stats.beta(prior_alpha + conversions_a,
prior_beta + visitors_a - conversions_a)
post_b = stats.beta(prior_alpha + conversions_b,
prior_beta + visitors_b - conversions_b)
# Monte Carlo: sample from posteriors and compare
n_samples = 100000
samples_a = post_a.rvs(n_samples, random_state=42)
samples_b = post_b.rvs(n_samples, random_state=42)
prob_b_better = (samples_b &gt; samples_a).mean()
# Expected loss (for decision making)
expected_loss_a = np.maximum(samples_b - samples_a, 0).mean()
expected_loss_b = np.maximum(samples_a - samples_b, 0).mean()
print(f&quot;Probability B &gt; A: {prob_b_better:.4f}&quot;)
print(f&quot;Expected loss if choosing A: {expected_loss_a:.6f}&quot;)
print(f&quot;Expected loss if choosing B: {expected_loss_b:.6f}&quot;)
return prob_b_better, expected_loss_a, expected_loss_b
prob, loss_a, loss_b = bayesian_ab_test(120, 1000, 150, 1000)</code></pre>
<p><strong>MCMC with PyMC</strong></p>
<pre><code class="lang-python"># Example: Bayesian linear regression
# Requires: pip install pymc arviz
try:
import pymc as pm
import arviz as az
# Generate synthetic data
np.random.seed(42)
n = 100
X = np.random.randn(n)
true_slope = 2.5
true_intercept = 1.0
y = true_intercept + true_slope * X + np.random.randn(n) * 0.5
with pm.Model() as model:
# Priors
intercept = pm.Normal(&#x27;intercept&#x27;, mu=0, sigma=10)
slope = pm.Normal(&#x27;slope&#x27;, mu=0, sigma=10)
sigma = pm.HalfNormal(&#x27;sigma&#x27;, sigma=1)
# Likelihood
mu = intercept + slope * X
likelihood = pm.Normal(&#x27;y&#x27;, mu=mu, sigma=sigma, observed=y)
# Inference
trace = pm.sample(2000, tune=1000, return_inferencedata=True, random_seed=42)
# Summary
print(az.summary(trace, var_names=[&#x27;intercept&#x27;, &#x27;slope&#x27;, &#x27;sigma&#x27;]))
# Posterior predictive check
with model:
ppc = pm.sample_posterior_predictive(trace, random_seed=42)
except ImportError:
print(&quot;PyMC not installed. Install with: pip install pymc arviz&quot;)</code></pre>
<h3>6. Statistical Modeling</h3>
<p><strong>Linear Regression</strong></p>
<pre><code class="lang-python">from scipy.stats import linregress
import statsmodels.api as sm
# Simple linear regression
x = np.random.randn(100)
y = 2 * x + 1 + np.random.randn(100) * 0.5
# SciPy
slope, intercept, r_value, p_value, std_err = linregress(x, y)
print(f&quot;Slope: {slope:.4f} ± {std_err:.4f}&quot;)
print(f&quot;R²: {r_value**2:.4f}&quot;)
print(f&quot;p-value: {p_value:.4e}&quot;)
# Statsmodels (more detailed)
X_with_const = sm.add_constant(x)
model = sm.OLS(y, X_with_const).fit()
print(model.summary())
# Multiple regression
X_multi = np.random.randn(100, 3)
y_multi = 2*X_multi[:, 0] + 3*X_multi[:, 1] - X_multi[:, 2] + np.random.randn(100)
X_multi_const = sm.add_constant(X_multi)
model_multi = sm.OLS(y_multi, X_multi_const).fit()
print(model_multi.summary())</code></pre>
<p><strong>Logistic Regression</strong></p>
<pre><code class="lang-python"># Binary outcome
X = np.random.randn(200, 2)
z = 1 + 2*X[:, 0] - X[:, 1]
prob = 1 / (1 + np.exp(-z))
y_binary = (np.random.rand(200) &lt; prob).astype(int)
X_const = sm.add_constant(X)
logit_model = sm.Logit(y_binary, X_const).fit()
print(logit_model.summary())
# Predictions
predictions = logit_model.predict(X_const)</code></pre>
<h2>Patterns and Best Practices</h2>
<h3>Pattern 1: Test Selection Guide</h3>
<pre><code class="lang-python">def select_statistical_test(data_type, n_groups, paired, normal):
&quot;&quot;&quot;Guide for selecting appropriate test&quot;&quot;&quot;
if data_type == &#x27;continuous&#x27;:
if n_groups == 1:
return &#x27;one-sample t-test&#x27; if normal else &#x27;Wilcoxon signed-rank&#x27;
elif n_groups == 2:
if paired:
return &#x27;paired t-test&#x27; if normal else &#x27;Wilcoxon signed-rank&#x27;
else:
return &#x27;t-test&#x27; if normal else &#x27;Mann-Whitney U&#x27;
else: # n_groups &gt; 2
return &#x27;ANOVA&#x27; if normal else &#x27;Kruskal-Wallis&#x27;
elif data_type == &#x27;categorical&#x27;:
if n_groups == 1:
return &#x27;Chi-square goodness-of-fit&#x27;
else:
return &#x27;Chi-square independence&#x27;
elif data_type == &#x27;proportion&#x27;:
return &#x27;Z-test for proportions&#x27;</code></pre>
<h3>Pattern 2: Multiple Testing Correction</h3>
<pre><code class="lang-python">from statsmodels.stats.multitest import multipletests
# Multiple comparisons: correct for family-wise error rate
p_values = np.array([0.01, 0.04, 0.03, 0.08, 0.005])
# Bonferroni correction
reject_bonf, pvals_corr_bonf, _, _ = multipletests(p_values, method=&#x27;bonferroni&#x27;)
# Benjamini-Hochberg (FDR control)
reject_bh, pvals_corr_bh, _, _ = multipletests(p_values, method=&#x27;fdr_bh&#x27;)
print(f&quot;Original p-values: {p_values}&quot;)
print(f&quot;Bonferroni corrected: {pvals_corr_bonf}&quot;)
print(f&quot;FDR corrected: {pvals_corr_bh}&quot;)</code></pre>
<h2>Anti-Patterns</h2>
<h3>Anti-Pattern 1: P-Value Misuse</h3>
<pre><code class="lang-python"># WRONG: p=0.06 means &quot;no effect&quot;
# RIGHT: p=0.06 means insufficient evidence against H0 at α=0.05
# WRONG: p=0.001 means &quot;large effect&quot;
# RIGHT: Statistical significance ≠ practical significance
# Always report effect size!</code></pre>
<h3>Anti-Pattern 2: Ignoring Assumptions</h3>
<pre><code class="lang-python"># WRONG: Using t-test without checking normality
ttest_ind(group1, group2)
# RIGHT: Check assumptions first
from scipy.stats import shapiro
stat, p = shapiro(group1)
if p &lt; 0.05:
print(&quot;Data not normal, use Mann-Whitney U instead&quot;)
mannwhitneyu(group1, group2)</code></pre>
<h3>Anti-Pattern 3: Sample Size Too Small</h3>
<pre><code class="lang-python"># WRONG: Testing with n=5 per group
small_group1 = np.random.randn(5)
small_group2 = np.random.randn(5) + 0.5
ttest_ind(small_group1, small_group2) # Low power!
# RIGHT: Calculate required sample size first
# Use power analysis before collecting data</code></pre>
<h2>Related Skills</h2>
<ul><li><code>linear-algebra-computation.md</code> - Matrix operations for statistics</li><li><code>numerical-methods.md</code> - Numerical integration, root finding</li><li><code>optimization-algorithms.md</code> - Maximum likelihood estimation</li></ul>
<h2>Learning Resources</h2>
<ul><li>SciPy Stats: https://docs.scipy.org/doc/scipy/reference/stats.html</li><li>Statsmodels: https://www.statsmodels.org/</li><li>Think Stats (Allen Downey)</li><li>Statistical Rethinking (Richard McElreath)</li></ul>
<footer>
<div class="fmap">
<div><b>Build</b>
<a href="https://hanzo.app">hanzo.app</a> app builder
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
<div><b>Run</b>
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
<div><b>Account</b>
<a href="https://hanzo.id">hanzo.id</a> sign in
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
<div><b>Company</b>
<a href="https://hanzo.ai">hanzo.ai</a> canonical
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
</div>
<div class="fend"><a href="/math-probability-statistics.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
</footer>
</div></body></html>