I'm trying to get the log-likelihood of a Gaussian in the form

$$ p(\textbf{x}|u,\sigma^2)=\prod_{n=1}^{N}\mathcal{N}(x_n|\mu,\sigma^2) \quad (1) $$

$$ \ln\ p(\textbf{x}|\mu,\sigma^2)=-\frac{1}{2\sigma^2}\sum_{n=1}^N{(x_n-\mu)^2}-\frac{N}{2}\ln\ \sigma^2-\frac{N}{2}\ln\ (2\pi) \quad (2) $$

I've have

Log[Product[PDF[NormalDistribution[μ, σ], Subscript[x, n]], {n, 1, bigN}]] 

which outputs

$$ \ln(\prod_{n=1}^{bigN}\frac{\mathrm e^{\frac{(-\mu+x_n)^2}{2\sigma^2}}}{\sqrt{2\pi}\sigma}). $$

I tried Expand, ExpandAll, PowerExpand, but I can't seem to get it to display like in Equation (2 RHS).


Product[Log[PDF[NormalDistribution[μ, σ], Subscript[x, n]]], {n, 1, bigN}] // PowerExpand` 

I get closer with

$$ \prod_{n=1}^{bigN}\left[\frac{1}{2}(-\ln 2-\ln\pi)-\ln\sigma-\frac{(-\mu+x_n)^2}{2\sigma^2}\right] $$

  • $\begingroup$ Looks like there's a typo in your first line of code -- x_n should be Subscript[x, n]. Though be careful, because using subscripts as variables can get you into trouble. $\endgroup$
    – jjc385
    Sep 12, 2018 at 15:26
  • $\begingroup$ related mathematica.stackexchange.com/a/65991/1089 $\endgroup$
    – chris
    Sep 13, 2018 at 8:45

2 Answers 2

logProd = Log[Product[PDF[NormalDistribution[μ, σ], Subscript[x, n]], {n, 1, bigN}]];

$Assumptions = {σ > 0};
product /: Log[product[a_, b_]] := Sum[FunctionExpand @ Log @ a, b]

Block[{Product = product}, logProd] 

enter image description here

% // TeXForm

$\sum _{n=1}^{\text{bigN}} \left(\log \left(e^{-\frac{\left(x_n-\mu \right){}^2}{2 \sigma ^2}}\right)-\log (\sigma )-\frac{1}{2} \log (2 \pi )\right)$


How about starting with a replacement rule?

logProduct /. Log[Product[expr_, spec_]] :> Sum[Log[expr], spec]


  • $\begingroup$ Since spec can be more than one argument, the matching pattern should probably be spec__ (with 2 blanks). $\endgroup$ Sep 13, 2018 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.