/
cumulative_std_normal_probability.js
42 lines (39 loc) · 1.64 KB
/
cumulative_std_normal_probability.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import standardNormalTable from "./standard_normal_table.js";
/**
* **[Cumulative Standard Normal Probability](http://en.wikipedia.org/wiki/Standard_normal_table)**
*
* Since probability tables cannot be
* printed for every normal distribution, as there are an infinite variety
* of normal distributions, it is common practice to convert a normal to a
* standard normal and then use the standard normal table to find probabilities.
*
* You can use `.5 + .5 * errorFunction(x / Math.sqrt(2))` to calculate the probability
* instead of looking it up in a table.
*
* @param {number} z
* @returns {number} cumulative standard normal probability
*/
function cumulativeStdNormalProbability(z) {
// Calculate the position of this value.
const absZ = Math.abs(z);
// Each row begins with a different
// significant digit: 0.5, 0.6, 0.7, and so on. Each value in the table
// corresponds to a range of 0.01 in the input values, so the value is
// multiplied by 100.
const index = Math.min(
Math.round(absZ * 100),
standardNormalTable.length - 1
);
// The index we calculate must be in the table as a positive value,
// but we still pay attention to whether the input is positive
// or negative, and flip the output value as a last step.
if (z >= 0) {
return standardNormalTable[index];
} else {
// due to floating-point arithmetic, values in the table with
// 4 significant figures can nevertheless end up as repeating
// fractions when they're computed here.
return +(1 - standardNormalTable[index]).toFixed(4);
}
}
export default cumulativeStdNormalProbability;