How to get log likelihood for exponential and gamma distributions
Question: I have some data and I can fit a gamma distribution using for example this
code taken from [Fitting a gamma distribution with (python)
Scipy](http://stackoverflow.com/questions/2896179/fitting-a-gamma-
distribution-with-python-scipy) .

    
    
    import scipy.stats as ss
    import scipy as sp
    

Generate some gamma data:

    
    
    alpha=5
    loc=100.5
    beta=22
    data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=10000)    
    print(data)
    # [ 202.36035683  297.23906376  249.53831795 ...,  271.85204096  180.75026301
    #   364.60240242]
    

Here we fit the data to the gamma distribution:

    
    
    fit_alpha,fit_loc,fit_beta=ss.gamma.fit(data)
    print(fit_alpha,fit_loc,fit_beta)
    # (5.0833692504230008, 100.08697963283467, 21.739518937816108)
    
    print(alpha,loc,beta)
    # (5, 100.5, 22)
    

I can also fit an exponential distribution to the same data. I would however
like to do a [likelihood ratio test](http://en.wikipedia.org/wiki/Likelihood-
ratio_test). To do this I don't just need to fit the distributions but I also
need to return the likelihood. How can you do that in python?


Answer: You can compute the log-likelihood of `data` by calling the `logpdf` method of
`stats.gamma` and then summing the array.

The first bit of code is from your example:

    
    
    In [63]: import scipy.stats as ss
    
    In [64]: np.random.seed(123)
    
    In [65]: alpha = 5
    
    In [66]: loc = 100.5
    
    In [67]: beta = 22
    
    In [68]: data = ss.gamma.rvs(alpha, loc=loc, scale=beta, size=10000)
    
    In [70]: data
    Out[70]: 
    array([ 159.73200869,  258.23458137,  178.0504184 , ...,  281.91672824,
            164.77152977,  145.83445141])
    
    In [71]: fit_alpha, fit_loc, fit_beta = ss.gamma.fit(data)
    
    In [72]: fit_alpha, fit_loc, fit_beta
    Out[72]: (4.9953385276512883, 101.24295938462399, 21.992307537192605)
    

Here's how to compute the log-likelihood:

    
    
    In [73]: loglh = ss.gamma.logpdf(data, fit_alpha, fit_loc, fit_beta).sum()
    
    In [74]: loglh
    Out[74]: -52437.410641032831
    

