CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-simple-statistics

A JavaScript implementation of descriptive, regression, and inference statistics

Pending
Overview
Eval results
Files

testing.mddocs/

Statistical Testing

Functions for hypothesis testing and goodness-of-fit analysis.

Core Imports

import { 
  tTest,
  tTestTwoSample,
  chiSquaredGoodnessOfFit,
  permutationTest,
  chiSquaredDistributionTable
} from "simple-statistics";

t-Tests

tTest { .api }

function tTest(sample: number[], expectedValue: number): number;

Performs a one-sample t-test to determine if the sample mean differs significantly from an expected value.

Parameters:

  • sample: number[] - Array of sample values
  • expectedValue: number - Hypothesized population mean

Returns: number - t-statistic value

Interpretation:

  • Larger absolute t-values indicate greater evidence against null hypothesis
  • Compare with critical t-value from t-distribution table
import { tTest } from "simple-statistics";

// Test if average height differs from 170cm
const heights = [165, 170, 175, 168, 172, 169, 171, 173];
const expectedHeight = 170;

const tStatistic = tTest(heights, expectedHeight);
// tStatistic ≈ 0.816

// For α=0.05, df=7: critical value ≈ ±2.365
// |0.816| < 2.365, so we fail to reject H₀
// No significant difference from 170cm

tTestTwoSample { .api }

function tTestTwoSample(sampleX: number[], sampleY: number[], difference?: number): number | null;

Performs a two-sample t-test to compare means of two independent groups.

Parameters:

  • sampleX: number[] - First sample
  • sampleY: number[] - Second sample
  • difference?: number - Expected difference between means (default: 0)

Returns: number | null - t-statistic value, or null if calculation fails

import { tTestTwoSample } from "simple-statistics";

// Compare test scores between two teaching methods
const method1 = [85, 90, 88, 92, 87, 89, 91];
const method2 = [78, 82, 80, 85, 83, 81, 84];

const tStatistic = tTestTwoSample(method1, method2);
// tStatistic ≈ 4.12

// If |t| > critical value, methods show significant difference
console.log(`t-statistic: ${tStatistic}`);

Chi-Squared Tests

chiSquaredGoodnessOfFit { .api }

function chiSquaredGoodnessOfFit(data: number[], distributionType: Function, significance: number): boolean;

Tests whether observed data follows an expected distribution.

Parameters:

  • data: number[] - Observed frequency data
  • distributionType: Function - Expected distribution function
  • significance: number - Significance level (e.g., 0.05)

Returns: boolean - true if data fits distribution at given significance level

import { chiSquaredGoodnessOfFit, poissonDistribution } from "simple-statistics";

// Test if customer arrivals follow Poisson distribution
const observedArrivals = [5, 8, 12, 18, 15, 10, 7, 3, 2, 1]; // frequencies
const expectedLambda = 3.2; // average arrivals
const expectedDistribution = () => poissonDistribution(expectedLambda);

const fitsPoisson = chiSquaredGoodnessOfFit(observedArrivals, expectedDistribution, 0.05);
console.log(`Fits Poisson distribution: ${fitsPoisson}`);

chiSquaredDistributionTable { .api }

const chiSquaredDistributionTable: Record<number, Record<number, number>>;

Critical values for chi-squared distribution lookups.

import { chiSquaredDistributionTable } from "simple-statistics";

// Get critical value for df=3, α=0.05
const criticalValue = chiSquaredDistributionTable[3][0.05]; // 7.815

Non-Parametric Tests

permutationTest { .api }

function permutationTest(sampleX: number[], sampleY: number[], testType?: string, k?: number): number;

Performs a permutation test - a non-parametric alternative when distributional assumptions aren't met.

Parameters:

  • sampleX: number[] - First sample
  • sampleY: number[] - Second sample
  • testType?: string - Type of test (optional)
  • k?: number - Number of permutations (optional)

Returns: number - p-value for the test

import { permutationTest } from "simple-statistics";

// Compare two groups without assuming normal distribution
const groupA = [23, 25, 28, 22, 26, 24, 27];
const groupB = [31, 33, 35, 30, 32, 29, 34];

const pValue = permutationTest(groupA, groupB);
console.log(`p-value: ${pValue}`);

// If p < 0.05, reject null hypothesis of no difference
if (pValue < 0.05) {
  console.log("Significant difference between groups");
} else {
  console.log("No significant difference detected");
}

Usage Examples

Complete Hypothesis Testing Workflow

import { 
  tTest, 
  tTestTwoSample, 
  sampleVariance, 
  mean, 
  standardDeviation 
} from "simple-statistics";

// Drug effectiveness study
const placeboGroup = [2.1, 2.5, 1.8, 2.3, 2.0, 2.4, 1.9, 2.2];
const treatmentGroup = [3.2, 3.8, 3.1, 3.5, 3.0, 3.6, 2.9, 3.4];

// Descriptive statistics
console.log("Placebo group:");
console.log(`Mean: ${mean(placeboGroup).toFixed(2)}`);
console.log(`Std Dev: ${standardDeviation(placeboGroup).toFixed(2)}`);

console.log("Treatment group:");
console.log(`Mean: ${mean(treatmentGroup).toFixed(2)}`);
console.log(`Std Dev: ${standardDeviation(treatmentGroup).toFixed(2)}`);

// Test if treatment group differs from placebo
const tStat = tTestTwoSample(placeboGroup, treatmentGroup);
console.log(`Two-sample t-statistic: ${tStat?.toFixed(3)}`);

// Test if treatment achieves target improvement of 1.0 unit
const targetImprovement = 1.0;
const actualImprovement = mean(treatmentGroup) - mean(placeboGroup);
const improvementTest = tTest(treatmentGroup, mean(placeboGroup) + targetImprovement);
console.log(`Target improvement test: ${improvementTest.toFixed(3)}`);

A/B Testing Analysis

import { tTestTwoSample, permutationTest, mean } from "simple-statistics";

// Website conversion rates (percentages)
const versionA = [2.1, 2.3, 1.9, 2.4, 2.0, 2.2, 1.8, 2.5]; // Control
const versionB = [2.8, 3.1, 2.6, 3.0, 2.9, 3.2, 2.7, 3.3]; // Treatment

const meanA = mean(versionA);
const meanB = mean(versionB);
const improvement = ((meanB - meanA) / meanA * 100);

console.log(`Version A average: ${meanA.toFixed(2)}%`);
console.log(`Version B average: ${meanB.toFixed(2)}%`);
console.log(`Relative improvement: ${improvement.toFixed(1)}%`);

// Parametric test
const tStat = tTestTwoSample(versionA, versionB);
console.log(`t-test statistic: ${tStat?.toFixed(3)}`);

// Non-parametric alternative
const pValue = permutationTest(versionA, versionB);
console.log(`Permutation test p-value: ${pValue.toFixed(4)}`);

if (pValue < 0.05) {
  console.log("✓ Statistically significant improvement");
} else {
  console.log("✗ No significant difference detected");
}

Quality Control Testing

import { tTest, chiSquaredGoodnessOfFit } from "simple-statistics";

// Manufacturing quality control
const targetWeight = 500; // grams
const batchWeights = [498, 502, 499, 501, 500, 497, 503, 498, 501, 499];

// Test if batch meets target weight
const weightTest = tTest(batchWeights, targetWeight);
console.log(`Weight test t-statistic: ${weightTest.toFixed(3)}`);

// Test process control
const toleranceLimit = 2.58; // 99% confidence (±2.58 standard deviations)
if (Math.abs(weightTest) > toleranceLimit) {
  console.log("⚠️  Process out of control - investigate immediately");
} else {
  console.log("✓ Process within control limits");
}

// Distribution testing for defect patterns
const defectCounts = [12, 8, 5, 3, 1, 1]; // defects per day
const expectedPoisson = () => [10, 8, 6, 4, 2, 1]; // expected pattern

const followsPattern = chiSquaredGoodnessOfFit(defectCounts, () => expectedPoisson(), 0.05);
console.log(`Defects follow expected pattern: ${followsPattern}`);

Install with Tessl CLI

npx tessl i tessl/npm-simple-statistics

docs

array-operations.md

combinatorics.md

data-manipulation.md

descriptive-statistics.md

distributions.md

index.md

machine-learning.md

math-utilities.md

quantiles.md

regression.md

testing.md

tile.json