1 | /**
|
2 | * The [Bernoulli distribution](http://en.wikipedia.org/wiki/Bernoulli_distribution)
|
3 | * is the probability discrete
|
4 | * distribution of a random variable which takes value 1 with success
|
5 | * probability `p` and value 0 with failure
|
6 | * probability `q` = 1 - `p`. It can be used, for example, to represent the
|
7 | * toss of a coin, where "1" is defined to mean "heads" and "0" is defined
|
8 | * to mean "tails" (or vice versa). It is
|
9 | * a special case of a Binomial Distribution
|
10 | * where `n` = 1.
|
11 | *
|
12 | * @param {number} p input value, between 0 and 1 inclusive
|
13 | * @returns {number[]} values of bernoulli distribution at this point
|
14 | * @throws {Error} if p is outside 0 and 1
|
15 | * @example
|
16 | * bernoulliDistribution(0.3); // => [0.7, 0.3]
|
17 | */
|
18 | function bernoulliDistribution(p) /*: number[] */ {
|
19 | // Check that `p` is a valid probability (0 ≤ p ≤ 1)
|
20 | if (p < 0 || p > 1) {
|
21 | throw new Error(
|
22 | "bernoulliDistribution requires probability to be between 0 and 1 inclusive"
|
23 | );
|
24 | }
|
25 |
|
26 | return [1 - p, p];
|
27 | }
|
28 |
|
29 | export default bernoulliDistribution;
|