o
    0Gf!                     @   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   s6   t t| tt| tgrt| S t| tjr| S | S )N)any
isinstancelisttuplenpZarrayndarray)x r
   @lib/python3.10/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   &   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 ]-\}}|d }|d t|d D ]}||  t||dd| |  |||   7  < q.q|dd  S )Nr      r   Texactr   	enumerateappendranger   )mcmeanmncnnmnkr
   r
   r   _local_counts*   s   
0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 ]1\}}|d t|d D ]!}d||  t||dd }||  || |  |||   7  < q q rI||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   r   )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 ]}||  t|d |dd| ||   ||  7  < q*q||d< |dd  S )Nr   g        r   r   Tr   r   )kappar   Zkappa0r   r   r   r    r
   r
   r   r!   ]   s   
4zcum2mc.<locals>._local_countsr   r"   )r,   r*   r!   r$   r
   r
   r   cum2mcS   s   r-   c                 C   r   )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 ]2\}}|d }|| td|D ]}t|d |d dd}||  |||  | ||   8  < q$q|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   (   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sig2skZkurZcntr
   r
   r   r!      s   
zmvsk2mc.<locals>._local_countsr   r   r   r#   r   r   r6   r*   r!   r$   r
   r
   r   mvsk2mc   s   	
r:   c                 C   r0   )z=convert mean, variance, skew, kurtosis to non-central momentsc                 S   s   | \}}}}|}|||  }||d  }|d| |  |d  }|d |d  }	|	d| |  d| | |  |d  }
||||
fS )Nr2   r5   r3   r4   r1      r
   )r6   r   mc2skewkurtr   mnc2mc3mnc3mc4mnc4r
   r
   r   r!      s   (zmvsk2mnc.<locals>._local_countsr   r8   r9   r
   r
   r   mvsk2mnc   s   

rD   c                 C   r0   )z9convert central moments to mean, variance, skew, kurtosisc                 S   s<   | \}}}}t ||d }t ||d d }||||fS )Nr2   r4   r3   )r   Zdivide)r6   r   r<   r@   rB   r=   r>   r
   r
   r   r!      s   zmc2mvsk.<locals>._local_countsr   r8   r9   r
   r
   r   mc2mvsk   s   
rE   c                 C   r0   )z>convert central moments to mean, variance, skew, kurtosis
    c           	      S   sl   | \}}}}|}|||  }|d| | |d   }|d| | d| | |  |d   }t ||||fS )Nr5   r1   r;   )rE   )	r6   r   r?   rA   rC   r   r<   r@   rB   r
   r
   r   r!      s   (zmnc2mvsk.<locals>._local_countsr   r8   r9   r
   r
   r   mnc2mvsk   s   	
rF   Fc                 C   s:   t | } t t | }| t || }|r||fS |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.
    )r   
asanyarraysqrtdiagouter)covZ
return_stdstd_corrr
   r
   r   cov2corr   s   
rN   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   rG   rJ   )rM   ZstdrL   rK   r
   r
   r   corr2cov  s   

rO   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   rH   rI   )rK   r
   r
   r   se_cov  s   rP   )T)F)__doc__Znumpyr   Zscipy.specialr   r   r   r   r%   r+   r-   r.   r/   r:   rD   rE   rF   rN   rO   rP   r
   r
   r
   r   <module>   s"    

