mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
hanzoskills.com/llms.txt now opens with the install line, the API reference index and the company index, so an agent that lands on any one of the three can find the rest. robots.txt claimed 537 documents while llms.txt counts 920. The number is gone from robots.txt; llms.txt derives it. public/ is regenerated from scripts/site.py, which it had fallen behind: the copy-prompt menu and agent bar were in the generator and not in the committed pages. tests/site.mjs passes against the result. Co-authored-by: Hanzo Dev <[email protected]>
747 lines
32 KiB
HTML
747 lines
32 KiB
HTML
<!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="alternate" type="text/markdown" href="/math-probability-statistics.md">
|
||
|
||
<link rel="icon" href="/favicon.svg">
|
||
<link rel="canonical" href="https://hanzoskills.com/math-probability-statistics">
|
||
</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"95th percentile: {quantile:.4f}") # ~1.645
|
||
|
||
# Random sampling
|
||
samples = normal.rvs(size=10000, random_state=42)
|
||
print(f"Sample mean: {samples.mean():.4f}")
|
||
print(f"Sample std: {samples.std():.4f}")
|
||
|
||
# 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"P(X=3) = {prob_3:.4f}")
|
||
|
||
# Probability of at most 5 successes
|
||
prob_le_5 = binomial.cdf(5)
|
||
print(f"P(X≤5) = {prob_le_5:.4f}")
|
||
|
||
# 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"t-statistic: {t_stat:.4f}")
|
||
print(f"p-value: {p_value:.4f}")
|
||
|
||
if p_value < 0.05:
|
||
print("Reject H0: mean is significantly different from 0")
|
||
else:
|
||
print("Fail to reject H0")
|
||
|
||
# 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'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"Paired t-test p-value: {p_value:.4f}")</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"Chi-square goodness-of-fit p-value: {p_value:.4f}")
|
||
|
||
# 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"Chi-square independence test:")
|
||
print(f"χ² = {chi2:.4f}, p = {p_value:.4f}, dof = {dof}")
|
||
|
||
if p_value < 0.05:
|
||
print("Reject H0: variables are not independent")</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"ANOVA F-statistic: {f_stat:.4f}, p-value: {p_value:.4f}")
|
||
|
||
# Kruskal-Wallis test (non-parametric alternative)
|
||
h_stat, p_value = kruskal(group1, group2, group3)
|
||
print(f"Kruskal-Wallis H: {h_stat:.4f}, p-value: {p_value:.4f}")</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='two-sided')
|
||
|
||
# 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, 'norm')
|
||
print(f"KS test p-value: {p_value:.4f}")</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):
|
||
"""
|
||
Compute confidence interval for mean.
|
||
"""
|
||
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"95% CI for mean: [{ci[0]:.4f}, {ci[1]:.4f}]")
|
||
|
||
# Proportion confidence interval (Wilson score interval)
|
||
def confidence_interval_proportion(successes, n, confidence=0.95):
|
||
"""Confidence interval for proportion."""
|
||
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"95% CI for proportion: [{ci_prop[0]:.4f}, {ci_prop[1]:.4f}]")</code></pre>
|
||
<p><strong>Bootstrap Confidence Intervals</strong></p>
|
||
<pre><code class="lang-python">from scipy.stats import bootstrap
|
||
|
||
def median_statistic(data, axis):
|
||
"""Statistic function for bootstrap"""
|
||
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"95% CI for median: [{result.confidence_interval.low:.4f}, "
|
||
f"{result.confidence_interval.high:.4f}]")
|
||
|
||
# Manual bootstrap
|
||
def bootstrap_ci(data, statistic, n_bootstrap=10000, confidence=0.95):
|
||
"""Manual bootstrap confidence interval"""
|
||
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):
|
||
"""
|
||
A/B test for conversion rates.
|
||
"""
|
||
# 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's h)
|
||
h = 2 * (np.arcsin(np.sqrt(rate_b)) - np.arcsin(np.sqrt(rate_a)))
|
||
|
||
print(f"Control rate: {rate_a:.4f}")
|
||
print(f"Treatment rate: {rate_b:.4f}")
|
||
print(f"Relative improvement: {relative_improvement:.2f}%")
|
||
print(f"p-value: {p_value:.4f}")
|
||
print(f"Effect size (h): {h:.4f}")
|
||
|
||
if p_value < alpha:
|
||
print("Result is statistically significant")
|
||
else:
|
||
print("Result is not statistically significant")
|
||
|
||
return {
|
||
'rate_a': rate_a,
|
||
'rate_b': rate_b,
|
||
'p_value': p_value,
|
||
'relative_improvement': relative_improvement,
|
||
'effect_size': 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):
|
||
"""
|
||
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
|
||
"""
|
||
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='two-sided')
|
||
|
||
return int(np.ceil(n))
|
||
|
||
n_required = calculate_sample_size(baseline_rate=0.10, mde=0.02)
|
||
print(f"Required sample size per group: {n_required}")</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):
|
||
"""
|
||
Bayesian inference for binomial proportion.
|
||
"""
|
||
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 > 1 else 0
|
||
|
||
# Credible interval (Bayesian CI)
|
||
credible_interval = posterior.interval(0.95)
|
||
|
||
print(f"Posterior mean: {mean:.4f}")
|
||
print(f"Posterior mode: {mode:.4f}")
|
||
print(f"95% credible interval: [{credible_interval[0]:.4f}, {credible_interval[1]:.4f}]")
|
||
|
||
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):
|
||
"""
|
||
Bayesian A/B test: probability that B is better than A.
|
||
"""
|
||
# 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 > 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"Probability B > A: {prob_b_better:.4f}")
|
||
print(f"Expected loss if choosing A: {expected_loss_a:.6f}")
|
||
print(f"Expected loss if choosing B: {expected_loss_b:.6f}")
|
||
|
||
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('intercept', mu=0, sigma=10)
|
||
slope = pm.Normal('slope', mu=0, sigma=10)
|
||
sigma = pm.HalfNormal('sigma', sigma=1)
|
||
|
||
# Likelihood
|
||
mu = intercept + slope * X
|
||
likelihood = pm.Normal('y', 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=['intercept', 'slope', 'sigma']))
|
||
|
||
# Posterior predictive check
|
||
with model:
|
||
ppc = pm.sample_posterior_predictive(trace, random_seed=42)
|
||
|
||
except ImportError:
|
||
print("PyMC not installed. Install with: pip install pymc arviz")</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"Slope: {slope:.4f} ± {std_err:.4f}")
|
||
print(f"R²: {r_value**2:.4f}")
|
||
print(f"p-value: {p_value:.4e}")
|
||
|
||
# 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) < 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):
|
||
"""Guide for selecting appropriate test"""
|
||
if data_type == 'continuous':
|
||
if n_groups == 1:
|
||
return 'one-sample t-test' if normal else 'Wilcoxon signed-rank'
|
||
elif n_groups == 2:
|
||
if paired:
|
||
return 'paired t-test' if normal else 'Wilcoxon signed-rank'
|
||
else:
|
||
return 't-test' if normal else 'Mann-Whitney U'
|
||
else: # n_groups > 2
|
||
return 'ANOVA' if normal else 'Kruskal-Wallis'
|
||
elif data_type == 'categorical':
|
||
if n_groups == 1:
|
||
return 'Chi-square goodness-of-fit'
|
||
else:
|
||
return 'Chi-square independence'
|
||
elif data_type == 'proportion':
|
||
return 'Z-test for proportions'</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='bonferroni')
|
||
|
||
# Benjamini-Hochberg (FDR control)
|
||
reject_bh, pvals_corr_bh, _, _ = multipletests(p_values, method='fdr_bh')
|
||
|
||
print(f"Original p-values: {p_values}")
|
||
print(f"Bonferroni corrected: {pvals_corr_bonf}")
|
||
print(f"FDR corrected: {pvals_corr_bh}")</code></pre>
|
||
<h2>Anti-Patterns</h2>
|
||
<h3>Anti-Pattern 1: P-Value Misuse</h3>
|
||
<pre><code class="lang-python"># WRONG: p=0.06 means "no effect"
|
||
# RIGHT: p=0.06 means insufficient evidence against H0 at α=0.05
|
||
|
||
# WRONG: p=0.001 means "large effect"
|
||
# 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 < 0.05:
|
||
print("Data not normal, use Mann-Whitney U instead")
|
||
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> |