
    M/Ph	                     *    d Z ddlZddZ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                 x   t          j        |           }|r|                    d          }nt          j        |j        d                   }||z  }t          j        |d          }t           j                            |          \  }}t          j        |          }	|	ddd         }	|dd|	f         }||	         }|dk    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               c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/tools/tools_pca.pypcar       sE   F 	A !FF1IIHQWQZ  FA 6!AD 9==&&LE5jGdddmG!!!G)E'NE{{w++aaaj!hwh % bgenn$ fQG vguw''!+HWeU**    c                    | j         \  }}t          j        |           }|r|                    d          }nd}||z  }t          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 FF1II FAimmACqm11GAq!fQS!# G 46'!!!HWH*-q8G8}??!C*G33 qD!'!*Q,EWQQQxxZ(%/1QQQxxZ=HHr!   r    r+   )r   r   T)r   T)__doc__numpyr   r    r+   __all__ r!   r   <module>r0      s_        E+ E+ E+ E+R:I :I :I :Iz (
r!   