# How to calculate $(X^tX)^{-1}X^tY$? [duplicate]

This is

MatrixForm[ {{1., 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1.5, 1.5, 1.5, 2, 2, 2.2, 2.4, 2.5, 2.5, 2.8, 2.8, 3, 3, 3.2, 3.3}}]

$$X^t$$ and this

MatrixForm[ {{101.4, 117.4, 117.1, 106.2, 131.9, 146.9, 146.8, 133.9, 111.3, 123, 125.1, 145.2, 134.3, 144.5, 143.7, 146.9}}] $$Y^t$$,

I tried [x*Transpose[x]] but Mathematica does nothing, does not gives the 2x2 matrix.

At first I tried to indicate the complete $$(X^tX)^{-1}X^tY$$ but returned a really big output, I didn't understand fully so I tried first by parts.

• Don't use MatrixForm. It is a wrapper only meant for display. – Henrik Schumacher Feb 7 '19 at 21:47

Xt = {{1., 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1.5, 1.5,
1.5, 2, 2, 2.2, 2.4, 2.5, 2.5, 2.8, 2.8, 3, 3, 3.2, 3.3}};

Yt = {101.4, 117.4, 117.1, 106.2, 131.9, 146.9, 146.8, 133.9, 111.3,
123, 125.1, 145.2, 134.3, 144.5, 143.7, 146.9};

Inverse[Xt.Transpose[Xt]].Xt.Yt


{93.3422, 15.6485}

Or

X = Transpose@{{1., 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1,
1.5, 1.5, 1.5, 2, 2, 2.2, 2.4, 2.5, 2.5, 2.8, 2.8, 3, 3, 3.2, 3.3}};

Y = {101.4, 117.4, 117.1, 106.2, 131.9, 146.9, 146.8, 133.9, 111.3,
123, 125.1, 145.2, 134.3, 144.5, 143.7, 146.9};

Inverse[Transpose[X].X].Transpose[X].Y


It looks like what you are after is the least squares solution. This is probably most robustly done using the LeastSquares function...

x = Transpose@{{1., 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1.5, 1.5, 1.5, 2, 2, 2.2, 2.4, 2.5, 2.5, 2.8, 2.8, 3, 3, 3.2, 3.3}};
y = {101.4, 117.4, 117.1, 106.2, 131.9, 146.9, 146.8, 133.9, 111.3, 123, 125.1,
145.2, 134.3, 144.5, 143.7, 146.9};
LeastSquares[x, y]