# `lsq`

_Least squares, matrix divide_
```syntax
x lsq y     lsq[x;y]
```

Where:

- `x` and `y` are float matrixes with the same number of columns
- the number of rows of `y` does not exceed the number of columns
- the rows of `y` are linearly independent

returns the least-squares solution of `x = (x lsq y) mmu y`. That is, if
```q
d:x - (x lsq y) mmu y
```

then `sum d*d` is minimized. If `y` is a square matrix, `d` is the zero matrix, up to rounding errors.
```q
q)a:1f+3 4#til 12
q)b:4 4#2 7 -2 5 5 3 6 1 -2 5 2 7 5 0 3 4f
q)a lsq b
-0.1233333 0.16      0.4766667 0.28
0.07666667 0.6933333 0.6766667 0.5466667
0.2766667  1.226667  0.8766667 0.8133333
q)1e-12>max abs raze a - (a lsq b) mmu b   / zero up to rounding errors
1b
q)a ~ (a lsq b) mmu b                      / tolerant match
1b

q)b:3 4#2 7 -2 5 5 3 6 1 -2 5 2 7f
q)a lsq b
-0.1055556 0.3333333 0.4944444
0.1113757  1.031746  0.7113757
0.3283069  1.730159  0.9283069
q)a - (a lsq b) mmu b                      / minimum squared difference
0.5333333 -0.7333333 -0.2       0.7333333
1.04127   -1.431746  -0.3904762 1.431746
1.549206  -2.130159  -0.5809524 2.130159
```

If the rows of `y` are linearly dependent, the result is null.
```q
q)all raze null a lsq (1 2 3 4f;2 4 6 8f)
1b
```

> **Warning — Use float matrixes**
>
>
> `lsq` is the same primitive as [`!` Dict](dict.md). With matrixes of other types, it returns a dictionary rather than a least-squares solution.
>
> ```q
> q)type a lsq 3 4#2 7 -2 5 5 3 6 1 -2 5 2 7
> 99h
> ```
>
`lsq` solves a normal equations matrix via Cholesky decomposition, which is more robust than matrix inversion and multiplication.
[`inv`](inv.md) uses LU decomposition.

## Polynomial fitting

You can use `lsq` to approximate `x` and `y` values by polynomials.
```q
q)lsfit:{(enlist y) lsq x xexp/: til 1+z} / fit y to poly in x with degree z
q)poly:{[c;x]sum c*x xexp til count c}    / polynomial with coefficients c
q)x:til 6
q)y:poly[1 5 -3 2] each x   / cubic
q)lsfit[x;y] each 1 2 3     / linear, quadratic, cubic (exact) fits
-33 37.6
7 -22.4 12
1 5 -3 2
```

`lsq` gives nearly the same result as `{x mmu inv y}`.
```q
q)A:(1.1 2.2 3.3;4.4 5.5 6.6;7.7 8.8 9.9)
q)B:(1.1 2.1 3.1; 2.3 3.4 4.5; 5.6 7.8 9.8)
q)0.001*floor 0.5+1000*A lsq B
1.211  -0.101 0
-2.119 2.927  0
-5.45  5.954  0
q)1e-10>max abs raze (A lsq B) - A mmu inv B
1b
```

----

[`inv`](inv.md),
[`mmu`](mmu.md)


[Mathematics](math.md)


[LU decomposition](https://en.wikipedia.org/wiki/LU_decomposition "Wikipedia"),
[Cholesky decomposition](https://en.wikipedia.org/wiki/Cholesky_decomposition#Matrix_inversion "Wikipedia")
