U
    md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rd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re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qd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

    Nr   )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 td   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  