ó
áp7]c           @   sC   d  Z  d d l Z d d e d  Z d e d  Z d d g Z d S(   s   Principal Component Analysis


Created on Tue Sep 29 20:11:23 2009
Author: josef-pktd

TODO : add class for better reuse of results
i˙˙˙˙Ni    c         C   sU  t  j |   } | r' | j d  } n t  j | j d  } | | 8} t  j | d d } t  j j |  \ } } t  j |  }	 |	 d d d  }	 | d d  |	 f } | |	 } | d k rţ | | j d k  rţ | d d  d |  f } | |  } n  | r| t  j	 |  } n  t  j
 | |  }
 t  j
 |
 | j  | } | |
 | | f S(   sá  principal components with eigenvector decomposition
    similar to princomp in matlab

    Parameters
    ----------
    data : ndarray, 2d
        data with observations by rows and variables in columns
    keepdim : integer
        number of eigenvectors to keep
        if keepdim is zero, then all eigenvectors are included
    normalize : boolean
        if true, then eigenvectors are normalized by sqrt of eigenvalues
    demean : boolean
        if true, then the column mean is subtracted from the data

    Returns
    -------
    xreduced : ndarray, 2d, (nobs, nvars)
        projection of the data x on the kept eigenvectors
    factors : ndarray, 2d, (nobs, nfactors)
        factor matrix, given by np.dot(x, evecs)
    evals : ndarray, 2d, (nobs, nfactors)
        eigenvalues
    evecs : ndarray, 2d, (nobs, nfactors)
        eigenvectors, normalized if normalize is true

    Notes
    -----

    See Also
    --------
    pcasvd : principal component analysis using svd

    i    i   t   rowvarNi˙˙˙˙(   t   npt   arrayt   meant   zerost   shapet   covt   linalgt   eigt   argsortt   sqrtt   dott   T(   t   datat   keepdimt	   normalizet   demeant   xt   mt   xcovt   evalst   evecst   indicest   factorst   xreduced(    (    sB   lib/python2.7/site-packages/statsmodels/sandbox/tools/tools_pca.pyt   pca   s&    #

c         C   sB  |  j  \ } } t j |   } | r6 | j d  } n d } | | 8} t j j | j d d \ } } }	 t j | j | j  j }
 | rÓ t j |
 d d  d |  f | d d  d |  f j  | } n |  } | } d | f | d | j  d d } | |
 d d  d |  f | |  | d d  d |  f f S(   s  principal components with svd

    Parameters
    ----------
    data : ndarray, 2d
        data with observations by rows and variables in columns
    keepdim : integer
        number of eigenvectors to keep
        if keepdim is zero, then all eigenvectors are included
    demean : boolean
        if true, then the column mean is subtracted from the data

    Returns
    -------
    xreduced : ndarray, 2d, (nobs, nvars)
        projection of the data x on the kept eigenvectors
    factors : ndarray, 2d, (nobs, nfactors)
        factor matrix, given by np.dot(x, evecs)
    evals : ndarray, 2d, (nobs, nfactors)
        eigenvalues
    evecs : ndarray, 2d, (nobs, nfactors)
        eigenvectors, normalized if normalize is true

    See Also
    --------
    pca : principal component analysis using eigenvector decomposition

    Notes
    -----
    This doesn't have yet the normalize option of pca.

    i    t   full_matricesi   Ns    print reassigning keepdim to maxi   (   R   R   R   R   R   t   svdR   R   (   R   R   R   t   nobst   nvarsR   R   t   Ut   st   vR   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/sandbox/tools/tools_pca.pyt   pcasvdW   s    !
$H
R   R!   (   t   __doc__t   numpyR   t   TrueR   R!   t   __all__(    (    (    sB   lib/python2.7/site-packages/statsmodels/sandbox/tools/tools_pca.pyt   <module>	   s   I=