Matrix calculus for statistics











up vote
2
down vote

favorite












I want to calculate the variance of a sum of random variables $ (X_1+X_2) $.



Doing statistics, I have to learn maths starting from the end and it is quite difficult, yet very interesting (please consider that I only have the very basic skills in maths).



For now I am doing this calculus manually with the formula $ mathrm{var}(X_1+X_2) = mathrm{var}(X_1) + mathrm{var}(X_2) + 2mathrm{cov}(X_1,X_2)$.



But I am now facing much larger sums (with some minus) and being able to do so with matrix calculation would save me a lot of time (and would be very satisfying too).



I searched every resource in matrix calculus but couldn't find anything usable with my knowledge.



How can I do this calculus from the variance-covariance matrix
$$
begin{pmatrix}
mathrm{var}(X_1) & mathrm{cov}(X_1,X_2) \
mathrm{cov}(X_1,X_2) & mathrm{var}(X_2) \
end{pmatrix}
$$

[preferentially extended to substractions and n terms, like $ (X_1+X_2-X_3) $]?



NB: this is not a statistic question and doesn't belong to stats.stackexchange. I want to understand the thought process of turning scalar calculation to matrix.










share|cite|improve this question









New contributor




Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    I want to calculate the variance of a sum of random variables $ (X_1+X_2) $.



    Doing statistics, I have to learn maths starting from the end and it is quite difficult, yet very interesting (please consider that I only have the very basic skills in maths).



    For now I am doing this calculus manually with the formula $ mathrm{var}(X_1+X_2) = mathrm{var}(X_1) + mathrm{var}(X_2) + 2mathrm{cov}(X_1,X_2)$.



    But I am now facing much larger sums (with some minus) and being able to do so with matrix calculation would save me a lot of time (and would be very satisfying too).



    I searched every resource in matrix calculus but couldn't find anything usable with my knowledge.



    How can I do this calculus from the variance-covariance matrix
    $$
    begin{pmatrix}
    mathrm{var}(X_1) & mathrm{cov}(X_1,X_2) \
    mathrm{cov}(X_1,X_2) & mathrm{var}(X_2) \
    end{pmatrix}
    $$

    [preferentially extended to substractions and n terms, like $ (X_1+X_2-X_3) $]?



    NB: this is not a statistic question and doesn't belong to stats.stackexchange. I want to understand the thought process of turning scalar calculation to matrix.










    share|cite|improve this question









    New contributor




    Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I want to calculate the variance of a sum of random variables $ (X_1+X_2) $.



      Doing statistics, I have to learn maths starting from the end and it is quite difficult, yet very interesting (please consider that I only have the very basic skills in maths).



      For now I am doing this calculus manually with the formula $ mathrm{var}(X_1+X_2) = mathrm{var}(X_1) + mathrm{var}(X_2) + 2mathrm{cov}(X_1,X_2)$.



      But I am now facing much larger sums (with some minus) and being able to do so with matrix calculation would save me a lot of time (and would be very satisfying too).



      I searched every resource in matrix calculus but couldn't find anything usable with my knowledge.



      How can I do this calculus from the variance-covariance matrix
      $$
      begin{pmatrix}
      mathrm{var}(X_1) & mathrm{cov}(X_1,X_2) \
      mathrm{cov}(X_1,X_2) & mathrm{var}(X_2) \
      end{pmatrix}
      $$

      [preferentially extended to substractions and n terms, like $ (X_1+X_2-X_3) $]?



      NB: this is not a statistic question and doesn't belong to stats.stackexchange. I want to understand the thought process of turning scalar calculation to matrix.










      share|cite|improve this question









      New contributor




      Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I want to calculate the variance of a sum of random variables $ (X_1+X_2) $.



      Doing statistics, I have to learn maths starting from the end and it is quite difficult, yet very interesting (please consider that I only have the very basic skills in maths).



      For now I am doing this calculus manually with the formula $ mathrm{var}(X_1+X_2) = mathrm{var}(X_1) + mathrm{var}(X_2) + 2mathrm{cov}(X_1,X_2)$.



      But I am now facing much larger sums (with some minus) and being able to do so with matrix calculation would save me a lot of time (and would be very satisfying too).



      I searched every resource in matrix calculus but couldn't find anything usable with my knowledge.



      How can I do this calculus from the variance-covariance matrix
      $$
      begin{pmatrix}
      mathrm{var}(X_1) & mathrm{cov}(X_1,X_2) \
      mathrm{cov}(X_1,X_2) & mathrm{var}(X_2) \
      end{pmatrix}
      $$

      [preferentially extended to substractions and n terms, like $ (X_1+X_2-X_3) $]?



      NB: this is not a statistic question and doesn't belong to stats.stackexchange. I want to understand the thought process of turning scalar calculation to matrix.







      matrices descriptive-statistics






      share|cite|improve this question









      New contributor




      Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|cite|improve this question









      New contributor




      Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|cite|improve this question




      share|cite|improve this question








      edited 11 hours ago









      Jean-Claude Arbaut

      14.5k63361




      14.5k63361






      New contributor




      Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 14 hours ago









      Dan Chaltiel

      1134




      1134




      New contributor




      Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Dan Chaltiel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          The variance-covariance matrix of $X$ is $frac1n(X-bar X)^T(X-bar X)$.



          Now, you want to compute the variance of the vector $u=Xbeta$ for some vector $beta$. This variance is



          $$Var(u)=frac1n(u-bar u)^T(u-bar u)=frac1n(Xbeta-bar Xbeta)^T(Xbeta-bar Xbeta)\
          =frac1nbeta^T(X-bar X)^T(X-bar X)beta=beta^TVar(X)beta$$






          share|cite|improve this answer























          • +1 Elegant solution
            – caverac
            13 hours ago










          • Great answer, thanks ! Learned a lot with this !
            – Dan Chaltiel
            13 hours ago


















          up vote
          2
          down vote













          The key point here is that



          $$
          mathbb{V}{rm ar}[X] = mathbb{C}{rm ov}[X, X]
          $$



          so that you can express your first expression as



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + a_2 X_2] &=& a_1^2mathbb{V}{rm ar}[X_1] + a_2^2mathbb{V}{rm ar}[X_2] + 2 a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + 2a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + a_1a_2 mathbb{C}{rm ov}[X_1, X_2] + a_2a_1 mathbb{C}{rm ov}[X_2, X_1] \
          &=& sum_{i=1}^2 sum_{j=1}^2 a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}



          In general



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + cdots a_n X_n] &=& sum_{i=1}^n sum_{j=1}^n a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}






          share|cite|improve this answer

















          • 1




            +1, but I think the OP wanted to see how this is done with matrices, that is how to write it as $a^TVar(X)a$.
            – Jean-Claude Arbaut
            13 hours ago











          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',
          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
          });


          }
          });






          Dan Chaltiel is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3009073%2fmatrix-calculus-for-statistics%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          The variance-covariance matrix of $X$ is $frac1n(X-bar X)^T(X-bar X)$.



          Now, you want to compute the variance of the vector $u=Xbeta$ for some vector $beta$. This variance is



          $$Var(u)=frac1n(u-bar u)^T(u-bar u)=frac1n(Xbeta-bar Xbeta)^T(Xbeta-bar Xbeta)\
          =frac1nbeta^T(X-bar X)^T(X-bar X)beta=beta^TVar(X)beta$$






          share|cite|improve this answer























          • +1 Elegant solution
            – caverac
            13 hours ago










          • Great answer, thanks ! Learned a lot with this !
            – Dan Chaltiel
            13 hours ago















          up vote
          3
          down vote



          accepted










          The variance-covariance matrix of $X$ is $frac1n(X-bar X)^T(X-bar X)$.



          Now, you want to compute the variance of the vector $u=Xbeta$ for some vector $beta$. This variance is



          $$Var(u)=frac1n(u-bar u)^T(u-bar u)=frac1n(Xbeta-bar Xbeta)^T(Xbeta-bar Xbeta)\
          =frac1nbeta^T(X-bar X)^T(X-bar X)beta=beta^TVar(X)beta$$






          share|cite|improve this answer























          • +1 Elegant solution
            – caverac
            13 hours ago










          • Great answer, thanks ! Learned a lot with this !
            – Dan Chaltiel
            13 hours ago













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          The variance-covariance matrix of $X$ is $frac1n(X-bar X)^T(X-bar X)$.



          Now, you want to compute the variance of the vector $u=Xbeta$ for some vector $beta$. This variance is



          $$Var(u)=frac1n(u-bar u)^T(u-bar u)=frac1n(Xbeta-bar Xbeta)^T(Xbeta-bar Xbeta)\
          =frac1nbeta^T(X-bar X)^T(X-bar X)beta=beta^TVar(X)beta$$






          share|cite|improve this answer














          The variance-covariance matrix of $X$ is $frac1n(X-bar X)^T(X-bar X)$.



          Now, you want to compute the variance of the vector $u=Xbeta$ for some vector $beta$. This variance is



          $$Var(u)=frac1n(u-bar u)^T(u-bar u)=frac1n(Xbeta-bar Xbeta)^T(Xbeta-bar Xbeta)\
          =frac1nbeta^T(X-bar X)^T(X-bar X)beta=beta^TVar(X)beta$$







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited 13 hours ago

























          answered 13 hours ago









          Jean-Claude Arbaut

          14.5k63361




          14.5k63361












          • +1 Elegant solution
            – caverac
            13 hours ago










          • Great answer, thanks ! Learned a lot with this !
            – Dan Chaltiel
            13 hours ago


















          • +1 Elegant solution
            – caverac
            13 hours ago










          • Great answer, thanks ! Learned a lot with this !
            – Dan Chaltiel
            13 hours ago
















          +1 Elegant solution
          – caverac
          13 hours ago




          +1 Elegant solution
          – caverac
          13 hours ago












          Great answer, thanks ! Learned a lot with this !
          – Dan Chaltiel
          13 hours ago




          Great answer, thanks ! Learned a lot with this !
          – Dan Chaltiel
          13 hours ago










          up vote
          2
          down vote













          The key point here is that



          $$
          mathbb{V}{rm ar}[X] = mathbb{C}{rm ov}[X, X]
          $$



          so that you can express your first expression as



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + a_2 X_2] &=& a_1^2mathbb{V}{rm ar}[X_1] + a_2^2mathbb{V}{rm ar}[X_2] + 2 a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + 2a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + a_1a_2 mathbb{C}{rm ov}[X_1, X_2] + a_2a_1 mathbb{C}{rm ov}[X_2, X_1] \
          &=& sum_{i=1}^2 sum_{j=1}^2 a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}



          In general



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + cdots a_n X_n] &=& sum_{i=1}^n sum_{j=1}^n a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}






          share|cite|improve this answer

















          • 1




            +1, but I think the OP wanted to see how this is done with matrices, that is how to write it as $a^TVar(X)a$.
            – Jean-Claude Arbaut
            13 hours ago















          up vote
          2
          down vote













          The key point here is that



          $$
          mathbb{V}{rm ar}[X] = mathbb{C}{rm ov}[X, X]
          $$



          so that you can express your first expression as



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + a_2 X_2] &=& a_1^2mathbb{V}{rm ar}[X_1] + a_2^2mathbb{V}{rm ar}[X_2] + 2 a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + 2a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + a_1a_2 mathbb{C}{rm ov}[X_1, X_2] + a_2a_1 mathbb{C}{rm ov}[X_2, X_1] \
          &=& sum_{i=1}^2 sum_{j=1}^2 a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}



          In general



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + cdots a_n X_n] &=& sum_{i=1}^n sum_{j=1}^n a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}






          share|cite|improve this answer

















          • 1




            +1, but I think the OP wanted to see how this is done with matrices, that is how to write it as $a^TVar(X)a$.
            – Jean-Claude Arbaut
            13 hours ago













          up vote
          2
          down vote










          up vote
          2
          down vote









          The key point here is that



          $$
          mathbb{V}{rm ar}[X] = mathbb{C}{rm ov}[X, X]
          $$



          so that you can express your first expression as



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + a_2 X_2] &=& a_1^2mathbb{V}{rm ar}[X_1] + a_2^2mathbb{V}{rm ar}[X_2] + 2 a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + 2a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + a_1a_2 mathbb{C}{rm ov}[X_1, X_2] + a_2a_1 mathbb{C}{rm ov}[X_2, X_1] \
          &=& sum_{i=1}^2 sum_{j=1}^2 a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}



          In general



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + cdots a_n X_n] &=& sum_{i=1}^n sum_{j=1}^n a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}






          share|cite|improve this answer












          The key point here is that



          $$
          mathbb{V}{rm ar}[X] = mathbb{C}{rm ov}[X, X]
          $$



          so that you can express your first expression as



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + a_2 X_2] &=& a_1^2mathbb{V}{rm ar}[X_1] + a_2^2mathbb{V}{rm ar}[X_2] + 2 a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + 2a_1a_2 mathbb{C}{rm ov}[X_1, X_2] \
          &=& a_1^2 mathbb{C}{rm ov}[X_1, X_1] + a_2^2 mathbb{C}{rm ov}[X_2, X_2] + a_1a_2 mathbb{C}{rm ov}[X_1, X_2] + a_2a_1 mathbb{C}{rm ov}[X_2, X_1] \
          &=& sum_{i=1}^2 sum_{j=1}^2 a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}



          In general



          begin{eqnarray}
          mathbb{V}{rm ar}[a_1 X_1 + cdots a_n X_n] &=& sum_{i=1}^n sum_{j=1}^n a_i a_j mathbb{C}{rm ov}[X_i, X_j]
          end{eqnarray}







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered 13 hours ago









          caverac

          11.2k21027




          11.2k21027








          • 1




            +1, but I think the OP wanted to see how this is done with matrices, that is how to write it as $a^TVar(X)a$.
            – Jean-Claude Arbaut
            13 hours ago














          • 1




            +1, but I think the OP wanted to see how this is done with matrices, that is how to write it as $a^TVar(X)a$.
            – Jean-Claude Arbaut
            13 hours ago








          1




          1




          +1, but I think the OP wanted to see how this is done with matrices, that is how to write it as $a^TVar(X)a$.
          – Jean-Claude Arbaut
          13 hours ago




          +1, but I think the OP wanted to see how this is done with matrices, that is how to write it as $a^TVar(X)a$.
          – Jean-Claude Arbaut
          13 hours ago










          Dan Chaltiel is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Dan Chaltiel is a new contributor. Be nice, and check out our Code of Conduct.













          Dan Chaltiel is a new contributor. Be nice, and check out our Code of Conduct.












          Dan Chaltiel is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3009073%2fmatrix-calculus-for-statistics%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

          Mouse cursor on multiple screens with different PPI

          Agildo Ribeiro

          Sometime when accessing a menu: “Ubuntu 16.04 has experienced an internal error”