U
    md                     @   s  d Z ddlZddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlm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dZejdfddZedkredddgdddgdddggZedddgdddgdddggZ edddgdddgdddggZ!e!Z"e#ej ej dgZ$e#d d d gZ$e#d!d!d!gZ%e#d"d"d"gZ%d#e$dd< d$e%dd< d%Z&e'e&Z(e)e
e$e%ed&d' e)ej*+e%d e&ej*+e$d e& d$  dZ,e)e
e,e$ e( e,e% e( e" d(Z&e)ee$e%e"e& e#ddgddggZ-e)ed%d)ge-d*d+ d,Z.ed%d)ge-d*e.Z/e)e0e/d%d)gk 1ddd e.  e)eej e2d* e3d*e"dd*dd*f d* dS ).a2  Multivariate Distribution

Probability of a multivariate t distribution

Now also mvstnormcdf has tests against R mvtnorm

Still need non-central t, extra options, and convenience function for
location, scale version.

Author: Josef Perktold
License: BSD (3-clause)

Reference:
Genz and Bretz for formula

    N)	integratestatsspecial)chi   )mvstdnormcdf)exp)log)gamma)gammalnc                 C   sB   ||d d  t | d  }|t|d d|d    }|S )zpdf of chi-square distribution       @r      )npr   r   r
   )selfxdfZPx r   g/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/sandbox/distributions/multivariate.pychi2_pdf   s     r   c                 C   sF   |d t |  |  |  d  |d d t d  t|d  }t|S N      ?      ?r   r   )np_logsps_gammalnnp_expr   r   tmpr   r   r   chi_pdf#   s    0
r   c                 C   sB   |d t |  |  |  d  |d d t d  t|d  }|S r   )r   r   r   r   r   r   
chi_logpdf)   s    0
r   c              	   C   sN   t |d }t| |}|tt| | | | | | |ddd7 }t|}|S )Nr   @B ư>Zmaxptsabseps)r   sqrtr   r   r   r   )sabRr   sqrt_dfretr   r   r   funbgh.   s    
 
r*   c              	   C   s`   t |}t|}t|d t|  | |  d  t| | | | | | |t|d ddd S )Nr   r   r   -C6?r!   )lenr   r#   r   r   r   Ztril_indices)r$   r%   r&   r'   r   nr(   r   r   r   funbgh26   s    
$ r/   c                 C   s    t dd| d  t| d  S )Nr   r   r   )r   power	sps_gamma)r   r   r   r   	bghfactor>   s    r2   h㈵>c                 C   sh   t | |||fdddd}|dk	r*|| t|d| g|\}}	tjt||	f|\}
}|
t| }|S )a8  
    Probability of rectangular area of standard t distribution

    assumes mean is zero and R is correlation matrix

    Notes
    -----
    This function does not calculate the estimate of the combined error
    between the underlying multivariate normal probability calculations
    and the integration.
    r,   g{Gz?   )argsZepsabsZepsrellimitNr   )dictupdater   Zppfr   quadr/   r2   )r%   r&   r'   r   ZiepsZquadkwdsZmvstkwdskwdslowerupperreserrZprobr   r   r   
mvstdtprobB   s    
r?   c                 C   sp   t | } t| }|t jkr(t |}nt j||| }t jt |||f}| |t 	|dddf   S )a  generate random variables of multivariate t distribution

    Parameters
    ----------
    m : array_like
        mean of random variable, length determines dimension of random variable
    S : array_like
        square array of covariance  matrix
    df : int or float
        degrees of freedom
    n : int
        number of observations, return random array will be (n, len(m))

    Returns
    -------
    rvs : ndarray, (n, len(m))
        each row is an independent draw of a multivariate t distributed
        random variable


    N)
r   asarrayr-   infonesrandomZ	chisquareZmultivariate_normalzerosr#   )mSr   r.   dr   zr   r   r   multivariate_t_rvsX   s    

rI   __main__r   r   g      YgQg        gQ?r+      g      $@r    )r"      g      4@r      i'  )r3   NN)4__doc__numpyr   Zscipyr   r   r   Zscipy.statsr   extrasr   r   r   r	   r   Zscipy.specialr
   r1   r   r   r   r   r   r*   r/   r2   r?   rA   rI   __name__r@   ZcorrZ
corr_indepZ
corr_equalr'   arrayr%   r&   r   r#   r(   printtZcdfr$   rF   ZnobsZrvstsumallrB   rD   r   r   r   r   <module>   sT   
"
"""
, &6