Matrix differentiation by a vector in Least Squares method












1












$begingroup$


In a book The Elements of
Statistical Learning
published by Springer we can find following statement:




We can write



$RSS(beta) = (mathbf{y}-mathbf{X}beta)^T(mathbf{y}-mathbf{X}beta)$



where $mathbf{X}$ is an $Ntimes p$ matrix with each row an input vector, and $mathbf{y}$ is an $N$-vector of the outputs in the training set. Differentiating w.r.t. $beta$ we get the normal equations



$mathbf{X}^T(mathbf{y}-mathbf{X}beta) = 0$




Questions



How do I formally derive the normal equations operating on matrix level calculations without diving into operating on scalar elements?



Is my Second attemp valid?



First attemp



Note: $beta$ is an $p$-vector. Let us assume that vectors are vertical matrixes.



As in The Matrix Cookbook (http://www.math.uwaterloo.ca/~hwolkowi//matrixcookbook.pdf) let us assume that
$partialmathbf{X}^T = (partialmathbf{X})^T$
and
$partial(mathbf{XY})=partial(mathbf{X})mathbf{Y}+mathbf{X}partial(mathbf{Y})$.



Let us differentiate with respect to $beta$ and observe that $partial (mathbf{y}-mathbf{X}beta)=-mathbf{X}$.



Now $partial RSS(beta)=(partial (mathbf{y}-mathbf{X}beta))^T(mathbf{y}-mathbf{X}beta)+(mathbf{y}-mathbf{X}beta)^T partial (mathbf{y}-mathbf{X}beta)$



Which gives us $partial RSS(beta)=-mathbf{X}^T (mathbf{y}-mathbf{X}beta)-(mathbf{X}^T (mathbf{y}-mathbf{X}beta))^T$



At this point we find contradiction because dimensions are incompatible to perform summation. $mathbf{X}^T (mathbf{y}-mathbf{X}beta)$ is vertical $p$-vector, while $(mathbf{X}^T (mathbf{y}-mathbf{X}beta))^T$ is horizontal $p$-vector.



Second attemp



If I assumed $partial(mathbf{X}^Tmathbf{Y})=partial(mathbf{X})^Tmathbf{Y}+(mathbf{X}^Tpartial(mathbf{Y}))^T$



I would get that
$partial RSS(beta)=-2mathbf{X}^T (mathbf{y}-mathbf{X}beta)$, which matches with the normal equations from the book.










share|cite|improve this question









$endgroup$








  • 1




    $begingroup$
    there must be a lot of examples to show the derivative of $|Y - Xbeta|^2$ with respect to $beta$... not index notation
    $endgroup$
    – user550103
    Dec 22 '18 at 21:39












  • $begingroup$
    The problem with your first attempt is that the product rule $$d(XY)=dX,Y+X,dY$$ applies, not to gradients, but to differentials (and to time derivatives). In your second attempt, the presumed rule only holds when $(X,Y)$ are vectors -- which, fortunately, they are. So there are problems with both approaches, but the 2nd is less problematic.
    $endgroup$
    – greg
    Dec 22 '18 at 21:54


















1












$begingroup$


In a book The Elements of
Statistical Learning
published by Springer we can find following statement:




We can write



$RSS(beta) = (mathbf{y}-mathbf{X}beta)^T(mathbf{y}-mathbf{X}beta)$



where $mathbf{X}$ is an $Ntimes p$ matrix with each row an input vector, and $mathbf{y}$ is an $N$-vector of the outputs in the training set. Differentiating w.r.t. $beta$ we get the normal equations



$mathbf{X}^T(mathbf{y}-mathbf{X}beta) = 0$




Questions



How do I formally derive the normal equations operating on matrix level calculations without diving into operating on scalar elements?



Is my Second attemp valid?



First attemp



Note: $beta$ is an $p$-vector. Let us assume that vectors are vertical matrixes.



As in The Matrix Cookbook (http://www.math.uwaterloo.ca/~hwolkowi//matrixcookbook.pdf) let us assume that
$partialmathbf{X}^T = (partialmathbf{X})^T$
and
$partial(mathbf{XY})=partial(mathbf{X})mathbf{Y}+mathbf{X}partial(mathbf{Y})$.



Let us differentiate with respect to $beta$ and observe that $partial (mathbf{y}-mathbf{X}beta)=-mathbf{X}$.



Now $partial RSS(beta)=(partial (mathbf{y}-mathbf{X}beta))^T(mathbf{y}-mathbf{X}beta)+(mathbf{y}-mathbf{X}beta)^T partial (mathbf{y}-mathbf{X}beta)$



Which gives us $partial RSS(beta)=-mathbf{X}^T (mathbf{y}-mathbf{X}beta)-(mathbf{X}^T (mathbf{y}-mathbf{X}beta))^T$



At this point we find contradiction because dimensions are incompatible to perform summation. $mathbf{X}^T (mathbf{y}-mathbf{X}beta)$ is vertical $p$-vector, while $(mathbf{X}^T (mathbf{y}-mathbf{X}beta))^T$ is horizontal $p$-vector.



Second attemp



If I assumed $partial(mathbf{X}^Tmathbf{Y})=partial(mathbf{X})^Tmathbf{Y}+(mathbf{X}^Tpartial(mathbf{Y}))^T$



I would get that
$partial RSS(beta)=-2mathbf{X}^T (mathbf{y}-mathbf{X}beta)$, which matches with the normal equations from the book.










share|cite|improve this question









$endgroup$








  • 1




    $begingroup$
    there must be a lot of examples to show the derivative of $|Y - Xbeta|^2$ with respect to $beta$... not index notation
    $endgroup$
    – user550103
    Dec 22 '18 at 21:39












  • $begingroup$
    The problem with your first attempt is that the product rule $$d(XY)=dX,Y+X,dY$$ applies, not to gradients, but to differentials (and to time derivatives). In your second attempt, the presumed rule only holds when $(X,Y)$ are vectors -- which, fortunately, they are. So there are problems with both approaches, but the 2nd is less problematic.
    $endgroup$
    – greg
    Dec 22 '18 at 21:54
















1












1








1





$begingroup$


In a book The Elements of
Statistical Learning
published by Springer we can find following statement:




We can write



$RSS(beta) = (mathbf{y}-mathbf{X}beta)^T(mathbf{y}-mathbf{X}beta)$



where $mathbf{X}$ is an $Ntimes p$ matrix with each row an input vector, and $mathbf{y}$ is an $N$-vector of the outputs in the training set. Differentiating w.r.t. $beta$ we get the normal equations



$mathbf{X}^T(mathbf{y}-mathbf{X}beta) = 0$




Questions



How do I formally derive the normal equations operating on matrix level calculations without diving into operating on scalar elements?



Is my Second attemp valid?



First attemp



Note: $beta$ is an $p$-vector. Let us assume that vectors are vertical matrixes.



As in The Matrix Cookbook (http://www.math.uwaterloo.ca/~hwolkowi//matrixcookbook.pdf) let us assume that
$partialmathbf{X}^T = (partialmathbf{X})^T$
and
$partial(mathbf{XY})=partial(mathbf{X})mathbf{Y}+mathbf{X}partial(mathbf{Y})$.



Let us differentiate with respect to $beta$ and observe that $partial (mathbf{y}-mathbf{X}beta)=-mathbf{X}$.



Now $partial RSS(beta)=(partial (mathbf{y}-mathbf{X}beta))^T(mathbf{y}-mathbf{X}beta)+(mathbf{y}-mathbf{X}beta)^T partial (mathbf{y}-mathbf{X}beta)$



Which gives us $partial RSS(beta)=-mathbf{X}^T (mathbf{y}-mathbf{X}beta)-(mathbf{X}^T (mathbf{y}-mathbf{X}beta))^T$



At this point we find contradiction because dimensions are incompatible to perform summation. $mathbf{X}^T (mathbf{y}-mathbf{X}beta)$ is vertical $p$-vector, while $(mathbf{X}^T (mathbf{y}-mathbf{X}beta))^T$ is horizontal $p$-vector.



Second attemp



If I assumed $partial(mathbf{X}^Tmathbf{Y})=partial(mathbf{X})^Tmathbf{Y}+(mathbf{X}^Tpartial(mathbf{Y}))^T$



I would get that
$partial RSS(beta)=-2mathbf{X}^T (mathbf{y}-mathbf{X}beta)$, which matches with the normal equations from the book.










share|cite|improve this question









$endgroup$




In a book The Elements of
Statistical Learning
published by Springer we can find following statement:




We can write



$RSS(beta) = (mathbf{y}-mathbf{X}beta)^T(mathbf{y}-mathbf{X}beta)$



where $mathbf{X}$ is an $Ntimes p$ matrix with each row an input vector, and $mathbf{y}$ is an $N$-vector of the outputs in the training set. Differentiating w.r.t. $beta$ we get the normal equations



$mathbf{X}^T(mathbf{y}-mathbf{X}beta) = 0$




Questions



How do I formally derive the normal equations operating on matrix level calculations without diving into operating on scalar elements?



Is my Second attemp valid?



First attemp



Note: $beta$ is an $p$-vector. Let us assume that vectors are vertical matrixes.



As in The Matrix Cookbook (http://www.math.uwaterloo.ca/~hwolkowi//matrixcookbook.pdf) let us assume that
$partialmathbf{X}^T = (partialmathbf{X})^T$
and
$partial(mathbf{XY})=partial(mathbf{X})mathbf{Y}+mathbf{X}partial(mathbf{Y})$.



Let us differentiate with respect to $beta$ and observe that $partial (mathbf{y}-mathbf{X}beta)=-mathbf{X}$.



Now $partial RSS(beta)=(partial (mathbf{y}-mathbf{X}beta))^T(mathbf{y}-mathbf{X}beta)+(mathbf{y}-mathbf{X}beta)^T partial (mathbf{y}-mathbf{X}beta)$



Which gives us $partial RSS(beta)=-mathbf{X}^T (mathbf{y}-mathbf{X}beta)-(mathbf{X}^T (mathbf{y}-mathbf{X}beta))^T$



At this point we find contradiction because dimensions are incompatible to perform summation. $mathbf{X}^T (mathbf{y}-mathbf{X}beta)$ is vertical $p$-vector, while $(mathbf{X}^T (mathbf{y}-mathbf{X}beta))^T$ is horizontal $p$-vector.



Second attemp



If I assumed $partial(mathbf{X}^Tmathbf{Y})=partial(mathbf{X})^Tmathbf{Y}+(mathbf{X}^Tpartial(mathbf{Y}))^T$



I would get that
$partial RSS(beta)=-2mathbf{X}^T (mathbf{y}-mathbf{X}beta)$, which matches with the normal equations from the book.







calculus matrix-equations matrix-calculus






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Dec 22 '18 at 20:08









Andrzej GolonkaAndrzej Golonka

488




488








  • 1




    $begingroup$
    there must be a lot of examples to show the derivative of $|Y - Xbeta|^2$ with respect to $beta$... not index notation
    $endgroup$
    – user550103
    Dec 22 '18 at 21:39












  • $begingroup$
    The problem with your first attempt is that the product rule $$d(XY)=dX,Y+X,dY$$ applies, not to gradients, but to differentials (and to time derivatives). In your second attempt, the presumed rule only holds when $(X,Y)$ are vectors -- which, fortunately, they are. So there are problems with both approaches, but the 2nd is less problematic.
    $endgroup$
    – greg
    Dec 22 '18 at 21:54
















  • 1




    $begingroup$
    there must be a lot of examples to show the derivative of $|Y - Xbeta|^2$ with respect to $beta$... not index notation
    $endgroup$
    – user550103
    Dec 22 '18 at 21:39












  • $begingroup$
    The problem with your first attempt is that the product rule $$d(XY)=dX,Y+X,dY$$ applies, not to gradients, but to differentials (and to time derivatives). In your second attempt, the presumed rule only holds when $(X,Y)$ are vectors -- which, fortunately, they are. So there are problems with both approaches, but the 2nd is less problematic.
    $endgroup$
    – greg
    Dec 22 '18 at 21:54










1




1




$begingroup$
there must be a lot of examples to show the derivative of $|Y - Xbeta|^2$ with respect to $beta$... not index notation
$endgroup$
– user550103
Dec 22 '18 at 21:39






$begingroup$
there must be a lot of examples to show the derivative of $|Y - Xbeta|^2$ with respect to $beta$... not index notation
$endgroup$
– user550103
Dec 22 '18 at 21:39














$begingroup$
The problem with your first attempt is that the product rule $$d(XY)=dX,Y+X,dY$$ applies, not to gradients, but to differentials (and to time derivatives). In your second attempt, the presumed rule only holds when $(X,Y)$ are vectors -- which, fortunately, they are. So there are problems with both approaches, but the 2nd is less problematic.
$endgroup$
– greg
Dec 22 '18 at 21:54






$begingroup$
The problem with your first attempt is that the product rule $$d(XY)=dX,Y+X,dY$$ applies, not to gradients, but to differentials (and to time derivatives). In your second attempt, the presumed rule only holds when $(X,Y)$ are vectors -- which, fortunately, they are. So there are problems with both approaches, but the 2nd is less problematic.
$endgroup$
– greg
Dec 22 '18 at 21:54












1 Answer
1






active

oldest

votes


















1












$begingroup$

If you have not found an example, then here it goes.





Before we start deriving the gradient, some facts and notations for brevity:




  • Trace and Frobenius product relation $$leftlangle A, B Crightrangle={rm tr}(A^TBC) := A : B C$$

  • Cyclic properties of Trace/Frobenius product
    begin{align}
    A : B C
    &= BC : A \
    &= B^T A : C \
    &= {text{etc.}} cr
    end{align}



Let $f := left|y- Xbeta right|^2 = left(y- Xbeta right)^T left(y- Xbeta right) = y- Xbeta:y- Xbeta$.



Now, we can obtain the differential first, and then the gradient.
begin{align}
df
&= dleft( y- Xbeta:y- Xbeta right) \
&= 2left(y- Xbeta right) : -X dbeta \
&= -2X^Tleft(y- Xbetaright) : dbeta\
end{align}



Thus, the gradient is
begin{align}
frac{partial}{partial beta} left( left|y - X beta right|^2 right)= -2X^Tleft(y- Xbetaright).
end{align}






share|cite|improve this answer











$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "69"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3049806%2fmatrix-differentiation-by-a-vector-in-least-squares-method%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1












    $begingroup$

    If you have not found an example, then here it goes.





    Before we start deriving the gradient, some facts and notations for brevity:




    • Trace and Frobenius product relation $$leftlangle A, B Crightrangle={rm tr}(A^TBC) := A : B C$$

    • Cyclic properties of Trace/Frobenius product
      begin{align}
      A : B C
      &= BC : A \
      &= B^T A : C \
      &= {text{etc.}} cr
      end{align}



    Let $f := left|y- Xbeta right|^2 = left(y- Xbeta right)^T left(y- Xbeta right) = y- Xbeta:y- Xbeta$.



    Now, we can obtain the differential first, and then the gradient.
    begin{align}
    df
    &= dleft( y- Xbeta:y- Xbeta right) \
    &= 2left(y- Xbeta right) : -X dbeta \
    &= -2X^Tleft(y- Xbetaright) : dbeta\
    end{align}



    Thus, the gradient is
    begin{align}
    frac{partial}{partial beta} left( left|y - X beta right|^2 right)= -2X^Tleft(y- Xbetaright).
    end{align}






    share|cite|improve this answer











    $endgroup$


















      1












      $begingroup$

      If you have not found an example, then here it goes.





      Before we start deriving the gradient, some facts and notations for brevity:




      • Trace and Frobenius product relation $$leftlangle A, B Crightrangle={rm tr}(A^TBC) := A : B C$$

      • Cyclic properties of Trace/Frobenius product
        begin{align}
        A : B C
        &= BC : A \
        &= B^T A : C \
        &= {text{etc.}} cr
        end{align}



      Let $f := left|y- Xbeta right|^2 = left(y- Xbeta right)^T left(y- Xbeta right) = y- Xbeta:y- Xbeta$.



      Now, we can obtain the differential first, and then the gradient.
      begin{align}
      df
      &= dleft( y- Xbeta:y- Xbeta right) \
      &= 2left(y- Xbeta right) : -X dbeta \
      &= -2X^Tleft(y- Xbetaright) : dbeta\
      end{align}



      Thus, the gradient is
      begin{align}
      frac{partial}{partial beta} left( left|y - X beta right|^2 right)= -2X^Tleft(y- Xbetaright).
      end{align}






      share|cite|improve this answer











      $endgroup$
















        1












        1








        1





        $begingroup$

        If you have not found an example, then here it goes.





        Before we start deriving the gradient, some facts and notations for brevity:




        • Trace and Frobenius product relation $$leftlangle A, B Crightrangle={rm tr}(A^TBC) := A : B C$$

        • Cyclic properties of Trace/Frobenius product
          begin{align}
          A : B C
          &= BC : A \
          &= B^T A : C \
          &= {text{etc.}} cr
          end{align}



        Let $f := left|y- Xbeta right|^2 = left(y- Xbeta right)^T left(y- Xbeta right) = y- Xbeta:y- Xbeta$.



        Now, we can obtain the differential first, and then the gradient.
        begin{align}
        df
        &= dleft( y- Xbeta:y- Xbeta right) \
        &= 2left(y- Xbeta right) : -X dbeta \
        &= -2X^Tleft(y- Xbetaright) : dbeta\
        end{align}



        Thus, the gradient is
        begin{align}
        frac{partial}{partial beta} left( left|y - X beta right|^2 right)= -2X^Tleft(y- Xbetaright).
        end{align}






        share|cite|improve this answer











        $endgroup$



        If you have not found an example, then here it goes.





        Before we start deriving the gradient, some facts and notations for brevity:




        • Trace and Frobenius product relation $$leftlangle A, B Crightrangle={rm tr}(A^TBC) := A : B C$$

        • Cyclic properties of Trace/Frobenius product
          begin{align}
          A : B C
          &= BC : A \
          &= B^T A : C \
          &= {text{etc.}} cr
          end{align}



        Let $f := left|y- Xbeta right|^2 = left(y- Xbeta right)^T left(y- Xbeta right) = y- Xbeta:y- Xbeta$.



        Now, we can obtain the differential first, and then the gradient.
        begin{align}
        df
        &= dleft( y- Xbeta:y- Xbeta right) \
        &= 2left(y- Xbeta right) : -X dbeta \
        &= -2X^Tleft(y- Xbetaright) : dbeta\
        end{align}



        Thus, the gradient is
        begin{align}
        frac{partial}{partial beta} left( left|y - X beta right|^2 right)= -2X^Tleft(y- Xbetaright).
        end{align}







        share|cite|improve this answer














        share|cite|improve this answer



        share|cite|improve this answer








        answered Dec 23 '18 at 8:01


























        community wiki





        user550103































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematics Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3049806%2fmatrix-differentiation-by-a-vector-in-least-squares-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen