U
    Ãmœd2  ã                   @   sŽ  d Z ddlZddlmZ ddlmZ dd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedk�rŠdZe�rPdZe d¡Zej dd¡Zedd…d d…f Ze ee¡eedd…df   Zd d d gZee
eeeƒƒ ee ee¡ dd…df e Zeeƒ eeee ee¡d eƒƒ eee ee¡ ƒ dd!lm Z m!Z! d"d#„ Z"d$d%„ Z#d&d'„ Z$d(d)„ Z%d*d+„ Z&d,d-„ Z'e�rìed.ƒ ee!j(e"d d/d d0dd1�ƒ ed2ed dd edƒƒ ed3ed dd edƒƒ ee!j(e"d4d5d d6dd1�ƒ ed3ed4dd ed7ƒƒ ee!j(e"d4d5d d8dd1�ƒ ed3ed4dded7ƒƒ ee!j(e"d4d5d d9dd1�ƒ ed3ed4d ded7ƒƒ ee!j(e#d d5d d:dd1�ƒ ee!j(e$dd5d d;dd1�ƒ d<\ZZ)Z*Z+ed3eee)e*ed7ƒƒ ee!j(e#e)d5d ee*e+fdd1�ƒ ee!j(e$e*d5d ee)e+fdd1�ƒ ed=ƒ ee!j(e%d d/d d>dd1�ƒ eed dd eƒƒ d?\ZZ)Z*ed3eee)e*eƒƒ ee!j(e&e)d5d ee*fdd1�ƒ ee!j(e'e*d5d ee)fdd1�ƒ d@\ZZ)Z*ed3eee)e*eƒƒ ee!j(e&e)d5d ee*fdd1�ƒ ee!j(e'e*d5d ee)fdd1�ƒ edAƒ eed e ,dd g¡dBƒe"d dd dBƒdCƒ eed e ,dd g¡dƒe"d dd dƒdCƒ eed e ,dd g¡dƒe"d dd dƒƒ eed e ,ddDg¡dƒe"dEddEdƒƒ eed e ,dd g¡dBƒe"d dd dBƒƒ eed e ,dd g¡dƒe"d ddEe -dF¡ dƒƒ ddGl.m/Z/ d>dHdIdJgZe 0dKdLdM¡Z1eD ]t\Z)Z*e!j(e&e)d5d e1e*fdd1�Z2e!j(e'e*d5d e1e)fdd1�Z3ee1e)e*eƒZ4e/e2e4d dNdOdP� e/e3e4d  dNdQdP� �qdRD ]øZ+eD ]ì\Z)Z*e!j(e#e)d5d e1e*e+fdd1�Z2e!j(e$e*d5d e1e)e+fdd1�Z3ee1e)e*ee+ƒZ4e/e2e4d dSdOdP� e/e3e4d  dSdQdP� e/ee1e ,e)e*d g¡e+ƒe"e1e)e*e+ƒdNdTdP� e/ee1e ,e)e*d g¡e+ƒe"e1e)e*e -e+dL e+ ¡ e+ƒdNdTdP� �q˜�q�dS )Ua  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

é    N)Úspecial)Úgammalnc                 C   s<   |j \}}dt dtj ¡t |¡ | | d |   }|S )ay  normal loglikelihood given observations and mean mu and variance sigma2

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

    Returns
    -------
    lls : ndarray
        contribution to loglikelihood for each observation
    g      à¿é   )ÚTÚnpÚlogÚpi)ÚyÚparamsÚmuÚsigma2Úlls© r   ú]/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/sandbox/regression/tools.pyÚnorm_lls   s    
.r   c                 C   sB   |j \}}| | | }| | d | d t |¡ }t ||f¡S )aB  Jacobian of normal loglikelihood wrt mean mu and variance sigma2

    Parameters
    ----------
    y : ndarray, 1d
        normally distributed random variable
    params : ndarray, (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

    r   é   )r   r   ÚsqrtÚcolumn_stack)r	   r
   r   r   ZdllsdmuZdllsdsigma2r   r   r   Únorm_lls_grad/   s    
r   c                 C   s   | S )z-gradient/Jacobian for d (x*beta)/ d beta
    r   )ÚxÚbetar   r   r   Ú	mean_gradK   s    r   c           
      C   sŠ   |dd… }|d t  t| ƒdf¡ }t||ƒ}t  ||¡}t  ||f¡}t| |ƒ}t  |dd…dd…f | |dd…dd…f f¡}	|	S )a³  Jacobian of normal loglikelihood wrt mean mu and variance sigma2

    Parameters
    ----------
    y : ndarray, 1d
        normally distributed random variable with mean x*beta, and variance sigma2
    x : ndarray, 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

    Néÿÿÿÿr   )r   ÚonesÚlenr   Údotr   r   )
r	   r   r
   r   r   Zdmudbetar   Zparams2ZdllsdmsZgradr   r   r   ÚnormgradP   s    

2r   c                 C   sŠ   |j \}}|d }t|d d ƒt|d ƒ dt |d tj ¡  }||d d t d| | d |d  |  ¡ dt |¡  8 }|S )aæ  t loglikelihood given observations and mean mu and variance sigma2 = 1

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

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

    Notes
    -----
    parametrized for garch
    ç      ð?r   ç       @ç      à?r   )r   r   r   r   r   ©r	   r
   Údfr   r   r   r   r   r   Útstd_llst   s
    
4@r"   c                 C   s   |  S )z?derivative of log pdf of standard normal with respect to y
    r   )r	   r   r   r   Ú
norm_dlldy“   s    r#   c                 C   sp   t  |d ¡}t  t |d d ¡t |d ¡ ¡t  |d t j ¡ }|d| d |d   |d d   }|S )zIpdf for standardized (not standard) t distribution, variance is one

    r   r   r   r   )r   ÚarrayÚexpr   r   r   r   )r   r!   ÚrZPxr   r   r   Útstd_pdf™   s    :$r'   c                 C   sŽ   t | ||ƒ |j\}}|d }t|d d ƒt|d ƒ dt |tj ¡  }||d d t d| | d | |  ¡ dt |¡  8 }|S )a  t loglikelihood given observations and mean mu and variance sigma2 = 1

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

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

    Notes
    -----
    parametrized 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))
    r   r   r   r   r   )Úprintr   r   r   r   r   r    r   r   r   Úts_lls£   s    
0<r)   c                 C   s*   |d }|d  | d| d |   |  S )a  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 : ndarray
        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)

    r   r   r   r   ©r	   r!   r   r   r   Úts_dlldyÊ   s    r+   c                 C   s*   |d  |d  d| d |d    |  S )a  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 : ndarray
        derivative of loglikelihood wrt random variable y evaluated at the
        points given in y


    Notes
    -----
    parametrized for garch, standardized to variance=1
    r   r   r   r   r*   r   r   r   Ú
tstd_dlldyå   s    r,   c                 G   sN   | | | }||f|žŽ  | }d| ||f|žŽ | |  |d   }||fS )aÏ  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 : ndarray
        derivative of loglikelihood wrt location evaluated at the
        points given in y
    dlldscale : ndarray
        derivative of loglikelihood wrt scale evaluated at the
        points given in y

    g      ð¿r   r   )r	   ÚlocÚscaleZdlldyÚargsZystZdlldlocZ	dlldscaler   r   r   Úlocscale_gradÿ   s    &r0   Ú__main__gš™™™™™¹?r   é
   é   r   )ÚstatsÚmiscc                 C   s   t  tjj| |||d�¡S ©N)r-   r.   ©r   r   r4   ÚtÚpdf)r	   r-   r.   r!   r   r   r   Úllt2  s    r:   c                 C   s   t  tjj||| |d�¡S r6   r7   )r-   r	   r.   r!   r   r   r   Úlltloc4  s    r;   c                 C   s   t  tjj|||| d�¡S r6   r7   )r.   r	   r-   r!   r   r   r   Úlltscale6  s    r<   c                 C   s   t  tjj| ||d�¡S r6   ©r   r   r4   Znormr9   )r	   r-   r.   r   r   r   Úllnorm9  s    r>   c                 C   s   t  tjj|| |d�¡S r6   r=   )r-   r	   r.   r   r   r   Ú	llnormloc;  s    r?   c                 C   s   t  tjj||| d�¡S r6   r=   )r.   r	   r-   r   r   r   Úllnormscale=  s    r@   z
gradient of tg�íµ ÷Æ°>)r   r   r2   )ZdxÚnr/   Úorderzt Útsç      ø?g»½×Ùß|Û=)r   r   é   rE   )r   r   rE   )r   r   rE   )rD   r   rE   )rD   r   rE   )rD   r   r   rE   z
gradient of norm)r   r   )rD   r   r   )rD   r   r   z
loglike of téd   zdifferently standardizedg      ô?r   gš™™™™™é?)Úassert_almost_equal)r   r   )g        r   )r   r   g       Àr   é   é   z	deriv loc)Úerr_msgzderiv scale)r3   r2   rF   é   Zloglike)5Ú__doc__Únumpyr   Zscipyr   Zscipy.specialr   r   r   r   r   r"   r#   r'   r)   r+   r,   r0   Ú__name__ÚverboseÚsigr   r   ÚrandomZrandnZrvsr   r   r	   r
   r(   Z	dllfdbetar4   r5   r:   r;   r<   r>   r?   r@   Z
derivativer-   r.   r!   r$   r   Znumpy.testingrG   ZlinspaceZytZdlldloZdlldscÚgrr   r   r   r   Ú<module>   sÀ   $
'

 
   

((&&&0 þ þ