LOGNORM
Overview
The LOGNORM function computes statistics and evaluations for the lognormal distribution, a continuous probability distribution of a random variable whose logarithm is normally distributed. If a random variable X follows a lognormal distribution, then Y = \ln(X) has a normal distribution. This distribution is widely used in finance, biology, hydrology, and reliability engineering to model quantities that are inherently positive and exhibit multiplicative growth processes.
This implementation uses the SciPy library’s scipy.stats.lognorm module. For detailed documentation, see the SciPy lognorm reference. The lognormal distribution is also discussed extensively on Wikipedia.
The probability density function (PDF) for the lognormal distribution in SciPy’s standardized form is:
f(x, s) = \frac{1}{s x \sqrt{2\pi}} \exp\left(-\frac{\ln^2(x)}{2s^2}\right)
for x > 0 and shape parameter s > 0. The shape parameter s corresponds to the standard deviation of the underlying normal distribution (\sigma). The relationship between normal and lognormal distributions is fundamental: if Y \sim N(\mu, \sigma^2), then X = e^Y is lognormally distributed with s = sigma and scale = exp(mu).
Key statistical properties of the lognormal distribution include:
- Mean: E[X] = e^{\mu + \sigma^2/2}
- Median: e^\mu (equal to the
scaleparameter) - Variance: (e^{\sigma^2} - 1) e^{2\mu + \sigma^2}
- Mode: e^{\mu - \sigma^2}
The distribution is positively skewed and commonly arises in phenomena governed by multiplicative processes, such as stock prices (forming the basis of the Black-Scholes model), particle size distributions, income distributions, and biological measurements.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=LOGNORM(value, s, loc, scale, lognorm_method)
value(float, optional, default: null): Input value for PDF/CDF/SF, or probability for ICDF/ISF methodss(float, optional, default: 1): Shape parameter, the standard deviation of the underlying normal distribution (must be > 0)loc(float, optional, default: 0): Location parameter for shifting the distributionscale(float, optional, default: 1): Scale parameter (must be > 0)lognorm_method(str, optional, default: “pdf”): Statistical method to compute
Returns (float): Result of the computation. str: Error message if input is invalid.
Examples
Example 1: PDF at x=2 with s=0.954
Inputs:
| value | s | loc | scale | lognorm_method |
|---|---|---|---|---|
| 2 | 0.954 | 0 | 1 |
Excel formula:
=LOGNORM(2, 0.954, 0, 1, "pdf")
Expected output:
0.16058279188106722
Example 2: CDF at x=2 with s=0.954
Inputs:
| value | s | loc | scale | lognorm_method |
|---|---|---|---|---|
| 2 | 0.954 | 0 | 1 | cdf |
Excel formula:
=LOGNORM(2, 0.954, 0, 1, "cdf")
Expected output:
0.7662551017048298
Example 3: Inverse CDF at probability 0.736686
Inputs:
| value | s | loc | scale | lognorm_method |
|---|---|---|---|---|
| 0.736686 | 0.954 | 0 | 1 | icdf |
Excel formula:
=LOGNORM(0.736686, 0.954, 0, 1, "icdf")
Expected output:
1.8294883551980028
Example 4: Mean of lognormal distribution with s=0.954
Inputs:
| s | loc | scale | lognorm_method |
|---|---|---|---|
| 0.954 | 0 | 1 | mean |
Excel formula:
=LOGNORM(0.954, 0, 1, "mean")
Expected output:
1.576264803741382
Python Code
from scipy.stats import lognorm as scipy_lognorm
import math
def lognorm(value=None, s=1, loc=0, scale=1, lognorm_method='pdf'):
"""
Compute lognormal distribution statistics and evaluations.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.lognorm.html
This example function is provided as-is without any representation of accuracy.
Args:
value (float, optional): Input value for PDF/CDF/SF, or probability for ICDF/ISF methods Default is None.
s (float, optional): Shape parameter, the standard deviation of the underlying normal distribution (must be > 0) Default is 1.
loc (float, optional): Location parameter for shifting the distribution Default is 0.
scale (float, optional): Scale parameter (must be > 0) Default is 1.
lognorm_method (str, optional): Statistical method to compute Valid options: PDF, CDF, ICDF, SF, ISF, Mean, Median, Var, Std. Default is 'pdf'.
Returns:
float: Result of the computation. str: Error message if input is invalid.
"""
valid_methods = {'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'}
if not isinstance(lognorm_method, str):
return "Invalid input: lognorm_method must be a string."
method = lognorm_method.lower()
if method not in valid_methods:
return f"Invalid method: {lognorm_method}. Must be one of {', '.join(sorted(valid_methods))}."
try:
s = float(s)
loc = float(loc)
scale = float(scale)
except (ValueError, TypeError):
return "Invalid input: s, loc, and scale must be numbers."
if s <= 0:
return "Invalid input: s must be > 0."
if scale <= 0:
return "Invalid input: scale must be > 0."
try:
dist = scipy_lognorm(s, loc=loc, scale=scale)
if method in {'pdf', 'cdf', 'sf', 'icdf', 'isf'}:
if value is None:
return f"Invalid input: missing required argument 'value' for method '{method}'."
try:
val = float(value)
except (ValueError, TypeError):
return "Invalid input: value must be a number."
if method == 'pdf':
result = dist.pdf(val)
elif method == 'cdf':
result = dist.cdf(val)
elif method == 'sf':
result = dist.sf(val)
elif method == 'icdf':
if not (0 <= val <= 1):
return "Invalid input: probability must be between 0 and 1."
result = dist.ppf(val)
elif method == 'isf':
if not (0 <= val <= 1):
return "Invalid input: probability must be between 0 and 1."
result = dist.isf(val)
else:
# Statistics methods
if method == 'mean':
result = dist.mean()
elif method == 'median':
result = dist.median()
elif method == 'var':
result = dist.var()
elif method == 'std':
result = dist.std()
if isinstance(result, float):
if math.isnan(result):
return "Result is NaN"
if math.isinf(result):
return "inf" if result > 0 else "-inf"
return float(result)
except Exception as e:
return f"scipy.stats.lognorm error: {str(e)}"