# Derivation of Normal-Wishart posterior

I am working on the derivation of a Normal-Wishart posterior but I'm stuck at one of the parameters (the posterior of the scale matrix, see at the bottom).

Just for context and completeness, here is the model and the rest of the derivations:

\begin{align} x_i &\sim \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Lambda})\\ \boldsymbol{\mu} &\sim \mathcal{N}(\boldsymbol{\mu_0}, (\kappa_0 \boldsymbol{\Lambda})^{-1})\\ \boldsymbol{\Lambda} &\sim \mathcal{W}(\upsilon_0, \mathbf{W}_0) \end{align}

The expanded forms of each of the three factors is (up to a proportionality constant) are:

• Likelihood: \begin{align} \mathcal{N}(\mathbf{x}_i &| \boldsymbol{\mu}, \boldsymbol{\Lambda}) \propto\notag\\ &|\boldsymbol{\Lambda}|^{N/2} \exp{\left(-\frac{1}{2}\sum_{i=1}^N \left( \mathbf{x}_i^T\boldsymbol{\Lambda}\mathbf{x}_i - 2 \boldsymbol{\mu}^T \boldsymbol{\Lambda}\mathbf{x}_i + \boldsymbol{\mu}^T\boldsymbol{\Lambda}\boldsymbol{\mu}\right) \right)} \end{align}

• Normal prior: \begin{align} \mathcal{N}(\boldsymbol{\mu} &| (\boldsymbol{\mu}_0, \kappa_0 \boldsymbol{\Lambda})^{-1}) \propto\notag\\ &|\boldsymbol{\Lambda}|^{1/2} \exp{\left(-\frac{1}{2}\left( \boldsymbol{\mu}^T\kappa_0 \boldsymbol{\Lambda}\boldsymbol{\mu} - 2 \boldsymbol{\mu}^T \kappa_0 \boldsymbol{\Lambda}\boldsymbol{\mu_0} + \boldsymbol{\mu_0}^T \kappa_0 \boldsymbol{\Lambda}\boldsymbol{\mu_0}\right) \right)} \end{align}

• Wishart prior: \begin{align} \mathcal{W}(\boldsymbol{\Lambda} | \upsilon_0, \mathbf{W}_0) \propto |\boldsymbol{\Lambda}|^{\frac{\upsilon_0-D-1}{2}} \exp{\left(-\frac{1}{2} tr(\mathbf{W}_0^{-1} \boldsymbol{\Lambda})\right)} \end{align}

We want the posterior Normal-Wishart($\mathbf{\mu}, \boldsymbol{\Lambda} | \boldsymbol{\mu}', \kappa', \upsilon', \mathbf{W}'$) which can be decomposed as well as $\mathcal{N}(\boldsymbol{\mu} | \boldsymbol{\mu}, \kappa' \boldsymbol{\Lambda}) \mathcal{W}(\boldsymbol{\Lambda} | \upsilon', \mathbf{W}')$:

### Degress of freedom $\upsilon'$

By merging the first factors of the likelihood and the Wishart we get the first factor of the Wishart factor in the posterior: \begin{align} |\boldsymbol{\Lambda}|^{\frac{\upsilon_0+N-D-1}{2}} \end{align} and therefore we have the first parameter of the posterior: \begin{align} \upsilon' = \upsilon_0 +N \end{align}

### Scale factor $\kappa'$

We identify the elements surrounded by $\boldsymbol{\mu}^T$ and $\boldsymbol{\mu}$ to find who the prior $\kappa_0 \boldsymbol{\Lambda}$ is updated by the likelihood: \begin{align} \boldsymbol{\mu}^T\left((\kappa_0 + N) \boldsymbol{\Lambda}\right)\boldsymbol{\mu} \end{align} and therefore we got the second parameter: \begin{align} \kappa' = \kappa_0 + N \end{align}

### Mean $\boldsymbol{\mu}'$

The third parameters comes from identifying what is inside $2\boldsymbol{\mu}^T...$: \begin{align} 2\boldsymbol{\mu}^T\left(\boldsymbol{\Lambda} N \mathbf{\overline{x} + \kappa_0 \boldsymbol{\Lambda}\boldsymbol{\mu}_0}\right) &= 2\boldsymbol{\mu}^T \kappa'\boldsymbol{\Lambda} \boldsymbol{\mu}'\\ \left(\boldsymbol{\Lambda} N \mathbf{\overline{x} + \kappa_0 \boldsymbol{\Lambda}\boldsymbol{\mu}_0}\right) &= \kappa'\boldsymbol{\Lambda} \boldsymbol{\mu}'\\ \left(N \mathbf{\overline{x} + \kappa_0 \boldsymbol{\mu}_0}\right) &= \kappa' \boldsymbol{\mu}' \end{align} And therefore we got the third parameter: \begin{align} \boldsymbol{\mu}' = \frac{1}{k'}(N\mathbf{\overline{x}} + \kappa_0 \boldsymbol{\mu}_0) \end{align}

### Scale matrix $\boldsymbol{W}'$

And the fourth parameter comes from working on the remaining parameters: \begin{align} tr(\mathbf{W}'^{-1} \boldsymbol{\Lambda}) &= tr(\mathbf{W}_0^{-1} \boldsymbol{\Lambda}) + \sum_{i=1}^{N}\mathbf{x}_i^T\boldsymbol{\Lambda}\mathbf{x}_i + \boldsymbol{\mu_0}^T \kappa_0 \boldsymbol{\Lambda}\boldsymbol{\mu_0}\\ &= tr(\mathbf{W}_0^{-1} \boldsymbol{\Lambda}) + \sum_{i=1}^{N}tr(\mathbf{x}_i^T\boldsymbol{\Lambda}\mathbf{x}_i) + tr(\boldsymbol{\mu_0}^T \kappa_0 \boldsymbol{\Lambda}\boldsymbol{\mu_0})\\ &= tr\bigg(\mathbf{W}_0^{-1} \boldsymbol{\Lambda} + \sum_{i=1}^{N}\mathbf{x}_i^T\boldsymbol{\Lambda}\mathbf{x}_i + \boldsymbol{\mu_0}^T \kappa_0 \boldsymbol{\Lambda}\boldsymbol{\mu_0}\bigg) \end{align}

How to go on from here (if I made no mistakes so far) and get the standard solution for $\mathbf{W}'$?

Edit 1:

Now we re-arrange the terms, add and substract some factors in order to get two squares as in the standard solution: \begin{align} tr(\mathbf{W}'^{-1}\boldsymbol{\Lambda}) =\;& tr\bigg(\mathbf{W}^{-1} \boldsymbol{\Lambda}\\ &+ \sum_{i=1}^N (\mathbf{x}_i^T \boldsymbol{\Lambda} \mathbf{x}_i + \overline{\mathbf{x}}^T \boldsymbol{\Lambda} \overline{\mathbf{x}} -2 \mathbf{x}_i^T \boldsymbol{\Lambda} \mathbf{\overline{x}})\\ &+\kappa_0 (\boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 + \boldsymbol{\overline{x}}^T \boldsymbol{\Lambda}\boldsymbol{\overline{x}} - 2\boldsymbol{\overline{x}}^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0) \notag\\% Substract added factors &-\sum_{i=1}^{N}\overline{\mathbf{x}}^T \boldsymbol{\Lambda} \overline{\mathbf{x}} +2\sum_{i=1}^{N}\mathbf{x}_i^T \boldsymbol{\Lambda} \mathbf{\overline{x}} - \kappa_0\boldsymbol{\overline{x}}^T \boldsymbol{\Lambda} \boldsymbol{\overline{x}} + 2\kappa_0\boldsymbol{\overline{x}}^T \boldsymbol{\Lambda}\boldsymbol{\mu}_0\bigg)\\ =\;& tr\bigg( \mathbf{W}^{-1} \boldsymbol{\Lambda}+ \sum_{i=1}^N (\mathbf{x}_i - \overline{\mathbf{x}})\boldsymbol{\Lambda}(\mathbf{x}_i - \overline{\mathbf{x}})^T + \kappa_0(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)\boldsymbol{\Lambda}(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)^T \notag\\% Substract added factors &-N\overline{\mathbf{x}} \boldsymbol{\Lambda} \overline{\mathbf{x}}^T +2N\mathbf{\overline{x}} \boldsymbol{\Lambda}\mathbf{\overline{x}}^T - \kappa_0\boldsymbol{\overline{x}}\boldsymbol{\Lambda}\boldsymbol{\overline{x}}^T + 2\kappa_0\boldsymbol{\overline{x}}\boldsymbol{\Lambda} \boldsymbol{\mu}_0^T \bigg) \end{align}

We simplify the factors that remain out of the squares: \begin{align} tr(\mathbf{W}'^{-1} \boldsymbol{\Lambda}) =\;& tr( \mathbf{W}^{-1} \boldsymbol{\Lambda}+ \sum_{i=1}^N (\mathbf{x}_i - \overline{\mathbf{x}})^T\boldsymbol{\Lambda}(\mathbf{x}_i - \overline{\mathbf{x}}) + \kappa_0(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)^T\boldsymbol{\Lambda}(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0) \notag\\% Substract added factors & +(N - \kappa_0)\mathbf{\overline{x}}^T\boldsymbol{\Lambda} \mathbf{\overline{x}} + 2\kappa_0\boldsymbol{\overline{x}}^T\boldsymbol{\Lambda}\boldsymbol{\mu}_0 \bigg) \end{align}

The trace is cyclic, so $tr(ABC) = tr(BCA) = tr(CAB)$. Then: \begin{align} tr(\mathbf{W}'^{-1} \boldsymbol{\Lambda}) =\;& tr\bigg( \mathbf{W}^{-1} \boldsymbol{\Lambda}+ \sum_{i=1}^N (\mathbf{x}_i - \overline{\mathbf{x}})(\mathbf{x}_i - \overline{\mathbf{x}})^T\boldsymbol{\Lambda} + \kappa_0(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)^T\boldsymbol{\Lambda} \notag\\% Substract added factors & +(N - \kappa_0)\mathbf{\overline{x}} \mathbf{\overline{x}}^T\boldsymbol{\Lambda} + 2\kappa_0\boldsymbol{\overline{x}}\boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \bigg) \end{align} and then: \begin{align} tr(\mathbf{W}'^{-1}) =\;& tr\bigg( \mathbf{W}^{-1}+ \sum_{i=1}^N (\mathbf{x}_i - \overline{\mathbf{x}})(\mathbf{x}_i - \overline{\mathbf{x}})^T + \kappa_0(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)^T \notag\\% Substract added factors & +(N - \kappa_0)\mathbf{\overline{x}} \mathbf{\overline{x}}^T + 2\kappa_0\boldsymbol{\overline{x}}\boldsymbol{\mu}_0^T \bigg) \end{align}

Almost! But still not there. The goal is: \begin{align} \mathbf{W}^{-1} + \sum_{i=1}^N (\mathbf{x}_i - \overline{\mathbf{x}})(\mathbf{x}_i - \overline{\mathbf{x}})^T + \frac{\kappa_0 N}{\kappa_0 + N}(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)(\boldsymbol{\overline{x}} - \boldsymbol{\mu}_0)^T \end{align}

The trace is cyclic, so $tr(ABC) = tr(BCA) = tr(CAB)$. Also the trace distributes over addition so that $tr(A+B) = tr(A) + tr(B)$. With these facts you should be able to cycle the $\Lambda$ term around to the back in the trace terms, combine the trace terms together. The result should look something like $$W'^{-1} = W^{-1} + \sum_{i=1}^N x_i x_i^\intercal + \mu_0\mu_0^\intercal$$

• thanks! yet, I don't see how to get from there to the standard results (en.wikipedia.org/wiki/Conjugate_prior) which contains $\sum(x_i-\overline{x})$ and $\overline{x} - \mu_0$. I don`t even have negative signs :O May 21 '15 at 5:21

The likelihood $\times$ prior is $$|\boldsymbol{\Lambda}|^{N/2} \exp\left\{-\frac{1}{2}\left(\sum_{i=1}^N \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{x}_i - N\boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\mu} - \boldsymbol{\mu}^T \boldsymbol{\Lambda} N\boldsymbol{\bar{x}} + N\boldsymbol{\mu}^T \boldsymbol{\Lambda} \boldsymbol{\mu} \right) \right\} \\ \times |\boldsymbol{\Lambda}|^{(\nu_0 - D - 1)/2} \exp\left\{-\frac{1}{2} tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right) \right\} \\ \times |\boldsymbol{\Lambda}|^{1/2} \exp \left\{ -\frac{\kappa_0}{2} \left( \boldsymbol{\mu}^T \boldsymbol{\Lambda} \boldsymbol{\mu} - \boldsymbol{\mu}^T\boldsymbol{\Lambda} \boldsymbol{\mu}_0 - \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu} + \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 \right) \right\}.$$ This can be rewritten as $$|\boldsymbol{\Lambda}|^{1/2} |\boldsymbol{\Lambda}|^{(\nu_0 + N - D - 1)/2} \\ \times \exp \left\{ -\frac{1}{2} \left( \left(\kappa_0 + N\right) \boldsymbol{\mu}^T \boldsymbol{\Lambda} \boldsymbol{\mu} - \boldsymbol{\mu}^T \boldsymbol{\Lambda} (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}}) - (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})^T \boldsymbol{\Lambda} \boldsymbol{\mu} \\ + \kappa_0 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 + \sum_{i=1}^N \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{x}_i + tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right) \right) \right\}$$ We can rewrite $$(\kappa_0 + N) \boldsymbol{\mu}^T \boldsymbol{\Lambda} \boldsymbol{\mu} - \boldsymbol{\mu}^T \boldsymbol{\Lambda} (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}}) - (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})^T \boldsymbol{\Lambda} \boldsymbol{\mu} \\ + \kappa_0 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 + \sum_{i=1}^N \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{x}_i + tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right)$$ as follows by adding and subtracting a term: $$(\kappa_0 + N) \boldsymbol{\mu}^T \boldsymbol{\Lambda} \boldsymbol{\mu} - \boldsymbol{\mu}^T \boldsymbol{\Lambda} (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}}) - (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})^T \boldsymbol{\Lambda} \boldsymbol{\mu} \\ + \frac{1}{\kappa_0 + N}(\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})^T \boldsymbol{\Lambda} (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}}) \\ - \frac{1}{\kappa_0 + N}(\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})^T \boldsymbol{\Lambda} (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}}) \\ + \kappa_0 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 + \sum_{i=1}^N \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{x}_i + tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right).$$ The top two lines now factorise as $$(\kappa_0 + N) \left( \boldsymbol{\mu} - \frac{\kappa_0 \boldsymbol{\mu} + N \boldsymbol{\bar{x}}}{\kappa_0 + N} \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{\mu} - \frac{\kappa_0 \boldsymbol{\mu} + N \boldsymbol{\bar{x}}}{\kappa_0 + N} \right).$$

Adding and subtracting $N \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}}$, the following: $$- \frac{1}{\kappa_0 + N}(\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})^T \boldsymbol{\Lambda} (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}}) + \kappa_0 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 + \sum_{i=1}^N \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{x}_i + tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right)$$ can be rewritten as $$\sum_{i=1}^N \left( \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{x}_i - \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} - \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{x}_i + \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} \right) + N \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} + \kappa_0 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 - \frac{1}{\kappa_0 + N}(\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})^T \boldsymbol{\Lambda} (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}}) + tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right).$$ The sum term $$\sum_{i=1}^N \left( \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{x}_i - \boldsymbol{x}_i^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} - \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{x}_i + \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} \right)$$ equals $$\sum_{i=1}^N \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right).$$ Now $$N \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} + \kappa_0 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 - \frac{1}{\kappa_0 + N}(\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})^T \boldsymbol{\Lambda} (\kappa_0 \boldsymbol{\mu}_0 + N \boldsymbol{\bar{x}})$$ can be expanded as $$N \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} + \kappa_0 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 - \frac{1}{\kappa_0 + N}(\kappa_0^2 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 + N \kappa_0 \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}}_0 + N \kappa_0 \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 + N^2 \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}}),$$ which equals $$\frac{N\kappa_0}{\kappa_0 + N} \left( \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} - \boldsymbol{\bar{x}}^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 - \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\bar{x}} + \boldsymbol{\mu}_0^T \boldsymbol{\Lambda} \boldsymbol{\mu}_0 \right) = \frac{N\kappa_0}{\kappa_0 + N}\left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right).$$

The following two terms are scalars: $$\sum_{i=1}^N \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right), \thinspace \frac{N\kappa_0}{\kappa_0 + N}\left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right).$$ And any scalar is equal to its trace, so $$tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right) + \sum_{i=1}^N \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right) + \frac{N\kappa_0}{\kappa_0 + N}\left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)$$ can be rewritten as $$tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right) + tr \left( \sum_{i=1}^N \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right) \right) + tr \left( \frac{N\kappa_0}{\kappa_0 + N}\left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right) \right).$$ Since $tr(\boldsymbol{A} \boldsymbol{B} \boldsymbol{C}) = tr(\boldsymbol{C} \boldsymbol{A} \boldsymbol{B})$, the above sum equals $$tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} \right) + tr \left( \sum_{i=1}^N \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right) \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right)^T \boldsymbol{\Lambda} \right) + tr \left( \frac{N\kappa_0}{\kappa_0 + N}\left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right) \left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)^T \boldsymbol{\Lambda} \right).$$ Using the fact that $tr(\boldsymbol{A} + \boldsymbol{B}) = tr(\boldsymbol{A}) + tr(\boldsymbol{B})$, we can rewrite the sum as $$tr \left( \boldsymbol{W}_0^{-1} \boldsymbol{\Lambda} + \sum_{i=1}^N \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right) \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right)^T \boldsymbol{\Lambda} + \frac{N\kappa_0}{\kappa_0 + N}\left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right) \left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)^T \boldsymbol{\Lambda} \right) = tr \left( \left( \boldsymbol{W}_0^{-1} + \sum_{i=1}^N \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right) \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right)^T + \frac{N\kappa_0}{\kappa_0 + N}\left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right) \left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)^T \right) \boldsymbol{\Lambda} \right).$$

Putting all of that together, if we let $\boldsymbol{S} = \sum_{i=1}^N \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right) \left( \boldsymbol{x}_i - \boldsymbol{\bar{x}} \right)^T$ we have that the likelihood $\times$ prior equals $$|\boldsymbol{\Lambda}|^{1/2} \exp \left\{-\frac{\kappa_0 + N}{2} \left( \boldsymbol{\mu} - \frac{\kappa_0 \boldsymbol{\mu} + N \boldsymbol{\bar{x}}}{\kappa_0 + N} \right)^T \boldsymbol{\Lambda} \left( \boldsymbol{\mu} - \frac{\kappa_0 \boldsymbol{\mu} + N \boldsymbol{\bar{x}}}{\kappa_0 + N} \right) \right\} \\ \times |\boldsymbol{\Lambda}|^{(\nu_0 + N - D - 1)/2} \exp \left\{ - \frac{1}{2} tr \left( \left( \boldsymbol{W}_0^{-1} + \boldsymbol{S} + \frac{N\kappa_0}{\kappa_0 + N}\left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right) \left( \boldsymbol{\bar{x}} - \boldsymbol{\mu}_0 \right)^T \right) \boldsymbol{\Lambda} \right) \right\},$$ as required.

Your error is in the start of your derivation of the scale matrix W':

$$tr(W'^{-1}\Lambda)=\ldots$$

should be

$$tr(W'^{-1}\Lambda) + \mu'^T\kappa'\Lambda\mu'=\ldots$$

In your current solution, you will be missing that final term ($$\mu'^T\kappa'\Lambda\mu'$$) in the normal-part of the posterior.