
    'YHh(O                     X   d dl Z d dlZd dlZd dlmZ d dlmc m	Z
 d dlmZ  G d dej                        Z G d de      Z G d d	e      Z G d
 de      Z G d dej$                        Z G d de
j(                        Z e
j,                  ee       d Z G d de      ZeZeZeZeZy)    N)model)ConvergenceWarningc                   (     e Zd ZdZ fdZd Z xZS )_DimReductionRegressionzB
    A base class for dimension reduction regression methods.
    c                 (    t        |   ||fi | y N)super__init__selfendogexogkwargs	__class__s       V/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/regression/dimred.pyr
   z _DimReductionRegression.__init__   s    //    c                    t        j                  | j                        }| j                  |d d f   }||j	                  d      z  }t        j
                  |j                  |      |j                  d   z  }t         j                  j                  |      }t         j                  j                  ||j                        j                  }|| _        || _        t        j                  ||      | _        y 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   _prepz_DimReductionRegression._prep   s     ZZ

#IIb!e 	
QVVAYvvacc1~
*		""4(IIOOE133'))
 NN1g6r   )__name__
__module____qualname____doc__r
   r'   __classcell__r   s   @r   r   r      s    07r   r   c                   0    e Zd ZdZddZd Zd Z	 	 ddZy)	SlicedInverseRega0  
    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                 F   t        |      dkD  rd}t        j                  |       | j                  j                  d   |z  }| j                  |       | j                  D cg c]  }|j                  d       }}| j                  D cg c]  }|j                  d    }}t        j                  |      }t        j                  |      }t        j                  |j                  |dddf   |z        |j                         z  }t        j                  j                  |      \  }	}
t        j                  |	       }|	|   }	|
dd|f   }
t        j                  j!                  | j"                  j                  |
      }t%        | ||	      }t'        |      S c c}w c c}w )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   fitzSlicedInverseReg.fit6   sF    v;?ECMM# ))//!$/

7!%!2!23AaffQi33!%!2!23AQWWQZ33ZZ^JJqM ffRTT1QW:?+aeeg5yy~~c"1ZZ^bEaeH2%dF;)'22 43s   !F	Fc                    | j                   }| j                  }| j                  }| j                  }d}t	        j
                  ||| j                  f      }t        | j                        D ]D  }t	        j                  | j                  |d d |f         }|t	        j                  ||z        z  }F t	        j                  ||      }	t        j                  j                  |	      \  }
}t	        j                  |
t	        j                  |
j                  |j                              }|j                  |z
  }|t	        j                  |||z  j                  d            z  }|S r   )k_vars_covx_slice_means_slice_propsr   reshapendimranger   pen_matr7   r   qrr   )r   Apr%   r>   phvkucovxaq_qdqus                 r   _regularized_objectivez'SlicedInverseReg._regularized_objective[   s    KKzzJJq1dii.) tyy! 	At||Qq!tW-AAA	
 tQyy||E"1VVArvvacc244()TTBY	RVVBba())r   c                    | j                   }| j                  }| j                  }| j                  }| j                  }| j
                  }|j                  ||f      }dt        j                  | j                  j                  t        j                  | j                  |            z  }|j                  ||f      }t        j                  ||      }	t        j                  ||	      }
t        j                  |	j                  |	      }t        j                  j                  |      }t        j                  ||f      }t        j                  j                  ||	j                        }d g||z  z  }t        |      D ]<  }t        |      D ]*  }|dz  }d|||f<   t        j                  |
j                  |      }||j                  z  }t        j                  |t        j                  ||             }t        j                  t        j                  ||      |      }|t        j                  |	t        j                  ||	j                              z  }|t        j                  |	t        j                  j                  |t        j                  |j                  |                  z  }||||z  |z   <   - ? t        j                  j                  |t        j                  |	j                  |j                              }|t        j                  |	|      j                  z
  }t        |      D ]  }||d d f   }||d d f   }t        |      D ]]  }t        |      D ]M  }t        j                  |t        j                  |||z  |z      |            }|||fxx   d||   z  |z  z  cc<   O _  |j!                         S )N   r      )rH   rM   rI   r"   rJ   rK   rL   r   r   rO   r   r   invzerosr   rN   ravel)r   rQ   rR   rM   r%   r"   r>   rS   grrW   covx2aQQijmqcvftrX   rumatfmatchcuirV   rT   fs                             r   _regularized_gradz"SlicedInverseReg._regularized_grads   s    KKyyzz,,IIq$i  t||Q(?@@IIq$i tQe$FF577E"YY]]1XXq$i iiooa)Vq4x q 
	&A4[ 	&a1a4vvfhh+r266$#344vvbffT2.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                 h   t        |      dkD  rd}t        j                  |       |t        d      |j	                  dd      }|j	                  dd      }| j
                  j                  d   |z  }	t        j                  | j                        }
| j
                  |
ddf   }||j                  d      z  }t        j                  |j                        }t        j                  ||	      }|D cg c]  }|j                  d       }}|D cg c]  }|j                  d    }}t        j                  |      }t        j                  |      }||j                         z  | _        || _        |j                  d   | _        || _        || _        |	| _        || _        |Bt        j.                  | j$                  |f      }t        j0                  |      |d|d|f<   |}n!|j                  d   |k7  rd	}t        |      |}t3        || j4                  | j6                  ||      \  }}}|sb| j7                  |j9                               }t        j:                  t        j<                  ||            }d
|z  }t        j                  |       t?        | |d      }tA        |      S c c}w c c}w )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_paramsr;      r_   z1Shape of start_params is not compatible with ndimz,SIR.fit_regularized did not converge, |g|=%fr1   )!r3   r4   r5   
ValueErrorgetr   r   r   r   r   r   covr   r    r6   r7   rK   rM   rH   rO   rI   r"   rJ   ra   eye
_grass_optr\   rq   rb   sqrtr   r9   r:   )r   rM   rO   r;   maxitergtolr   r<   rs   r"   r#   r$   r%   
split_exogr=   r>   r?   rD   rY   cnvrgggnrE   s                          r   fit_regularizedz SlicedInverseReg.fit_regularized   sW   T v;?GCMM#?=>>zz.$7 **Y+ ))//!$/ ZZ

#IIb!e	QVVAYvvacc{ ^^Aw/
!+,AaffQi,,!+,AQWWQZ,,ZZ^JJqMK	jjm
XXt{{D12F%'VVD\F1T61T6>"F!!!$,I o%!F%fd.I.I&*&<&<gtM5 &&v||~6A1&B@2ECMM#%dF>)'22A -,s   2J*J/)rt   )r_   Nrt   d   gMbP?)r(   r)   r*   r+   rF   r\   rq   r    r   r   r/   r/   %   s(     #3J0-^ IL!c3r   r/   c                       e Zd ZdZd Zy)PrincipalHessianDirectionsa  
    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                    |j                  dd      }| j                  | j                  j                         z
  }| j                  | j                  j                  d      z
  }|r)ddlm}  |||      j                         }|j                  }t        j                  d|||      }|t        |      z  }t        j                  |j                        }t        j                  j                  ||      }	t        j                  j                  |	      \  }
}t        j                   t        j"                  |
             }|
|   }
|dd|f   }t%        | ||
      }t'        |      S )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->jkNr1   )rv   r   r   r   #statsmodels.regression.linear_modelr   rF   r   r   einsumr3   rw   r   r   r   eigr   absr9   r:   )r   r   r   yr$   r   rj   cmcxcbrA   rB   rC   rD   rE   s                  r   rF   zPrincipalHessianDirections.fit  s   " 

7E*JJ**II		q))?Aq	AAYY}aA.
c!fVVACC[YY__R$yy}}R 1ZZ
#bE1b5%dF;)'22r   N)r(   r)   r*   r+   rF   r   r   r   r   r     s    ,'3r   r   c                   (     e Zd ZdZ fdZd Z xZS )SlicedAverageVarianceEstimationa]  
    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                 f    t        t        | 
  ||fi | d| _        d|v r|d   du rd| _        y y y )NFbcT)r	   SAVEr
   r   r   s       r   r
   z(SlicedAverageVarianceEstimation.__init__a  s@    dD"5$9&96>fTld2DG 3>r   c                    |j                  dd      }| j                  j                  d   |z  }| j                  |       | j                  D cg c]!  }t        j                  |j                        # }}| j                  D cg c]  }|j                  d    }}| j                  j                  d   }| j                  sZd}t        ||      D ]9  \  }	}
t        j                  |      |
z
  }||	t        j                  ||      z  z  }; |t        |      z  }n@d}|D ]  }|t        j                  ||      z  } |t        |      z  }d}| j                  D ]k  }||j                  d      z
  }t        |j                  d         D ]:  }||ddf   }t        j                   ||      }|t        j                  ||      z  }< m || j                  j                  d   z  }t        j                  |      }||dz
  z  |dz
  dz  dz   z  }|dz
  |dz
  dz  dz   z  }||z  ||z  z
  }t        j                  |      dt#        |      z  t        |      z  z
  |z   }t
        j$                  j'                  |      \  }}t        j(                  |       }||   }|dd|f   }t
        j$                  j+                  | j,                  j                  |      }t/        | ||      }t1        |      S c c}w c c}w )z
        Estimate the EDR space.

        Parameters
        ----------
        slice_n : int
            Number of observations per slice
        r;   2   r   r_   Nr^   r1   )rv   r   r   r'   r!   r   rw   r   r   r   ziprx   r   r3   r   rN   outerr7   r   r8   r   r   r   r9   r:   )r   r   r;   r"   r=   cvnsrR   vmwcvxicvavcvnr$   rj   ro   rV   mk1k2av2rA   rB   rC   rD   rE   s                               r   rF   z#SlicedAverageVarianceEstimation.fith  s    **Y+ ))//!$/

7#'#4#45abffQSSk55"&"3"34Qaggaj44JJQwwBb"+ +3ffQi#oa"&&c***+ #b'MB
 B #bffQl"##b'MB B&& 'q	Mqwwqz* 'A!Q$AAA"&&A,&B'' $))//!$$BA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.K3)r(   r)   r*   r+   r
   rF   r,   r-   s   @r   r   r   I  s    .?3r   r   c                   "     e Zd ZdZ fdZ xZS )r9   aV  
    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                 4    t         |   ||       || _        y r   )r	   r
   r2   )r   r   rD   r2   r   s       r   r
   zDimReductionResults.__init__  s    V		r   )r(   r)   r*   r+   r
   r,   r-   s   @r   r9   r9     s    
 r   r9   c                       e Zd ZddiZeZy)r:   rD   columnsN)r(   r)   r*   _attrs_wrap_attrsr   r   r   r:   r:     s    )F Kr   r:   c                    | j                   \  }}| j                         }  ||       }d}t        |      D ]  }	 ||       }
|
t        j                  |
|       | z  t        j                  | |       z  z  }
t        j
                  t        j                  |
|
z              |k  rd} n|
j                  ||f      }t        j                  j                  |d      \  | j                  ||f      }t        j                  |j                        fd}d}|dkD  s ||       } ||      }||k  r|} |}|dz  }|dkD  r' | j                  ||f      } | ||fS )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        j                  | z        z  t        j                  | z        z  z   }t        j                  |      j	                         S r   )r   cossinr   rb   )tpapa0srV   vts     r   geoz_grass_opt.<locals>.geo  sJ     rvva!e}$q266!a%='88B66"b>''))r   g       @g|=r^   )r   rb   rN   r   r   rz   r7   rL   r   svdr   )rD   fungradr{   r|   rR   df0r~   rY   r   gmparamsmr   stepr   f1r   r   rV   r   s                    @@@@r   ry   ry     sZ   L <<DAq\\^F	VBE7^  L	RVVAv'"&&*@@@77266!a%=!D(EYY1v99==Q'1b..!Q(ffWbdd#	* UldUBRBBwAID Ul1B ^^QF#F2ur   c                   6     e Zd ZdZ fdZd Zd ZddZ xZS )CovarianceReductiona/  
    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         |   ||       g g }}t        j                  | j                  | j
                        }|j                  |j                        D ]L  \  }}|j                  |j                         j                         |j                  |j                  d          N t        |      | _        d}	t        |      D ]  \  }
}|	||
   ||
   z  z  }	 |	| j                  z  }	|	| _        || _        || _        || _        y )N)indexr   )r	   r
   pd	DataFramer   r   groupbyr   appendrw   valuesr   r3   nobs	enumeratecovmcovsr   dim)r   r   r   r   r   r   dfrY   rT   r   ro   r   s              r   r
   zCovarianceReduction.__init__@  s    %rb\\$))4::6JJrxx( 	"DAqKK'IIaggaj!	" J	 dO 	$DAqDGbeO#D	$				r   c                 l   | j                   j                  d   }|j                  || j                  f      }t	        j
                  |j                  t	        j
                  | j                   |            }t        j                  j                  |      \  }}| j                  |z  dz  }t        | j                        D ]s  \  }}t	        j
                  |j                  t	        j
                  ||            }t        j                  j                  |      \  }}|| j                  |   |z  dz  z  }u |S )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   r^   )r   r   rL   r   r   r   r   r   slogdetr   r   r   r   )	r   rD   rR   projr   rY   ldetrp   js	            r   loglikezCovarianceReduction.loglikeW  s     IIOOA~~q$((m,FF466266$))T23))##A&4IIq dii( 	'DAqtvvrvva/Aii''*GAtd"Q&&A	'
 r   c                 (   | j                   j                  d   }|j                  || j                  f      }t	        j
                  |j                  t	        j
                  | j                   |            }t	        j
                  | j                   |      }| j                  t        j                  j                  ||j                        j                  z  }t        | j                        D ]  \  }}t	        j
                  |j                  t	        j
                  ||            }t	        j
                  ||      }|| j                  |   t        j                  j                  ||j                        j                  z  z  } |j                         S )a  
        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   rL   r   r   r   r   r   r   r   r   r   r   rb   )	r   rD   rR   r   c0cPr   r   r   s	            r   scorezCovarianceReduction.scorer  s    IIOOA~~q$((m,VVDFFBFF499d34VVDIIt$II		BDD1333dii( 	:DAqq$0B4Bbiioob"$$79999A	:
 wwyr   c                      j                   j                  d   } j                  }|8t        j                  ||f      }t        j
                  |      |d|d|f<   |}n|}t        | fd fd||      \  }}}|dz  }|si j                  |j                               }	t        j                  t        j                  |	|	z              }
d|
z  }t        j                  |t               t         |d      }||_        t!        |      S )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                 (    j                  |        S r   )r   r$   r   s    r   <lambda>z)CovarianceReduction.fit.<locals>.<lambda>  s    4<<?:J r   c                 (    j                  |        S r   )r   r   s    r   r   z)CovarianceReduction.fit.<locals>.<lambda>  s    4::a=. r   z/CovReduce optimization did not converge, |g|=%fr1   )r   r   r   r   ra   rx   ry   r   rb   rz   r7   r4   r5   r   r9   llfr:   )r   rs   r{   r|   rR   r   rD   r   r~   r   r   r<   rE   s   `            r   rF   zCovarianceReduction.fit  s    ( IIOOAHH XXq!f%F!vvayF1Q3!8F!F (0J(@'(,.U 	r	

6<<>*AA'BCbHCMM#12%dF>)'22r   )N   g-C6?)	r(   r)   r*   r+   r
   r   r   rF   r,   r-   s   @r   r   r     s    %N.66-3r   r   )r4   numpyr   pandasr   statsmodels.baser   statsmodels.base.wrapperbasewrapperwrapstatsmodels.tools.sm_exceptionsr   Modelr   r/   r   r   Resultsr9   ResultsWrapperr:   populate_wrapperry   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   