# weighted least squares error

32 views

### Walter Whitlock

Nov 19, 2016, 7:32:18 PM11/19/16
to efficient-java-matrix-library-discuss
I am able to use the LinearSolverFactory.leastSquares() solver to solve my problem.  So far so good.

But my data vary over several orders of magnitude so I want to weight the fit error by 1/(error in data).
Is there an easy way to incorporate using error weight terms into the Linear Solvers?

Walt

### Walter Whitlock

Nov 25, 2016, 9:20:14 AM11/25/16
to efficient-java-matrix-library-discuss
I think I've answered my own question. I found a reference for turning a weighted least squares problem into an ordinary least squares problem. See section 1.2.4 WLS as OLS
in "Least Squares Adjustment: Linear and Nonlinear Weighted Regression Analysis" by Allan Aasbjerg Nielsen available at www2.imm.dtu.dk/pubdb/views/edoc_download.php/2804/pdf/imm2804.pdf. The notation is slightly different, but in ejml terms, to solve a WLS problem, create an mxm diagonal matrix P where the diagonal contains sqrt(weight) and then pre-multiply both the A and y terms by P and then solve the new problem as an OLS.

P = CommonOps.diag(double ... weight[]), I use weight[i] = sqrt(1/(error in y[i]))
C = CommonOps.mult(P, A, C)
z = CommonOps.mult(P, y, z)
and then use a linear solver to solve C * x = z for x.

Walt

On Saturday, November 19, 2016 at 7:32:18 PM UTC-5, Walter Whitlock wrote:
I am able to use the LinearSolverFactory.leastSquares() solver to solve my problem. So far so good.

But my data vary over several orders of magnitude so I wantto weight the fit error by 1/(error in data).

### Peter A

Dec 4, 2016, 2:24:41 PM12/4/16
That looks correct.  Thanks for sharing!  This would probably make for a good example.  I'll add it to the TODO list.

- Peter

--
You received this message because you are subscribed to the Google Groups "efficient-java-matrix-library-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to efficient-java-matrix-library-discuss+unsubscribe@googlegroups.com.