a
    \ą:b!  ă                   @   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argsortZsqrtÚdotÚT)ÚdataÚkeepdimZ	normalizeÚdemeanÚxÚmZxcovÚevalsZevecsÚindicesÚfactorsÚxreduced© r   úBlib/python3.9/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__Znumpyr   r   r   Ú__all__r   r   r   r   Ú<module>   s   	
I
=