I have an expression involving the inverse of a symmetric matrix, which I want to differentiate with respect to one of the elements of that matrix. I've been working through the problem with help from the matrix cookbook (https://www.math.uwaterloo.ca/~hwolkowi/matrixcookbook.pdf) but am unsure if I've correctly worked through - I'd be glad of advice as to whether the below is correct, that I have correctly adjusted for the fact that the matrix is symmetric.
(This is for some algebra I am working out in order to implement the move step of an MCMC algorithm)
I have an expression $\mathbf{A}\mathbf{B}^{-1}$, where $\mathbf{A}$ and $\mathbf{B}$ are both square matrices, and $\mathbf{B}$ is symmetric. I want to evaluate the expression:
$\frac{d\mathbf{A}\mathbf{B}^{-1}}{dB_{gh}}$
Where $B_{gh}$ is the elementof matrix $\mathbf{B}$ in the $g$th row, $h$th colummn.
So far I have:
$\frac{d\mathbf{A}\mathbf{B}^{-1}}{dB_{gh}} = \{\frac{d\mathbf{A}}{dB_{gh}}\mathbf{B}^{-1}\}+\{\mathbf{A}\frac{d\mathbf{B}^{-1}}{dB_{gh}}\} $
With $\frac{d\mathbf{A}}{dB_{gh}}=\mathbf{0}$ as $\mathbf{A}$ doesn't contain any elements of $\mathbf{B}$.
Then, that
$\frac{d\mathbf{B}^{-1}}{dB_{gh}}= -\mathbf{B}^{-1}\frac{d\mathbf{B}}{dB_{gh}}\mathbf{B}^{-1}$
Then that as $\mathbf{B}$ is symmetric we have:
$\frac{d\mathbf{B}}{dB_{gh}}=Tr\{\{\frac{d\mathbf{B}}{d\mathbf{B}}\}^T \frac{d\mathbf{B}}{dB_{gh}}\}$
As $\mathbf{B}$ is symmetric, $\frac{d\mathbf{B}}{dB_{gh}}$ evaluates to $\mathbf{S}^{gh}$, where:
$\mathbf{S}^{gh}=\mathbf{J}^{gh}+\mathbf{J}^{hg}-\mathbf{J}^{gh}\mathbf{J}^{gh}$
Where $\mathbf{J}^{gh}$ is a matrix with a $1$ in the $g$th row and $h$th column, with $0$ elsewhere.
However, from this question (How to take the derivative of a matrix with respect to itself?), am I correct in understanding that $\frac{d\mathbf{B}}{d\mathbf{B}}$ evaluates to $\mathbf{B}$?
Which would leave the solution to be the following?
$\frac{d\mathbf{A}\mathbf{B}^{-1}}{dB_{gh}}=-\mathbf{B}^{-1}\{\mathbf{B}\mathbf{S^{gh}}\}\mathbf{B}^{-1}$