
    >h\                          S SK r " S S5      rg)    Nc                       \ rS rSrSrSrS rSS jrS rS r	S	 r
S
 rS rSS jrSS jrS rS rSS jrSS jrSS jrSrg)Pca   z|
A basic class for Principal Component Analysis (PCA).

p is the number of dimensions, while N is the number of data points
)rgbcymkc                     U R                   nU[        R                  " USS9-
  nU[        R                  " USS9-  nX l        X0l        S U l        g )Nr   axis)AnpmeanstdMN_eig)selfr   r   r   s       jC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/sandbox/pca.py__calc
Pca.__calc   sF    FF
BGGA1

BFF1!
	    Nc           	      L   [         R                  " U5      R                  nUR                  u  pEXEsU l        U l        XT:  a  SSKJn  U" S[        5        X0l	        UR                  5       U l        U R                  5         [         R                  " U R                  [        US-
  [!        U R                  5      -  5      S-   5      SU U l        Ub  [!        U5      U:w  a  [#        S5      eUc  SU l        g[%        U Vs/ s H  n['        U5      PM     sn5      U l        gs  snf )z
p X N matrix input
r   )warnzp > n - intentional?   Nznames must match data dimension)r   arrayTshapenpwarningsr   RuntimeWarningr   copy_origA
_Pca__calctile_colorsintlen
ValueErrortuplestrnames)r   datar0   r   r"   r#   r   xs           r   __init__Pca.__init__   s     HHTNggtv5%'8FFHggdll3!S5F/F+G+IJ2ANUq>??"]T
u6Mu!s1vu6M0N
6Ms   ?D!c                 V    [         R                  " U R                  R                  5      $ )z/
returns the covariance matrix for the dataset
)r   covr   r    r   s    r   getCovarianceMatrixPca.getCovarianceMatrix+   s     vvdffhhr   c                     U R                   cf  [        R                  R                  U R	                  5       5      n[        R
                  " US   5      SSS2   nUS   U   US   SS2U4   4nXl         U R                   $ )zA
returns a tuple of (eigenvalues,eigenvectors) for the data set.
Nr   r   )r   r   linalgeigr8   argsort)r   ressortis      r   getEigensystemPca.getEigensystem1   sq     99))-- 8 8 :;C**SV$TrT*EQs1vag/CIyyr   c                 (    U R                  5       S   $ )Nr   rA   r7   s    r   getEigenvaluesPca.getEigenvalues<       ""$Q''r   c                 (    U R                  5       S   $ )Nr   rD   r7   s    r   getEigenvectorsPca.getEigenvectors?   rG   r   c                 T    U R                  5       nU[        R                  " U5      -  $ )z-
"energies" are just normalized eigenvectors
)rE   r   sum)r   vs     r   getEnergiesPca.getEnergiesB   s$     


{r   c                 ~   SSK Jn  U R                  SS2U4   U R                  SS2U4   peU(       a  UR                  5         UR	                  XV5        U R                  5       u  pxUR                  5       u  pUR                  5       u  pX-
  X-
  p[        XxR                  U R                  5       H3  u  nnnUR                  SSUUU   -  UUU   -  SX-  S-  S-  -  UUS9  M5     U R                  bI  UR                  SU R                  U   -   S-   5        UR                  SU R                  U   -   S-   5        gg)	z
Generates a 2-dimensional plot of the data set and principle components
using matplotlib.

ix specifies which p-dimension to put on the x-axis of the plot
and iy specifies which to put on the y-axis (0-indexed)
r   Ng?   g      ?)
head_widthfcec$z/\sigma$)matplotlib.pyplotpyplotr   clfscatterrA   xlimylimzipr    r*   arrowr0   xlabelylabel)r   ixiyrX   pltr2   r
   valsevsxlxuylyudxdyvalvecr	   s                     r   plot2d
Pca.plot2dI   s    	(FF1R4L"!GGIA$$&hhjhhjuru2T%%5ICAIIa#c"g+c#b'kT257S.=PTUYZI[ 6 ::!JJs4::b>)+56JJs4::b>)+56 "r   c           
      6   SSK Js  Jn  U(       a  UR                  5         [        R
                  " S5      nU R                  5       U R                  5       -  nUR                  XfXgU   Xr   Xs   SS9  UR                  U R                  SS2U4   U R                  SS2U4   U R                  SS2U4   SS9  U R                  (       aB  UR                  U R                  U   S-   U R                  U   S-   U R                  U   S-   S9  gUR                  5         g)	z
Generates a 3-dimensional plot of the data set and principle components
using mayavi.

ix, iy, and iz specify which of the input p-dimensions to place on each of
the x,y,z axes, respectively (0-indexed).
r   N   r   )scale_factorg333333?z/sigma)r^   r_   zlabel)enthought.mayavi.mlabmayavimlabrX   r   zerosrI   rE   quiver3dpoints3dr   r0   axes)r   r`   ra   izrX   r   z3rM   s           r   plot3d
Pca.plot3dc   s     	*)EEG88A;!$"5"5"77	

2bE!%1
=	

466!B$<qtTVVAbD\s
K::FF$**R.1B9PX\XbXbceXfgoXoFpFFHr   c                    [         R                  " U5      (       a0  U[         R                  " U R                  R                  S   5      -  nU[         R
                  " U R                  SS9-  nU R                  R                  S   n[         R                  " [         R                  " U R                  5      U:  SS9nU R                  U   U l        U R                  5         U[        U5      -
  $ )z
clips out all data points that are more than a certain number
of standard deviations from the mean.

sigs can be either a single value or a length-p sequence that
specifies the number of standard deviations along each of the
p dimensions.
r   r   r   )r   isscalaronesr   r!   r   allabsr   r(   rL   )r   sigsr"   r   s       r   sigclipPca.sigclipw   s     ;;tbggdffll1o..DBFF466q))FFLLOFF266$&&>D(a0vvayQxr   c                 b    U R                   R                  5       U l        U R                  5         g N)r'   r&   r   r(   r7   s    r   reset	Pca.reset   s    !!#r   c                    [        X#U4 Vs/ s H  oUSLPM     sn5      nUS:X  a  [        S5      nOnUS:  a  [        S5      eUb  U R                  5       U:  nOFUb  [        SU5      nO6Ub(  [        R
                  " U R                  5       5      U:  nO[        S5      eUc  U R                  R                  nOT[        R                  " USS9nU R                  R                  R                  S   UR                  S   :w  a  [        S5      e[        R                  " U R                  5       5      R                  U-  nX   R                  $ s  snf )	a  
projects the normalized values onto the components

enthresh, nPCs, and cumen determine how many PCs to use

if vals is None, the normalized data vectors are the values to project.
Otherwise, it should be convertable to a p x N array

returns n,p(>threshold) dimension array
Nr   r   z&cannot specify more than one thresholdzShould be unreachableF)r&   zshape for vals does not match)rL   slicer-   energiesr   cumsumRuntimeErrorr   r    r   r!   matrixrI   )	r   rc   enthreshnPCscumenenonnonesr   projs	            r   projectPca.project   s    /FG/F!/FGHq=dA\EFF#MMOh.!$t$"IIdmmo.%7"#:;;<6688D88De,Dvvxx~~a DJJqM1 !@AAyy--/02247wyy- Hs   Ec                    [         R                  " U5      nUR                  u  p4U R                  R                  S   nXE:  a  [	        S5      e[         R
                  R                  [         R                  " U R                  5       5      R                  5      n[         R                  " X545      nXSS2SU24'   XgR                  -  nU(       a*  [         R                  " UR                  5      R                  $ [         R                  " U R                  SS9n	[         R                  " U R                  SS9n
[         R                  " UR                  5      U
-  U	-   R                  $ )z8
input is an n X q array, where q <= p

output is p X n
r   zq > pNr   r   )r   
atleast_2dr!   r   r-   r<   invr   rI   r    rv   r   r   r   r   )r   r   normedr"   qr#   evinvzsr   mnssdss              r   	deprojectPca.deproject   s     --
ggFFLLO5W%%iimmBIId&:&:&<=??@XXqe_1RaR4TTz88DFF#%%%A&Ctvv1%CHHTVV$S(,///r   c                    Uc  U R                   nOAUR                  nUR                  S   U R                   R                  S   :w  a  [        S5      eU R	                  5       n[
        R                  " U5      nUSS2U4   USS2U4'   U R                  US5      nUR                  U-
  nUR                  [
        R                  " U R                  SS9-  nU[
        R                  " U R                   SS9-   $ )z
pc can be a scalar or any sequence of pc indecies

if vals is None, the source data is self.A, else whatever is in vals
(which must be p x m)
Nr   z1vals do not have the correct number of componentsFr   r   )r   r    r!   r-   r   r   
zeros_liker   r   r   r   )r   pcrc   pcszpcsupcr   Bs           r   
subtractPCPca.subtractPC   s     <66D66Dzz!}tvv||A. !TUULLN]]3qt9QrT
NN4&FF3JCCtvv1%%Q'''r   )	r   r   r   r*   r   r'   r"   r0   r#   r   )r   r   T)r   r      T)NNNN)T)__name__
__module____qualname____firstlineno____doc__r*   r(   r3   r8   rA   rE   rI   rN   rm   r|   r   r   r   r   r   __static_attributes__ r   r   r   r      sV    
 *GO* 	((74($
!F04(r   r   )numpyr   r   r   r   r   <module>r      s    [( [(r   