TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Bernoulli Probability Mass Function & Moments

Probability and Statistics
Easy

Evaluate a Bernoulli distribution at each value in x:

P(X=x)={1−p,x=0p,x=1P(X=x) = \begin{cases}1-p, & x=0 \\ p, & x=1\end{cases}P(X=x)={1−p,p,​x=0x=1​

Its mean and variance are:

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

Here, ppp is the success probability. Return a dictionary containing pmf as a NumPy array and mean and variance as Python floats.

Loading visualization...

Examples

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

Output: {"pmf": [0.7, 0.3, 0.3], "mean": 0.3, "variance": 0.21}

Explanation: Failure has probability 0.7, success has probability 0.3, and the distribution moments depend only on p.

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

Output: {"pmf": [0.5, 0.5, 0.5, 0.5], "mean": 0.5, "variance": 0.25}

Input: x = [1], p = 0.8

Output: {"pmf": [0.8], "mean": 0.8, "variance": 0.16}

Hint 1

Use np.where(x == 1, p, 1.0 - p) for the PMF array.

Hint 2

Compute the variance with p * (1.0 - p).

Requirements

  • Evaluate the Bernoulli PMF for every value in x
  • Compute the distribution mean and variance
  • Return exactly pmf, mean, and variance in a dictionary
  • pmf must be a NumPy array and both moments must be Python floats

Constraints

  • x is a nonempty list containing only 0 and 1
  • p is between 0 and 1
  • Use NumPy only
Try Similar Problems
Binomial Pmf CdfGeometric Pmf MeanPoisson Pmf CdfExpected Value DiscreteNaive Bayes Bernoulli

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Bernoulli Probability Mass Function & Moments

Probability and Statistics
Easy

Evaluate a Bernoulli distribution at each value in x:

P(X=x)={1−p,x=0p,x=1P(X=x) = \begin{cases}1-p, & x=0 \\ p, & x=1\end{cases}P(X=x)={1−p,p,​x=0x=1​

Its mean and variance are:

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

Here, ppp is the success probability. Return a dictionary containing pmf as a NumPy array and mean and variance as Python floats.

Loading visualization...

Examples

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

Output: {"pmf": [0.7, 0.3, 0.3], "mean": 0.3, "variance": 0.21}

Explanation: Failure has probability 0.7, success has probability 0.3, and the distribution moments depend only on p.

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

Output: {"pmf": [0.5, 0.5, 0.5, 0.5], "mean": 0.5, "variance": 0.25}

Input: x = [1], p = 0.8

Output: {"pmf": [0.8], "mean": 0.8, "variance": 0.16}

Hint 1

Use np.where(x == 1, p, 1.0 - p) for the PMF array.

Hint 2

Compute the variance with p * (1.0 - p).

Requirements

  • Evaluate the Bernoulli PMF for every value in x
  • Compute the distribution mean and variance
  • Return exactly pmf, mean, and variance in a dictionary
  • pmf must be a NumPy array and both moments must be Python floats

Constraints

  • x is a nonempty list containing only 0 and 1
  • p is between 0 and 1
  • Use NumPy only
Try Similar Problems
Binomial Pmf CdfGeometric Pmf MeanPoisson Pmf CdfExpected Value DiscreteNaive Bayes Bernoulli

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: number

You must run your code first.