derivatives of double summation equation

102 Views Asked by At

f(x)

I want to get the derivative of this function wrt xi.(dy/dxi)

I tried to work this out and I got (2 * f(x) * (Qi@x + Qii * xi + Qji * xj)).

Q is an nxn covariance matrix and x are the weights of a portfolio(nx1).

@ is matrix multiplication.

Can someone help me with it?

1

There are 1 best solutions below

4
On BEST ANSWER

$ \def\o{{\tt1}}\def\p{\partial} \def\LR#1{\left(#1\right)} \def\BR#1{\Big[#1\Big]} \def\vecc#1{\operatorname{vec}\LR{#1}} \def\diag#1{\operatorname{diag}\LR{#1}} \def\Diag#1{\operatorname{Diag}\LR{#1}} \def\trace#1{\operatorname{Tr}\LR{#1}} \def\qiq{\quad\implies\quad} \def\grad#1#2{\frac{\p #1}{\p #2}} \def\gradLR#1#2{\LR{\grad{#1}{#2}}} \def\S{\sum_{i=1}^n\sum_{j=1}^n} $For typing convenience, define the all-ones vector $\o$, the all-ones matrix $J$, and the variables $$\eqalign{ w &= Qx &\qiq dw &= Q\,dx \\ W &= \Diag w \\ X &= \Diag x \\ C &= I - \tfrac 1nJ &&\quad\big\{{\rm Centering\;Matrix}\big\} \\ p &= x\odot w &\qiq dp&=w\odot dx + x\odot dw \\ &&&= W\,dx + X\,dw \\ &&&= \LR{W+XQ}dx \\ }$$ where $(\odot)$ denotes the elementwise/Hadamard product.

Let's also introduce the Frobenius product as a convenient notation for the trace $$\eqalign{ A:B &= \S A_{ij}B_{ij} \;=\; \trace{A^TB} \\ A:A &= \|A\|^2_F \\ }$$ The properties of the underlying trace function allow the terms in a Frobenius product to be rearranged in many different but equivalent ways, e.g. $$\eqalign{ A:B &= B:A \\ A:B &= A^T:B^T \\ C:\LR{AB} &= \LR{CB^T}:A \\&= \LR{A^TC}:B \\ \\ }$$


Use the above notation to rewrite the objective function $$\eqalign{ \phi &= \S\LR{p_i-p_j}^2 \\ &= \S\LR{p_i^2 - 2p_ip_j + p_j^2} \\ &= \S\LR{p_i^2\o_j - 2p_ip_j + \o_ip_j^2} \\ &= n\LR{\sum_{i=1}^np_i^2} - 2\LR{\S p_ip_j} + n\LR{\sum_{j=1}^np_j^2} \\ &= 2n\LR{I:pp^T} - 2\LR{J:pp^T} \\ \phi &= 2n{C:pp^T} \\ }$$ and calculate its gradient vector $$\eqalign{ d\phi &= 2nC:\LR{dp\,p^T + p\,dp^T} \qquad\qquad\qquad\qquad\qquad\quad \\ &= 4nC:\LR{dp\,p^T} \\ &= 4nCp:{dp} \\ &= 4nCp:\LR{W+XQ}dx \\ &= 4n\LR{W+Q^TX}Cp:dx \\ \grad{\phi}{x} &= 4n\LR{W+Q^TX}Cp \\ }$$ To extract the $k^{th}$ component, multiply by the Euclidean basis vector $e_k$ $$\eqalign{ \grad{\phi}{x_k} &= \gradLR{\phi}{x}^Te_k \\ &= 4n\;p^TC\LR{W+XQ}e_k \\ &= 4n\,\BR{\LR{p^TCe_k}w_k + \LR{p^TCXQe_k}} \\ }$$