F_DIST
Overview
The F_DIST
function provides a unified interface to the main methods of the F-distribution, including PDF, CDF, inverse CDF, survival function, and distribution statistics.
Excel provides the F.DIST and F.INV functions, which can compute the PDF, CDF, and quantile (inverse CDF) for the F-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 F-distribution is commonly used in statistics, especially in the analysis of variance (ANOVA), to compare variances between two populations. The PDF is given by:
for , , .
The F-distribution is a continuous probability distribution that arises frequently as the null distribution of a test statistic, especially in ANOVA. The parameters df_1
and df_2
are the degrees of freedom for the numerator and denominator, respectively.
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:
=F_DIST(value, dfn, dfd, [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
icdf
,isf
: the probability (must be between 0 and 1) - For
mean
,median
,var
,std
: skip parameter
- For
dfn
(float, required): Numerator degrees of freedom. Must be .dfd
(float, required): Denominator degrees of freedom. 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=2
Inputs:
value | dfn | dfd | loc | scale | method |
---|---|---|---|---|---|
2 | 5 | 10 | 0 | 1 |
Excel formula:
=F_DIST(2, 5, 10, 0, 1, "pdf")
Expected output:
Result |
---|
0.162006 |
Example 2: CDF at x=2
Inputs:
value | dfn | dfd | loc | scale | method |
---|---|---|---|---|---|
2 | 5 | 10 | 0 | 1 | cdf |
Excel formula:
=F_DIST(2, 5, 10, 0, 1, "cdf")
Expected output:
Result |
---|
0.835805 |
Example 3: Inverse CDF (Quantile) at q=0.835805
Inputs:
value | dfn | dfd | loc | scale | method |
---|---|---|---|---|---|
0.835805 | 5 | 10 | 0 | 1 | icdf |
Excel formula:
=F_DIST(0.835805, 5, 10, 0, 1, "icdf")
Expected output:
Result |
---|
2.0 |
Example 4: Mean of the distribution
Inputs:
value | dfn | dfd | loc | scale | method |
---|---|---|---|---|---|
5 | 10 | 0 | 1 | mean |
Excel formula:
=F_DIST( , 5, 10, 0, 1, "mean")
Expected output:
Result |
---|
1.25 |
Python Code
from scipy.stats import f as scipy_f
import math
def f_dist(value=None, dfn=1.0, dfd=1.0, loc=0.0, scale=1.0, method="pdf"):
"""
Unified interface to the main methods of the F-distribution, including PDF, CDF, inverse CDF, survival function, and distribution statistics.
Parameters:
value (float, optional):
For 'pdf', 'cdf', 'sf': the value x at which to evaluate the function (must be > 0)
For 'icdf', 'isf': the probability q (must be between 0 and 1)
For 'mean', 'median', 'var', 'std': skip parameter
dfn (float): Numerator degrees of freedom. Must be > 0.
dfd (float): Denominator degrees of freedom. Must be > 0.
loc (float, optional): Location parameter. Default is 0.0.
scale (float, optional): Scale parameter. Must be > 0. Default is 1.0.
method (str, optional): One of 'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'. Default is 'pdf'.
Returns:
float or str: The result of the requested method, or an error message if the input is invalid.
"""
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:
dfn = float(dfn)
dfd = float(dfd)
loc = float(loc)
scale = float(scale)
except Exception:
return "Invalid input: dfn, dfd, loc, and scale must be numbers."
if dfn <= 0 or dfd <= 0:
return "Invalid input: dfn and dfd must be > 0."
if scale <= 0:
return "Invalid input: scale must be > 0."
dist = scipy_f(dfn, dfd, loc, scale)
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 value <= 0:
return "Invalid input: value must be > 0 for this method."
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':
if not (0 <= value <= 1):
return "Invalid input: value (probability) must be between 0 and 1 for isf."
result = dist.isf(value)
elif method == 'icdf':
if not (0 <= value <= 1):
return "Invalid input: value (probability) must be between 0 and 1 for icdf."
result = dist.ppf(value)
except Exception as e:
return f"scipy.stats.f 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
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.f 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 .