Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weighted linear least squares in OpenCV

OpenCV's cvSolve can solve a linear least-squares problem like this:

// model: y = a1*x1 + a2*x2 + a3
CvMat *y = cvCreateMat(N, 1, CV_64FC1);
CvMat *X = cvCreateMat(N, 3, CV_64FC1);
CvMat *coeff = cvCreateMat(3, 1, CV_64FC1);

// fill vector y and matrix X
for (int i=0; i<N; ++i)
{
    cvmSet(y, i, 0, my_y_value(i) );
    cvmSet(X, i, 0, my_x1_value(i) );
    cvmSet(X, i, 1, my_x2_value(i) );
    cvmSet(X, i, 2, 1 );
} 

cvSolve(X, y, coeff, CV_SVD);
// now coeff contains a1, a2, a3

However, I would like to apply different weights to my data points. How do I apply the weights?

like image 679
Hendrik Avatar asked Feb 06 '13 15:02

Hendrik


1 Answers

I found out it's actually not that difficult:

for (int i=0; i<N; ++i)
{
    double w = weight(i);

    cvmSet(y, i, 0, w * my_y_value(i) );
    cvmSet(X, i, 0, w * my_x1_value(i) );
    cvmSet(X, i, 1, w * my_x2_value(i) );
    cvmSet(X, i, 2, w );
}

cvSolve(X, y, coeff, CV_SVD);

This fragment simply multiplies both the left-hand side and the right-hand side of the linear equation with weight w. The error term for sample i is effectively multiplied by w².

like image 127
Hendrik Avatar answered Nov 13 '22 18:11

Hendrik