Bernoulli Distribution

Overview


A Bernoulli random variable is a random variable that can one of two values, where the probabilities are, where the variable takes on the first value with probability {% p %}, and the second value with probability {% 1-p %}.

Fitting to Data


Given a Bernoulli variable {% B(\omega) %} such that {% B=1 %} for the event in question, and {% 0 %} for no event ocurring, the probability that {% B=1 %} is given by the following
{% \mathbb{P}( B=1) = \mathbb{E}(B) %}
This then implies that a simple way to fit a Bernoulli variable is simply to take the average value and use that as the probability. Equivalently, one can take the number of ocurrences and divide by the total number of observations in the dataset.
{% \mathbb{P}( B=1) \approx Average(B) = \frac{\#( B=1) }{n} %}
where {% n %} is the sized of the observed dataset.

For more information about estimated distribution parameters, see estimation

Generating Bernoullis


To simulate a random bernoulli variable, you just need to use the following simple algorithm:

  • Generate {% U \sim U(0,1) %} (uniform random)
  • if {% U \leq p %} , return 1, else return 0


Bernoulli Library


The Bernoulli Distribution Library contains a method for generating random Bernoullis.


let bl = await import('/lib/statistics/distributions/bernoulli/v1.0.0/bernoulli.mjs');
let rand = bl.random(0.2);

let rand2 = bl.random(0.2, random)