Honeycomb  0.1
Component-Model Framework
Public Types | Public Member Functions | List of all members
honey::LinearLeastSqr< Real > Class Template Reference

Linear least squares solver. More...

#include <LinearLeastSqr.h>

Public Types

typedef Matrix< matrix::dynamic, matrix::dynamic, RealMatrix
 
typedef Vec< matrix::dynamic, RealVec
 

Public Member Functions

 LinearLeastSqr ()
 
void calc (const Matrix &x, const Vec &y, Vec &b)
 Linear least squares. More...
 
void calc (const Matrix &x, const Vec &y, const Vec &w, Vec &b)
 Weighted linear least squares. Each of the m equations in $ X \vec{b} = \vec{y} $ has an associated weight. A relatively low weight corresponds to high uncertainty. More...
 
void calc (const Matrix &x, const Vec &y, const Vec &w, const Matrix &c, const Vec &d, Vec &b)
 Constrained weighted linear least squares. Get a best-fit solution to $ X \vec{b} = \vec{y} $, subject to the constraints $ C \vec{b} = \vec{d} $. More...
 

Detailed Description

template<class Real>
class honey::LinearLeastSqr< Real >

Linear least squares solver.

Member Typedef Documentation

template<class Real >
typedef Vec<matrix::dynamic, Real> honey::LinearLeastSqr< Real >::Vec

Constructor & Destructor Documentation

template<class Real >
honey::LinearLeastSqr< Real >::LinearLeastSqr ( )
inline

Member Function Documentation

template<class Real >
void honey::LinearLeastSqr< Real >::calc ( const Matrix x,
const Vec y,
Vec b 
)

Linear least squares.

Get a best-fit solution to the system: $ X \vec{b} = \vec{y} $ where the rows of (m x n) X and m-dim $\vec{y}$ form a system of m linear equations, and n-dim $\vec{b}$ contains unknowns assumed to be linearly related.

Given the residual (error) as $ \vec{r} = X \vec{b} - \vec{y} $, the least squares approach is to minimize the residual's Euclidean $\ell^2$-norm (aka. magnitude) $ \| \vec{r} \| $.
Thus, the least squared problem becomes: $\displaystyle \min_{\vec{b}} \left\| X \vec{b} - \vec{y} \right\|_2 $
It can be shown that this minimization problem can be uniquely solved using the SVD and the pseudo-inverse.

Example: Curve Fitting


Consider a quadratic curve: $ 1 + x + x^2 = y $
Let's say we have a series of $(x,y)$ samples on a graph that we want to fit to our curve. If we plug in our samples directly we will find the left/right-hand sides don't agree, there are errors and the best we can do is minimize them.

Let's introduce the linearly related unknowns into the curve equation:
$ b_0 + b_1 x + b_2 x^2 = y $
We want to find the coefficients of $\vec{b}$ which minimize the errors across all samples (a best-fit curve).

With the model formulated we must now work it into a linear system for the solver. For each $(x,y)$ sample we append a row $(1,x,x^2)$ to X, and a row (y) to $\vec{y}$. We now have all the parameters needed to solve for $\vec{b}$.

Parameters
xA (m x n) matrix of function coefficients. In the usual case m > n, ie. there are more samples than unknowns.
yA m-dim vector of function results
bResult: a n-dim vector of coefficients that best approximates the solution
template<class Real >
void honey::LinearLeastSqr< Real >::calc ( const Matrix x,
const Vec y,
const Vec w,
Vec b 
)

Weighted linear least squares. Each of the m equations in $ X \vec{b} = \vec{y} $ has an associated weight. A relatively low weight corresponds to high uncertainty.

Ideally, a sample's weight should be the inverse of its variance. The residuals that least squares minimizes are multiplied by the weights.

Parameters
x
y
wA m-dim vector of sample weights
b
template<class Real >
void honey::LinearLeastSqr< Real >::calc ( const Matrix x,
const Vec y,
const Vec w,
const Matrix c,
const Vec d,
Vec b 
)

Constrained weighted linear least squares. Get a best-fit solution to $ X \vec{b} = \vec{y} $, subject to the constraints $ C \vec{b} = \vec{d} $.

This is similar to solving with k additional equations that are infinitely weighted.

Parameters
x
y
w
cA (k x n) matrix of constraint coefficients, where k < n. X and C col sizes must match.
dA k-dim vector of constraint results.
b

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