bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / stats / _morestats / boxcox
function
scipy.stats._morestats:boxcox
source: build-install/usr/lib/python3.14/site-packages/scipy/stats/_morestats.py:1088
Signature
def boxcox(x, lmbda = None, alpha = None, optimizer = None, *, nan_policy = propagate)Summary
Return a dataset transformed by a Box-Cox power transformation.
Parameters
x: ndarrayInput array to be transformed.
If
lmbdais not None, this is an alias of scipy.special.boxcox. Returns nan ifx < 0; returns -inf ifx == 0 and lmbda < 0.If
lmbdais None, array must be positive, 1-dimensional, and non-constant.lmbda: scalar, optionalIf
lmbdais None (default), find the value oflmbdathat maximizes the log-likelihood function and return it as the second output argument.If
lmbdais not None, do the transformation for that value.alpha: float, optionalIf
lmbdais None andalphais not None (default), return the100 * (1-alpha)%confidence interval forlmbdaas the third output argument. Must be between 0.0 and 1.0.If
lmbdais not None,alphais ignored.optimizer: callable, optionalIf
lmbdais None,optimizeris the scalar optimizer used to find the value oflmbdathat minimizes the negative log-likelihood function.optimizeris a callable that accepts one argument:fun
The objective function, which evaluates the negative log-likelihood function at a provided value of
lmbda
and returns an object, such as an instance of scipy.optimize.OptimizeResult, which holds the optimal value of
lmbdain an attributex.See the example in boxcox_normmax or the documentation of scipy.optimize.minimize_scalar for more information.
If
lmbdais not None,optimizeris ignored.nan_policy: {'propagate', 'omit', 'raise'}Defines how to handle NaNs in
x.propagate: if a NaN is present, all outputs will contain NaNs.omit: NaNs will be omitted when calculating the optimal maxlog and confidence interval; NaNs inxwill remain NaNs in the transformed data.raise: if a NaN is present, aValueErrorwill be raised.
Returns
boxcox: ndarrayBox-Cox power transformed array.
maxlog: float, optionalIf the
lmbdaparameter is None, the second returned argument is thelmbdathat maximizes the log-likelihood function.(min_ci, max_ci): tuple of float, optionalIf
lmbdaparameter is None andalphais not None, this returned tuple of floats represents the minimum and maximum confidence limits givenalpha.
Notes
The Box-Cox transform is given by:
boxcox requires the input data to be positive. Sometimes a Box-Cox transformation provides a shift parameter to achieve this; boxcox does not. Such a shift parameter is equivalent to adding a positive constant to x before calling boxcox.
The confidence limits returned when alpha is provided give the interval where:
with the log-likelihood function and the chi-squared function.
Examples
from scipy import stats import matplotlib.pyplot as plt✓
fig = plt.figure() ax1 = fig.add_subplot(211) x = stats.loggamma.rvs(5, size=500) + 5 prob = stats.probplot(x, dist=stats.norm, plot=ax1)✓
ax1.set_xlabel('') ax1.set_title('Probplot against normal distribution')✗
Text(0.5, 1.0, 'Probplot against normal distribution')We now use `boxcox` to transform the data so it's closest to normal:
ax2 = fig.add_subplot(212) xt, _ = stats.boxcox(x) prob = stats.probplot(xt, dist=stats.norm, plot=ax2)✓
ax2.set_title('Probplot after Box-Cox transformation')
✗Text(0.5, 1.0, 'Probplot after Box-Cox transformation')
plt.show()
✓
See also
Aliases
scipy.stats.boxcox