solve_polynomial_fit¶
- sofia_redux.toolkit.resampling.solve_polynomial_fit(phi_samples, phi_point, data, error, distance_weight, weight, derivative_term_map=None, calculate_variance=True, calculate_rchi2=True, calculate_derivative_mscp=True, error_weighting=True, estimate_covariance=False)[source]¶
Derive a polynomial fit from samples, then calculate fit at single point.
The fit to the sample distribution is given as
\[f(\Phi) = \hat{c} \cdot \Phi\]where \(\Phi\) contains products of the sample coordinates for each coefficient term. The coefficients \(\hat{c}\) are solved for using least-squares fitting and then applied to calculate the fitted value at a single point \(f(\Phi_{fit})\).
It is also possible to return an on the fit as a variance. If a valid error is supplied, it will be propagated. If no valid errors are available, they will be calculated from residuals on the fit.
The reduced chi-squared (\(\chi^2\)) statistic may also be calculated, but is only really meaningful if valid errors were supplied. Otherwise, \(\chi^2 \equiv 1\).
Finally, the covariance of gradients between dimensions may also be returned. Note that these are the weighted mean of all sample gradients.
- Parameters:
- phi_samplesnumpy.ndarray (n_terms, n_samples)
The array of independent terms for each sample.
- phi_pointnumpy.ndarray (n_terms,)
The array containing the independent terms at the fitting point.
- datanumpy.ndarray (n_samples,)
The array of sample values.
- errornumpy.ndarray (n_samples,)
The array of error values for each sample. Note that if errors are unavailable, an array of size 0 may be supplied. If this is the case, and an error on the fit is required, it will be derived from the residuals of the fit from the data. In addition, the reduced chi-squared statistic will always be 1.0 if derived from residuals.
- distance_weightnumpy.ndarray (n_samples,)
The distance weighting factor (not including any error weighting) applied to each sample in the fit.
- weightnumpy.ndarray (n_samples,)
The full weighting factor applied to each sample in the fit.
- derivative_term_mapnumpy.ndarray, optional
A mapping array for the determination of derivatives from the coefficients of the fit, and available terms in “phi”. The shape of the array is (n_dimensions, 3, n_derivative_terms). This is only required if the gradient is required as an output. For a full description of the derivative map, please see
polynomial_derivative_map()
.- calculate_variancebool, optional
If
True
, calculate the variance on the fit. The variance will be calculated irrespectively if a valid error was supplied, and the reduced chi-squared statistic is required as a return value.- calculate_rchi2bool, optional
If
True
, calculate the reduced chi-squared statistic of the fit. Note that valid errors must be supplied for this to be meaningful.- calculate_derivative_mscpbool, optional
If
True
, calculate the covariance of the derivatives at the fit point.- error_weightingbool, optional
If
True
, indicates thatweights
includes an error weighting factor of 1/sigma^2. This allows for a slight speed increase when performing the fit as some mathematical terms will not need to be recalculated.- estimate_covariancebool, optional
If
True
, usesestimated_covariance_matrix_inverse()
instead ofcovariance_matrix_inverse()
when determining the variance. This is suggested if the errors are not well-behaved.
- Returns:
- fit_value, variance, rchi2, gradientsfloat, float, float, numpy.ndarray
The value of the fit at the fit point. The variance and reduced chi-squared will only be calculated if
calculate_variance
andcalculate_rchi2
are respectively set toTrue
. Thegradients
matrix is an (n_dimensions, n_dimensions) array where gradients[i, j] = dx_i * dx_j.