0
# Statistical Testing
1
2
Functions for hypothesis testing and goodness-of-fit analysis.
3
4
## Core Imports
5
6
```typescript
7
import {
8
tTest,
9
tTestTwoSample,
10
chiSquaredGoodnessOfFit,
11
permutationTest,
12
chiSquaredDistributionTable
13
} from "simple-statistics";
14
```
15
16
## t-Tests
17
18
### tTest { .api }
19
20
```typescript { .api }
21
function tTest(sample: number[], expectedValue: number): number;
22
```
23
24
Performs a one-sample t-test to determine if the sample mean differs significantly from an expected value.
25
26
**Parameters:**
27
- `sample: number[]` - Array of sample values
28
- `expectedValue: number` - Hypothesized population mean
29
30
**Returns:** `number` - t-statistic value
31
32
**Interpretation:**
33
- Larger absolute t-values indicate greater evidence against null hypothesis
34
- Compare with critical t-value from t-distribution table
35
36
```typescript
37
import { tTest } from "simple-statistics";
38
39
// Test if average height differs from 170cm
40
const heights = [165, 170, 175, 168, 172, 169, 171, 173];
41
const expectedHeight = 170;
42
43
const tStatistic = tTest(heights, expectedHeight);
44
// tStatistic ≈ 0.816
45
46
// For α=0.05, df=7: critical value ≈ ±2.365
47
// |0.816| < 2.365, so we fail to reject H₀
48
// No significant difference from 170cm
49
```
50
51
### tTestTwoSample { .api }
52
53
```typescript { .api }
54
function tTestTwoSample(sampleX: number[], sampleY: number[], difference?: number): number | null;
55
```
56
57
Performs a two-sample t-test to compare means of two independent groups.
58
59
**Parameters:**
60
- `sampleX: number[]` - First sample
61
- `sampleY: number[]` - Second sample
62
- `difference?: number` - Expected difference between means (default: 0)
63
64
**Returns:** `number | null` - t-statistic value, or null if calculation fails
65
66
```typescript
67
import { tTestTwoSample } from "simple-statistics";
68
69
// Compare test scores between two teaching methods
70
const method1 = [85, 90, 88, 92, 87, 89, 91];
71
const method2 = [78, 82, 80, 85, 83, 81, 84];
72
73
const tStatistic = tTestTwoSample(method1, method2);
74
// tStatistic ≈ 4.12
75
76
// If |t| > critical value, methods show significant difference
77
console.log(`t-statistic: ${tStatistic}`);
78
```
79
80
## Chi-Squared Tests
81
82
### chiSquaredGoodnessOfFit { .api }
83
84
```typescript { .api }
85
function chiSquaredGoodnessOfFit(data: number[], distributionType: Function, significance: number): boolean;
86
```
87
88
Tests whether observed data follows an expected distribution.
89
90
**Parameters:**
91
- `data: number[]` - Observed frequency data
92
- `distributionType: Function` - Expected distribution function
93
- `significance: number` - Significance level (e.g., 0.05)
94
95
**Returns:** `boolean` - true if data fits distribution at given significance level
96
97
```typescript
98
import { chiSquaredGoodnessOfFit, poissonDistribution } from "simple-statistics";
99
100
// Test if customer arrivals follow Poisson distribution
101
const observedArrivals = [5, 8, 12, 18, 15, 10, 7, 3, 2, 1]; // frequencies
102
const expectedLambda = 3.2; // average arrivals
103
const expectedDistribution = () => poissonDistribution(expectedLambda);
104
105
const fitsPoisson = chiSquaredGoodnessOfFit(observedArrivals, expectedDistribution, 0.05);
106
console.log(`Fits Poisson distribution: ${fitsPoisson}`);
107
```
108
109
### chiSquaredDistributionTable { .api }
110
111
```typescript { .api }
112
const chiSquaredDistributionTable: Record<number, Record<number, number>>;
113
```
114
115
Critical values for chi-squared distribution lookups.
116
117
```typescript
118
import { chiSquaredDistributionTable } from "simple-statistics";
119
120
// Get critical value for df=3, α=0.05
121
const criticalValue = chiSquaredDistributionTable[3][0.05]; // 7.815
122
```
123
124
## Non-Parametric Tests
125
126
### permutationTest { .api }
127
128
```typescript { .api }
129
function permutationTest(sampleX: number[], sampleY: number[], testType?: string, k?: number): number;
130
```
131
132
Performs a permutation test - a non-parametric alternative when distributional assumptions aren't met.
133
134
**Parameters:**
135
- `sampleX: number[]` - First sample
136
- `sampleY: number[]` - Second sample
137
- `testType?: string` - Type of test (optional)
138
- `k?: number` - Number of permutations (optional)
139
140
**Returns:** `number` - p-value for the test
141
142
```typescript
143
import { permutationTest } from "simple-statistics";
144
145
// Compare two groups without assuming normal distribution
146
const groupA = [23, 25, 28, 22, 26, 24, 27];
147
const groupB = [31, 33, 35, 30, 32, 29, 34];
148
149
const pValue = permutationTest(groupA, groupB);
150
console.log(`p-value: ${pValue}`);
151
152
// If p < 0.05, reject null hypothesis of no difference
153
if (pValue < 0.05) {
154
console.log("Significant difference between groups");
155
} else {
156
console.log("No significant difference detected");
157
}
158
```
159
160
## Usage Examples
161
162
### Complete Hypothesis Testing Workflow
163
164
```typescript
165
import {
166
tTest,
167
tTestTwoSample,
168
sampleVariance,
169
mean,
170
standardDeviation
171
} from "simple-statistics";
172
173
// Drug effectiveness study
174
const placeboGroup = [2.1, 2.5, 1.8, 2.3, 2.0, 2.4, 1.9, 2.2];
175
const treatmentGroup = [3.2, 3.8, 3.1, 3.5, 3.0, 3.6, 2.9, 3.4];
176
177
// Descriptive statistics
178
console.log("Placebo group:");
179
console.log(`Mean: ${mean(placeboGroup).toFixed(2)}`);
180
console.log(`Std Dev: ${standardDeviation(placeboGroup).toFixed(2)}`);
181
182
console.log("Treatment group:");
183
console.log(`Mean: ${mean(treatmentGroup).toFixed(2)}`);
184
console.log(`Std Dev: ${standardDeviation(treatmentGroup).toFixed(2)}`);
185
186
// Test if treatment group differs from placebo
187
const tStat = tTestTwoSample(placeboGroup, treatmentGroup);
188
console.log(`Two-sample t-statistic: ${tStat?.toFixed(3)}`);
189
190
// Test if treatment achieves target improvement of 1.0 unit
191
const targetImprovement = 1.0;
192
const actualImprovement = mean(treatmentGroup) - mean(placeboGroup);
193
const improvementTest = tTest(treatmentGroup, mean(placeboGroup) + targetImprovement);
194
console.log(`Target improvement test: ${improvementTest.toFixed(3)}`);
195
```
196
197
### A/B Testing Analysis
198
199
```typescript
200
import { tTestTwoSample, permutationTest, mean } from "simple-statistics";
201
202
// Website conversion rates (percentages)
203
const versionA = [2.1, 2.3, 1.9, 2.4, 2.0, 2.2, 1.8, 2.5]; // Control
204
const versionB = [2.8, 3.1, 2.6, 3.0, 2.9, 3.2, 2.7, 3.3]; // Treatment
205
206
const meanA = mean(versionA);
207
const meanB = mean(versionB);
208
const improvement = ((meanB - meanA) / meanA * 100);
209
210
console.log(`Version A average: ${meanA.toFixed(2)}%`);
211
console.log(`Version B average: ${meanB.toFixed(2)}%`);
212
console.log(`Relative improvement: ${improvement.toFixed(1)}%`);
213
214
// Parametric test
215
const tStat = tTestTwoSample(versionA, versionB);
216
console.log(`t-test statistic: ${tStat?.toFixed(3)}`);
217
218
// Non-parametric alternative
219
const pValue = permutationTest(versionA, versionB);
220
console.log(`Permutation test p-value: ${pValue.toFixed(4)}`);
221
222
if (pValue < 0.05) {
223
console.log("✓ Statistically significant improvement");
224
} else {
225
console.log("✗ No significant difference detected");
226
}
227
```
228
229
### Quality Control Testing
230
231
```typescript
232
import { tTest, chiSquaredGoodnessOfFit } from "simple-statistics";
233
234
// Manufacturing quality control
235
const targetWeight = 500; // grams
236
const batchWeights = [498, 502, 499, 501, 500, 497, 503, 498, 501, 499];
237
238
// Test if batch meets target weight
239
const weightTest = tTest(batchWeights, targetWeight);
240
console.log(`Weight test t-statistic: ${weightTest.toFixed(3)}`);
241
242
// Test process control
243
const toleranceLimit = 2.58; // 99% confidence (±2.58 standard deviations)
244
if (Math.abs(weightTest) > toleranceLimit) {
245
console.log("⚠️ Process out of control - investigate immediately");
246
} else {
247
console.log("✓ Process within control limits");
248
}
249
250
// Distribution testing for defect patterns
251
const defectCounts = [12, 8, 5, 3, 1, 1]; // defects per day
252
const expectedPoisson = () => [10, 8, 6, 4, 2, 1]; // expected pattern
253
254
const followsPattern = chiSquaredGoodnessOfFit(defectCounts, () => expectedPoisson(), 0.05);
255
console.log(`Defects follow expected pattern: ${followsPattern}`);
256
```