5
$\begingroup$

I have a Dirichlet distribution from which I'm maximizing $\alpha$ by calculating the log likelihood with the following equation

$p\left(s|\alpha\right) = \sum\limits_{i=1}^O\log\left(\frac{n_i!}{\prod\limits_{j=1}^kn_{ij}!}\frac{\Gamma\left(\alpha_0\right)}{\Gamma\left(\sum\limits_{j=1}^k\left(n_{ij}+a_j\right)\right)} \prod\limits_{j=1}^k \frac{\Gamma\left(n_{ij}+\alpha_j\right)}{\Gamma\left(\alpha_j\right)}\right)$

I have a python script to calculate the log likelihood. It works for small values, but when $n_{ij}$ gets high $\Gamma\left(\sum\limits_{j=1}^k\left(n_{ij}+a_j\right)\right)$ and $\Gamma\left(n_{ij}+\alpha_j\right)$ python runs into overflow error (the max for math.gamma is between 171 and 172).

Is there a way to change the likelihood function so that $\Gamma\left(\sum\limits_{j=1}^k\left(n_{ij}+a_j\right)\right)$ and $\Gamma\left(n_{ij}+\alpha_j\right)$ don't run into overflow errors. Can I just divide the values in $\Gamma$ by for example 100, if I then compare the likelihood between $p(s|\alpha)$ with different $\alpha$ values do the best $\alpha$ values still have the highest likelihood?

$\endgroup$
7
  • 3
    $\begingroup$ A common solution is to use a function which computes the log of gamma. I'd be surprised if there weren't already one in Python. Gamma is not a linear function - you can't just scale the arguments to gamma by the same constant and hope it cancels out. $\endgroup$
    – Glen_b
    Commented Oct 7, 2013 at 22:03
  • $\begingroup$ Looking up the digamma function might be your best bet: en.wikipedia.org/wiki/Digamma_function $\endgroup$
    – bdeonovic
    Commented Oct 7, 2013 at 22:09
  • 2
    $\begingroup$ @Benjamin Could you explain how that solves the OP's problem? $\endgroup$
    – Glen_b
    Commented Oct 7, 2013 at 22:10
  • $\begingroup$ @Glen_b python does have a log gamma function. I don't have the mathematical knowledge to know how changing the $\Gamma$ to log $\Gamma$ will affect the likelihood though. Can I just substitute them and still use the likelihood to maximimize $\alpha$? $\endgroup$
    – Niek
    Commented Oct 7, 2013 at 22:11
  • 3
    $\begingroup$ This question is asked in a different guise and answered at stats.stackexchange.com/questions/71030/…. For complex calculations of this sort some caution concerning loss of precision is called for; that too is addressed in the other thread. $\endgroup$
    – whuber
    Commented Oct 8, 2013 at 3:17

1 Answer 1

6
$\begingroup$

You suggest in comments that you don't know how the log-gamma function will work with the likelihood.

You need some basic facts:

1) log of a product of terms is the sum of the logs ($\log(ab) = \log(a) + \log(b)$)

2) log of a reciprocal is the negative of the log ($\log(1/c) = -\log(c)$)

3) The relationship between factorials and Gamma functions ($x! = \Gamma(x+1)$)

Using those three facts you just take the log inside that product (making it a sum of log-terms via rule 1), and apply rules 1,2, and 3 repeatedly until you have nothing but sums and differences of log-gamma functions inside the summation.

(This is not really a statistical issue at all, but a mathematical one, and the problem being solved is a computational one.)

If Python also has a function for the log of a binomial coefficient, you may be able to shave some time off your calculation.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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