U
    Ãmœd!  ã                   @   s,   d Z ddlZddd„Zd	dd„ZddgZdS )
z„Principal 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ÚmeanZzerosÚshapeZcovÚlinalgZeigZargsortÚsqrtÚdotÚT)ÚdataÚkeepdimÚ	normalizeÚdemeanÚxÚmZxcovÚevalsZevecsÚindicesÚfactorsÚxreduced© r   ú\/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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}
|r’t |
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__Únumpyr   r   r   Ú__all__r   r   r   r   Ú<module>   s   	
I
=