0
# Probability Distributions
1
2
Functions for working with various probability distributions and normal distribution calculations.
3
4
## Core Imports
5
6
```typescript
7
import {
8
// Discrete distributions
9
bernoulliDistribution,
10
binomialDistribution,
11
poissonDistribution,
12
13
// Normal distribution
14
zScore,
15
cumulativeStdNormalProbability,
16
errorFunction,
17
inverseErrorFunction,
18
probit,
19
20
// Constants and tables
21
standardNormalTable,
22
chiSquaredDistributionTable
23
} from "simple-statistics";
24
```
25
26
## Discrete Distributions
27
28
### bernoulliDistribution { .api }
29
30
```typescript { .api }
31
function bernoulliDistribution(p: number): number[];
32
```
33
34
Generates probabilities for a Bernoulli distribution - a discrete distribution with two possible outcomes.
35
36
**Parameters:**
37
- `p: number` - Probability of success (between 0 and 1)
38
39
**Returns:** `number[]` - Array with [P(X=0), P(X=1)]
40
41
```typescript
42
import { bernoulliDistribution } from "simple-statistics";
43
44
// Coin flip with 60% chance of heads
45
const coinFlip = bernoulliDistribution(0.6);
46
// [0.4, 0.6] - 40% chance tails, 60% chance heads
47
```
48
49
### binomialDistribution { .api }
50
51
```typescript { .api }
52
function binomialDistribution(trials: number, probability: number): number[];
53
```
54
55
Generates probabilities for a binomial distribution - number of successes in a fixed number of trials.
56
57
**Parameters:**
58
- `trials: number` - Number of independent trials
59
- `probability: number` - Probability of success in each trial (0-1)
60
61
**Returns:** `number[]` - Array where index i contains P(X=i)
62
63
```typescript
64
import { binomialDistribution } from "simple-statistics";
65
66
// 10 coin flips with 50% success probability
67
const distribution = binomialDistribution(10, 0.5);
68
// distribution[0] = P(0 heads)
69
// distribution[5] = P(5 heads) ≈ 0.246 (most likely)
70
// distribution[10] = P(10 heads) ≈ 0.001
71
72
console.log(`P(exactly 5 heads): ${distribution[5]}`);
73
```
74
75
### poissonDistribution { .api }
76
77
```typescript { .api }
78
function poissonDistribution(lambda: number): number[];
79
```
80
81
Generates probabilities for a Poisson distribution - models number of events in a fixed interval.
82
83
**Parameters:**
84
- `lambda: number` - Average number of events per interval (rate parameter)
85
86
**Returns:** `number[]` - Array where index i contains P(X=i)
87
88
```typescript
89
import { poissonDistribution } from "simple-statistics";
90
91
// Average 3 customers per hour
92
const customerArrival = poissonDistribution(3);
93
// customerArrival[0] = P(0 customers) ≈ 0.0498
94
// customerArrival[3] = P(3 customers) ≈ 0.224 (most likely)
95
// customerArrival[6] = P(6 customers) ≈ 0.0504
96
97
console.log(`P(exactly 2 customers): ${customerArrival[2]}`);
98
```
99
100
## Normal Distribution Functions
101
102
### zScore { .api }
103
104
```typescript { .api }
105
function zScore(x: number, mean: number, standardDeviation: number): number;
106
```
107
108
Calculates the z-score (standard score) - how many standard deviations a value is from the mean.
109
110
**Parameters:**
111
- `x: number` - The value to standardize
112
- `mean: number` - Population mean
113
- `standardDeviation: number` - Population standard deviation
114
115
**Returns:** `number` - Z-score (standardized value)
116
117
```typescript
118
import { zScore } from "simple-statistics";
119
120
// Test score analysis
121
const score = 85;
122
const classAverage = 75;
123
const classStdDev = 10;
124
125
const z = zScore(score, classAverage, classStdDev); // 1.0
126
// Score is 1 standard deviation above the mean
127
```
128
129
### cumulativeStdNormalProbability { .api }
130
131
```typescript { .api }
132
function cumulativeStdNormalProbability(z: number): number;
133
```
134
135
Calculates the cumulative probability for the standard normal distribution (mean=0, std=1).
136
137
**Parameters:**
138
- `z: number` - Z-score value
139
140
**Returns:** `number` - Probability that a standard normal variable is ≤ z
141
142
```typescript
143
import { cumulativeStdNormalProbability, zScore } from "simple-statistics";
144
145
// What percentage scored below 85?
146
const z = zScore(85, 75, 10); // 1.0
147
const percentile = cumulativeStdNormalProbability(z); // 0.841
148
// 84.1% of students scored below 85
149
```
150
151
### errorFunction (alias: erf) { .api }
152
153
```typescript { .api }
154
function errorFunction(x: number): number;
155
```
156
157
Calculates the error function, fundamental to many statistical calculations.
158
159
**Parameters:**
160
- `x: number` - Input value
161
162
**Returns:** `number` - Error function value
163
164
```typescript
165
import { errorFunction } from "simple-statistics";
166
167
const erfValue = errorFunction(1); // 0.843...
168
```
169
170
### inverseErrorFunction { .api }
171
172
```typescript { .api }
173
function inverseErrorFunction(x: number): number;
174
```
175
176
Calculates the inverse error function.
177
178
**Parameters:**
179
- `x: number` - Input value (between -1 and 1)
180
181
**Returns:** `number` - Inverse error function value
182
183
### probit { .api }
184
185
```typescript { .api }
186
function probit(p: number): number;
187
```
188
189
Calculates the probit function (inverse of cumulative standard normal distribution).
190
191
**Parameters:**
192
- `p: number` - Probability value (between 0 and 1)
193
194
**Returns:** `number` - Z-score corresponding to the probability
195
196
```typescript
197
import { probit } from "simple-statistics";
198
199
// What z-score corresponds to 95th percentile?
200
const z95 = probit(0.95); // 1.645
201
// 95% of values fall below z = 1.645 in standard normal distribution
202
```
203
204
## Distribution Tables and Constants
205
206
### standardNormalTable { .api }
207
208
```typescript { .api }
209
const standardNormalTable: number[];
210
```
211
212
Pre-calculated standard normal distribution values for quick lookup.
213
214
```typescript
215
import { standardNormalTable } from "simple-statistics";
216
217
// Access pre-calculated values
218
const tableValue = standardNormalTable[0]; // Value at index 0
219
```
220
221
### chiSquaredDistributionTable { .api }
222
223
```typescript { .api }
224
const chiSquaredDistributionTable: Record<number, Record<number, number>>;
225
```
226
227
Critical values for chi-squared distribution at various degrees of freedom and significance levels.
228
229
**Usage:** Access critical values by degrees of freedom and significance level.
230
231
```typescript
232
import { chiSquaredDistributionTable } from "simple-statistics";
233
234
// Get critical value for df=5, α=0.05
235
const criticalValue = chiSquaredDistributionTable[5][0.05];
236
237
// Statistical testing
238
function chiSquaredTest(observed: number[], expected: number[], df: number, alpha = 0.05): boolean {
239
let chiSquared = 0;
240
for (let i = 0; i < observed.length; i++) {
241
chiSquared += Math.pow(observed[i] - expected[i], 2) / expected[i];
242
}
243
244
const criticalValue = chiSquaredDistributionTable[df][alpha];
245
return chiSquared > criticalValue; // Reject null hypothesis if true
246
}
247
```
248
249
## Usage Examples
250
251
### Normal Distribution Analysis
252
253
```typescript
254
import {
255
zScore,
256
cumulativeStdNormalProbability,
257
probit
258
} from "simple-statistics";
259
260
// SAT scores: mean=1000, std=200
261
const satMean = 1000;
262
const satStd = 200;
263
264
// What percentile is a score of 1200?
265
const z1200 = zScore(1200, satMean, satStd); // 1.0
266
const percentile = cumulativeStdNormalProbability(z1200); // 0.841
267
console.log(`1200 is the ${Math.round(percentile * 100)}th percentile`);
268
269
// What score is needed for 90th percentile?
270
const z90th = probit(0.90); // 1.282
271
const score90th = satMean + (z90th * satStd); // 1256.4
272
console.log(`90th percentile score: ${Math.round(score90th)}`);
273
```
274
275
### Quality Control with Normal Distribution
276
277
```typescript
278
import { zScore, cumulativeStdNormalProbability } from "simple-statistics";
279
280
// Manufacturing: parts should be 100mm ± 2mm (99.7% within spec)
281
const targetLength = 100;
282
const tolerance = 2;
283
const processStd = tolerance / 3; // 3-sigma process
284
285
function qualityCheck(measurement: number): string {
286
const z = zScore(measurement, targetLength, processStd);
287
const probability = cumulativeStdNormalProbability(Math.abs(z));
288
289
if (Math.abs(z) > 3) {
290
return `REJECT: ${measurement}mm is ${Math.abs(z).toFixed(2)} standard deviations from target`;
291
} else {
292
return `ACCEPT: Within ${(probability * 100).toFixed(1)}% confidence interval`;
293
}
294
}
295
296
console.log(qualityCheck(102.1)); // Within tolerance
297
console.log(qualityCheck(106.5)); // Outside tolerance
298
```
299
300
### Event Modeling with Poisson Distribution
301
302
```typescript
303
import { poissonDistribution } from "simple-statistics";
304
305
// Customer service: average 4 calls per hour
306
const avgCallsPerHour = 4;
307
const callDistribution = poissonDistribution(avgCallsPerHour);
308
309
// Staffing decisions based on probability
310
let cumulative = 0;
311
for (let calls = 0; calls < callDistribution.length; calls++) {
312
cumulative += callDistribution[calls];
313
console.log(`P(≤${calls} calls): ${(cumulative * 100).toFixed(1)}%`);
314
315
if (cumulative >= 0.95) {
316
console.log(`Need capacity for ${calls} calls (95% confidence)`);
317
break;
318
}
319
}
320
```