
    M/Ph+                     r    d Z ddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZmZ d Z G d d          Z	 	 ddZdS )zLPrincipal Component Analysis

Author: josef-pktd
Modified by Kevin Sheppard
    N)ValueWarningEstimationWarning)string_like
array_like	bool_like
float_likeint_likec                 T    t          j        t          j        | | z                      S )N)npsqrtsum)xs    \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/multivariate/pca.py_normr      s    726!a%==!!!    c                       e Zd ZdZ	 	 	 	 dd	Zd
 Zd Zd Zd Zd Z	d Z
d Zd Zd Zd Zd Zd Zd ZddZd Z	 	 ddZddZdS )PCAa  
    Principal Component Analysis

    Parameters
    ----------
    data : array_like
        Variables in columns, observations in rows.
    ncomp : int, optional
        Number of components to return.  If None, returns the as many as the
        smaller of the number of rows or columns in data.
    standardize : bool, optional
        Flag indicating to use standardized data with mean 0 and unit
        variance.  standardized being True implies demean.  Using standardized
        data is equivalent to computing principal components from the
        correlation matrix of data.
    demean : bool, optional
        Flag indicating whether to demean data before computing principal
        components.  demean is ignored if standardize is True. Demeaning data
        but not standardizing is equivalent to computing principal components
        from the covariance matrix of data.
    normalize : bool , optional
        Indicates whether to normalize the factors to have unit inner product.
        If False, the loadings will have unit inner product.
    gls : bool, optional
        Flag indicating to implement a two-step GLS estimator where
        in the first step principal components are used to estimate residuals,
        and then the inverse residual variance is used as a set of weights to
        estimate the final principal components.  Setting gls to True requires
        ncomp to be less then the min of the number of rows or columns.
    weights : ndarray, optional
        Series weights to use after transforming data according to standardize
        or demean when computing the principal components.
    method : str, optional
        Sets the linear algebra routine used to compute eigenvectors:

        * 'svd' uses a singular value decomposition (default).
        * 'eig' uses an eigenvalue decomposition of a quadratic form
        * 'nipals' uses the NIPALS algorithm and can be faster than SVD when
          ncomp is small and nvars is large. See notes about additional changes
          when using NIPALS.
    missing : {str, None}
        Method for missing data.  Choices are:

        * 'drop-row' - drop rows with missing values.
        * 'drop-col' - drop columns with missing values.
        * 'drop-min' - drop either rows or columns, choosing by data retention.
        * 'fill-em' - use EM algorithm to fill missing value.  ncomp should be
          set to the number of factors required.
        * `None` raises if data contains NaN values.
    tol : float, optional
        Tolerance to use when checking for convergence when using NIPALS.
    max_iter : int, optional
        Maximum iterations when using NIPALS.
    tol_em : float
        Tolerance to use when checking for convergence of the EM algorithm.
    max_em_iter : int
        Maximum iterations for the EM algorithm.
    svd_full_matrices : bool, optional
        If the 'svd' method is selected, this flag is used to set the parameter
        'full_matrices' in the singular value decomposition method. Is set to
        False by default.

    Attributes
    ----------
    factors : array or DataFrame
        nobs by ncomp array of principal components (scores)
    scores :  array or DataFrame
        nobs by ncomp array of principal components - identical to factors
    loadings : array or DataFrame
        ncomp by nvar array of principal component loadings for constructing
        the factors
    coeff : array or DataFrame
        nvar by ncomp array of principal component loadings for constructing
        the projections
    projection : array or DataFrame
        nobs by var array containing the projection of the data onto the ncomp
        estimated factors
    rsquare : array or Series
        ncomp array where the element in the ith position is the R-square
        of including the fist i principal components.  Note: values are
        calculated on the transformed data, not the original data
    ic : array or DataFrame
        ncomp by 3 array containing the Bai and Ng (2003) Information
        criteria.  Each column is a different criteria, and each row
        represents the number of included factors.
    eigenvals : array or Series
        nvar array of eigenvalues
    eigenvecs : array or DataFrame
        nvar by nvar array of eigenvectors
    weights : ndarray
        nvar array of weights used to compute the principal components,
        normalized to unit length
    transformed_data : ndarray
        Standardized, demeaned and weighted data used to compute
        principal components and related quantities
    cols : ndarray
        Array of indices indicating columns used in the PCA
    rows : ndarray
        Array of indices indicating rows used in the PCA

    Notes
    -----
    The default options perform principal component analysis on the
    demeaned, unit variance version of data.  Setting standardize to False
    will instead only demean, and setting both standardized and
    demean to False will not alter the data.

    Once the data have been transformed, the following relationships hold when
    the number of components (ncomp) is the same as tne minimum of the number
    of observation or the number of variables.

    .. math:

        X' X = V \Lambda V'

    .. math:

        F = X V

    .. math:

        X = F V'

    where X is the `data`, F is the array of principal components (`factors`
    or `scores`), and V is the array of eigenvectors (`loadings`) and V' is
    the array of factor coefficients (`coeff`).

    When weights are provided, the principal components are computed from the
    modified data

    .. math:

        \Omega^{-\frac{1}{2}} X

    where :math:`\Omega` is a diagonal matrix composed of the weights. For
    example, when using the GLS version of PCA, the elements of :math:`\Omega`
    will be the inverse of the variances of the residuals from

    .. math:

        X - F V'

    where the number of factors is less than the rank of X

    References
    ----------
    .. [*] J. Bai and S. Ng, "Determining the number of factors in approximate
       factor models," Econometrica, vol. 70, number 1, pp. 191-221, 2002

    Examples
    --------
    Basic PCA using the correlation matrix of the data

    >>> import numpy as np
    >>> from statsmodels.multivariate.pca import PCA
    >>> x = np.random.randn(100)[:, None]
    >>> x = x + np.random.randn(100, 100)
    >>> pc = PCA(x)

    Note that the principal components are computed using a SVD and so the
    correlation matrix is never constructed, unless method='eig'.

    PCA using the covariance matrix of the data

    >>> pc = PCA(x, standardize=False)

    Limiting the number of factors returned to 1 computed using NIPALS

    >>> pc = PCA(x, ncomp=1, method='nipals')
    >>> pc.factors.shape
    (100, 1)
    NTFsvdHj>  d   c                 V	   d | _         g | _        t          |t          j                  r|j        | _         |j        | _        t          |dd          | _        t          |d          | _
        t          |d          | _        t          |d          | _        t          |
d          | _        d| j        cxk     rd	k     sn t          d
          t!          |d          | _        t!          |d          | _        t          |d          | _        t          |d          | _        t          |d          | _        | j        j        \  | _        | _        t          |dd	d          }|t3          j        | j                  }nwt3          j        |                                          }|j        d         | j        k    rt          d          |t3          j        |dz                                            z  }|| _        tA          | j        | j                  }||n|| _!        | j!        |k    r(dd l"}d}|#                    |tH                     || _!        || _%        | j%        dvrt          d| d          | j%        dk    rd| _        t3          j&        | j                  | _'        t3          j&        | j                  | _(        tS          |	dd          | _*        | j        | _+        | ,                                 | j+        j        \  | _        | _        | j!        t3          j         | j        j                  k    r$t3          j         | j+        j                  | _!        n6| j!        t3          j         | j+        j                  k    rt          d          d| _-        d | _.        d | _/        d | _0        d | _1        d | _2        d | _3        d x| _4        | _5        d | _6        d | _7        d | _8        d | _9        d | _:        d | _;        d | _<        | =                                | _/        | >                                 |rA| ?                                 | =                                | _/        | >                                 | @                                 | j         | A                                 d S d S )Ndata   )ndimgls	normalizesvd_fmtolr      z$tol must be strictly between 0 and 1r	   max_em_itertol_emstandardizedemeanweightsT)maxdimoptionalz!weights should have nvar elements       @zThe requested number of components is more than can be computed from data. The maximum number of components is the minimum of the number of observations or variables)eigr   nipalszmethod z is not known.r   missing)r'   zWhen adjusting for missing values, user provided ncomp must be no larger than the smallest dimension of the missing-value-adjusted data size.        )B_index_columns
isinstancepd	DataFrameindexcolumnsr   r   r   _gls
_normalize_svd_full_matricesr   _tol
ValueErrorr	   	_max_iter_max_em_iter_tol_em_standardize_demeanshape_nobs_nvarr   onesarrayflattenr   meanr%   min_ncompwarningswarnr   _methodarangerowscolsr   _missing_adjusted_data_adjust_missing_tss_esstransformed_data_mu_sigma
_ess_indiv
_tss_indivscoresfactorsloadingscoeff	eigenvals	eigenvecs
projectionrsquareic_prepare_data_pca_compute_gls_weights_compute_rsquare_and_ic
_to_pandas)selfr   ncompr#   r$   r   r   r%   methodr+   r   max_iterr"   r!   svd_full_matricesmin_dimrG   rH   s                     r   __init__zPCA.__init__   sN    dBL)) 	)*DK LDMtV!444	c5))	#I{;;"+,=x"H"HsE**	49    q    CDDD!(J77$[-@@!&(33 &k=AA 22!%
DJWiDIII?gdj))GGhw''//11G}Q4:-- !DEEEC(=(=(?(? @ @@G dj$*--!&ggE;  OOOLD MM$---!DK<777=v===>>><5  &*D#Idj))	Idj))	#GYFFF"i "&!4!:
DJ;"&1111&!4!:;;DKK[26$"5";<<<< A B B B 		 $%))dl
 !% 2 2 4 4		 	%%'''$($6$6$8$8D!IIKKK 	$$&&&;"OO #"r   c                 $   d }d }| j         dk    rK || j                  \  | _        }t          j        |          d         | _        | j        |         | _        n_| j         dk    r9 || j                  \  | _        }t          j        |          d         | _        n| j         dk    r || j                  \  }}|j        } || j                  \  }}|j        }	|	|k    r'|| _        t          j        |          d         | _        n|| _        | j        |         | _        t          j        |          d         | _        nv| j         dk    r| 	                                | _        nQ| j         ;t          j
        | j                                                  st          d	          nt          d
          | j        .| j        | j                 | _        | j        | j                 | _        | j        j        dk    rt          d          dS )zE
        Implements alternatives for handling missing values
        c                     t          j        t          j        t          j        |           d                    }| d d |f         |fS )Nr   r   logical_notanyisnanr   r2   s     r   keep_colz%PCA._adjust_missing.<locals>.keep_col3  s<    N26"(1++q#9#9::EQQQX;%%r   c                     t          j        t          j        t          j        |           d                    }| |d d f         |fS )Nr    rn   rr   s     r   keep_rowz%PCA._adjust_missing.<locals>.keep_row7  s<    N26"(1++q#9#9::EUAAAX;%%r   zdrop-colr   zdrop-rowzdrop-minzfill-emNzdata contains non-finite values (inf, NaN). You should drop these values or
use one of the methods for adjusting data for missing-values.zmissing method is not known.z2Removal of missing values has eliminated all data.)rM   r   rN   r   whererL   r%   rK   size_fill_missing_emisfiniteallr8   r-   r.   )
re   rs   ru   r2   drop_coldrop_col_indexdrop_col_sizedrop_rowdrop_row_indexdrop_row_sizes
             r   rO   zPCA._adjust_missing.  s"   
	& 	& 	&	& 	& 	& =J&&)1$))<)<&D*DI<.DLL]j(()1$))<)<&D*DII]j(('/x	':':$Hn$MM'/x	':':$Hn$MM},,&.#H^44Q7		&.##|N;H^44Q7		]i''"&"7"7"9"9D]";t2337799 B  "A B B BB
 ;<<<;" M$)4DM+di0DK #q(( ) * * * )(r   c                 T   t          j        |                     d                    }| j        |z
  }| j        | j        k    rt          d          |dz                      d          }d|z  }|t          j        |dz                                            z  }| j        }dt          ||	                                z  dz            z  |z  }|dk     rLt          t          j        ||z                      }ddl}d	| d
| d}	|                    |	t                     || _        dS )zF
        Computes GLS weights based on percentage of data fit
        F)	transformzOgls can only be used when ncomp < nvar so that residuals have non-zero variancer(   r         ?g?Nz3Many series are being down weighted by GLS. Of the z- series, the GLS
estimates are based on only z (effective) series.)r   asarrayprojectrR   rF   r@   r8   rD   r   r   introundrG   rH   r   r%   )
re   r]   errorsvarr%   nvareff_series_perc
eff_seriesrG   rH   s
             r   rb   zPCA._compute_gls_weightsc  sJ    Zu = =>>
&3;$*$$ H I I I}""1%%)BGW^$9$9$;$;<<<zg&=#%E!F!FF$NS  RXo&<==>>JOOO@48@ @'@ @ @D MM$ 1222r   c                     |                                   |                                  |                                 | _        dS )z"
        Main PCA routine
        N)_compute_eig_compute_pca_from_eigr   r]   re   s    r   ra   zPCA._pca|  s;     	""$$$,,..r   c                     |                                  }|d d         }|dt          t          |                     z   dz   z  }|S )Nz, id: ))__str__hexid)re   strings     r   __repr__zPCA.__repr__  sA    (SD]]*S00r   c                 z   d}|dt          | j                  z   dz   z  }|dt          | j                  z   dz   z  }| j        rd}n| j        rd}nd}|d|z   dz   z  }| j        r|d	z  }|d
t          | j                  z   dz   z  }|dt          | j                  z   dz   z  }|| j        dk    rdndz  }|dz  }|S )NzPrincipal Component Analysis(znobs: z, znvar: zStandardize (Correlation)zDemean (Covariance)Noneztransformation: zGLS, znormalization: znumber of components: r)   zmethod: EigenvalueSVDr   )	strr?   r@   r<   r=   r4   r5   rF   rI   )re   r   kinds      r   r   zPCA.__str__  s    0(S__,t33(S__,t33 	.DD\ 	(DDD$t+d229 	gF#c$/&:&::TAA*S-=-==DDt|u/D/D++%O#r   c                    | j         }t          j        t          j        |                    r<t          j        |j        d                                       t          j                  S t          j        |d          | _	        t          j
        t          j        || j	        z
  dz  d                    | _        | j        r|| j	        z
  | j        z  }n| j        r|| j	        z
  }n|}|t          j
        | j                  z  S )z-
        Standardize or demean data.
        r    r   axisr(   )rN   r   rz   rq   emptyr>   fillnannanmeanrS   r   rT   r<   r=   r%   )re   adj_datar   s      r   r`   zPCA._prepare_data  s     &6"(8$$%% 	<8HN1-..33BF;;;:hQ///gbj(TX*=#)EANNNOO 	tx'4;6DD\ 	tx'DDDbgdl++++r   c                     | j         dk    r|                                 S | j         dk    r|                                 S |                                 S )zz
        Wrapper for actual eigenvalue method

        This is a workaround to avoid instance methods in __dict__
        r)   r   )rI   _compute_using_eig_compute_using_svd_compute_using_nipalsr   s    r   r   zPCA._compute_eig  sT     <5  **,,,\U""**,,,--///r   c                     | j         }t          j                            || j                  \  }}}|dz  | _        |j        | _        dS )z/SVD method to compute eigenvalues and eigenvecs)full_matricesr(   N)rR   r   linalgr   r6   r[   Tr\   )re   r   usvs        r   r   zPCA._compute_using_svd  sB    !)--1H-II1acr   c                     | j         }t          j                            |j                            |                    \  | _        | _        dS )zY
        Eigenvalue decomposition method to compute eigenvalues and eigenvectors
        N)rR   r   r   eighr   dotr[   r\   )re   r   s     r   r   zPCA._compute_using_eig  s8     !)+

)C)C&r   c                    | j         }| j        dk    r|dz   }| j        | j        | j        }}}t	          j        | j                  }t	          j        | j        | j        f          }t          |          D ]W}t	          j        |	                    d                    }|dd|gf         }	d}
d}||k    r|
|k     r|j
                            |	          |	j
                            |	          z  }|t	          j        |j
                            |                    z  }|	}|                    |          |j
                            |          z  }	t          |	|z
            t          |	          z  }|
dz  }
||k    r|
|k     |	dz                                  ||<   ||dd|gf<   |dk    r||	                    |j
                  z  }Y|| _        || _        dS )zg
        NIPALS implementation to compute small number of eigenvalues
        and eigenvectors
        r    r,   r   Nr   r   )rR   rF   r7   r9   r   zerosr@   rangeargmaxr   r   r   r   r   r   r[   r\   )re   r   r   rh   rf   valsvecsimax_var_indfactor_iterdiffvecfactor_lasts                 r   r   zPCA._compute_using_nipals  s   
 !;??CA#y$.$+uXx$$xT[122u 	' 	'A)AEE!HH--Kqqq;-'(FED**!1!1cggfoof)=)=>BGCEIIcNN333$ssuyy~~6Vk122U6]]B
 **!1!1 {''))DGDQCLqyyVZZ&&&r   c                 j   t          j        t          j        | j                            }t          j        |          r| j        S t          j        |                                           x}| _        | j        }t          j	        |d          }t          j	        |d          }t          j
        ||k               st          j
        ||k               rt          d          t          j        |          }t          j        |d          }t          j        | j        df          |z  }||         }	|	||<   d}
d}|
| j        k    r|| j        k     r|	}|| _        |                                  |                                  t          j        |                     dd                    }||         }	|	||<   ||	z
  }t)          |          t)          |	          z  }
|dz  }|
| j        k    r|| j        k     | j        dz   }t          j        |                                           }||         ||<   |S )z5
        EM algorithm to fill missing values
        r    r   z\Implementation requires that all columns and all rows have at least ncomp non-missing valuesr   F)r   unweightr,   )r   ro   rq   r   rz   r   r`   rR   rF   r   rp   r8   r   rA   r?   r;   r:   r   r   r   r   rN   )re   non_missingr   rf   col_non_missingrow_non_missingmaskmur]   projection_maskedr   r   last_projection_maskeddeltas                 r   rx   zPCA._fill_missing_em  sD    nRXdi%8%899 6+ 	9 (*z$2D2D2F2F'G'GGt$ &a00&a006/E)** 	Pbf_u5L.M.M 	P O P P P x~~ Za   Wdj!_--2
&t,&T
 T\!!ed.?&?&?%6"$(D!&&(((DLL5:? %1 %A %A B BJ *4 0*DJ*->>E<<%(9":"::DQJE T\!!ed.?&?&?  "S(Z//
%T
r   c                 v   | j         | j        }}t          j        |          }|ddd         }||         }|dd|f         }|dk                                    r|j        d         |dk                                    z
  }|| j        k     rbddl}|	                    d
                    |          t                     || _        t          j        t          j                  j        ||d<   |d| j                 }|ddd| j        f         }||c| _         | _        | j                            |          x| _        | _        || _        |j        | _        | j        r[| j        j        t          j        |          z  j        | _        | xj        t          j        |          z  c_        | j        | _        dS dS )zR
        Compute relevant statistics after eigenvalues have been computed
        Nr   r   zgOnly {num:d} eigenvalues are positive.  This is the maximum number of components that can be extracted.)num)r[   r\   r   argsortrp   r>   r   rF   rG   rH   formatr   finfofloat64tinyrR   r   rW   rX   rY   r   rZ   r5   r   )re   r   r   indicesnum_goodrG   s         r   r   zPCA._compute_pca_from_eig#  s   
 ^T^d*T""$$B$-G}AAAwJAI?? 	<z!}	'8'88H$+%% 77=v(v7K7K/1 1 1
 '"$(2:"6"6";XYYLT[L!AAA||O$)-t&%)%:%>%>t%D%DDdlV
? 	'*,69DJLLBGDMM)LL,DKKK	' 	'r   c                    | j         }| j        t          j        |          z  }t          j        |dz  d          | _        t          j        | j                  | _        t          j        | j        dz             | _	        t          j        | j        dz   | j
        f          | _        t          | j        dz             D ]o}|                     |dd          }|dz                      d          }|                                }| j        |z
  | j	        |<   | j        |z
  | j        |ddf<   pd| j	        | j        z  z
  | _        | j	        }|dk    }|                                r6t          j        |          d                                         }	|d|	         }t          j        |          }
t          j        |j        d                   }| j        | j
        }}||z   ||z  z  }t#          ||          }t          j        |t          j        d|z            z  |t          j        |          z  t          j        |          |z  g          }|dddf         }|
||z  z   }|j        | _        dS )	z-
        Final statistics to compute
        r   r   r    F)rf   r   r   r   Nr   )r%   rR   r   r   r   rV   rP   r   rF   rQ   r@   rU   r   r   r^   rp   rv   rE   logrJ   r>   r?   rB   r   r_   )re   r%   ss_datar   r]   	indiv_rssrssessinvalidlast_obslog_essrnobsr   sum_to_prodrj   	penaltiesr_   s                     r   rc   zPCA._compute_rsquare_and_icG  s8    ,'"''*:*::&Aq11F4?++	HT[1_--	(DK!OTZ#@AAt{Q'' 	@ 	@AAOOJ#q--1-55I--//C9s?DIaL$(Oi$?DOAqqqD!!TY22i(;;== 	!))!,1133Hixi.C&++Icil##Zdd{td{3dD//HkBF33D,E,EE)BF7OO; fWoo79 : :	 aaag&	q9}$$r   c                    || j         n|}|| j         k    rt          d          t          j        | j                  }t          j        | j                  }|ddd|f                             |d|ddf                   }|s|r|t          j        | j                  z  }|r)| j	        r
|| j
        z  }| j	        s| j        r
|| j        z  }| j        !t          j        || j        | j                  }|S )a  
        Project series onto a specific number of factors.

        Parameters
        ----------
        ncomp : int, optional
            Number of components to use.  If omitted, all components
            initially computed are used.
        transform : bool, optional
            Flag indicating whether to return the projection in the original
            space of the data (True, default) or in the space of the
            standardized/demeaned data.
        unweight : bool, optional
            Flag indicating whether to undo the effects of the estimation
            weights.

        Returns
        -------
        array_like
            The nobs by nvar array of the projection onto ncomp factors.

        Notes
        -----
        Nz=ncomp must be smaller than the number of components computed.r3   r2   )rF   r8   r   r   rX   rZ   r   r   r%   r<   rT   r=   rS   r-   r0   r1   r.   )re   rf   r   r   rX   rZ   r]   s          r   r   zPCA.projectp  s$   4  %}%4; 4 5 5 5*T\**
4:&&QQQY'++E&5&!!!),<==
 	0 	0"'$,///J 	'  *dk)
  'DL 'dh&
;"j.2m,0K9 9 9J r   c                 (   | j         }t          j        t          j        | j                            }dt          t          |                    z   dz   fdt          | j                  D             }t          j	        | j
        ||          }|x| _        | _
        t          j	        | j        | j        |          }|| _        t          j	        | j        || j                  }|| _        t          j	        | j        | j        |          }|| _        t          j        | j                  | _        d| j        _                            dd          fd	t          | j        j        d
                   D             }t          j	        | j        |          | _        t          j        | j                  | _        d| j        j        _        d| j        _        t          j	        | j        g d          | _        d| j        j        _        dS )z:
        Returns pandas DataFrames for all values
        z	comp_{0:0zd}c                 :    g | ]}                     |          S  r   ).0r   comp_strs     r   
<listcomp>z"PCA._to_pandas.<locals>.<listcomp>  s%    ???q""???r   r   )r2   r3   r[   compeigenvecc                 :    g | ]}                     |          S r   r   )r   r   vec_strs     r   r   z"PCA._to_pandas.<locals>.<listcomp>  s%    JJJaq!!JJJr   r    )r3   rf   r^   )IC_p1IC_p2IC_p3N)r-   r   ceillog10rF   r   r   r   r0   r1   rX   rW   r]   r.   rZ   rY   Seriesr[   namereplacer\   r>   r^   r2   r_   )re   r2   	num_zerosrL   dfr   r   s        @@r   rd   zPCA._to_pandas  s    GBHT[1122	S^^!4!44t;????E$+,>,>???\$,EBBB%''dl\$/"&- %' ' ' \$*D"&-1 1 1
\$- $t= = =4>22)""6:66JJJJ51Ea1H+I+IJJJdndCCCy..")%,tw0K0K0KLLL$r   c           	         ddl mc m} |                    |          \  }}|| j        n|}t          j        | j                  }|d| j                 }|rt          j        |          }|r|	                    d           |
                    t          j        |          |d|         d           |                    d           t          j        |                                          }|d         |d         z
  }	|dt          j        |	 |	g          z  z  }|                    |           t          j        |                                          }
d}|rt          j        |
d         |
d         z            }	t          j        t          j        t          j        |
d                   ||	z  z
  t          j        |
d                   ||	z  z   g                    }
n.|
d         |
d         z
  }	|
|t          j        |	 |	g          z  z  }
|                    |
           |                    d	           |                    d
           |                    d           |                                 |S )a  
        Plot of the ordered eigenvalues

        Parameters
        ----------
        ncomp : int, optional
            Number of components ot include in the plot.  If None, will
            included the same as the number of components computed
        log_scale : boot, optional
            Flag indicating whether ot use a log scale for the y-axis
        cumulative : bool, optional
            Flag indicating whether to plot the eigenvalues or cumulative
            eigenvalues
        ax : AxesSubplot, optional
            An axes on which to draw the graph.  If omitted, new a figure
            is created

        Returns
        -------
        matplotlib.figure.Figure
            The handle to the figure.
        r   Nr   boT)tightr    g{Gz?z
Scree Plot
EigenvaluezComponent Number)statsmodels.graphics.utilsgraphicsutilscreate_mpl_axrF   r   r   r[   cumsum
set_yscaleplotrJ   	autoscalerB   get_xlimset_xlimget_ylimr   expset_ylim	set_title
set_ylabel
set_xlabeltight_layout)re   rf   	log_scale
cumulativeaxgutilsfigr   xlimspylimscales               r   
plot_screezPCA.plot_scree  sX   0 	433333333&&r**R$}%z$.))LT[L! 	#9T??D 	!MM%   
	%  $ww-666
4   x&&!WtAwrx"b	****
Dx&& 	0Q$q')**B6"(BF47OOebj$@$&F47OOebj$@$B C C D DDD a47"BEBHrc2Y////D
D
\"""
l###
()))
r   c                 |   ddl mc m} |                    |          \  }}|dn|}t	          || j                  }d| j        | j        z  z
  }|dd         }|d|         }|                    |j	                   |
                    d           |                    d           |                    d           |S )	a  
        Box plots of the individual series R-square against the number of PCs.

        Parameters
        ----------
        ncomp : int, optional
            Number of components ot include in the plot.  If None, will
            plot the minimum of 10 or the number of computed components.
        ax : AxesSubplot, optional
            An axes on which to draw the graph.  If omitted, new a figure
            is created.

        Returns
        -------
        matplotlib.figure.Figure
            The handle to the figure.
        r   N
   r   r    zIndividual Input $R^2$z$R^2$z'Number of Included Principal Components)r   r   r   r   rE   rF   rU   rV   boxplotr   r	  r
  r  )re   rf   r  r  r  r2ss         r   plot_rsquarezPCA.plot_rsquare  s    $ 	433333333&&r**RmE4;''DOdo55!""g&5&k


35
-...
g
?@@@
r   )NTTTFNr   Nr   r   r   r   F)NTT)NTFN)NN)__name__
__module____qualname____doc__rk   rO   rb   ra   r   r   r`   r   r   r   r   rx   r   rc   r   rd   r  r  r   r   r   r   r      sk       k kZ CGAF?C49f f f fP3* 3* 3*j  2) ) )    &, , ,$0 0 0  D D D  @7 7 7r"' "' "'H' ' 'R. . . .`%% %% %%N 04(,: : : :x! ! ! ! ! !r   r   TFr   c           
          t          | |||||||          }|j        |j        |j        |j        |j        |j        |j        fS )aZ
  
    Perform Principal Component Analysis (PCA).

    Parameters
    ----------
    data : ndarray
        Variables in columns, observations in rows.
    ncomp : int, optional
        Number of components to return.  If None, returns the as many as the
        smaller to the number of rows or columns of data.
    standardize : bool, optional
        Flag indicating to use standardized data with mean 0 and unit
        variance.  standardized being True implies demean.
    demean : bool, optional
        Flag indicating whether to demean data before computing principal
        components.  demean is ignored if standardize is True.
    normalize : bool , optional
        Indicates whether th normalize the factors to have unit inner
        product.  If False, the loadings will have unit inner product.
    gls : bool, optional
        Flag indicating to implement a two-step GLS estimator where
        in the first step principal components are used to estimate residuals,
        and then the inverse residual variance is used as a set of weights to
        estimate the final principal components
    weights : ndarray, optional
        Series weights to use after transforming data according to standardize
        or demean when computing the principal components.
    method : str, optional
        Determines the linear algebra routine uses.  'eig', the default,
        uses an eigenvalue decomposition. 'svd' uses a singular value
        decomposition.

    Returns
    -------
    factors : {ndarray, DataFrame}
        Array (nobs, ncomp) of principal components (also known as scores).
    loadings : {ndarray, DataFrame}
        Array (ncomp, nvar) of principal component loadings for constructing
        the factors.
    projection : {ndarray, DataFrame}
        Array (nobs, nvar) containing the projection of the data onto the ncomp
        estimated factors.
    rsquare : {ndarray, Series}
        Array (ncomp,) where the element in the ith position is the R-square
        of including the fist i principal components.  The values are
        calculated on the transformed data, not the original data.
    ic : {ndarray, DataFrame}
        Array (ncomp, 3) containing the Bai and Ng (2003) Information
        criteria.  Each column is a different criteria, and each row
        represents the number of included factors.
    eigenvals : {ndarray, Series}
        Array of eigenvalues (nvar,).
    eigenvecs : {ndarray, DataFrame}
        Array of eigenvectors. (nvar, nvar).

    Notes
    -----
    This is a simple function wrapper around the PCA class. See PCA for
    more information and additional methods.
    )rf   r#   r$   r   r   r%   rg   )r   rX   rY   r]   r^   r_   r[   r\   )	r   rf   r#   r$   r   r   r%   rg   pcs	            r   pcar"  '  sW    | 
TK c76
K 
K 
KB JR]BJL",( (r   )NTTTFNr   )r  numpyr   pandasr0   statsmodels.tools.sm_exceptionsr   r   statsmodels.tools.validationr   r   r   r   r	   r   r   r"  r   r   r   <module>r'     s           @ @ @ @ @ @ @ @, , , , , , , , , , , , , ," " "L L L L L L L L^ DH(-B( B( B( B( B( B(r   