LSST Applications g02d81e74bb+86cf3d8bc9,g180d380827+7a4e862ed4,g2079a07aa2+86d27d4dc4,g2305ad1205+e1ca1c66fa,g29320951ab+012e1474a1,g295015adf3+341ea1ce94,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+c429d67c83,g48712c4677+f88676dd22,g487adcacf7+27e1e21933,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+b41db86c35,g5a732f18d5+53520f316c,g64a986408d+86cf3d8bc9,g858d7b2824+86cf3d8bc9,g8a8a8dda67+585e252eca,g99cad8db69+84912a7fdc,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+a2b54eae19,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+6681f309db,gc120e1dc64+f0fcc2f6d8,gc28159a63d+0e5473021a,gcf0d15dbbd+c429d67c83,gdaeeff99f8+f9a426f77a,ge6526c86ff+0433e6603d,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+86cf3d8bc9,w.2024.17
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | List of all members
lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc > Class Template Reference

Fit an lsst::afw::math::Function1 object to a set of data points in two dimensions. More...

#include <LeastSqFitter2d.h>

Public Member Functions

 LeastSqFitter2d (const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, const std::vector< double > &s, int order)
 Fit a 2d polynomial to a set of data points z(x, y)
 
Eigen::MatrixXd getParams ()
 Build up a triangular matrix of the parameters.
 
Eigen::MatrixXd getErrors ()
 Companion function to getParams(). Returns uncertainties in the parameters as a matrix.
 
double valueAt (double x, double y)
 Return the value of the best fit function at a given position (x,y)
 
std::vector< double > residuals ()
 Return a vector of residuals of the fit (i.e the difference between the input z values, and the value of the fitting function at that point.
 
double getChiSq ()
 Return a measure of the goodness of fit.
 
double getReducedChiSq ()
 Return a measure of the goodness of fit.
 

Detailed Description

template<class FittingFunc>
class lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >

Fit an lsst::afw::math::Function1 object to a set of data points in two dimensions.

The class is templated over the kind of object to fit. Note that we fit the 1d object in each dimension, not the 2d one.

Input is a list of x and y ordinates for a set of points, the z coordinate, and the uncertainties, s. order is order of the polynomial to fit (e.g if the templated function is lsst::afw::math::PolynomialFunction1, then order=3 => fit a function of the form \(ax^2+bx+c\)

Template Parameters
FittingFuncThe 1d function to fit in both dimensions. Must inherit from lsst::afw::math::Function1
Parameters
xOrdinate of points to fit
yOrdinate of points to fit
zCo-ordinate of pionts to fit
s1 \(\sigma\) uncertainties in z
orderPolynomial order to fit
See also
LeastSqFitter1d

Definition at line 65 of file LeastSqFitter2d.h.

Constructor & Destructor Documentation

◆ LeastSqFitter2d()

template<class FittingFunc >
lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::LeastSqFitter2d ( const std::vector< double > & x,
const std::vector< double > & y,
const std::vector< double > & z,
const std::vector< double > & s,
int order )

Fit a 2d polynomial to a set of data points z(x, y)

Template Parameters
FittingFuncThe type of function to fit in each dimension. This function should extend the base class of lsst::afw::math::Function1. Note that this is a 1d function
Parameters
xvector of x positions of data
yvector of y positions of data
zValue of data for a given x,y. \(z = z_i = z_i(x_i, y_i)\)
sVector of measured uncertainties in the values of z
orderOrder of 2d function to fit

Definition at line 109 of file LeastSqFitter2d.h.

112 : _x(x), _y(y), _z(z), _s(s), _order(order), _nPar(0), _par(1) {
113 //_nPar, the number of terms to fix (x^2, xy, y^2 etc.) is \Sigma^(order+1) 1
114 _nPar = 0;
115 for (int i = 0; i < order; ++i) {
116 _nPar += i + 1;
117 }
118
119 // Check input vectors are the same size
120 _nData = _x.size();
121 if (_nData != static_cast<int>(_y.size())) {
122 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "x and y vectors of different lengths");
123 }
124 if (_nData != static_cast<int>(_s.size())) {
125 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "x and s vectors of different lengths");
126 }
127 if (_nData != static_cast<int>(_z.size())) {
128 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "x and z vectors of different lengths");
129 }
130
131 for (int i = 0; i < _nData; ++i) {
132 if (_s[i] == 0.0) {
133 std::string msg = "Illegal zero value for fit weight encountered.";
134 throw LSST_EXCEPT(pex::exceptions::RuntimeError, msg);
135 }
136 }
137
138 if (_nData < _order) {
139 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Fewer data points than parameters");
140 }
141
142 initFunctions();
143
144 Eigen::MatrixXd design(_nData, _nPar);
145 Eigen::VectorXd rhs(_nData);
146 for (int i = 0; i < _nData; ++i) {
147 rhs[i] = z[i] / s[i];
148 for (int j = 0; j < _nPar; ++j) {
149 design(i, j) = func2d(_x[i], _y[i], j) / _s[i];
150 }
151 }
152 _svd.compute(design, Eigen::ComputeThinU | Eigen::ComputeThinV);
153 _par = _svd.solve(rhs);
154}
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
double z
Definition Match.cc:44
int y
Definition SpanSet.cc:48
T size(T... args)
table::Key< int > order

Member Function Documentation

◆ getChiSq()

template<class FittingFunc >
double lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::getChiSq ( )

Return a measure of the goodness of fit.

\[ \chi^2 = \sum \left( \frac{z_i - f(x_i, y_i)}{s_i} \right)^2 \]

Definition at line 186 of file LeastSqFitter2d.h.

186 {
187 double chisq = 0;
188 for (int i = 0; i < _nData; ++i) {
189 double val = _z[i] - valueAt(_x[i], _y[i]);
190 val /= _s[i];
191 chisq += pow(val, 2);
192 }
193
194 return chisq;
195}
double valueAt(double x, double y)
Return the value of the best fit function at a given position (x,y)
T pow(T... args)
ImageT val
Definition CR.cc:146

◆ getErrors()

template<class FittingFunc >
Eigen::MatrixXd lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::getErrors ( )

Companion function to getParams(). Returns uncertainties in the parameters as a matrix.

Definition at line 235 of file LeastSqFitter2d.h.

235 {
236 Eigen::ArrayXd variance(_nPar);
237 for (int i = 0; i < _nPar; ++i) {
238 variance[i] = _svd.matrixV().row(i).dot(
239 (_svd.singularValues().array().inverse().square() * _svd.matrixV().col(i).array()).matrix());
240 }
241 return expandParams(variance.sqrt().matrix());
242}
afw::table::Key< afw::table::Array< VariancePixelT > > variance

◆ getParams()

template<class FittingFunc >
Eigen::MatrixXd lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::getParams ( )

Build up a triangular matrix of the parameters.

The shape of the matrix is such that the values correspond to the coefficients of the following polynomials
1 y y^2 y^3
x xy xy^2 0
x^2 x^2y 0 0
x^3 0 0 0
(order==4)
where row x column < order

Definition at line 166 of file LeastSqFitter2d.h.

166 {
167 return expandParams(_par);
168}

◆ getReducedChiSq()

template<class FittingFunc >
double lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::getReducedChiSq ( )

Return a measure of the goodness of fit.

\[ \chi_r^2 = \sum \left( \frac{z_i - f(x_i, y_i)}{s} \right)^2 \div (N-p) \]

Where \( N \) is the number of data points, and \( p \) is the number of parameters in the fit

Definition at line 203 of file LeastSqFitter2d.h.

203 {
204 return getChiSq() / (double)(_nData - _nPar);
205}
double getChiSq()
Return a measure of the goodness of fit.

◆ residuals()

template<class FittingFunc >
std::vector< double > lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::residuals ( )

Return a vector of residuals of the fit (i.e the difference between the input z values, and the value of the fitting function at that point.

Definition at line 222 of file LeastSqFitter2d.h.

222 {
224 out.reserve(_nData);
225
226 for (int i = 0; i < _nData; ++i) {
227 out.push_back(_z[i] - valueAt(_x[i], _y[i]));
228 }
229
230 return out;
231}
T push_back(T... args)
T reserve(T... args)

◆ valueAt()

template<class FittingFunc >
double lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::valueAt ( double x,
double y )

Return the value of the best fit function at a given position (x,y)

Definition at line 209 of file LeastSqFitter2d.h.

209 {
210 double val = 0;
211
212 // Sum the values of the different orders to get the value of the fitted function
213 for (int i = 0; i < _nPar; ++i) {
214 val += _par[i] * func2d(x, y, i);
215 }
216 return val;
217}

The documentation for this class was generated from the following file: