Ë
    £�DjéQ  ã                   ó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       €úaC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\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°   € ô �Z‰Z˜Ÿ
™
Ó#ˆØ�I‰I�bš!�eÑˆð 	
ˆQ�V‰V�A‹Y‰ˆÜ�v‰v�a—c‘c˜1‹~ §¡¨¡
Ñ*ˆÜ—	‘	×"Ñ" 4Ó(ˆÜ�I‰I�O‰O˜E 1§3¡3Ó'×)Ñ)ˆØˆŒ
ØˆŒô ŸN™N¨1¨gÓ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‹;˜Š?ØEˆCÜ�M‰M˜#Ôð —)‘)—/‘/ !Ñ$¨Ñ/ˆà�
‰
�7Ôà!%×!2Ñ!2Ö3˜Aˆa�f‰f�Q�iÐ3ˆÐ3Ø!%×!2Ñ!2Ö3˜AˆQ�W‰W�Q‹ZÐ3ˆÐ3Ü�Z‰Z˜‹^ˆÜ�J‰J�q‹Mˆô �f‰f�R—T‘T˜1šQ ˜W™:¨™?Ó+¨a¯e©e«gÑ5ˆä�y‰y�~‰~˜cÓ"‰ˆˆ1Ü�Z‰Z˜˜‹^ˆØˆb‰EˆØŠa�ˆe‰HˆÜ—‘—‘ §¡§¡°Ó2ˆä% d¨F¸Ô;ˆÜ)¨'Ó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  € ð �K‰KˆØ�z‰zˆØ×ÑˆØ×ÑˆØˆÜ�J‰J�q˜1˜dŸi™i˜.Ó)ˆô �t—y‘yÓ!ò 	ˆAÜ—‘�t—|‘| Q¢q¨! t¡WÓ-ˆAØ”—‘˜˜A™“Ñ‰Að	ô
 —‘�t˜Q“ˆÜ�y‰y�|‰|˜EÓ"‰ˆˆ1Ü�V‰V�A”r—v‘v˜aŸc™c 2§4¡4Ó(Ó)ˆØ�T‰T�B‰YˆØ	ŒR�V‰V�B˜˜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÷  € ð �K‰KˆØ�y‰yˆØ�z‰zˆØ—,‘,ˆØ×ÑˆØ×ÑˆØ�I‰I�q˜$�iÓ ˆð ”—‘˜Ÿ™Ÿ™¬¯©¨t¯|©|¸QÓ(?Ó@Ñ@ˆà�I‰I�q˜$�iÓ ˆÜ—‘�t˜Q“ˆÜ—‘˜˜eÓ$ˆÜ�F‰F�5—7‘7˜EÓ"ˆÜ�Y‰Y�]‰]˜1ÓˆÜ�X‰X�q˜$�iÓ ˆÜ�i‰i�o‰o˜a §¡Ó)ˆàˆV�q˜4‘xÑ ˆÜ�q“ó 
	&ˆAÜ˜4“[ó 	&�Ø�a‘�Ø��1�a�4‘Ü—v‘v˜fŸh™h¨Ó+�Ø˜Ÿ™‘�ÜŸ™˜r¤2§6¡6¨$°Ó#3Ó4Ð4�Ü—v‘vœbŸf™f T¨2Ó.°Ó4�ØœŸ™˜u¤b§f¡f¨T°5·7±7Ó&;Ó<Ñ<�ØœŸ™˜u¤b§i¡i§o¡o°a¼¿¹ÀÇÁÀdÓ9KÓ&LÓMÑM�Ø!%��1�T‘6˜A‘:“ò	&ð
	&ô �Y‰Y�_‰_˜Q¤§¡ u§w¡w°·±Ó 5Ó6ˆØ”"—&‘&˜ Ó#×%Ñ%Ñ%ˆÜ�w“ò 	.ˆAØ�1’a�4‘ˆAØ�1’a�4‘ˆAÜ˜1“Xò .�Ü˜t›ò .�AÜŸ™˜q¤"§&¡&¨¨A¨d©F°Q©J©¸Ó";Ó<�AØ�q˜!�t“H  B q¡E¡	¨A¡Ñ-”Hñ.ñ.ð	.ð �x‰x‹zÐ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‹;˜Š?ØGˆCÜ�M‰M˜#Ôàˆ?ÜÐ=Ó>Ð>à—z‘z .°$Ó7ˆð —*‘*˜Y¨Ó+ˆð —)‘)—/‘/ !Ñ$¨Ñ/ˆô �Z‰Z˜Ÿ
™
Ó#ˆØ�I‰I�bš!�eÑˆØ	ˆQ�V‰V�A‹Y‰ˆä�v‰v�a—c‘c‹{ˆô —^‘^ A wÓ/ˆ
à!+Ö,˜Aˆa�f‰f�Q�iÐ,ˆÐ,Ø!+Ö,˜AˆQ�W‰W�Q‹ZÐ,ˆÐ,Ü�Z‰Z˜‹^ˆÜ�J‰J�q‹MˆØ §¡£™KˆÔØˆŒ	Ø—j‘j ‘mˆŒØˆŒØˆŒ
ØˆŒØˆÔàÐÜ—X‘X˜tŸ{™{¨DÐ1Ó2ˆFÜ%'§V¡V¨D£\ˆF�1�T�6˜1˜T˜6�>Ñ"Ø‰Fà×!Ñ! !Ñ$¨Ò,ØI�Ü  “oÐ%Ø!ˆFä% f¨d×.IÑ.IØ&*×&<Ñ&<¸gÀtóMÑˆ��5ñ Ø×&Ñ& v§|¡|£~Ó6ˆAÜ—‘œŸ™  1›Ó&ˆBØ@À2ÑEˆCÜ�M‰M˜#Ôä% d¨F¸Ô>ˆÜ)¨'Ó2Ð2ùòA -ùÚ,s   Ã2J*ÄJ/)rt   )r_   Nrt   éd   gü©ñÒMbP?)r(   r)   r*   r+   rF   r\   rq   r�   © r   r   r/   r/   %   s(   „ ñó #3òJò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  € ð" —
‘
˜7 EÓ*ˆà�J‰J˜Ÿ™Ÿ™Ó*Ñ*ˆØ�I‰I˜Ÿ	™	Ÿ™ qÓ)Ñ)ˆáÝ?Ù�A�q“	—‘“ˆAØ—‘ˆAä�Y‰Y�} a¨¨AÓ.ˆØ
Œc�!‹f‰ˆä�V‰V�A—C‘C‹[ˆÜ�Y‰Y�_‰_˜R Ó$ˆä�y‰y�}‰}˜RÓ ‰ˆˆ1Ü�Z‰ZœŸ™ ›˜
Ó#ˆØˆb‰EˆØ’1�b�5‘ˆä% d¨F¸Ô;ˆÜ)¨'Ó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@   ø€ ÜŒd�DÑ" 5¨$Ñ9°&Ò9àˆŒØ�6‰>˜f T™l¨dÑ2ØˆD�Gð 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Ö5˜aŒb�f‰f�Q—S‘S�kÐ5ˆÐ5Ø"&×"3Ñ"3Ö4˜Qˆa�g‰g�a‹jÐ4ˆÐ4à�J‰J×Ñ˜QÑˆà�wŠwàˆBÜ˜b "›+ò +‘��3Ü—f‘f˜Q“i #‘o�Ø�aœ"Ÿ&™&  cÓ*Ñ*Ñ*‘ð+ð ”#�b“'‰MŠBð
 ˆBØò #�Ø”b—f‘f˜Q “lÑ"‘ð#à”#�b“'‰MˆBð ˆBØ×&Ñ&ò '�Ø˜Ÿ™˜q›	‘M�Ü˜qŸw™w q™zÓ*ò '�AØ˜!šQ˜$™�AÜŸ™  A›�AØœ"Ÿ&™&  A›,Ñ&‘Bñ'ð'ð �$—)‘)—/‘/ !Ñ$Ñ$ˆBä—‘˜“ˆAØ�a˜!‘e‘  Q¡¨¡
¨Q¡Ñ/ˆBØ�a‘%˜Q ™U Q™J¨™NÑ+ˆBØ�r‘'˜B ™GÑ#ˆCä—‘˜“˜Q¤ R£™[¬3¨r«7Ñ2Ñ2°SÑ8ˆBä�y‰y�~‰~˜bÓ!‰ˆˆ1Ü�Z‰Z˜˜‹^ˆØˆb‰EˆØŠa�ˆe‰HˆÜ—‘—‘ §¡§¡°Ó2ˆä% d¨F¸Ô;ˆÜ)¨'Ó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   ø€ ð ”r—v‘v˜a !™e“}Ñ$ q¬2¯6©6°!°a±%«=Ñ'8Ñ8ˆBÜ—6‘6˜"˜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 �<‰<�D€A€qØ�\‰\‹^€Fá	ˆV‹€BØ€Eä�7‹^ó ˆñ �‹LˆØ	ŒR�V‰V�A�vÓ Ñ'¬"¯&©&°¸Ó*@Ñ@Ñ@ˆä�7‰7”2—6‘6˜!˜a™%“=Ó! DÒ(ØˆEÙà�Y‰Y˜˜1�vÓˆÜ—9‘9—=‘=  QÓ'‰ˆˆ1ˆbà—.‘. ! Q Ó(ˆÜ�f‰f�W˜bŸd™dÓ#ˆ÷	*ð ˆØ�U‹lÙ�d�U“ˆBÙ�R“ˆBØ�BŠwØ�Ø�ÙØ�A‰IˆDð �U�lð1ðB �^‰^˜Q ˜FÓ#€FØ�2�uÐÐ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è   ø€ ä‰Ñ˜ Ô%à�rˆbˆÜ�\‰\˜$Ÿ)™)¨4¯:©:Ô6ˆØ—J‘J˜rŸx™xÓ(ò 	"‰DˆAˆqØ�K‰K˜Ÿ™›Ÿ™Ô'Ø�I‰I�a—g‘g˜a‘jÕ!ð	"ô ˜“JˆŒ	ð ˆÜ˜d“Oò 	$‰DˆAˆqØ�D˜‘G˜b ™e‘OÑ#‰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ï   € ð �I‰I�O‰O˜AÑˆØ�~‰~˜q $§(¡(˜mÓ,ˆä�F‰F�4—6‘6œ2Ÿ6™6 $§)¡)¨TÓ2Ó3ˆÜ—)‘)×#Ñ# AÓ&‰ˆˆ4Ø�I‰I˜Ñ˜qÑ ˆä˜dŸi™iÓ(ò 	'‰DˆAˆqÜ—‘�t—v‘vœrŸv™v a¨›Ó/ˆAÜ—i‘i×'Ñ'¨Ó*‰GˆAˆtØ�—‘˜‘˜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  € ð �I‰I�O‰O˜AÑˆØ�~‰~˜q $§(¡(˜mÓ,ˆä�V‰V�D—F‘FœBŸF™F 4§9¡9¨dÓ3Ó4ˆÜ�V‰V�D—I‘I˜tÓ$ˆØ�I‰IœŸ	™	Ÿ™¨¨B¯D©DÓ1×3Ñ3Ñ3ˆä˜dŸi™iÓ(ò 	:‰DˆAˆqÜ—‘˜Ÿ™¤§¡ q¨$£Ó0ˆBÜ—‘˜˜4“ˆBØ�—‘˜‘œbŸi™iŸo™o¨b°"·$±$Ó7×9Ñ9Ñ9Ñ9‰Að	:ð
 �w‰w‹yÐ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÷   ø€ ð( �I‰I�O‰O˜AÑˆØ�H‰Hˆð ÐÜ—X‘X˜q !˜fÓ%ˆFÜ!Ÿv™v a›yˆF�1�Q�3˜˜!˜�8ÑØ‰Fà!ˆFô (¨Ó0JÛ(@À'Ø(,ó.Ñˆ��Uð 	ˆr‰	ˆÙØ—
‘
˜6Ÿ<™<›>Ó*ˆAÜ—‘œŸ™  A¡›Ó'ˆBØCÀbÑHˆCÜ�M‰M˜#Ô1Ô2ä% d¨F¸Ô>ˆØˆŒÜ)¨'Ó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¿   ðÛ ã Û å "ß 'Ð 'Ý >ô7˜eŸk™kô 7ô4`3Ð.ô `3ôF>3Ð!8ô >3ôB^3Ð&=ô ^3ôB˜%Ÿ-™-ô ô& ×!4Ñ!4ô ð €× Ñ Ð0Ø)ô+òNôbb3Ð1ô b3ðL €Ø €Ø&€Ø�r   