Maxima Function
lsquares_estimates (D, x, e, a)
lsquares_estimates(D,x,e,a,initial=L,tol=t)
Estimate parameters a to best fit the equation e
in the variables x and a to the data D,
as determined by the method of least squares.
lsquares_estimates
first seeks an exact solution,
and if that fails, then seeks an approximate solution.
The return value is a list of lists of equations of the form [a = ..., b = ..., c = ...]
.
Each element of the list is a distinct, equivalent minimum of the mean square error.
The data D must be a matrix.
Each row is one datum (which may be called a `record' or `case' in some contexts),
and each column contains the values of one variable across all data.
The list of variables x gives a name for each column of D,
even the columns which do not enter the analysis.
The list of parameters a gives the names of the parameters for which
estimates are sought.
The equation e is an expression or equation in the variables x and a;
if e is not an equation, it is treated the same as e = 0
.
Additional arguments to lsquares_estimates
are specified as equations and passed on verbatim to the function lbfgs
which is called to find estimates by a numerical method
when an exact result is not found.
If some exact solution can be found (via solve
),
the data D may contain non-numeric values.
However, if no exact solution is found,
each element of D must have a numeric value.
This includes numeric constants such as %pi
and %e
as well as literal numbers
(integers, rationals, ordinary floats, and bigfloats).
Numerical calculations are carried out with ordinary floating-point arithmetic,
so all other kinds of numbers are converted to ordinary floats for calculations.
load(lsquares)
loads this function.
See also
lsquares_estimates_exact
,
lsquares_estimates_approximate
,
lsquares_mse
,
lsquares_residuals
,
and lsquares_residual_mse
.
Examples:
A problem for which an exact solution is found.
(%i1) load (lsquares)$ (%i2) M : matrix ([1, 1, 1], [3/2, 1, 2], [9/4, 2, 1], [3, 2, 2], [2, 2, 1]); [ 1 1 1 ] [ ] [ 3 ] [ - 1 2 ] [ 2 ] [ ] (%o2) [ 9 ] [ - 2 1 ] [ 4 ] [ ] [ 3 2 2 ] [ ] [ 2 2 1 ] (%i3) lsquares_estimates (M, [z, x, y], (z + D)^2 = A*x + B*y + C, [A, B, C, D]); 59 27 10921 107 (%o3) [[A = - --, B = - --, C = -----, D = - ---]] 16 16 1024 32
A problem for which no exact solution is found,
so lsquares_estimates
resorts to numerical approximation.
(%i1) load (lsquares)$ (%i2) M : matrix ([1, 1], [2, 7/4], [3, 11/4], [4, 13/4]); [ 1 1 ] [ ] [ 7 ] [ 2 - ] [ 4 ] [ ] (%o2) [ 11 ] [ 3 -- ] [ 4 ] [ ] [ 13 ] [ 4 -- ] [ 4 ] (%i3) lsquares_estimates (M, [x, y], y = a*x^b + c, [a, b, c], initial = [3, 3, 3], iprint = [-1, 0]); (%o3) [[a = 1.387365874920637, b = .7110956639593767, c = - .4142705622439105]]