This is a pre-release version (2.0.0.dev0+git20260907.41eeb59).Go to latest (1.18.0)
{ } Raw JSON

bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / stats / _morestats / boxcox_llf

function

scipy.stats._morestats:boxcox_llf

source: build-install/usr/lib/python3.14/site-packages/scipy/stats/_morestats.py:910

Signature

def boxcox_llf(lmb, data, *, axis = 0, keepdims = False, nan_policy = propagate)

Summary

The boxcox log-likelihood function.

Parameters

lmb : scalar

Parameter for Box-Cox transformation. See boxcox for details.

data : array_like

Data to calculate Box-Cox log-likelihood for. If data is multi-dimensional, the log-likelihood is calculated along the first axis.

axis : int, default: 0

If an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If None, the input will be raveled before computing the statistic.

keepdims : bool, default: False

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

nan_policy : {'propagate', 'omit', 'raise'}

Defines how to handle input NaNs.

  • propagate: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.

  • omit: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.

  • raise: if a NaN is present, a ValueError will be raised.

Returns

llf : float or ndarray

Box-Cox log-likelihood of data given lmb. A float for 1-D data, an array otherwise.

Notes

The Box-Cox log-likelihood function is defined here as

where is the number of data points data and is the Box-Cox transformed input data. This corresponds to the profile log-likelihood of the original data with some constant terms dropped.

Examples

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
✓
Generate some random variates and calculate Box-Cox log-likelihood values for them for a range of ``lmbda`` values:
rng = np.random.default_rng()
x = stats.loggamma.rvs(5, loc=10, size=1000, random_state=rng)
lmbdas = np.linspace(-2, 10)
llf = np.zeros(lmbdas.shape, dtype=float)
for ii, lmbda in enumerate(lmbdas):
    llf[ii] = stats.boxcox_llf(lmbda, x)
✓
Also find the optimal lmbda value with `boxcox`:
x_most_normal, lmbda_optimal = stats.boxcox(x)
✓
Plot the log-likelihood as function of lmbda. Add the optimal lmbda as a horizontal line to check that that's really the optimum:
fig = plt.figure()
ax = fig.add_subplot(111)
✓
ax.plot(lmbdas, llf, 'b.-')
ax.axhline(stats.boxcox_llf(lmbda_optimal, x), color='r')
ax.set_xlabel('lmbda parameter')
ax.set_ylabel('Box-Cox log-likelihood')
✗
Text(0, 0.5, 'Box-Cox log-likelihood')
Now add some probability plots to show that where the log-likelihood is maximized the data transformed with `boxcox` looks closest to normal:
locs = [3, 10, 4]  # 'lower left', 'center', 'lower right'
✓
for lmbda, loc in zip([-1, lmbda_optimal, 9], locs):
    xt = stats.boxcox(x, lmbda=lmbda)
    (osm, osr), (slope, intercept, r_sq) = stats.probplot(xt)
    ax_inset = inset_axes(ax, width="20%", height="20%", loc=loc)
    ax_inset.plot(osm, osr, 'c.', osm, slope*osm + intercept, 'k-')
    ax_inset.set_xticklabels([])
    ax_inset.set_yticklabels([])
    ax_inset.set_title(r'$\lambda=%1.2f$' % lmbda)
✗
[<matplotlib.lines.Line2D object at 0x7f3f58a89e80>, <matplotlib.lines.Line2D object at 0x7f3f58a8a3c0>]
[Text(-4.0, 0, ''), Text(-3.0, 0, ''), Text(-2.0, 0, ''), Text(-1.0, 0, ''), Text(0.0, 0, ''), Text(1.0, 0, ''), Text(2.0, 0, ''), Text(3.0, 0, ''), Text(4.0, 0, '')]
[Text(0, 0.895, ''), Text(0, 0.9, ''), Text(0, 0.905, ''), Text(0, 0.91, ''), Text(0, 0.915, ''), Text(0, 0.92, ''), Text(0, 0.925, ''), Text(0, 0.93, '')]
Text(0.5, 1.0, '$\\lambda=-1.00$')
[<matplotlib.lines.Line2D object at 0x7f3f58892660>, <matplotlib.lines.Line2D object at 0x7f3f601c3e00>]
[Text(-4.0, 0, ''), Text(-3.0, 0, ''), Text(-2.0, 0, ''), Text(-1.0, 0, ''), Text(0.0, 0, ''), Text(1.0, 0, ''), Text(2.0, 0, ''), Text(3.0, 0, ''), Text(4.0, 0, '')]
[Text(0, 2000.0, ''), Text(0, 4000.0, ''), Text(0, 6000.0, ''), Text(0, 8000.0, ''), Text(0, 10000.0, ''), Text(0, 12000.0, ''), Text(0, 14000.0, ''), Text(0, 16000.0, '')]
Text(0.5, 1.0, '$\\lambda=4.35$')
[<matplotlib.lines.Line2D object at 0x7f3f69733e00>, <matplotlib.lines.Line2D object at 0x7f3f69731be0>]
[Text(-4.0, 0, ''), Text(-3.0, 0, ''), Text(-2.0, 0, ''), Text(-1.0, 0, ''), Text(0.0, 0, ''), Text(1.0, 0, ''), Text(2.0, 0, ''), Text(3.0, 0, ''), Text(4.0, 0, '')]
[Text(0, -200000000.0, ''), Text(0, 0.0, ''), Text(0, 200000000.0, ''), Text(0, 400000000.0, ''), Text(0, 600000000.0, ''), Text(0, 800000000.0, ''), Text(0, 1000000000.0, ''), Text(0, 1200000000.0, '')]
Text(0.5, 1.0, '$\\lambda=9.00$')
plt.show()
✓
fig-9d978aef6d9ecb4e.png

See also

boxcox
boxcox_normmax
boxcox_normplot
probplot

Aliases

  • scipy.stats.boxcox_llf

Referenced by