ó
áp7]c           @  sÙ	  d  Z  d d l m Z d d l Z d d l m Z d d l m Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d k rÕ	d Z e rÈd Z e j d ƒ Z e j j d d ƒ Z e d d … d d … f Z e j e e ƒ e e d d … d f Z d d d g Z e  e e e e ƒ ƒ e e j e e ƒ d d … d f e Z" e  e" ƒ e  e e e j e e ƒ d e ƒ ƒ e  e e j e e ƒ ƒ n  d d l m# Z# m$ Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* e r[e  d ƒ e  e$ j+ e% d d  d! d" d d# d= d$ d ƒƒ e  d% e d d d e d ƒ ƒ e  d& e d d d e d ƒ ƒ e  e$ j+ e% d' d  d( d" d d# d> d$ d ƒƒ e  d& e d' d d e d) ƒ ƒ e  e$ j+ e% d' d  d( d" d d# d? d$ d ƒƒ e  d& e d' d d e d) ƒ ƒ e  e$ j+ e% d' d  d( d" d d# d@ d$ d ƒƒ e  d& e d' d d e d) ƒ ƒ e  e$ j+ e& d d  d( d" d d# dA d$ d ƒƒ e  e$ j+ e' d d  d( d" d d# dB d$ d ƒƒ dC \ Z Z, Z- Z. e  d& e e e, e- e d) ƒ ƒ e  e$ j+ e& e, d  d( d" d d# e e- e. f d$ d ƒƒ e  e$ j+ e' e- d  d( d" d d# e e, e. f d$ d ƒƒ e  d* ƒ e  e$ j+ e( d d  d! d" d d# dD d$ d ƒƒ e  e d d d e ƒ ƒ dE \ Z Z, Z- e  d& e e e, e- e ƒ ƒ e  e$ j+ e) e, d  d( d" d d# e e- f d$ d ƒƒ e  e$ j+ e* e- d  d( d" d d# e e, f d$ d ƒƒ dF \ Z Z, Z- e  d& e e e, e- e ƒ ƒ e  e$ j+ e) e, d  d( d" d d# e e- f d$ d ƒƒ e  e$ j+ e* e- d  d( d" d d# e e, f d$ d ƒƒ e  d+ ƒ e  e d e j/ d d g ƒ d, ƒ e% d d d d, ƒ d- ƒ e  e d e j/ d d g ƒ d ƒ e% d d d d ƒ d- ƒ e  e d e j/ d d g ƒ d ƒ e% d d d d ƒ ƒ e  e d e j/ d dG d0 g ƒ d ƒ e% d. d d. d ƒ ƒ e  e d e j/ d d g ƒ d, ƒ e% d d d d, ƒ ƒ e  e d e j/ d d g ƒ d ƒ e% d d d. e j0 d1 d/ ƒ d ƒ ƒ n  d d2 l1 m2 Z2 dH dI dJ dK g Z e j3 d5 d4 d6 ƒ Z4 x½ e D]µ \ Z, Z- e$ j+ e) e, d  d( d" d d# e4 e- f d$ d ƒZ5 e$ j+ e* e- d  d( d" d d# e4 e, f d$ d ƒZ6 e e4 e, e- e ƒ Z7 e2 e5 e7 d d7 d8 d9 ƒe2 e6 e7 d d7 d8 d: ƒq™Wx€d d d, g D]lZ. xce D][\ Z, Z- e$ j+ e& e, d  d( d" d d# e4 e- e. f d$ d ƒZ5 e$ j+ e' e- d  d( d" d d# e4 e, e. f d$ d ƒZ6 e e4 e, e- e e. ƒ Z7 e2 e5 e7 d d; d8 d9 ƒe2 e6 e7 d d; d8 d: ƒe2 e e4 e j/ e, e- d g ƒ e. ƒ e% e4 e, e- e. ƒ d7 d8 d< ƒe2 e e4 e j/ e, e- d g ƒ e. ƒ e% e4 e, e- e j0 e. d4 e. ƒ e. ƒ d7 d8 d< ƒqoWqbWn  d S(L   s  gradient/Jacobian of normal and t loglikelihood

use chain rule

normal derivative wrt mu, sigma and beta

new version: loc-scale distributions, derivative wrt loc, scale

also includes "standardized" t distribution (for use in GARCH)

TODO:
* use sympy for derivative of loglike wrt shape parameters
  it works for df of t distribution dlog(gamma(a))da = polygamma(0,a) check
  polygamma is available in scipy.special
* get loc-scale example to work with mean = X*b
* write some full unit test examples

A: josef-pktd

iÿÿÿÿ(   t   print_functionN(   t   special(   t   gammalnc         C  sJ   | j  \ } } d t j d t j ƒ t j | ƒ |  | d | } | S(   sr  normal loglikelihood given observations and mean mu and variance sigma2

    Parameters
    ----------
    y : array, 1d
        normally distributed random variable
    params: array, (nobs, 2)
        array of mean, variance (mu, sigma2) with observations in rows

    Returns
    -------
    lls : array
        contribution to loglikelihood for each observation
    g      à¿i   (   t   Tt   npt   logt   pi(   t   yt   paramst   mut   sigma2t   lls(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   norm_lls   s    7c         C  sS   | j  \ } } |  | | } |  | d | d t j | ƒ } t j | | f ƒ S(   s=  Jacobian of normal loglikelihood wrt mean mu and variance sigma2

    Parameters
    ----------
    y : array, 1d
        normally distributed random variable
    params: array, (nobs, 2)
        array of mean, variance (mu, sigma2) with observations in rows

    Returns
    -------
    grad : array (nobs, 2)
        derivative of loglikelihood for each observation wrt mean in first
        column, and wrt variance in second column

    Notes
    -----
    this is actually the derivative wrt sigma not sigma**2, but evaluated
    with parameter sigma2 = sigma**2

    i   i   (   R   R   t   sqrtt   column_stack(   R   R   R	   R
   t   dllsdmut   dllsdsigma2(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   norm_lls_grad0   s    #c         C  s   |  S(   s-   gradient/Jacobian for d (x*beta)/ d beta
    (    (   t   xt   beta(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt	   mean_gradL   s    c   
      C  s»   | d  } | d t  j t |  ƒ d f ƒ } t | | ƒ } t  j | | ƒ } t  j | | f ƒ } t |  | ƒ } t  j | d d … d d … f | | d d … d d … f f ƒ }	 |	 S(   s®  Jacobian of normal loglikelihood wrt mean mu and variance sigma2

    Parameters
    ----------
    y : array, 1d
        normally distributed random variable with mean x*beta, and variance sigma2
    x : array, 2d
        explanatory variables, observation in rows, variables in columns
    params: array_like, (nvars + 1)
        array of coefficients and variance (beta, sigma2)

    Returns
    -------
    grad : array (nobs, 2)
        derivative of loglikelihood for each observation wrt mean in first
        column, and wrt scale (sigma) in second column
    assume params = (beta, sigma2)

    Notes
    -----
    TODO: for heteroscedasticity need sigma to be a 1d array

    iÿÿÿÿi   N(   R   t   onest   lenR   t   dotR   R   (
   R   R   R   R   R
   t   dmudbetaR	   t   params2t   dllsdmst   grad(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   normgradQ   s    
#Ec         C  s£   | j  \ } } | d } t | d d ƒ t | d ƒ d t j | d t j ƒ } | | d d t j d |  | d | d | ƒ d t j | ƒ 8} | S(   sä  t loglikelihood given observations and mean mu and variance sigma2 = 1

    Parameters
    ----------
    y : array, 1d
        normally distributed random variable
    params: array, (nobs, 2)
        array of mean, variance (mu, sigma2) with observations in rows
    df : integer
        degrees of freedom of the t distribution

    Returns
    -------
    lls : array
        contribution to loglikelihood for each observation

    Notes
    -----
    parameterized for garch
    g      ð?i   g       @g      à?i   (   R   R   R   R   R   (   R   R   t   dfR	   R
   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   tstd_llsu   s
    
>Hc         C  s   |  S(   s?   derivative of log pdf of standard normal with respect to y
    (    (   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt
   norm_dlldy”   s    c         C  s†   t  j | d ƒ } t  j t j | d d ƒ t j | d ƒ ƒ t  j | d t  j ƒ } | d |  d | d | d d :} | S(   sI   pdf for standardized (not standard) t distribution, variance is one

    g      ð?i   g       @i   (   R   t   arrayt   expR   R   R   R   (   R   R   t   rt   Px(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   tstd_pdfš   s    I&c         C  s«   t  |  | | ƒ | j \ } } | d } t | d d ƒ t | d ƒ d t j | t j ƒ } | | d d t j d |  | d | | ƒ d t j | ƒ 8} | S(   s  t loglikelihood given observations and mean mu and variance sigma2 = 1

    Parameters
    ----------
    y : array, 1d
        normally distributed random variable
    params: array, (nobs, 2)
        array of mean, variance (mu, sigma2) with observations in rows
    df : integer
        degrees of freedom of the t distribution

    Returns
    -------
    lls : array
        contribution to loglikelihood for each observation

    Notes
    -----
    parameterized for garch
    normalized/rescaled so that sigma2 is the variance

    >>> df = 10; sigma = 1.
    >>> stats.t.stats(df, loc=0., scale=sigma.*np.sqrt((df-2.)/df))
    (array(0.0), array(1.0))
    >>> sigma = np.sqrt(2.)
    >>> stats.t.stats(df, loc=0., scale=sigma*np.sqrt((df-2.)/df))
    (array(0.0), array(2.0))
    g      ð?i   g       @g      à?i   (   t   printR   R   R   R   R   (   R   R   R   R	   R
   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   ts_lls¤   s    
:Dc         C  s+   | d } | d | d |  d | |  S(   s  derivative of log pdf of standard t with respect to y

    Parameters
    ----------
    y : array_like
        data points of random variable at which loglike is evaluated
    df : array_like
        degrees of freedom,shape parameters of log-likelihood function
        of t distribution

    Returns
    -------
    dlldy : array
        derivative of loglikelihood wrt random variable y evaluated at the
        points given in y

    Notes
    -----
    with mean 0 and scale 1, but variance is df/(df-2)

    g      ð?i   i   (    (   R   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   ts_dlldyË   s    
c         C  s)   | d | d d |  d | d |  S(   s  derivative of log pdf of standardized t with respect to y

        Parameters
        ----------
    y : array_like
        data points of random variable at which loglike is evaluated
    df : array_like
        degrees of freedom,shape parameters of log-likelihood function
        of t distribution

    Returns
    -------
    dlldy : array
        derivative of loglikelihood wrt random variable y evaluated at the
        points given in y


    Notes
    -----
    parameterized for garch, standardized to variance=1
    i   g       @i   (    (   R   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt
   tstd_dlldyæ   s    c         G  sS   |  | | } | | | Œ | } d | | | | Œ |  | | d } | | f S(   sË  derivative of log-likelihood with respect to location and scale

    Parameters
    ----------
    y : array_like
        data points of random variable at which loglike is evaluated
    loc : float
        location parameter of distribution
    scale : float
        scale parameter of distribution
    dlldy : function
        derivative of loglikelihood fuction wrt. random variable x
    args : array_like
        shape parameters of log-likelihood function

    Returns
    -------
    dlldloc : array
        derivative of loglikelihood wrt location evaluated at the
        points given in y
    dlldscale : array
        derivative of loglikelihood wrt scale evaluated at the
        points given in y

    g      ð¿i   (    (   R   t   loct   scalet   dlldyt   argst   ystt   dlldloct	   dlldscale(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   locscale_grad   s    't   __main__i    gš™™™™™¹?i   i
   i   i   (   t   statst   miscc         C  s(   t  j t j j |  | d | d | ƒƒ S(   NR)   R*   (   R   R   R2   t   tt   pdf(   R   R)   R*   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   llt3  s    c         C  s(   t  j t j j | | d |  d | ƒƒ S(   NR)   R*   (   R   R   R2   R4   R5   (   R)   R   R*   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   lltloc5  s    c         C  s(   t  j t j j | | d | d |  ƒƒ S(   NR)   R*   (   R   R   R2   R4   R5   (   R*   R   R)   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   lltscale7  s    c         C  s%   t  j t j j |  d | d | ƒƒ S(   NR)   R*   (   R   R   R2   t   normR5   (   R   R)   R*   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   llnorm:  s    c         C  s%   t  j t j j | d |  d | ƒƒ S(   NR)   R*   (   R   R   R2   R9   R5   (   R)   R   R*   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt	   llnormloc<  s    c         C  s%   t  j t j j | d | d |  ƒƒ S(   NR)   R*   (   R   R   R2   R9   R5   (   R*   R   R)   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   llnormscale>  s    s   
gradient of tt   dxgíµ ÷Æ°>t   nR,   t   orders   t t   tsg      ø?g»½×Ùß|Û=i   s   
gradient of norms   
loglike of tid   s   differently standardizedg      ð?g      $@g       @i   (   t   assert_almost_equalg        g       @g       Ài   i   t   err_msgs	   deriv locs   deriv scalei   t   loglike(   i    i   i
   (   i    i   i   (   i    i   i   (   i   i   i   (   g      ø?i   i   (   g      ø?i   i   (   g      ø?i   i   i   (   i    i   (   g      ø?i   i   (   g      ø?i    i   g      $@(   i    i   (   g      ð?g      ð?(   g        g       @(   g      ð?g       @(8   t   __doc__t
   __future__R    t   numpyR   t   scipyR   t   scipy.specialR   R   R   R   R   R   R   R$   R&   R'   R(   R0   t   __name__t   verboset   sigR   R   t   randomt   randnt   rvsR   R   R   R   R%   t   Nonet	   dllfdbetaR2   R3   R6   R7   R8   R:   R;   R<   t
   derivativeR)   R*   R   R    R   t   numpy.testingRA   t   linspacet   ytt   dlldlot   dlldsct   gr(    (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/regression/tools.pyt   <module>   sº   				$			
	'			**
%						
......77
.4444
::7;7K0033%%-