UNPKG

als-statistics

Version:

A powerful and lightweight JavaScript library for descriptive statistics, regression, clustering, outlier detection, and noise analysis using a flexible table/column architecture.

38 lines (34 loc) 1.56 kB
const PearsonP = require('./pearson') class Comparative { constructor(sample1, sample2) { if (sample1.n !== sample2.n) throw new Error("Length of samples must match"); this.sample1 = sample1; this.sample2 = sample2; this.n = this.sample1.n } get sumCov() { const { sample1: { mean: m1, values: values1 }, sample2: { mean: m2, values: values2 }, n } = this let sumCov = 0; for (let i = 0; i < n; i++) { sumCov += (values1[i] - m1) * (values2[i] - m2) } return sumCov } get covariancePopulation() { return this.sumCov / this.n } get covarianceSample() { return this.n < 2 ? 0 : this.sumCov / (this.n - 1) } get correlationPopulation() { return this.pearsonPopulation.r } get correlationSample() { return this.pearsonSample.r } get pearsonPopulation() { return new PearsonP(this.n, this.covariancePopulation, this.sample1.stdDevPopulation, this.sample2.stdDevPopulation) } get pearsonSample() { return new PearsonP(this.n, this.covarianceSample, this.sample1.stdDevSample, this.sample2.stdDevSample) } twoSampleTTest() { const { n, sample1: { mean: m1, varianceSample: var1 }, sample2: { mean: m2, varianceSample: var2 } } = this const pooledVariance = ((n - 1) * var1 + (n - 1) * var2) / (2 * n - 2); const sp = Math.sqrt(pooledVariance); const t = (m1 - m2) / (sp * Math.sqrt(2 / n)); const df = 2 * n - 2; const F = var1 / var2; return { t, df, F }; } } module.exports = Comparative