BERNOULLI
Overview
The BERNOULLI
function models a Bernoulli discrete random variable, which represents the probability of success (p) or failure (1-p) for a single trial. It provides methods for calculating the probability mass function (pmf), cumulative distribution function (cdf), and generating random variates (rvs). It also includes functions to compute mean, variance, skew, and kurtosis. For more details, see the SciPy documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=BERNOULLI(p, x, [method])
p
(float, required): Probability of success(0 <= p <= 1)
.x
(float, optional): The value at which to evaluate the PMF or CDF. If omitted, the function will return the mean.method
(str, optional, default=“pmf”): The method to apply. Can be “pmf”, “cdf”, “mean”, “var”, “skew”, or “kurt”.
The function returns a single value (float) representing the result of the chosen method, or an error message (string) if the input is invalid.
Examples
Example 1: Probability Mass Function (PMF) at x=1
This example calculates the probability of success (x=1) for a Bernoulli distribution with p=0.5.
Inputs:
p | x | method |
---|---|---|
0.5 | 1 | pmf |
Excel formula:
=BERNOULLI(0.5, 1, "pmf")
Expected output:
Result |
---|
0.5 |
This means the probability of getting a success (1) is 0.5.
Example 2: Cumulative Distribution Function (CDF) at x=0
This example calculates the cumulative probability up to x=0 for a Bernoulli distribution with p=0.7.
Inputs:
p | x | method |
---|---|---|
0.7 | 0 | cdf |
Excel formula:
=BERNOULLI(0.7, 0, "cdf")
Expected output:
Result |
---|
0.3 |
This means the probability of getting a value less than or equal to 0 is 0.3.
Example 3: Mean of the distribution
This example calculates the mean of a Bernoulli distribution with p=0.2.
Inputs:
p | x | method |
---|---|---|
0.2 | mean |
Excel formula:
=BERNOULLI(0.2, , "mean")
Expected output:
Result |
---|
0.2 |
This shows that the mean of a Bernoulli distribution is equal to its probability of success.
Example 4: Variance of the distribution
This example calculates the variance of a Bernoulli distribution with p=0.9.
Inputs:
p | x | method |
---|---|---|
0.9 | var |
Excel formula:
=BERNOULLI(0.9, , "var")
Expected output:
Result |
---|
0.09 |
This demonstrates the variance of the distribution.
Python Code
import scipy.stats as stats
def bernoulli(p, x=None, method="pmf"):
"""
Calculates properties of a Bernoulli discrete random variable.
Args:
p: Probability of success (0 <= p <= 1).
x: The value at which to evaluate the PMF or CDF. If omitted, the function will return the mean.
method: The method to apply. Can be "pmf", "cdf", "mean", "var", "skew", or "kurt".
Returns:
The result of the chosen method (float), or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
try:
p_val = float(p)
except Exception:
return "Invalid input: p must be a number."
if not (0 <= p_val <= 1):
return "Invalid input: p must be between 0 and 1."
dist = stats.bernoulli(p_val)
if method == "pmf":
if x is None:
return "Invalid input: x is required for pmf method."
try:
x_val = float(x)
except Exception:
return "Invalid input: x must be a number for pmf method."
return float(dist.pmf(x_val))
elif method == "cdf":
if x is None:
return "Invalid input: x is required for cdf method."
try:
x_val = float(x)
except Exception:
return "Invalid input: x must be a number for cdf method."
return float(dist.cdf(x_val))
elif method == "mean":
return float(dist.mean())
elif method == "var":
return float(dist.var())
elif method == "skew":
return float(dist.skew())
elif method == "kurt":
return float(dist.kurtosis())
else:
return "Invalid method. Choose from 'pmf', 'cdf', 'mean', 'var', 'skew', or 'kurt'."