
    'YHh	                     (    d Z ddlZddZddZddgZy)	zPrincipal Component Analysis


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

TODO : add class for better reuse of results
    Nc                 j   t        j                  |       }|r|j                  d      }n"t        j                  |j                  d         }||z  }t        j
                  |d      }t         j                  j                  |      \  }}t        j                  |      }	|	ddd   }	|dd|	f   }||	   }|dkD  r"||j                  d   k  r|ddd|f   }|d| }|r|t        j                  |      z  }t        j                  ||      }
t        j                  |
|j                        |z   }||
||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      )rowvarN)nparraymeanzerosshapecovlinalgeigargsortsqrtdotT)datakeepdim	normalizedemeanxmxcovevalsevecsindicesfactorsxreduceds               \/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/sandbox/tools/tools_pca.pypcar       s#   F 	AFF1IHHQWWQZ FA 66!AD 99==&LE5jjGddmG!G)E'NE{w+aj!hw bggen$ ffQG vvguww'!+HWeU**    c                 ,   | j                   \  }}t        j                  |       }|r|j                  d      }nd}||z  }t        j                  j                  |j                  d      \  }}}	t        j                  |j                  |j                        j                  }
|r6t        j                  |
ddd|f   |ddd|f   j                        |z   }n| }|}d|f |dz  |j                   d   dz
  z  }||
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   )full_matricesNz print reassigning keepdim to max   )r   r   r   r	   r   svdr   r   )r   r   r   nobsnvarsr   r   Usvr   r   r   s                r   pcasvdr+   V   s   B **KD%
AFF1I FAiimmACCqm1GAq!ffQSS!##  G66'!HWH*-q8G8}?!C*G3 qD!''!*Q,EWQxxZ(%/1QxxZ=HHr!   r    r+   )r   r   T)r   T)__doc__numpyr   r    r+   __all__ r!   r   <module>r0      s)    E+R:Iz (
r!   