a
    \:b!                     @   s   d Z ddlZddlmZ dd Zefdd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

    N)combc                 C   s:   t t| tt| tgr"t| S t| tjr2| S | S d S )N)any
isinstancelisttuplenpZarrayndarray)x r
   ?lib/python3.9/site-packages/statsmodels/stats/moment_helpers.py_convert_to_multidim   s
    
r   c                 C   s   t | jdk r|| S | jS )N   )lenshapeT)r	   Ztotyper
   r
   r   _convert_from_multidim   s    r   c                 C   s&   t | }dd }t|d|}t|S )zvconvert central to non-central moments, uses recursive formula
    optionally adjusts first moment to return mean
    c              
   S   s   | d }dgt |  } d| d< d|g}t| dd  D ]Z\}}|d }|d t|d D ]2}||  t||dd| |  |||   7  < q\q6|dd  S )Nr      r   Texactr   	enumerateappendranger   )mcmeanmncnnmnkr
   r
   r   _local_counts*   s    
2zmc2mnc.<locals>._local_countsr   r   r   apply_along_axisr   )r   r	   r    resr
   r
   r   mc2mnc$   s    r$   Tc                    s*   t | } fdd}t|d|}t|S )zvconvert non-central to central moments, uses recursive formula
    optionally adjusts first moment to return mean
    c              	      s   | d }dgt |  } g }t| D ]b\}}|d t|d D ]B}d||  t||dd }||  || |  |||   7  < q@q" r||d< |dd  S )Nr   r   Tr   r   )r   r   mur   r   r   Zsgn_combwmeanr
   r   r    A   s    
(zmnc2mc.<locals>._local_countsr   r!   )r   r(   Xr    r#   r
   r'   r   mnc2mc;   s    r*   c                 C   s&   t | }dd }t|d|}t|S )zconvert non-central moments to cumulants
    recursive formula produces as many cumulants as moments

    References
    ----------
    Kenneth Lange: Numerical Analysis for Statisticians, page 40
    c              
   S   s   ddg}| d }dgt |  } t| dd  D ]^\}}|d }|d t|d D ]6}||  t|d |dd| ||   ||  7  < qTq.||d< |dd  S )Nr   g        r   r   Tr   r   )kappar   Zkappa0r   r   r   r   r
   r
   r   r    ]   s    
6zcum2mc.<locals>._local_countsr   r!   )r+   r)   r    r#   r
   r
   r   cum2mcS   s    r,   c                 C   s&   t | }dd }t|d|}t|S )zconvert non-central moments to cumulants
    recursive formula produces as many cumulants as moments

    https://en.wikipedia.org/wiki/Cumulant#Cumulants_and_moments
    c              	   S   s   dgt |  } dg}t| dd  D ]d\}}|d }|| td|D ]>}t|d |d dd}||  |||  | ||   8  < qHq$|dd  S )Nr   Tr   r   )r   r+   r   r   r   r   Znum_waysr
   r
   r   r    v   s    
(zmnc2cum.<locals>._local_countsr   r!   )r   r)   r    r#   r
   r
   r   mnc2cumn   s    r-   c                 C   s"   t | }t|tjr|j}t|S )z9
    just chained because I have still the test case
    )r$   r   r   r   r   r-   )r   Z
first_stepr
   r
   r   mc2cum   s    r.   c                 C   s(   t | }dd }t|d|}t|tS )z9convert mean, variance, skew, kurtosis to central momentsc                 S   sR   | \}}}}d gd }||d< ||d< ||d  |d< |d |d  |d< t |S )	N   r   r         ?r         @       @   )r   )argsr&   Zsig2ZskZkurZcntr
   r
   r   r       s    
zmvsk2mc.<locals>._local_countsr   r   r   r"   r   r   r4   r)   r    r#   r
   r
   r   mvsk2mc   s    	r7   c                 C   s(   t | }dd }t|d|}t|tS )z=convert mean, variance, skew, kurtosis to non-central momentsc                 S   s   | \}}}}|}|||  }||d  }|d| |  |d  }|d |d  }	|	d| |  d| | |  |d  }
||||
fS )Nr0   r3   r1   r2   r/      r
   )r4   r   mc2skewkurtr   mnc2mc3mnc3mc4mnc4r
   r
   r   r       s    (zmvsk2mnc.<locals>._local_countsr   r5   r6   r
   r
   r   mvsk2mnc   s    
rA   c                 C   s(   t | }dd }t|d|}t|tS )z9convert central moments to mean, variance, skew, kurtosisc                 S   s<   | \}}}}t ||d }t ||d d }||||fS )Nr0   r2   r1   )r   Zdivide)r4   r   r9   r=   r?   r:   r;   r
   r
   r   r       s    zmc2mvsk.<locals>._local_countsr   r5   r6   r
   r
   r   mc2mvsk   s    rB   c                 C   s(   t | }dd }t|d|}t|tS )z>convert central moments to mean, variance, skew, kurtosis
    c           	      S   sl   | \}}}}|}|||  }|d| | |d   }|d| | d| | |  |d   }t ||||fS )Nr3   r/   r8   )rB   )	r4   r   r<   r>   r@   r   r9   r=   r?   r
   r
   r   r       s    (zmnc2mvsk.<locals>._local_countsr   r5   r6   r
   r
   r   mnc2mvsk   s    	rC   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    
rK   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   rD   rG   )rJ   ZstdrI   rH   r
   r
   r   corr2cov  s    

rL   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   rE   rF   )rH   r
   r
   r   se_cov  s    rM   )T)F)__doc__Znumpyr   Zscipy.specialr   r   r   r   r$   r*   r,   r-   r.   r7   rA   rB   rC   rK   rL   rM   r
   r
   r
   r   <module>   s    

