U
    Kvf!                     @   s,   d Z ddlZdddZd	ddZddgZdS )
zPrincipal Component Analysis


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

TODO : add class for better reuse of results
    NTc                 C   s   t | }|r|d}nt |jd }||8 }t j|dd}t j|\}}t |}	|	ddd }	|dd|	f }||	 }|dkr||jd k r|ddd|f }|d| }|r|t 	| }t 
||}
t 
|
|j| }||
||fS )a  principal components with eigenvector decomposition
    similar to princomp in matlab

    Parameters
    ----------
    data : ndarray, 2d
        data with observations by rows and variables in columns
    keepdim : int
        number of eigenvectors to keep
        if keepdim is zero, then all eigenvectors are included
    normalize : bool
        if true, then eigenvectors are normalized by sqrt of eigenvalues
    demean : bool
        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

    r      )ZrowvarN)nparraymeanzerosshapeZcovlinalgZeigZargsortsqrtdotT)datakeepdim	normalizedemeanxmZxcovevalsZevecsindicesfactorsxreduced r   G/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/sandbox/tools/tools_pca.pypca   s&    #

r   c                 C   s   | j \}}t| }|r$|d}nd}||8 }tjj|jdd\}}}	t|j|jj}
|rt|
ddd|f |ddd|f j| }n| }|}d|f |d |j d d  }||
ddd|f |d| |ddd|f fS )a  principal components with svd

    Parameters
    ----------
    data : ndarray, 2d
        data with observations by rows and variables in columns
    keepdim : int
        number of eigenvectors to keep
        if keepdim is zero, then all eigenvectors are included
    demean : bool
        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 does not have yet the normalize option of pca.

    r   r   )Zfull_matricesNz print reassigning keepdim to max   )r   r   r   r   r	   Zsvdr   r   )r   r   r   ZnobsZnvarsr   r   Usvr   r   r   r   r   r   pcasvdW   s    !

4r   )r   r   T)r   T)__doc__Znumpyr   r   r   __all__r   r   r   r   <module>   s   	
I
=