viewof p = Inputs.range([0, 1], {
step: 0.01,
value: 0.5,
label: tex`p`,
width: 200
})
data = {
return [
{outcome: "0", probability: 1 - p},
{outcome: "1", probability: p}
];
}
Plot.plot({
style: "overflow: visible; display: block; margin: 0 auto;",
width: 600,
height: 400,
y: {
grid: true,
label: "Probability",
domain: [0, 1]
},
x: {
label: "Outcome",
padding: 0.2
},
marks: [
Plot.barY(data, {
x: "outcome",
y: "probability",
fill: "steelblue"
}),
Plot.ruleY([0])
]
})Bernoulli Distribution
p_{X}(x) = \begin{cases} p & \text{if } x = 1, \\ q = 1-p & \text{if } x = 0. \end{cases}
- The discrete random variable can take value 1 with probability p or value 0 with probability q = 1 - p
- Not to be confused with the binomial distribution, since only one trial is being conducted.
- \mathbb{E}[X] = p
- Var[X] = pq
Python Code: Bernoulli Distribution
To create Bernoulli distributed data using numpy:
import numpy as np
interval = [0,1]
size = (1000,1)
p = [1-0.5, 0.5]
data = np.random.choice(interval, size, p = p)