
    >hQ                     X   S SK r S SKrS SKrS SKJr  S SKJs  J	r
  S SKJr   " S S\R                  5      r " S S\5      r " S S	\5      r " S
 S\5      r " S S\R$                  5      r " S S\
R(                  5      r\
R,                  " \\5        S r " S S\5      r\r\r\r\rg)    N)model)ConvergenceWarningc                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )_DimReductionRegression   z:
A base class for dimension reduction regression methods.
c                 (   > [         TU ]  " X40 UD6  g N)super__init__selfendogexogkwargs	__class__s       pC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/regression/dimred.pyr    _DimReductionRegression.__init__   s    //    c                    [         R                  " U R                  5      nU R                  US S 24   nX3R	                  S5      -  n[         R
                  " UR                  U5      UR                  S   -  n[         R                  R                  U5      n[         R                  R                  XSR                  5      R                  nX0l        XPl        [         R                  " X15      U l        g Nr   )npargsortr   r   meandotTshapelinalgcholeskysolvewexog_covxrarray_split_split_wexog)r   n_sliceiixcovxcovxrs         r   _prep_DimReductionRegression._prep   s     ZZ

#IIb!e 	
VVAYvvacc1~
*		""4(IIOOE33'))
 NN16r   )r!   r#   r    )	__name__
__module____qualname____firstlineno____doc__r   r)   __static_attributes____classcell__r   s   @r   r   r      s    07 7r   r   c                   <    \ rS rSrSrS	S jrS rS r  S
S jrSr	g)SlicedInverseReg%   a   
Sliced Inverse Regression (SIR)

Parameters
----------
endog : array_like (1d)
    The dependent variable
exog : array_like (2d)
    The covariates

References
----------
KC Li (1991).  Sliced inverse regression for dimension reduction.
JASA 86, 316-342.
c                 N   [        U5      S:  a  Sn[        R                  " U5        U R                  R                  S   U-  nU R                  U5        U R                   Vs/ s H  oUR                  S5      PM     nnU R                   Vs/ s H  oUR                  S   PM     nn[        R                  " U5      n[        R                  " U5      n[        R                  " UR                  USS2S4   U-  5      UR                  5       -  n[        R                  R                  U5      u  p[        R                  " U	* 5      nX   n	U
SS2U4   n
[        R                  R!                  U R"                  R                  U
5      n[%        XU	S9n['        U5      $ s  snf s  snf )z
Estimate the EDR space using Sliced Inverse Regression.

Parameters
----------
slice_n : int, optional
    Target number of observations per slice
r   z1SIR.fit does not take any extra keyword argumentsNeigs)lenwarningswarnr   r   r)   r#   r   r   asarrayr   r   sumr   eighr   r   r!   DimReductionResultsDimReductionResultsWrapper)r   slice_nr   msgr$   zmnnmncabjjparamsresultss                 r   fitSlicedInverseReg.fit6   sB    v;?ECMM# ))//!$/

7!%!2!23!2AffQi!23!%!2!23!2AWWQZ!23ZZ^JJqM ffRTT1QW:?+aeeg5yy~~c"ZZ^EaeH2%d;)'22 43s   #FF"c                    U R                   nU R                  nU R                  nU R                  nSn[        R
                  " XU R                  45      n[        U R                  5       HF  n[        R                  " U R                  US S 2U4   5      nU[        R                  " X-  5      -  nMH     [        R                  " X15      n	[        R                  R                  U	5      u  p[        R                  " U
[        R                  " U
R                  UR                  5      5      nUR                  U-
  nU[        R                  " X]U-  R                  S5      5      -  nU$ r   )k_vars_covx_slice_means_slice_propsr   reshapendimranger   pen_matr=   r   qrr   )r   Apr'   rD   phvkucovxaq_qdqus                 r   _regularized_objective'SlicedInverseReg._regularized_objective[   s    KKzzJJqdii.) tyy!At||Qq!tW-AA "
 tyy||E"VVArvvacc244()TTBY	RVVBba())r   c                    U R                   nU R                  nU R                  nU R                  nU R                  nU R
                  nUR                  X#45      nS[        R                  " U R                  R                  [        R                  " U R                  U5      5      -  nUR                  X#45      n[        R                  " XA5      n	[        R                  " XI5      n
[        R                  " U	R                  U	5      n[        R                  R                  U5      n[        R                  " X#45      n[        R                  R                  XR                  5      nS /X#-  -  n[        U5       GHG  n[        U5       GH3  nUS-  nSUUU4'   [        R                  " U
R                  U5      nUUR                  -  n[        R                  " U[        R                  " UU5      5      * n[        R                  " [        R                  " XM5      U5      nU[        R                  " U	[        R                  " UU	R                  5      5      -  nU[        R                  " U	[        R                  R                  U[        R                  " UR                  U5      5      5      -  nUUUU-  U-   '   GM6     GMJ     [        R                  R                  U[        R                  " U	R                  UR                  5      5      nU[        R                  " U	U5      R                  -
  n[        U5       H  nUUS S 24   nUUS S 24   n[        U5       Hb  n[        U5       HP  n[        R                  " U[        R                  " UUU-  U-      U5      5      nUUU4==   SUU   -  U-  -  ss'   MR     Md     M     UR!                  5       $ )N   r      )rO   rT   rP   r$   rQ   rR   rS   r   r   rV   r   r   invzerosr   rU   ravel)r   rX   rY   rT   r'   r$   rD   rZ   grr^   covx2aQQijmqcvftr_   rumatfmatchcuir]   r[   fs                             r   _regularized_grad"SlicedInverseReg._regularized_grads   s    KKyyzz,,IIqi  t||Q(?@@IIqi t$FF577E"YY]]1XXqi iiooa)Vqx qA4[a1a4vvfhh+r266$#344vvbffT.4ubffT577&;<<ubiiooad9K&LMM!%1T6A: !  YY__Quww 56"&&#%%%wA1a4A1a4A1XtAq"&&AdFQJ";<Aq!tHBqE	A-H %    xxzr   Nc                    [        U5      S:  a  Sn[        R                  " U5        Uc  [        S5      eUR	                  SS5      nUR	                  SS5      nU R
                  R                  S   U-  n	[        R                  " U R                  5      n
U R
                  U
SS24   nXR                  S5      -  n[        R                  " UR                  5      n[        R                  " X5      nU Vs/ s H  oR                  S5      PM     nnU Vs/ s H  oR                  S   PM     nn[        R                  " U5      n[        R                  " U5      nUUR                  5       -  U l        Xl        UR                  S   U l        X l        Xl        Xl        Xl        UcD  [        R.                  " U R$                  U45      n[        R0                  " U5      USU2SU24'   UnO"UR                  S   U:w  a  S	n[        U5      eUn[3        UU R4                  U R6                  XE5      u  nnnU(       de  U R7                  UR9                  5       5      n[        R:                  " [        R<                  " UU5      5      nS
U-  n[        R                  " U5        [?        U USS9n[A        U5      $ s  snf s  snf )a  
Estimate the EDR space using regularized SIR.

Parameters
----------
ndim : int
    The number of EDR directions to estimate
pen_mat : array_like
    A 2d array such that the squared Frobenius norm of
    `dot(pen_mat, dirs)`` is added to the objective function,
    where `dirs` is an orthogonal array whose columns span
    the estimated EDR space.
slice_n : int, optional
    Target number of observations per slice
maxiter :int
    The maximum number of iterations for estimating the EDR
    space.
gtol : float
    If the norm of the gradient of the objective function
    falls below this value, the algorithm has converged.

Returns
-------
A results class instance.

Notes
-----
If each row of `exog` can be viewed as containing the values of a
function evaluated at equally-spaced locations, then setting the
rows of `pen_mat` to [[1, -2, 1, ...], [0, 1, -2, 1, ..], ...]
will give smooth EDR coefficients.  This is a form of "functional
SIR" using the squared second derivative as a penalty.

References
----------
L. Ferre, A.F. Yao (2003).  Functional sliced inverse regression
analysis.  Statistics: a journal of theoretical and applied
statistics 37(6) 475-488.
r   z3SIR.fit_regularized does not take keyword argumentsNzpen_mat is a required argumentstart_paramsrA      rg   z1Shape of start_params is not compatible with ndimz,SIR.fit_regularized did not converge, |g|=%fr7   )!r9   r:   r;   
ValueErrorgetr   r   r   r   r   r   covr   r"   r<   r=   rR   rT   rO   rV   rP   r$   rQ   ri   eye
_grass_optrc   ry   rj   sqrtr   r?   r@   )r   rT   rV   rA   maxitergtolr   rB   r|   r$   r%   r&   r'   
split_exogrC   rD   rE   rJ   r`   cnvrgggnrK   s                          r   fit_regularized SlicedInverseReg.fit_regularized   sE   T v;?GCMM#?=>>zz.$7 **Y+ ))//!$/ ZZ

#IIb!e	VVAYvvacc{ ^^A/
!+,AffQi,!+,AWWQZ,ZZ^JJqMK	jjm
XXt{{D12F%'VVD\F1T61T6>"F!!!$,I o%!F%fd.I.I&*&<&<gM5 &&v||~6A1&B@2ECMM#%dF>)'22A -,s   6J7J<)rP   rQ   rR   rO   r$   rT   rV   )r}   )rg   Nr}   d   gMbP?)
r+   r,   r-   r.   r/   rL   rc   ry   r   r0    r   r   r4   r4   %   s(     #3J0-^ IL!c3r   r4   c                       \ rS rSrSrS rSrg)PrincipalHessianDirectionsi  a  
Principal Hessian Directions (PHD)

Parameters
----------
endog : array_like (1d)
    The dependent variable
exog : array_like (2d)
    The covariates

Returns
-------
A model instance.  Call `fit` to obtain a results instance,
from which the estimated parameters can be obtained.

References
----------
KC Li (1992).  On Principal Hessian Directions for Data
Visualization and Dimension Reduction: Another application
of Stein's lemma. JASA 87:420.
c                    UR                  SS5      nU R                  U R                  R                  5       -
  nU R                  U R                  R                  S5      -
  nU(       a(  SSKJn  U" X45      R                  5       nUR                  n[        R                  " SX4U5      nU[        U5      -  n[        R                  " UR                  5      n[        R                  R                  X5      n	[        R                  R                  U	5      u  p[        R                   " [        R"                  " U
5      * 5      nX   n
USS2U4   n[%        XU
S9n['        U5      $ )a?  
Estimate the EDR space using PHD.

Parameters
----------
resid : bool, optional
    If True, use least squares regression to remove the
    linear relationship between each covariate and the
    response, before conducting PHD.

Returns
-------
A results instance which can be used to access the estimated
parameters.
residFr   )OLSzi,ij,ik->jkNr7   )r   r   r   r   #statsmodels.regression.linear_modelr   rL   r   r   einsumr9   r   r   r   r   eigr   absr?   r@   )r   r   r   yr&   r   rr   cmcxcbrG   rH   rI   rJ   rK   s                  r   rL   PrincipalHessianDirections.fit  s    " 

7E*JJ**II		q))?A	AAYY}aA.
c!fVVACC[YY__R$yy}}R ZZ
#E1b5%d;)'22r   r   N)r+   r,   r-   r.   r/   rL   r0   r   r   r   r   r     s    ,'3r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )SlicedAverageVarianceEstimationiI  a  
Sliced Average Variance Estimation (SAVE)

Parameters
----------
endog : array_like (1d)
    The dependent variable
exog : array_like (2d)
    The covariates
bc : bool, optional
    If True, use the bias-corrected CSAVE method of Li and Zhu.

References
----------
RD Cook.  SAVE: A method for dimension reduction and graphics
in regression.
http://www.stat.umn.edu/RegGraph/RecentDev/save.pdf

Y Li, L-X Zhu (2007). Asymptotics for sliced average
variance estimation.  The Annals of Statistics.
https://arxiv.org/pdf/0708.0462.pdf
c                 l   > [         [        U ]
  " X40 UD6  SU l        SU;   a  US   SL a  SU l        g g g )NFbcT)r
   SAVEr   r   r   s       r   r   (SlicedAverageVarianceEstimation.__init__a  s>    dD"59&96>fTld2DG 3>r   c                    UR                  SS5      nU R                  R                  S   U-  nU R                  U5        U R                   Vs/ s H#  n[
        R                  " UR                  5      PM%     nnU R                   Vs/ s H  oDR                  S   PM     nnU R                  R                  S   nU R                  (       dZ  Sn[        Xe5       H9  u  p[
        R                  " U5      U
-
  nX[
        R                  " X5      -  -  nM;     U[        U5      -  nGOGSnU H  nU[
        R                  " X5      -  nM     U[        U5      -  nSnU R                   Ho  nXR                  S5      -
  n[        UR                  S   5       H=  nUUSS24   n[
        R                   " UU5      nU[
        R                  " UU5      -  nM?     Mq     XR                  R                  S   -  n[
        R                  " U5      nXS-
  -  US-
  S-  S-   -  nUS-
  US-
  S-  S-   -  nUU-  UU-  -
  n[
        R                  " U5      S[#        U5      -  [        U5      -  -
  U-   n[
        R$                  R'                  U5      u  nn[
        R(                  " U* 5      nUU   nUSS2U4   n[
        R$                  R+                  U R,                  R                  U5      n[/        U UUS9n[1        U5      $ s  snf s  snf )zc
Estimate the EDR space.

Parameters
----------
slice_n : int
    Number of observations per slice
rA   2   r   rg   Nrf   r7   )r   r   r   r)   r#   r   r   r   r    r   zipr   r   r9   r   rU   outerr=   r   r>   r   r   r!   r?   r@   )r   r   rA   r$   rC   cvnsrY   vmwcvxicvavcvnr&   rr   rw   r]   mk1k2av2rG   rH   rI   rJ   rK   s                               r   rL   #SlicedAverageVarianceEstimation.fith  s    **Y+ ))//!$/

7#'#4#45#4abffQSSk#45"&"3"34"3Qggaj"34JJQwwBb+ffQi#o"&&*** & #b'MB
 BbffQl" #b'MB B&&q	Mqwwqz*A!Q$AAA"&&A,&B + ' ))//!$$BA!eQ
Q/Ba%QUQJN+Br'BG#CQR[3r722S8Byy~~b!1ZZ^bEaeH2%dF;)'22[ 64s   *K>L)r   )	r+   r,   r-   r.   r/   r   rL   r0   r1   r2   s   @r   r   r   I  s    .?3 ?3r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )r?   i  a2  
Results class for a dimension reduction regression.

Notes
-----
The `params` attribute is a matrix whose columns span
the effective dimension reduction (EDR) space.  Some
methods produce a corresponding set of eigenvalues
(`eigs`) that indicate how much information is contained
in each basis direction.
c                 0   > [         TU ]  X5        X0l        g r	   )r
   r   r8   )r   r   rJ   r8   r   s       r   r   DimReductionResults.__init__  s    		r   r7   )r+   r,   r-   r.   r/   r   r0   r1   r2   s   @r   r?   r?     s    
 r   r?   c                        \ rS rSrSS0r\rSrg)r@   i  rJ   columnsr   N)r+   r,   r-   r.   _attrs_wrap_attrsr0   r   r   r   r@   r@     s    )F Kr   r@   c                   ^^^^ U R                   u  pVU R                  5       n U" U 5      nSn[        U5       GH  n	U" U 5      n
U
[        R                  " X5      U -  [        R                  " X 5      -  -  n
[        R
                  " [        R                  " X-  5      5      U:  a  Sn  OU
R                  XV45      n[        R                  R                  US5      u  mmmU R                  XV45      n[        R                  " UTR                  5      mUUUU4S jnSnUS:  d  M  U" U* 5      nU" U5      nUU:  a  Un UnGM  US-  nUS:  a  M+  GM     U R                  XV45      n XU4$ )a  
Minimize a function on a Grassmann manifold.

Parameters
----------
params : array_like
    Starting value for the optimization.
fun : function
    The function to be minimized.
grad : function
    The gradient of fun.
maxiter : int
    The maximum number of iterations.
gtol : float
    Convergence occurs when the gradient norm falls below this value.

Returns
-------
params : array_like
    The minimizing value for the objective function.
fval : float
    The smallest achieved value of the objective function.
cnvrg : bool
    True if the algorithm converged to a limit point.

Notes
-----
`params` is 2-d, but `fun` and `grad` should take 1-d arrays
`params.ravel()` as arguments.

Reference
---------
A Edelman, TA Arias, ST Smith (1998).  The geometry of algorithms with
orthogonality constraints. SIAM J Matrix Anal Appl.
http://math.mit.edu/~edelman/publications/geometry_of_algorithms.pdf
FTr   c                    > T[         R                  " TU -  5      -  T[         R                  " TU -  5      -  -   n[         R                  " UT5      R	                  5       $ r	   )r   cossinr   rj   )tpapa0sr]   vts     r   geo_grass_opt.<locals>.geo  sJ     rvva!e}$q266!a%='88B66"b>''))r   g       @g|=rf   )r   rj   rU   r   r   r   r=   rS   r   svdr   )rJ   fungradr   r   rY   df0r   r`   r   gmparamsmr   stepr   f1r   r   r]   r   s                    @@@@r   r   r     sL   L <<DA\\^F	VBE7^ L	RVVA'"&&*@@@77266!%=!D(EYYv99==Q'1b..!(ffWbdd#	* 	* UldUBRBBwAID Ull1 B ^^QF#Fur   c                   B   ^  \ rS rSrSrU 4S jrS rS rSS jrSr	U =r
$ )	CovarianceReductioni  a  
Dimension reduction for covariance matrices (CORE).

Parameters
----------
endog : array_like
    The dependent variable, treated as group labels
exog : array_like
    The independent variables.
dim : int
    The dimension of the subspace onto which the covariance
    matrices are projected.

Returns
-------
A model instance.  Call `fit` on the model instance to obtain
a results instance, which contains the fitted model parameters.

Notes
-----
This is a likelihood-based dimension reduction procedure based
on Wishart models for sample covariance matrices.  The goal
is to find a projection matrix P so that C_i | P'C_iP and
C_j | P'C_jP are equal in distribution for all i, j, where
the C_i are the within-group covariance matrices.

The model and methodology are as described in Cook and Forzani.
The optimization method follows Edelman et. al.

References
----------
DR Cook, L Forzani (2008).  Covariance reducing models: an alternative
to spectral modeling of covariance matrices.  Biometrika 95:4.

A Edelman, TA Arias, ST Smith (1998).  The geometry of algorithms with
orthogonality constraints. SIAM J Matrix Anal Appl.
http://math.mit.edu/~edelman/publications/geometry_of_algorithms.pdf
c                   > [         TU ]  X5        / / pT[        R                  " U R                  U R
                  S9nUR                  UR                  5       HL  u  pxUR                  UR                  5       R                  5        UR                  UR                  S   5        MN     [        U5      U l        Sn	[        U5       H  u  pXU
   XZ   -  -  n	M     XR                  -  n	Xl        X@l        XPl        X0l        g )N)indexr   )r
   r   pd	DataFramer   r   groupbyr   appendr   valuesr   r9   nobs	enumeratecovmcovsr   dim)r   r   r   r   r   r   dfr`   r[   r   rw   r   s              r   r   CovarianceReduction.__init__@  s    %rb\\$))4::6JJrxx(DAKK'IIaggaj! ) J	 dODAGbeO#D $				r   c                 l   U R                   R                  S   nUR                  X R                  45      n[        R
                  " UR                  [        R
                  " U R                   U5      5      n[        R                  R                  U5      u  pVU R                  U-  S-  n[        U R                  5       Hr  u  p[        R
                  " UR                  [        R
                  " XC5      5      n[        R                  R                  U5      u  pVXpR                  U   U-  S-  -  nMt     U$ )z
Evaluate the log-likelihood

Parameters
----------
params : array_like
    The projection matrix used to reduce the covariances, flattened
    to 1d.

Returns the log-likelihood.
r   rf   )r   r   rS   r   r   r   r   r   slogdetr   r   r   r   )	r   rJ   rY   projr   r`   ldetrx   js	            r   loglikeCovarianceReduction.loglikeW  s     IIOOA~~q((m,FF466266$))T23))##A&IIq dii(DAtvvrvva/Aii''*GAd"Q&&A )
 r   c                 *   U R                   R                  S   nUR                  X R                  45      n[        R
                  " UR                  [        R
                  " U R                   U5      5      n[        R
                  " U R                   U5      nU R                  [        R                  R                  XER                  5      R                  -  n[        U R                  5       H  u  px[        R
                  " UR                  [        R
                  " X5      5      n[        R
                  " X5      nX`R                  U   [        R                  R                  XER                  5      R                  -  -  nM     UR                  5       $ )z
Evaluate the score function.

Parameters
----------
params : array_like
    The projection matrix used to reduce the covariances,
    flattened to 1d.

Returns the score function evaluated at 'params'.
r   )r   r   rS   r   r   r   r   r   r   r   r   r   r   rj   )	r   rJ   rY   r   c0cPr   r   r   s	            r   scoreCovarianceReduction.scorer  s     IIOOA~~q((m,VVDFFBFF499d34VVDIIt$II		DD1333dii(DAq0BBbiioob$$79999A )
 wwyr   c                 $  ^  T R                   R                  S   nT R                  nUc9  [        R                  " XE45      n[        R
                  " U5      USU2SU24'   UnOUn[        UU 4S jU 4S jUU5      u  pgnUS-  nU(       dk  T R                  UR                  5       5      n	[        R                  " [        R                  " X-  5      5      n
SU
-  n[        R                  " U[        5        [        T USS9nX|l        [!        U5      $ )a~  
Fit the covariance reduction model.

Parameters
----------
start_params : array_like
    Starting value for the projection matrix. May be
    rectangular, or flattened.
maxiter : int
    The maximum number of gradient steps to take.
gtol : float
    Convergence criterion for the gradient norm.

Returns
-------
A results instance that can be used to access the
fitted parameters.
r   Nc                 (   > TR                  U 5      * $ r	   )r   r&   r   s    r   <lambda>)CovarianceReduction.fit.<locals>.<lambda>  s    4<<?:Jr   c                 (   > TR                  U 5      * $ r	   )r   r   s    r   r   r     s    4::a=.r   z/CovReduce optimization did not converge, |g|=%fr7   )r   r   r   r   ri   r   r   r   rj   r   r=   r:   r;   r   r?   llfr@   )r   r|   r   r   rY   r   rJ   r   r   r   r   rB   rK   s   `            r   rL   CovarianceReduction.fit  s    ( IIOOAHH XXqf%F!vvayF1Q3!8F!F (0J(@'(,.U 	r	

6<<>*A'BCbHCMM#12%dF>)'22r   )r   r   r   r   r   )N   g-C6?)r+   r,   r-   r.   r/   r   r   r   rL   r0   r1   r2   s   @r   r   r     s"    %N.66-3 -3r   r   )r:   numpyr   pandasr   statsmodels.baser   statsmodels.base.wrapperbasewrapperwrapstatsmodels.tools.sm_exceptionsr   Modelr   r4   r   r   Resultsr?   ResultsWrapperr@   populate_wrapperr   r   SIRPHDr   COREr   r   r   <module>r     s       " ' ' >7ekk 74`3. `3F>3!8 >3B^3&= ^3B%-- &!4!4    0)+Nbb31 b3L  &r   