BETA
Overview
The BETA
function provides a unified interface to the main methods of the Beta distribution, including PDF, CDF, inverse CDF, survival function, and distribution statistics.
Excel provides the BETA.DIST and BETA.INV functions, which can compute the PDF, CDF, and quantile (inverse CDF) for the beta distribution. The Python function in Excel here also supports the survival function, inverse survival, and distribution statistics (mean, median, variance, standard deviation), as well as location and scale parameters, which are not available in native Excel functions.
The Beta distribution is a continuous probability distribution defined on the interval [0, 1], parameterized by two positive shape parameters and . It is widely used in Bayesian statistics, modeling proportions, and random variables limited to intervals of finite length. The PDF is given by:
for , , , where is the gamma function.
The Beta distribution is flexible and can take on a variety of shapes depending on the values of and . For more details, see the official SciPy documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=BETA(value, a, b, [loc], [scale], [method])
value
(float, required for pdf, cdf, icdf, sf, isf):- For
pdf
,cdf
,sf
: the value at which to evaluate the function (must be for standard beta) - For
icdf
,isf
: the probability (must be between 0 and 1) - For
mean
,median
,var
,std
: skip parameter
- For
a
(float, required): First shape parameter. Must be .b
(float, required): Second shape parameter. Must be .loc
(float, optional, default=0.0): Location parameter.scale
(float, optional, default=1.0): Scale parameter. Must be .method
(string, optional, default=“pdf”): One ofpdf
,cdf
,icdf
,sf
,isf
,mean
,median
,var
,std
.
Method | Description | Output |
---|---|---|
pdf | Probability Density Function: , the likelihood of a specific value . | Density at |
cdf | Cumulative Distribution Function: , the probability that is less than or equal to . | Probability |
icdf | Inverse CDF (Quantile Function): Returns such that for a given probability . | Value |
sf | Survival Function: , the probability that is greater than . | Probability |
isf | Inverse Survival Function: Returns such that for a given probability . | Value |
mean | Mean (expected value) of the distribution. | Mean value |
median | Median of the distribution. | Median value |
var | Variance of the distribution. | Variance |
std | Standard deviation of the distribution. | Standard deviation |
The function returns a single value (float): the result of the requested method, or an error message (string) if the input is invalid.
Examples
Example 1: PDF at x=0.5
Inputs:
value | a | b | loc | scale | method |
---|---|---|---|---|---|
0.5 | 2 | 3 | 0 | 1 |
Excel formula:
=BETA(0.5, 2, 3, 0, 1, "pdf")
Expected output:
Result |
---|
1.5000000000000004 |
Example 2: CDF at x=0.5
Inputs:
value | a | b | loc | scale | method |
---|---|---|---|---|---|
0.5 | 2 | 3 | 0 | 1 | cdf |
Excel formula:
=BETA(0.5, 2, 3, 0, 1, "cdf")
Expected output:
Result |
---|
0.6875 |
Example 3: Inverse CDF (Quantile) at q=0.6875
Inputs:
value | a | b | loc | scale | method |
---|---|---|---|---|---|
0.6875 | 2 | 3 | 0 | 1 | icdf |
Excel formula:
=BETA(0.6875, 2, 3, 0, 1, "icdf")
Expected output:
Result |
---|
0.5 |
Example 4: Mean of the distribution
Inputs:
value | a | b | loc | scale | method |
---|---|---|---|---|---|
2 | 3 | 0 | 1 | mean |
Excel formula:
=BETA( , 2, 3, 0, 1, "mean")
Expected output:
Result |
---|
0.4 |
Python Code
from scipy.stats import beta as scipy_beta
import math
def beta(value=None, a=1.0, b=1.0, loc=0.0, scale=1.0, method="pdf"):
"""
Generalized Beta distribution function supporting multiple methods.
Args:
value: Input value (float), required for methods except 'mean', 'median', 'var', 'std'.
a: First shape parameter (float, >0).
b: Second shape parameter (float, >0).
loc: Location parameter (float, default: 0.0).
scale: Scale parameter (float, default: 1.0, >0).
method: Which method to compute (str): 'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'. Default is 'pdf'.
Returns:
Result of the requested method (float or str), or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
valid_methods = ['pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std']
if not isinstance(method, str) or method.lower() not in valid_methods:
return f"Invalid method: {method}. Must be one of {valid_methods}."
method = method.lower()
try:
a = float(a)
b = float(b)
loc = float(loc)
scale = float(scale)
except Exception:
return "Invalid input: a, b, loc, and scale must be numbers."
if a <= 0:
return "Invalid input: a must be > 0."
if b <= 0:
return "Invalid input: b must be > 0."
if scale <= 0:
return "Invalid input: scale must be > 0."
dist = scipy_beta(a, b, loc, scale)
# Methods that require value
if method in ['pdf', 'cdf', 'icdf', 'sf', 'isf']:
if value is None:
return f"Invalid input: missing required argument 'value' for method '{method}'."
try:
value = float(value)
except Exception:
return "Invalid input: value must be a number."
if method in ['pdf', 'cdf', 'sf'] and not (loc <= value <= loc + scale):
return "Invalid input: value must be within [loc, loc + scale] for this method."
if method in ['isf', 'icdf'] and not (0 <= value <= 1):
return "Invalid input: value (probability) must be between 0 and 1 for isf/icdf."
try:
if method == 'pdf':
result = dist.pdf(value)
elif method == 'cdf':
result = dist.cdf(value)
elif method == 'sf':
result = dist.sf(value)
elif method == 'isf':
result = dist.isf(value)
elif method == 'icdf':
result = dist.ppf(value)
except Exception as e:
return f"scipy.stats.beta error: {e}"
if isinstance(result, float):
if math.isnan(result):
return "Result is NaN (not a number)"
if math.isinf(result):
return "inf" if result > 0 else "-inf"
return result
# Methods that do not require value
try:
if method == 'mean':
result = dist.mean()
elif method == 'median':
result = dist.median()
elif method == 'var':
result = dist.var()
elif method == 'std':
result = dist.std()
except Exception as e:
return f"scipy.stats.beta error: {e}"
if isinstance(result, float):
if math.isnan(result):
return "Result is NaN (not a number)"
if math.isinf(result):
return "inf" if result > 0 else "-inf"
return result
Live Notebook
Edit this function in a live notebook .