Source code for irfpy.util.likelihood

""" Functions for likelihood.

.. codeauthor:: Yoshifumi Futaana

.. autosummary::

    log_likelihood_normal
    log_likelihood_poisson

For details about likelihood, see http://en.wikipedia.org/wiki/Likelihood_function
"""
import numpy as np

[docs]def log_likelihood_normal(observed, predicted): r""" Return the log likelihood for normal distribution. The log-likelihood for normal distribution is calculated from .. math:: \log L = -\sum (y_o - y_p)^2 where *yo* is the observed values and *yp* is the predicted values. The standard deviation of the normal distribution is assumed constant over each observed value. Normally, predicted values are derived from some parametric function, and therefore the likelihood is a function of the parameters. :param observed: Observed value as np.array :param predicted: Predicted value as np.array :returns: Log likelihood under normal distribution. >>> observed = [3, 2, 3, 4, 6, 2, 1] >>> predicted = [1.8, 2.2, 3.4, 4.6, 6.7, 5.1, 3.2] >>> print('{:.3f}'.format(log_likelihood_normal(observed, predicted))) -16.940 """ return float(-((np.array(observed) - np.array(predicted)) ** 2).sum())
[docs]def log_likelihood_poisson(observed, predicted): r""" Return the log likelihood for poisson statistics The log likelihood for Poisson distribution is calculated from .. math:: \log L = (y_o \times \ln y_p - y_p) where *yo* is the observed values (thus, integer, for Poisson distribution) and *yp* is the predicted values (usually, float). Normally, predicted values are derived from some parametric function, and therefore the likelihood is a function of the parameters. :param observed: Observed value as np.array :param predicted: Predicted value as np.array :returns: Log likelihood under Poisson distribution. >>> observed = [3, 2, 3, 4, 6, 2, 1] >>> predicted = [1.8, 2.2, 3.4, 4.6, 6.7, 5.1, 3.2] >>> print('{:.3f}'.format(log_likelihood_poisson(observed, predicted))) 1.950 """ return float((np.array(observed) * np.log(predicted) - np.array(predicted)).sum())
[docs]def aic(number_of_parameter, log_likelihood): r""" Return the AIC value The AIC (https://en.wikipedia.org/wiki/Akaike_information_criterion) is calculated and returned. .. math:: AIC = 2 k - 2 \ln(L) If your observations are >>> observed = [3, 2, 3, 4, 6, 2, 1] and the best fitted values are >>> predicted = [1.8, 2.2, 3.4, 4.6, 6.7, 5.1, 3.2] the log-likelihood calculate from Poisson distribution is obtained as >>> print('{:.3f}'.format(log_likelihood_poisson(observed, predicted))) 1.950 Then, if the number of parameters used for the best fitted values are 3, the AIC is simply obtained as >>> print('{:.3f}'.format(aic(3, log_likelihood_poisson(observed, predicted)))) 2.100 """ return 2 * number_of_parameter - 2 * log_likelihood