lsq

From DavinciWiki
Jump to: navigation, search

Description

Unconstrained least squares fitting

Arguments and Return Values

Arguments: A two-dimensional matrix, a column vector, and an optional third input specifying the format to return

Return Value: A vector with the fitted coefficients, or a structure containing the fitted coefficients and other fit quantities

Usage

Syntax: lsq(library_matrix, vector_to_be_fitted[, mode])

library_matrix - A two-dimensional matrix (dimensions [m, n, 1]) with the library of known samples (e.g. spectra from mineral library), and a column vector with the unknown sample (e.g. spectrum of some rock) to be fitted with the library.

vector_to_be_fitted - A column vector (dimensions [1, n, 1]) with the sample to be fitted.

The first two inputs must have the same y-dimension.


If using this function for spectral deconvolution, the first input should be a matrix where each column corresponds to a different known sample (usually a known mineral) and each row gives the emissivity or reflectivity value of the samples at a certain wavelength or wavenumber. The second input should be a vector with the unknown sample to be analyzed, and the returned coefficients will be the concentration of each known sample.

The fitted coefficients calculated by this function may be positive, negative, or zero. In some cases, such as spectral deconvolution, it does not make physical sense to have negative coefficients. (What does it mean to have a sample that is -5% quartz?) For cases where none of the coefficients are allowed to be negative, use lsqnn. For cases where some but not all of the coefficients are allowed to be negative, use lsqsn.


An optional third input (passed by value) controls the output mode. If $3=0, the function will only return the fitted coefficients. If $3 is nonzero, or if it is omitted, the function will display a summary of the fit errors and will return a structure with the fitted coefficients, the fitted spectrum, and the fit errors.

If $3=0, lsq returns a column vector containing the fitted coefficients. Otherwise, it returns a structure with the fields A (fitted coefficients), Yfit (fitted spectrum), and other fields quantifying the errors in the fitted spectrum.


This function returns A such that X*A best approximates y, such that the sum of the squared errors is minimized. The data are fit to y = a1*x1 + a2*x2 + ... + an*xn. Note that no constant offset (e.g. blackbody for emissivity spectra) is automatically included by this function; that must be added elsewhere if needed. Many spectral fitting functions (e.g. sma) have their own option to add the constant term.


If lsq() is entered with no arguments, it prints its description, as shown below.

Examples

dv> lsq()

Unconstrained least squares fitting
lsq(X,y) returns A such that X*A best approximates y
 by minimizing the sum of squares of the residuals.
 That is, A for which sum((y_i - A_i*X_i)^2) is minimized.
The data input X should have m rows, n columns, and 1 plane.
The test input y should have m rows, 1 column, and 1 plane.
Will also display summary of errors, unless given a third
 input equal to zero
S.Marshall 09-09-2010

0
dv> X
73x923x1 array of float, bsq format [269,516 bytes]
dv> y
1x923x1 array of float, bsq format [3,692 bytes]
dv> A = lsq(X, y, 0)
1x73x1 array of double, bsq format [584 bytes]
dv> A = lsq(X, y, 1)
Errors in fitted spectrum:
MAD error: 0.007514
RMS error: 0.009874
Maximum absolute error: 0.040065
struct, 5 elements
    A: 1x73x1 array of double, bsq format [584 bytes]
    Yfit: 1x923x1 array of double, bsq format [7,384 bytes]
    MADE: 0.00751428542362132
    RMSE: 0.00987431262035916
    maxE: 0.0400653928518295
dv> A = lsq(X, y)
Errors in fitted spectrum:
MAD error: 0.007514
RMS error: 0.009874
Maximum absolute error: 0.040065
struct, 5 elements
    A: 1x73x1 array of double, bsq format [584 bytes]
    Yfit: 1x923x1 array of double, bsq format [7,384 bytes]
    MADE: 0.00751428542362132
    RMSE: 0.00987431262035916
    maxE: 0.0400653928518295

DavinciWiki Mini-Nav Bar

Contents


Contact Developers

  • davinci-dev [AT] mars.asu.edu

All other topics

  • See navigation on the left

Major Sub-Functions

Related Functions

Recent Library Changes

Created On: 12-02-2010
Modified On: 12-04-2010

Personal tools