microcalorimetry.math.fitting¶
Analsysis functions for performing fitting using DataArrays.
Functions¶
|
Derive polynomial coefficients. |
|
Evaluate a polynomial. |
|
Polynomial fit a dataset. |
|
Evaluate polynomial coefficients on x and y. |
|
Fit polynominal coefficients c_n for y = Sum(c_n * x^n) for n<=deg. |
|
Solve's for solution to polynomial. |
|
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:
coeffs (xr.DataArray) – Coefficients along dim ‘degree’ as last dimension.
- Returns:
derivative – Derivative of the derived coefficients
- Return type:
xr.DataArray
- microcalorimetry.math.fitting.polyval(coord: xarray.DataArray, coeffs: xarray.DataArray, degree_dim: str = 'deg')[source]¶
Evaluate a polynomial.
- Parameters:
coord (xr.DataArray) – Array to evaluate
coeffs (xr.DataArray) – Coefficients along dim ‘deg’ as last dimension.
degree_dim (str, optional) – Dimension that stores the degrees. The default is ‘deg’.
- Returns:
val – Array of results of evaluated polynomial.
- Return type:
xr.DataArray
- microcalorimetry.math.fitting.polyfit(xarr: xarray.DataArray, dim: str, deg: int)[source]¶
Polynomial fit a dataset.
- Parameters:
xarr (xr.DataArray) – Data to fit.
dim (str) – dimension to fit along.
deg (int) – degree of fit.
- Returns:
coeffs – Coefficients along dim ‘degree’ as last dimension.
- Return type:
xr.DataArray
- 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:
coeffs (xr.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.
x (xr.DataArray.) – (…, n) shape, last dimension is the fit dimension.
- Returns:
y – x evaluated at polynomial coefficients shape (…, n)
- Return type:
xr.DataArray
- 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:
x (xr.DataArray.) – (…, n) shape, last dimension is the fit dimension.
y (xr.DataArray.) – (…, n) shape, last dimension is the fit dimension. Must be same shape as x.
deg (int) – Degree of polynomial fit.
constrain_zero (bool) – 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:
coeffs – Coefficients along dimension ‘deg’. Index of ‘deg’ corresponds to power of polynomial coefficient.
- Return type:
xr.DataArray
- 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.
y (xr.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 – Shape (…, n, m-1) solutions to polynomial equation at y.
- Return type:
xr.DataArray
- 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:
X (xr.DataArray) – With shape (…,n,m)
Y (xr.DataArray) – With shape (…,n,1).
W (xr.DataArray, optional) – Weight matrix with shape (…, n,m). If none is provide, identity matrix is used (i.e. no weighting).
- Returns:
coeff – Array of coefficiencts with dimensions (…,m) corresponding to the columns along dimension m in X.
- Return type:
xr.DataArray