Problems
Loading...
1 / 1

Bernoulli Probability Mass Function & Moments

Probability and Statistics
Easy

Implement the Bernoulli Probability Mass Function (PMF) and compute the distribution's mean and variance for given inputs.

Bernoulli Distribution:

Probability Mass Function:

P(X=1)=p,P(X=0)=1pP(X = 1) = p,\qquad P(X = 0) = 1 - p

Mean and Variance:

μ=p,σ2=p(1p)\mu = p,\qquad \sigma^2 = p(1 - p)

Function Arguments

  • x: scalar, list, or array - Values of 0s and 1s
  • p: float - Success probability (0 ≤ p ≤ 1)
Loading visualization...

Examples

Input: x=[0, 1, 1], p=0.3

Output: pmf=[0.7, 0.3, 0.3], mean=0.3, var=0.21

Input: x=[1], p=0.8

Output: pmf=[0.8], mean=0.8, var=0.16

Input: x=[0, 1, 0, 1], p=0.5

Output: pmf=[0.5, 0.5, 0.5, 0.5], mean=0.5, var=0.25

Hint 1

Use np.where() to assign probabilities.

Hint 2

Mean is simply p, variance is p * (1 - p).

Hint 3

Convert to float using float() for mean and variance return values.

Requirements

  • Return tuple: (pmf, mean, var)
  • pmf: NumPy array of probabilities
  • mean, var: scalar floats
  • Vectorized implementation (no loops)
  • Convert x to NumPy array

Constraints

  • len(x) ≤ 1e6
  • 0 ≤ p ≤ 1
  • NumPy only; time limit: 200ms
Try Similar Problems