Python module#

Summary#

powerfit.Limit()

Small helper class to keep track of the lower and upper limits of a data set.

powerfit.powerlaw(xdata, ydata[, yerr, ...])

Fit a powerlaw \(y = c x^b\) by a linear fitting of \(\ln y = \ln c + b \ln x\).

powerfit.exp(xdata, ydata[, yerr, ...])

Fit an exponential \(y = c \exp(b x)\) by linear fitting of :math`ln y = ln c + b x`.

powerfit.log(xdata, ydata[, yerr])

Fit a logarithm \(y = a + b \ln x\).

powerfit.linear(xdata, ydata[, yerr, ...])

Fit a linear function \(y = a + b x\).

Details#

class powerfit.Limit#

Small helper class to keep track of the lower and upper limits of a data set.

For example:

lim = Limit()

for data in data_set:
    lim += data

print(lim.lower, lim.upper)
powerfit.evaluate_exp(x: ArrayLike, prefactor: float, exponent: float, **kwargs)#

Evaluate an exponential \(y = c \exp(b x)\)

Parameters:
  • x – Data points along \(x\).

  • prefactor – Prefactor \(c\).

  • exponent – Exponent \(b\).

Returns:

The evaluated exponential.

powerfit.evaluate_linear(x: ArrayLike, offset: float, slope: float, **kwargs)#

Evaluate a linear function \(y = a + b x\) at x.

Parameters:
  • x – Data points along \(x\).

  • offset – Offset \(a\).

  • slope – Slope \(b\).

Returns:

The evaluated linear function.

powerfit.evaluate_powerlaw(x: ArrayLike, prefactor: float, exponent: float, **kwargs)#

Evaluate a powerlaw \(y = c x^b\) at x.

Parameters:
  • x – Data points along \(x\).

  • prefactor – Prefactor \(c\).

  • exponent – Exponent \(b\).

Returns:

The evaluated powerlaw.

powerfit.exp(xdata: ArrayLike, ydata: ArrayLike, yerr: ArrayLike = None, yerr_mode: str = 'differentials', absolute_sigma: bool = True, prefactor: float = None, exponent: float = None) dict#

Fit an exponential \(y = c \exp(b x)\) by linear fitting of :math`ln y = ln c + b x`. This function does not support more customised operation like fitting an offset, but custom code can be easily written by copy/pasting from here.

Warning

If this function is used to plot the fit, beware that the fit is plotted using just two data-points if the axis is set to semilogy-scale (as the fit will be a straight line on that scale).

Different modes are available to treat yerr:

  • "differentials": assume that yerr << ydata, such that

    \[\begin{split}z &\equiv \ln y \\ \delta z &= \left| \frac{\partial z}{\partial y} \right| \delta y \\ \delta z &= \frac{\delta y}{y}\end{split}\]
Parameters:
  • xdata – Data points along the x-axis.

  • ydata – Data points along the y-axis.

  • yerr – Error-bar for ydata.

  • yerr_mode – How to treat the error in ydata, see above.

  • absolute_sigma – Treat (the effective) yerr as absolute error.

  • prefactor – Prefactor \(c\) (fitted if not specified).

  • exponent – Exponent \(b\) (fitted if not specified).

Returns:

The fit details as a dictionary:

prefactor: (Fitted) prefactor :math:`c`.
exponent: (Fitted) exponent :math:`b`.
prefactor_error: Estimated error of prefactor.
exponent_error: Estimated error of exponent.
pcov: Covariance of fit.

powerfit.linear(xdata: ArrayLike, ydata: ArrayLike, yerr: ArrayLike = None, absolute_sigma: bool = True, offset: float = None, slope: float = None) dict#

Fit a linear function \(y = a + b x\).

Parameters:
  • xdata – Data points along the x-axis.

  • ydata – Data points along the y-axis.

  • yerr – Error-bar for ydata.

  • absolute_sigma – Treat (the effective) yerr as absolute error.

  • offset – Offset \(a\) (fitted if not specified).

  • slope – Slope \(b\) (fitted if not specified).

Returns:

The fit details as a dictionary:

offset: (Fitted) offset :math:`a`.
slope: (Fitted) slope :math:`b`.
offset_error: Estimated error of offset.
slope_error: Estimated error of slope.
pcov: Covariance of fit.

powerfit.log(xdata: ArrayLike, ydata: ArrayLike, yerr: ArrayLike = None, **kwargs) dict#

Fit a logarithm \(y = a + b \ln x\). See documentation of linear().

powerfit.powerlaw(xdata: ArrayLike, ydata: ArrayLike, yerr: ArrayLike = None, yerr_mode: str = 'differentials', absolute_sigma: bool = True, prefactor: float = None, exponent: float = None, cutoff_upper: int = 0, cutoff_lower: int = False) dict#

Fit a powerlaw \(y = c x^b\) by a linear fitting of \(\ln y = \ln c + b \ln x\).

Note

This function does not support more customised operation like fitting an offset, but custom code can be easily written by copy/pasting from here.

Warning

If this function is used to plot the fit, beware that the fit is plotted using just two data-points if the axis is set to log-log scale (as the fit will be a straight line on that scale).

Different modes are available to treat an error estimate (yerr) in ydata:

  • "differentials": assume that yerr << ydata, such that

    \[\begin{split}z &\equiv \ln y \\ \delta z &= \left| \frac{\partial z}{\partial y} \right| \delta y \\ \delta z &= \frac{\delta y}{y}\end{split}\]
Parameters:
  • xdata – Data points along the x-axis.

  • ydata – Data points along the y-axis.

  • yerr – Error-bar for ydata (should be the standard deviation).

  • yerr_mode – How to treat the error in ydata, see above.

  • absolute_sigma – Treat (the effective) yerr as absolute error.

  • prefactor – Prefactor \(c\) (fitted if not specified).

  • exponent – Exponent \(b\) (fitted if not specified).

  • cutoff_upper – Automatically remove upper cutoff below (-1) or above (+1) the fit.

  • cutoff_lower – Automatically remove lower cutoff below (-1) or above (+1) the fit.

Returns:

The fit details as a dictionary:

prefactor: (Fitted) prefactor :math:`c`.
exponent: (Fitted) exponent :math:`b`.
prefactor_error: Estimated error of prefactor.
exponent_error: Estimated error of exponent.
pcov: Covariance of fit.
slice: Slice of the data used for the fit. Warning: negative/NaN entries remove first.