scaled_adaptive_weight_matrix¶
- sofia_redux.toolkit.resampling.scaled_adaptive_weight_matrix(sigma, rchi2, fixed=None)[source]¶
Scales a Gaussian weighting kernel based on a prior fit.
In the standard resampling algorithm, a polynomial fit may weight each sample (\(x\)) according to its distance from the reference position at which the fit is derived (\(x_{ref}\)) such that samples closer to the reference position have more influence on the fit than those that are farther. The weighting function used is:
\[w(x) = exp \left( -\sum_{k=1}^{K}{\frac{(x_{ref, k} - x_k)^2}{2 \sigma_k^2}} \right)\]in \(K\) dimensions where \(\sigma\) (supplied to this function via
sigma
) is a scaling factor, equivalent to the standard deviation of a normal distribution. Following a fit, it is also possible to generate a reduced chi-squared statistic (\(\chi_r^2\)) which measures the “goodness” of fit.With this information we can rescale
sigma
in an attempt to get \(\chi_r^2 \rightarrow 1\) i.e., get a good fit within noise limitations. This function assumes that if \(\chi_r^2 < 1\), the samples have been over-fit, and therefore, the weighting function should be “widened” to allow more distant samples to have a stronger influence on the fit and subsequent \(\chi_r^2\) calculation. Likewise, if \(\chi_r^2 > 1\), this implies that the weighting function should be truncated so that the fit focuses more strongly on providing a good fit to nearby samples. i.e., there is likely structure away from the fit location that cannot be modelled well by a polynomial of the given order.To accomplish this, the weighting kernel is rescaled such that:
\[\chi_r^2 \prod_{k=1}^{K}{\sigma_{scaled, k}^2} = \prod_{k=1}^{K}{\sigma_k^2}\]The reason is that for a multivariate Gaussian:
\[\int_{R^K} exp \left( -\frac{1}{2} (x - x_{ref})^T \Sigma^{-1} (x - x_{ref}) \right) = (2 \pi)^{K/2} |\Sigma|^{1/2}\]where \(\sigma^2 = diag(\Sigma)\):
\[|\Sigma| \propto \prod_{k=1}^{K}{\sigma_k^2} \propto \chi_r\]Note that in this specific implementation, the shape of the weighting kernel remains unchanged and only the overall size is allowed to vary. Therefore, a single scaling factor (\(\beta\)) is applied over all dimensions such that:
\[\sigma_{scaled, k} = \frac{\sigma_k}{\sqrt{\beta}}\]where
\[\beta = \chi_r^{1 / K}\]To reduce subsequent calculations, a scaled \(\alpha\) value is passed out instead of \(\sigma_{scaled}\) where:
\[\alpha = 2 \sigma^2\]Therefore, the final output value will be:
\[\alpha_{scaled, k}^{-1} = \frac{\beta}{2 \sigma_k^2}\]Finally, scaling does not need to occur across all dimensions, and it is possible to fix the shape of the kernel in one or more dimensions by using the
fixed
parameter. If this is the case:\[\beta = \chi_r^{\frac{1}{K - K_{fixed}}}\]where \(K_{fixed}\) is the number dimensions in which scaling has been disabled.
- Parameters:
- sigmanumpy.ndarray (n_dimensions,)
The standard deviations of the Gaussian for each dimensional component used for distance weighting of each sample in the initial fit.
- rchi2float
The reduced chi-squared statistic of the fit.
- fixednumpy.ndarray of bool (n_dimensions,), optional
If supplied,
True
values indicate that the width of the Gaussian along the corresponding axis should not be altered in the output result.
- Returns:
- inverse_alphanumpy.ndarray (n_dimensions,)
The scaled
sigma
values converted to the inversealpha
array, required bycalculate_adaptive_distance_weights_scaled()
to create a set of weighting factors.