microcalorimetry.math.fitting

Analsysis functions for performing fitting using DataArrays.

Functions

polyderive(→ xarray.DataArray)

Derive polynomial coefficients.

polyval(coord, coeffs[, degree_dim])

Evaluate a polynomial.

polyfit(xarr, dim, deg)

Polynomial fit a dataset.

polyval2(coeffs, x)

Evaluate polynomial coefficients on x and y.

polyfit2(x, y, deg[, constrain_zero, yunc])

Fit polynominal coefficients c_n for y = Sum(c_n * x^n) for n<=deg.

polysolve2(coeffs, y)

Solve's for solution to polynomial.

ordinary_least_squares(→ xarray.DataArray)

Perform ordinary least squares on observation M and results y.

Module Contents

microcalorimetry.math.fitting.polyderive(coeffs: xarray.DataArray, degree_dim: str = 'deg') xarray.DataArray[source]

Derive polynomial coefficients.

Parameters:
coeffsxr.DataArray

Coefficients along dim ‘degree’ as last dimension.

Returns:
derivativexr.DataArray

Derivative of the derived coefficients

microcalorimetry.math.fitting.polyval(coord: xarray.DataArray, coeffs: xarray.DataArray, degree_dim: str = 'deg')[source]

Evaluate a polynomial.

Parameters:
coordxr.DataArray

Array to evaluate

coeffsxr.DataArray

Coefficients along dim ‘deg’ as last dimension.

degree_dimstr, optional

Dimension that stores the degrees. The default is ‘deg’.

Returns:
valxr.DataArray

Array of results of evaluated polynomial.

microcalorimetry.math.fitting.polyfit(xarr: xarray.DataArray, dim: str, deg: int)[source]

Polynomial fit a dataset.

Parameters:
xarrxr.DataArray

Data to fit.

dimstr

dimension to fit along.

degint

degree of fit.

Returns:
coeffsxr.DataArray

Coefficients along dim ‘degree’ as last dimension.

microcalorimetry.math.fitting.polyval2(coeffs: xarray.DataArray, x: xarray.DataArray)[source]

Evaluate polynomial coefficients on x and y.

coeffs x assumed to have dimension ‘deg’ with index corresponding to the power. I.E. deg = 2 is the coeffcient for c_2*x**2.

Parameters:
coeffsxr.DataArray

Coefficients along dimension ‘deg’. Index of ‘deg’ corresponds to power of polynomial coefficient. coeffs should have shape (…, m) where m is the number of coefficients and (…) is the same shape as x.

xxr.DataArray.

(…, n) shape, last dimension is the fit dimension.

Returns:
yxr.DataArray

x evaluated at polynomial coefficients shape (…, n)

microcalorimetry.math.fitting.polyfit2(x: xarray.DataArray, y: xarray.DataArray, deg: int, constrain_zero: bool = False, yunc: xarray.DataArray = None)[source]

Fit polynominal coefficients c_n for y = Sum(c_n * x^n) for n<=deg.

Parameters:
xxr.DataArray.

(…, n) shape, last dimension is the fit dimension.

yxr.DataArray.

(…, n) shape, last dimension is the fit dimension. Must be same shape as x.

degint

Degree of polynomial fit.

constrain_zerobool

Constrain fit to cross zero.

yunc: xr.DataArray, optional

(n,) 1d-array. Uncertainty on the yvalues that is diagonalized and used as weighting in the ordinary least squares. If not provided, no weighting is used.

Returns:
coeffsxr.DataArray

Coefficients along dimension ‘deg’. Index of ‘deg’ corresponds to power of polynomial coefficient.

microcalorimetry.math.fitting.polysolve2(coeffs: xarray.DataArray, y: xarray.DataArray)[source]

Solve’s for solution to polynomial.

Solve for x in y = Sum( c_n * x^n).

Parameters:
coeffs: xr.DataArray,

Coefficients with along last dimension called deg. Index is integer with ineteger value corresponding to coefficient degree. Shape (…, m) for degree m polynomial. The 0th degree coefficient can optionally be omitted.

yxr.DataArray, optional

Value to solve for solutions to polynomial at (if provided). Shape (…, n) where (…) matches the dimensions (…) in coeffs. Assumed to be zero if not provided.

Returns:
result: xr.DataArray

Shape (…, n, m-1) solutions to polynomial equation at y.

microcalorimetry.math.fitting.ordinary_least_squares(X: xarray.DataArray, Y: xarray.DataArray, W: xarray.DataArray = None) xarray.DataArray[source]

Perform ordinary least squares on observation M and results y.

This function is designed to operate on stacks of arrays,where the Last 2 dimensions of M are treated as regressor array and last 2 dimensions of Y are treated as the response matrix. Formulation follows the linear matrix formulation outlined in:

https://en.wikipedia.org/wiki/Ordinary_least_squares.

All dimensions ‘…’ in (…,n,m) are treates as copies/stacks. It is assmued y has the same dimensions (…).

Weight matrix defined as follows: https://online.stat.psu.edu/stat501/lesson/13/13.1

Typically a diagonal matrix of the uncertainties.

Parameters:
Xxr.DataArray

With shape (…,n,m)

Yxr.DataArray

With shape (…,n,1).

Wxr.DataArray, optional

Weight matrix with shape (…, n,m). If none is provide, identity matrix is used (i.e. no weighting).

Returns:
coeffxr.DataArray

Array of coefficiencts with dimensions (…,m) corresponding to the columns along dimension m in X.