# Deriving ordinary least squares (OLS) in matrix form

How can I instruct Mathematica to derive the OLS in matrix form with respect to $\beta$ and obtain the result ${-2X}^{T}(y-X\beta)$?

The matrices have the following dimensions: $y_{n \times 1}$, $X_{n \times k}$, ${\beta}_{k \times 1}$. At some point, Mathematica should be aware that ${\beta}^{T}{X}^{T}y={y}^{T}X\beta$ because they represent the same scalar. More details about this topic here: http://isites.harvard.edu/fs/docs/icb.topic515975.files/OLSDerivation.pdf

I am a beginner in Mathematica and I tried the following code without any success:

$Assumptions = {Element[y, Matrices[{n, 1}]], Element[X, Matrices[{n, k}]], Element[B, Matrices[{k, 1}]]}; D[(Transpose[(y-X.B)]).(y-X.B), {B}]  • Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Dec 28 '15 at 17:57 • I know its frustrating but mathematica simply has very little capability for dealing with abstract matrices. An important thing to be aware of, typically only functions that support an Assumptions option will make use of $Assumptions. D, Dot and Transpose do not so your first statement does nothing here. – george2079 Dec 28 '15 at 18:59