Example 19
intermediate
19
Statistics
Hypothesis Testing
Correlation

Statistical Analysis

This example covers the full statistical analysis toolkit in Deepbox. You compute descriptive statistics (mean, median, mode, standard deviation, variance, skewness, kurtosis, percentiles) on sample data, perform hypothesis tests (ttest_1samp to test if a mean differs from a known value, ttest_ind to compare two independent groups, shapiro for normality testing, f_oneway for one-way ANOVA), and compute correlations (pearsonr for linear relationships, spearmanr for monotonic relationships, kendalltau for ordinal data). Each test returns a statistic and p-value, and the example explains how to interpret them with significance level α = 0.05.

Deepbox Modules Used

deepbox/ndarraydeepbox/stats

What You Will Learn

  • Compute descriptive statistics: mean, median, std, skewness, kurtosis, percentiles
  • Measure relationships with pearsonr (linear), spearmanr (monotonic), kendalltau (ordinal)
  • Use ttest_1samp to test if a sample mean differs from a known value
  • Use shapiro to test if data follows a normal distribution
  • Interpret p-values: reject H₀ when p < 0.05 (significance level)

Source Code

19-statistics/index.ts
1import { tensor } from "deepbox/ndarray";2import {3  mean, median, std, variance, skewness, kurtosis, percentile,4  pearsonr, spearmanr, kendalltau,5  ttest_1samp, ttest_ind, shapiro, f_oneway6} from "deepbox/stats";78console.log("=== Statistical Analysis ===\n");910const data = tensor([2, 4, 4, 4, 5, 5, 7, 9]);1112// Descriptive statistics13console.log("Mean:    ", mean(data).toString());14console.log("Median:  ", median(data).toString());15console.log("Std Dev: ", std(data).toString());16console.log("Variance:", variance(data).toString());17console.log("Skewness:", skewness(data).toString());18console.log("Kurtosis:", kurtosis(data).toString());19console.log("P75:     ", percentile(data, 75).toString());2021// Correlation22const x = tensor([1, 2, 3, 4, 5]);23const y = tensor([2, 4, 5, 4, 5]);24const [r, pval] = pearsonr(x, y);25console.log("\nPearson r:", r.toFixed(4), "p-value:", pval.toFixed(4));26const [rho, p2] = spearmanr(x, y);27console.log("Spearman:", rho.toFixed(4));2829// Hypothesis tests30const sample = tensor([2.3, 1.9, 2.5, 2.1, 2.7]);31const { statistic, pvalue } = ttest_1samp(sample, 0);32console.log("\nt-test vs 0: t=", statistic.toFixed(3), "p=", pvalue.toFixed(6));33if (pvalue < 0.05) console.log("→ Reject H₀: mean ≠ 0");3435// Normality test36const norm = shapiro(tensor([1.2, 2.3, 1.8, 2.1, 1.9, 2.5, 2.0]));37console.log("\nShapiro-Wilk p:", norm.pvalue.toFixed(4));38if (norm.pvalue > 0.05) console.log("→ Cannot reject normality");

Console Output

$ npx tsx 19-statistics/index.ts
=== Statistical Analysis ===

Mean:     5.0
Median:   4.5
Std Dev:  2.0
Variance: 4.0
Skewness: 0.656
Kurtosis: -0.762
P75:      6.5

Pearson r: 0.8321  p-value: 0.0805
Spearman: 0.8208

t-test vs 0: t= 14.907 p= 0.000121
→ Reject H₀: mean ≠ 0

Shapiro-Wilk p: 0.9372
→ Cannot reject normality