Differentiation of series with vectors and matrices I am trying to find the gradient of an equal risk contribution formula as shown below:
$$R = \sum_{i=1}^n\sum_{j=1}^n\left(w_i(Qw)_i - w_j(Qw)_j\right)^2$$
where $w$ is a column vector of $m \times 1$ size
and $Q$ is a co-variance matrix of $m \times m$ size. 
I tried to find the partial derivative for $w_1$ with three assets $w = (w_1, w_2, w_3)$ to start and then I can take that for $n$ amount of partials; however I am not sure I am correct.
My attempt is shown below: /n
$$\frac{\partial R}{\partial w_1} = -2(2) [Qw_1 + w_1Q]*([w_1(Qw)_1 - w_2(Qw)_2] + [w_1(Qw)_1 - w_3(Qw)_3])$$
Please note that I have not done derivatives for a very long time (8 years......), but I was told to find the gradient analytically in this case. /n
Any help would be appreciated. Thanks.  
 A: Let's denote the elementwise (Hadamard) product by $(A\odot B)$, the inner (Frobenius) product by $(A:B)$ and the ordinary matrix product by $(AB)$. 
Let $\{e_k\}$ denote the standard euclidean base vectors. The vector of all ones is then $$\eqalign{u=\sum_{k=1}^n e_k \cr}$$
Finally, let's define a vector specific to your problem
$$\eqalign{x = w\odot(Qw)\cr\cr}$$
Putting this all together, your function can be written as
$$\eqalign{
R&=\sum_{i=1}^n\sum_{j=1}^n \,\,(x_i-x_j)^2  \,=\,\sum_{i=1}^n\sum_{j=1}^n\,\Big((e_i-e_j)^Tx\Big)^2 \cr
 &=\sum_{i=1}^n\sum_{j=1}^n\,\Big((e_i-e_j)(e_i-e_j)^T\Big):(xx^T) \cr
 &=\sum_{i=1}^n\sum_{j=1}^n\,\Big(e_ie_i^T-e_je_i^T-e_ie_j^T+e_je_j^T\Big):(xx^T) \cr
 &=\sum_{i=1}^n\,\Big(ne_ie_i^T-ue_i^T-e_iu^T+I\Big):(xx^T) \cr
 &=\Big(nI-uu^T-uu^T+nI\Big):(xx^T) \cr
 &= 2(nI-uu^T):(xx^T) \cr
\cr
}$$
In this form, it's straightforward to find the differential and gradient
$$\eqalign{
 dR &= 2(nI-uu^T):2\,{\rm sym}(dx\,x^T) \cr
    &= 4\,{\rm sym}(nI-uu^T):(dx\,x^T) \cr
    &= (4nI-4uu^T)x:dx \cr
    &= (4nx-4\beta u):dx \cr
    &= (4nx-4\beta u):(dw\odot Qw + w\odot Qdw) \cr
    &= \Big(Qw\odot(4nx-4\beta u) + Q(w\odot (4nx-4\beta u))\Big) : dw \cr
    &= \Big(4n\,Qw\odot x-4\beta Qw + 4nQ(w\odot x)-4\beta Qw\Big) : dw \cr
    &= \Big(4n\,Qw\odot x-8\beta Qw + 4nQ(w\odot x)\Big) : dw \cr
    &= \Big(4n\,Qw\odot Qw\odot w-8\beta Qw + 4nQ(w\odot Qw\odot w)\Big) : dw \cr
\cr
\frac{\partial R}{\partial w}
 &= 4n\,Qw\odot Qw\odot w-8\beta Qw + 4nQ(w\odot Qw\odot w) \cr
}$$
The key property used in the derivation is the mutual commutability of the Hadamard and Frobenius products
$$\eqalign{
 A:B &= B:A \cr
 A\odot B &= B\odot A \cr
 A\odot B:C &= A:B\odot C \cr
}$$
And that the all ones matrix is the identity element for the Hadamard product.
To get rid of the Hadamard products in the final result, change the vectors into diagonal matrices. Then you can use ordinary matrix products
$$\eqalign{
 A &= {\rm Diag}(Qw) \cr
 W &= {\rm Diag}(w) \cr
 \beta &= u^Tx = w^TQw \cr\cr
\frac{\partial R}{\partial w}
   &= (4n\,A^2-8\beta Q+ 4n\,QWA)\,w
}$$
