Example 43
intermediate
43
Statistics
Tensors

Statistical Distributions & Hypothesis Tests

Comprehensive statistics module new in v1.0.0: probability distributions, hypothesis tests, correlations, and confidence intervals. This example uses deepbox/stats, deepbox/ndarray and focuses on norm, t, chi2, binom, poisson, ttest_1samp, ttest_ind, chisquare, kstest, shapiro, pearsonr, spearmanr, kendalltau, f_oneway; tensor.

Deepbox Modules Used

deepbox/statsdeepbox/ndarray

What You Will Learn

  • Use deepbox/stats for norm, t, chi2, binom, poisson, ttest_1samp, ttest_ind, chisquare, kstest, shapiro, pearsonr, spearmanr, kendalltau, f_oneway.
  • Use deepbox/ndarray for tensor.
  • Comprehensive statistics module new in v1.0.0: probability distributions, hypothesis tests, correlations, and confidence intervals.

Source Files

index.ts
1/**2 * Example 43: Statistical Distributions & Hypothesis Tests3 *4 * New in v1.0.0: Full stats module with probability distributions (normal, t,5 * chi2, F, binomial, Poisson, etc.), hypothesis tests (t-test, chi-square,6 * KS, Shapiro-Wilk, ANOVA), correlations, and confidence intervals.7 */89import { tensor } from "deepbox/ndarray";10import {11  beta,12  binom,13  chi2,14  chisquare,15  expon,16  f_oneway,17  kendalltau,18  kstest,19  norm,20  pearsonr,21  poisson,22  shapiro,23  spearmanr,24  t,25  ttest_1samp,26  ttest_ind,27  ttest_rel,28  uniform,29} from "deepbox/stats";3031console.log("=".repeat(60));32console.log("Example 43: Statistical Distributions & Hypothesis Tests");33console.log("=".repeat(60));3435// ============================================================================36// Part 1: Continuous Distributions37// ============================================================================38console.log("\n📊 Part 1: Continuous Distributions");39console.log("-".repeat(60));4041// Normal distribution42const normal = norm(0, 1); // mean=0, std=143console.log("Normal(0, 1):");44console.log(`  PDF at x=0:   ${normal.pdf(0).toFixed(6)}`);45console.log(`  CDF at x=0:   ${normal.cdf(0).toFixed(6)}`);46console.log(`  PPF at p=0.975: ${normal.ppf(0.975).toFixed(6)} (z-critical)`);47console.log(`  Mean:  ${normal.mean().toFixed(4)}, Var: ${normal.variance().toFixed(4)}`);4849// Student's t distribution50const tDist = t(10); // 10 degrees of freedom51console.log("\nStudent's t(df=10):");52console.log(`  PDF at x=0:   ${tDist.pdf(0).toFixed(6)}`);53console.log(`  CDF at x=2:   ${tDist.cdf(2).toFixed(6)}`);54console.log(`  PPF at p=0.975: ${tDist.ppf(0.975).toFixed(6)}`);5556// Chi-squared distribution57const chi2Dist = chi2(5); // 5 degrees of freedom58console.log("\nChi-squared(df=5):");59console.log(`  PDF at x=5:   ${chi2Dist.pdf(5).toFixed(6)}`);60console.log(`  CDF at x=5:   ${chi2Dist.cdf(5).toFixed(6)}`);61console.log(`  Mean: ${chi2Dist.mean().toFixed(4)}`);6263// Exponential distribution64const expDist = expon(0.5); // rate=0.565console.log("\nExponential(rate=0.5):");66console.log(`  PDF at x=1:   ${expDist.pdf(1).toFixed(6)}`);67console.log(`  CDF at x=2:   ${expDist.cdf(2).toFixed(6)}`);68console.log(`  Mean: ${expDist.mean().toFixed(4)}`);6970// Beta distribution71const betaDist = beta(2, 5);72console.log("\nBeta(α=2, β=5):");73console.log(`  PDF at x=0.3: ${betaDist.pdf(0.3).toFixed(6)}`);74console.log(`  Mean: ${betaDist.mean().toFixed(4)}`);7576// Uniform distribution77const uniformDist = uniform(0, 10);78console.log("\nUniform(0, 10):");79console.log(`  PDF at x=5:   ${uniformDist.pdf(5).toFixed(6)}`);80console.log(`  Mean: ${uniformDist.mean().toFixed(4)}`);8182// ============================================================================83// Part 2: Discrete Distributions84// ============================================================================85console.log("\n🎲 Part 2: Discrete Distributions");86console.log("-".repeat(60));8788// Binomial distribution89const binDist = binom(20, 0.3); // 20 trials, p=0.390console.log("Binomial(n=20, p=0.3):");91console.log(`  PMF at k=6:   ${binDist.pmf(6).toFixed(6)}`);92console.log(`  CDF at k=6:   ${binDist.cdf(6).toFixed(6)}`);93console.log(`  Mean: ${binDist.mean().toFixed(4)}, Var: ${binDist.variance().toFixed(4)}`);9495// Poisson distribution96const poisDist = poisson(5); // lambda=597console.log("\nPoisson(λ=5):");98console.log(`  PMF at k=5:   ${poisDist.pmf(5).toFixed(6)}`);99console.log(`  PMF at k=3:   ${poisDist.pmf(3).toFixed(6)}`);100console.log(`  CDF at k=7:   ${poisDist.cdf(7).toFixed(6)}`);101console.log(`  Mean: ${poisDist.mean().toFixed(4)}, Var: ${poisDist.variance().toFixed(4)}`);102103// ============================================================================104// Part 3: T-Tests105// ============================================================================106console.log("\n🧪 Part 3: T-Tests");107console.log("-".repeat(60));108109// One-sample t-test: is the mean significantly different from 0?110const sample1 = tensor([2.1, 2.5, 2.3, 2.8, 2.2, 2.6, 2.4, 2.7, 2.3, 2.5]);111const onesamp = ttest_1samp(sample1, 0);112console.log("One-sample t-test (H0: mean = 0):");113console.log(`  t-statistic: ${onesamp.statistic.toFixed(4)}`);114console.log(`  p-value:     ${onesamp.pvalue.toFixed(6)}`);115console.log(`  Reject H0 at α=0.05: ${onesamp.pvalue < 0.05 ? "Yes" : "No"}`);116117// Independent two-sample t-test118const groupA = tensor([5.1, 5.3, 5.0, 5.5, 5.2, 5.4, 5.1, 5.3]);119const groupB = tensor([4.8, 4.6, 4.9, 4.5, 4.7, 4.6, 4.8, 4.5]);120const twosamp = ttest_ind(groupA, groupB);121console.log("\nIndependent two-sample t-test (H0: mean_A = mean_B):");122console.log(`  t-statistic: ${twosamp.statistic.toFixed(4)}`);123console.log(`  p-value:     ${twosamp.pvalue.toFixed(6)}`);124console.log(`  Reject H0 at α=0.05: ${twosamp.pvalue < 0.05 ? "Yes" : "No"}`);125126// Paired t-test127const before = tensor([85, 90, 78, 92, 88, 76, 95, 89]);128const after = tensor([88, 93, 82, 95, 91, 80, 97, 92]);129const paired = ttest_rel(before, after);130console.log("\nPaired t-test (H0: no difference before/after):");131console.log(`  t-statistic: ${paired.statistic.toFixed(4)}`);132console.log(`  p-value:     ${paired.pvalue.toFixed(6)}`);133console.log(`  Reject H0 at α=0.05: ${paired.pvalue < 0.05 ? "Yes" : "No"}`);134135// ============================================================================136// Part 4: Chi-Square Test137// ============================================================================138console.log("\n📐 Part 4: Chi-Square Goodness of Fit");139console.log("-".repeat(60));140141// Test if observed frequencies match expected (uniform)142const observed = tensor([18, 22, 20, 15, 25]);143const expected = tensor([20, 20, 20, 20, 20]);144const chiResult = chisquare(observed, expected);145console.log("Chi-square test (H0: observed matches expected):");146console.log(`  Observed: [18, 22, 20, 15, 25]`);147console.log(`  Expected: [20, 20, 20, 20, 20]`);148console.log(`  χ² statistic: ${chiResult.statistic.toFixed(4)}`);149console.log(`  p-value:      ${chiResult.pvalue.toFixed(6)}`);150console.log(`  Reject H0 at α=0.05: ${chiResult.pvalue < 0.05 ? "Yes" : "No"}`);151152// ============================================================================153// Part 5: Normality Tests154// ============================================================================155console.log("\n📏 Part 5: Normality Tests");156console.log("-".repeat(60));157158// Shapiro-Wilk test for normality159const normalSample = tensor([160  -0.2, 0.5, 1.1, -0.8, 0.3, -0.1, 0.7, -0.4, 0.9, -0.6, 0.2, 0.8, -0.3, 0.4, -0.7, 0.1, 0.6, -0.5,161  0.0, 0.3,162]);163const shapResult = shapiro(normalSample);164console.log("Shapiro-Wilk test (H0: data is normally distributed):");165console.log(`  W-statistic: ${shapResult.statistic.toFixed(4)}`);166console.log(`  p-value:     ${shapResult.pvalue.toFixed(6)}`);167console.log(168  `  Normal at α=0.05: ${shapResult.pvalue > 0.05 ? "Yes (fail to reject)" : "No (rejected)"}`169);170171// KS test against normal distribution172const ksResult = kstest(normalSample, "norm");173console.log("\nKolmogorov-Smirnov test (H0: data follows standard normal):");174console.log(`  KS statistic: ${ksResult.statistic.toFixed(4)}`);175console.log(`  p-value:      ${ksResult.pvalue.toFixed(6)}`);176177// ============================================================================178// Part 6: ANOVA (One-way)179// ============================================================================180console.log("\n📊 Part 6: One-way ANOVA");181console.log("-".repeat(60));182183// Test if three groups have the same mean184const group1 = tensor([6.1, 5.8, 6.3, 5.9, 6.0]);185const group2 = tensor([7.2, 7.0, 7.4, 7.1, 7.3]);186const group3 = tensor([5.5, 5.3, 5.7, 5.4, 5.6]);187const anovaResult = f_oneway(group1, group2, group3);188console.log("One-way ANOVA (H0: all group means are equal):");189console.log(`  Groups: [~6.0], [~7.2], [~5.5]`);190console.log(`  F-statistic: ${anovaResult.statistic.toFixed(4)}`);191console.log(`  p-value:     ${anovaResult.pvalue.toFixed(6)}`);192console.log(`  Reject H0 at α=0.05: ${anovaResult.pvalue < 0.05 ? "Yes" : "No"}`);193194// ============================================================================195// Part 7: Correlation Analysis196// ============================================================================197console.log("\n🔗 Part 7: Correlation Analysis");198console.log("-".repeat(60));199200const x = tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);201const y = tensor([2.1, 3.9, 6.2, 7.8, 10.1, 12.0, 13.8, 16.1, 18.2, 19.9]);202203// Pearson correlation (linear relationship)204const [rPearson, pPearson] = pearsonr(x, y);205console.log("Pearson correlation (linear relationship):");206console.log(`  r = ${rPearson.toFixed(4)}, p-value = ${pPearson.toFixed(6)}`);207208// Spearman rank correlation (monotonic relationship)209const [rSpearman, pSpearman] = spearmanr(x, y);210console.log("\nSpearman rank correlation (monotonic relationship):");211console.log(`  ρ = ${rSpearman.toFixed(4)}, p-value = ${pSpearman.toFixed(6)}`);212213// Kendall's tau (ordinal association)214const [tauKendall, pKendall] = kendalltau(x, y);215console.log("\nKendall's tau (ordinal association):");216console.log(`  τ = ${tauKendall.toFixed(4)}, p-value = ${pKendall.toFixed(6)}`);217218// Non-linear relationship example219const xNonlin = tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);220const yNonlin = tensor([1, 4, 9, 16, 25, 36, 49, 64, 81, 100]); // y = x²221const [rLin] = pearsonr(xNonlin, yNonlin);222const [rMono] = spearmanr(xNonlin, yNonlin);223console.log(224  `\nNon-linear (y=x²): Pearson r = ${rLin.toFixed(4)}, Spearman ρ = ${rMono.toFixed(4)}`225);226console.log("  (Spearman captures monotonic relationship better)");227228// ============================================================================229// Part 8: Distribution Sampling & Quantiles230// ============================================================================231console.log("\n🎯 Part 8: Distribution Quantiles & Summary");232console.log("-".repeat(60));233234const stdNorm = norm(0, 1);235console.log("Standard Normal — Key Quantiles:");236for (const p of [0.01, 0.025, 0.05, 0.5, 0.95, 0.975, 0.99]) {237  console.log(`  P(X < ${stdNorm.ppf(p).toFixed(4).padStart(7)}) = ${p}`);238}239240console.log("\n68-95-99.7 Rule Verification:");241console.log(`  P(-1 < X < 1) = ${(stdNorm.cdf(1) - stdNorm.cdf(-1)).toFixed(4)} (expect ~0.6827)`);242console.log(`  P(-2 < X < 2) = ${(stdNorm.cdf(2) - stdNorm.cdf(-2)).toFixed(4)} (expect ~0.9545)`);243console.log(`  P(-3 < X < 3) = ${(stdNorm.cdf(3) - stdNorm.cdf(-3)).toFixed(4)} (expect ~0.9973)`);244245// ============================================================================246// Summary247// ============================================================================248console.log("\n💡 Key Takeaways");249console.log("-".repeat(60));250console.log("• Continuous distributions: norm, t, chi2, F, expon, beta, uniform, gamma, etc.");251console.log("• Discrete distributions: binom, poisson, geom, hypergeom, nbinom");252console.log("• Each distribution has: pdf/pmf, cdf, ppf (quantile), mean, variance");253console.log("• T-tests: one-sample, independent, paired — compare means");254console.log("• Chi-square: goodness of fit and contingency tables");255console.log("• Normality: Shapiro-Wilk, KS test, Anderson-Darling");256console.log("• ANOVA: compare means across 3+ groups");257console.log("• Correlations: Pearson (linear), Spearman (monotonic), Kendall (ordinal)");258259console.log("\n✅ Statistical Distributions & Tests Example Complete!");260console.log("=".repeat(60));261

Console Output

$ npx tsx 43-statistical-tests/index.ts
See the example README for the expected console and artifact output.