ó
áp7]c           @   s½   d  Z  d d l m Z d d l Z d d l m Z d „  Z e d „ Z	 d „  Z
 e d „ Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z d S(   sô   helper 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

iÿÿÿÿ(   t   rangeN(   t   combc         C   sO   t  t |  t ƒ t |  t ƒ g ƒ r1 t j |  ƒ St |  t j ƒ rG |  S|  Sd  S(   N(   t   anyt
   isinstancet   listt   tuplet   npt   arrayt   ndarray(   t   x(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   _convert_to_multidim   s
    $c         C   s&   t  |  j ƒ d k  r | |  ƒ S|  j S(   Ni   (   t   lent   shapet   T(   R	   t   totype(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   _convert_from_multidim   s    
c         C   s4   t  |  ƒ } d „  } t j | d | ƒ } t | ƒ S(   sw   convert 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 } xƒ t |  d ƒ D]q \ } } | d } | j d ƒ xK t | d ƒ D]9 } | | c t | | d t ƒ|  | | | | 7<qx WqD W| d S(   Ni    i   i   t   exact(   R   t	   enumeratet   appendR    R   t   True(   t   mct   meant   mnct   nnt   mt   nt   k(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   _local_counts,   s    


;i    (   R
   R   t   apply_along_axisR   (   R   R	   R   t   res(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   mc2mnc%   s    	c            s:   t  |  ƒ } ‡  f d †  } t j | d | ƒ } t | ƒ S(   sw   convert non-central to central moments, uses recursive formula
    optionally adjusts first moment to return mean

    c      	      sÈ   |  d } d g t  |  ƒ }  g  } x‡ t |  ƒ D]y \ } } | j d ƒ x] t | d ƒ D]K } d | | t | | d t ƒ} | | c | |  | | | | 7<qZ Wq0 Wˆ  rÀ | | d <n  | d S(   Ni    i   iÿÿÿÿR   (   R   R   R   R    R   R   (   R   R   t   muR   R   R   t   sgn_comb(   t   wmean(    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyR   D   s    
!,i    (   R
   R   R   R   (   R   R!   t   XR   R   (    (   R!   s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   mnc2mc=   s    c         C   s4   t  |  ƒ } d „  } t j | d | ƒ } t | ƒ S(   sÉ   convert 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  |  ƒ }  x‡ t |  d ƒ D]u \ } } | d } | j d ƒ xO t | d ƒ D]= } | | c t | d | d t ƒ|  | | | | 7<qn Wq: W| | d <| d S(   Ni   g        i    i   R   (   R   R   R   R    R   R   (   t   kappaR   t   kappa0R   R   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyR   `   s    

?
i    (   R
   R   R   R   (   R$   R"   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   cum2mcV   s    	c         C   s4   t  |  ƒ } d „  } t j | d | ƒ } t | ƒ S(   sª   convert non-central moments to cumulants
    recursive formula produces as many cumulants as moments

    http://en.wikipedia.org/wiki/Cumulant#Cumulants_and_moments
    c      	   S   s·   d g t  |  ƒ }  d g } x t |  d ƒ D]~ \ } } | d } | j | ƒ xX t d | ƒ D]G } t | d | d d t ƒ} | | c | | | |  | | 8<q` Wq- W| d S(   Ni   R   (   R   R   R   R    R   R   (   R   R$   R   R   R   R   t   num_ways(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyR   y   s    	
,i    (   R
   R   R   R   (   R   R"   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   mnc2cumq   s    	c         C   s4   t  |  ƒ } t | t j ƒ r* | j } n  t | ƒ S(   s9   
    just chained because I have still the test case
    (   R   R   R   R   R   R(   (   R   t
   first_step(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   mc2cum‰   s    c         C   s7   t  |  ƒ } d „  } t j | d | ƒ } t | t ƒ S(   s9   convert mean, variance, skew, kurtosis to central momentsc         S   se   |  \ } } } } d  g d } | | d <| | d <| | d | d <| d | d | d <t | ƒ S(	   Ni   i    i   g      ø?i   g      @g       @i   (   t   NoneR   (   t   argsR   t   sig2t   skt   kurt   cnt(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyR   ˜   s    

i    (   R
   R   R   R   R   (   R,   R"   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   mvsk2mc”   s    		c         C   s7   t  |  ƒ } d „  } t j | d | ƒ } t | t ƒ S(   s=   convert mean, variance, skew, kurtosis to non-central momentsc         S   sš   |  \ } } } } | } | | | } | | d } | d | | | d } | d | d }	 |	 d | | d | | | | d }
 | | | |
 f S(   Ng      ø?i   g      @g       @i   i   (    (   R,   R   t   mc2t   skewt   kurtR   t   mnc2t   mc3t   mnc3t   mc4t   mnc4(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyR   ª   s    *i    (   R
   R   R   R   R   (   R,   R"   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   mvsk2mnc¦   s    	
c         C   s7   t  |  ƒ } d „  } t j | d | ƒ } t | t ƒ S(   s9   convert central moments to mean, variance, skew, kurtosisc         S   sR   |  \ } } } } t  j | | d ƒ } t  j | | d ƒ d } | | | | f S(   Ng      ø?g       @g      @(   R   t   divide(   R,   R   R2   R6   R8   R3   R4   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyR   ½   s    i    (   R
   R   R   R   R   (   R,   R"   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   mc2mvsk¹   s    	c         C   s7   t  |  ƒ } d „  } t j | d | ƒ } t | t ƒ S(   s>   convert central moments to mean, variance, skew, kurtosis
    c   	      S   s€   |  \ } } } } | } | | | } | d | | | d } | d | | d | | | | d } t  | | | | f ƒ S(   Ni   i   i   (   R<   (	   R,   R   R5   R7   R9   R   R2   R6   R8   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyR   Í   s    *i    (   R
   R   R   R   R   (   R,   R"   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   mnc2mvskÈ   s    		c         C   sU   t  j |  ƒ }  t  j t  j |  ƒ ƒ } |  t  j | | ƒ } | rM | | f S| Sd S(   s/  
    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   t
   asanyarrayt   sqrtt   diagt   outer(   t   covt
   return_stdt   std_t   corr(    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   cov2corrç   s    
c         C   s8   t  j |  ƒ }  t  j | ƒ } |  t  j | | ƒ } | S(   só  
    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>   RA   (   RE   t   stdRD   RB   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   corr2cov  s    c         C   s   t  j t  j |  ƒ ƒ S(   s  
    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?   R@   (   RB   (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   se_cov#  s    (   t   __doc__t   statsmodels.compat.pythonR    t   numpyR   t   scipy.specialR   R
   R   R   R   R   R#   R&   R(   R*   R1   R:   R<   R=   t   FalseRF   RH   RI   (    (    (    s?   lib/python2.7/site-packages/statsmodels/stats/moment_helpers.pyt   <module>   s"   										