B
    Z                 @   s   d Z ddlmZ ddlZddlmZ dd Zd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dZdd Zdd ZdS )!zhelper functions conversion between moments

contains:

* conversion between central and non-central moments, skew, kurtosis and
  cummulants
* cov2corr : convert covariance matrix to correlation matrix


Author: Josef Perktold
License: BSD-3

    )rangeN)combc          
   C   s   t | }| d }dgt|  } d| d< d|g}xrt| dd D ]^\}}|d }|d xBt|d D ]2}||  t||dd| |  |||   7  < qhW q@W |dd S )zwconvert central to non-central moments, uses recursive formula
    optionally adjusts first moment to return mean

    r         N)exact)lenlist	enumerateappendr   r   )mcnmeanmncnnmk r   ?lib/python3.7/site-packages/statsmodels/stats/moment_helpers.pymc2mnc   s    
6r   Tc             C   s   t | }| d }dgt|  } g }xnt| D ]b\}}|d xNt|d D ]>}||  d||  t||dd | |  |||   7  < qLW q,W |r||d< |dd S )zwconvert non-central to central moments, uses recursive formula
    optionally adjusts first moment to return mean

    r   r   )r   N)r   r   r	   r
   r   r   )r   Zwmeanr   r   mur   r   r   r   r   mnc2mc)   s    
Br   c          
   C   s   ddg}| d }dgt |  } xvt| dd D ]b\}}|d }|d xFt|d D ]6}||  t|d |dd| ||   ||  7  < qXW q0W ||d< |dd S )a  convert non-central moments to cumulants
    recursive formula produces as many cumulants as moments

    References
    ----------
    Kenneth Lange: Numerical Analysis for Statisticians, page 40
    (http://books.google.ca/books?id=gm7kwttyRT0C&pg=PA40&lpg=PA40&dq=convert+cumulants+to+moments&source=web&ots=qyIaY6oaWH&sig=cShTDWl-YrWAzV7NlcMTRQV6y0A&hl=en&sa=X&oi=book_result&resnum=1&ct=result)


    r   g        r   r   N)r   )r   r	   r
   r   r   )kappar   Zkappa0r   r   r   r   r   r   r   cum2mc<   s    
:r   c          
   C   s   dgt |  } dg}xxt| dd D ]d\}}|d }|| xHtd|D ]:}||  t|d |d dd||  | ||   8  < qLW q&W |dd S )zconvert non-central moments to cumulants
    recursive formula produces as many cumulants as moments

    http://en.wikipedia.org/wiki/Cumulant#Cumulants_and_moments
    r   N)r   )r   r	   r
   r   r   )r   r   r   r   r   r   r   r   r   mnc2cumT   s    
>r   c             C   s   t t| S )z4just chained because I have still the test case
    )r   r   )r   r   r   r   mc2cume   s    r   c             C   sR   | \}}}}dgd }||d< ||d< ||d  |d< |d |d  |d	< t |S )
z9convert mean, variance, skew, kurtosis to central momentsN   r   r   g      ?r   g      @g       @   )tuple)argsr   Zsig2ZskZkurZcntr   r   r   mvsk2mck   s    
r    c             C   s   | \}}}}|}|||  }||d  }|d| |  |d  }|d |d  }	|	d| |  d| | |  |d  }
||||
fS )z=convert mean, variance, skew, kurtosis to non-central momentsg      ?r   g      @g       @r      r   )r   r   mc2skewkurtr   mnc2mc3mnc3mc4mnc4r   r   r   mvsk2mncw   s    (r*   c             C   s<   | \}}}}t ||d }t ||d d }||||fS )z>convert central moments to mean, variance, skew, kurtosis
    g      ?g       @g      @)npZdivide)r   r   r"   r&   r(   r#   r$   r   r   r   mc2mvsk   s    r,   c       	      C   sl   | \}}}}|}|||  }|d| | |d   }|d| | d| | |  |d   }t ||||fS )z>convert central moments to mean, variance, skew, kurtosis
    r   r   r!   )r,   )	r   r   r%   r'   r)   r   r"   r&   r(   r   r   r   mnc2mvsk   s    (r-   Fc             C   s>   t | } t t | }| t || }|r6||fS |S dS )a+  convert covariance matrix to correlation matrix

    Parameters
    ----------
    cov : array_like, 2d
        covariance matrix, see Notes

    Returns
    -------
    corr : ndarray (subclass)
        correlation matrix
    return_std : bool
        If this is true then the standard deviation is also returned.
        By default only the correlation matrix is returned.

    Notes
    -----
    This function does not convert subclasses of ndarrays. This requires
    that division is defined elementwise. np.ma.array and np.matrix are allowed.

    N)r+   
asanyarraysqrtdiagouter)covZ
return_stdstd_corrr   r   r   cov2corr   s    
r5   c             C   s(   t | } t |}| t || }|S )a  convert correlation matrix to covariance matrix given standard deviation

    Parameters
    ----------
    corr : array_like, 2d
        correlation matrix, see Notes
    std : array_like, 1d
        standard deviation

    Returns
    -------
    cov : ndarray (subclass)
        covariance matrix

    Notes
    -----
    This function does not convert subclasses of ndarrays. This requires
    that multiplication is defined elementwise. np.ma.array are allowed, but
    not matrices.

    )r+   r.   r1   )r4   Zstdr3   r2   r   r   r   corr2cov   s    

r6   c             C   s   t t | S )a  get standard deviation from covariance matrix

    just a shorthand function np.sqrt(np.diag(cov))

    Parameters
    ----------
    cov : array_like, square
        covariance matrix

    Returns
    -------
    std : ndarray
        standard deviation from diagonal of cov

    )r+   r/   r0   )r2   r   r   r   se_cov   s    r7   )T)F)__doc__Zstatsmodels.compat.pythonr   Znumpyr+   Z
scipy.miscr   r   r   r   r   r   r    r*   r,   r-   r5   r6   r7   r   r   r   r   <module>   s   
	
