
    >h:                     v    S SK Jr  S SKrS SKJr  S SKJr  SS/r " S S5      r	 " S S5      r
 " S	 S
\5      rg)    )GLSN)LikelihoodModelResults)sparseSURSem2SLSc                   J    \ rS rSrSrSS jrS rS rS rS r	SS	 jr
S
 rSrg)r      aF
  
Seemingly Unrelated Regression

Parameters
----------
sys : list
    [endog1, exog1, endog2, exog2,...] It will be of length 2 x M,
    where M is the number of equations endog = exog.
sigma : array_like
    M x M array where sigma[i,j] is the covariance between equation i and j
dfk : None, 'dfk1', or 'dfk2'
    Default is None.  Correction for the degrees of freedom
    should be specified for small samples.  See the notes for more
    information.

Attributes
----------
cholsigmainv : ndarray
    The transpose of the Cholesky decomposition of `pinv_wexog`
df_model : ndarray
    Model degrees of freedom of each equation. p_{m} - 1 where p is
    the number of regressors for each equation m and one is subtracted
    for the constant.
df_resid : ndarray
    Residual degrees of freedom of each equation. Number of observations
    less the number of parameters.
endog : ndarray
    The LHS variables for each equation in the system.
    It is a M x nobs array where M is the number of equations.
exog : ndarray
    The RHS variable for each equation in the system.
    It is a nobs x sum(p_{m}) array.  Which is just each
    RHS array stacked next to each other in columns.
history : dict
    Contains the history of fitting the model. Probably not of interest
    if the model is fit with `igls` = False.
iterations : int
    The number of iterations until convergence if the model is fit
    iteratively.
nobs : float
    The number of observations of the equations.
normalized_cov_params : ndarray
    sum(p_{m}) x sum(p_{m}) array
    :math:`\left[X^{T}\left(\Sigma^{-1}\otimes\boldsymbol{I}\right)X\right]^{-1}`
pinv_wexog : ndarray
    The pseudo-inverse of the `wexog`
sigma : ndarray
    M x M covariance matrix of the cross-equation disturbances. See notes.
sp_exog : CSR sparse matrix
    Contains a block diagonal sparse matrix of the design so that
    exog1 ... exogM are on the diagonal.
wendog : ndarray
    M * nobs x 1 array of the endogenous variables whitened by
    `cholsigmainv` and stacked into a single column.
wexog : ndarray
    M*nobs x sum(p_{m}) array of the whitened exogenous variables.

Notes
-----
All individual equations are assumed to be well-behaved, homoskedastic
iid errors.  This is basically an extension of GLS, using sparse matrices.

.. math:: \Sigma=\left[\begin{array}{cccc}
          \sigma_{11} & \sigma_{12} & \cdots & \sigma_{1M}\\
          \sigma_{21} & \sigma_{22} & \cdots & \sigma_{2M}\\
          \vdots & \vdots & \ddots & \vdots\\
          \sigma_{M1} & \sigma_{M2} & \cdots & \sigma_{MM}\end{array}\right]

References
----------
Zellner (1962), Greene (2003)
Nc                 .  ^ [        T5      S-  S:w  a  [        S[        T5      -  5      eU(       a"  UR                  5       S;  a  [        SU-  5      eX0l        [        TSS S2   5      nX@l        [
        R                  " U4S j[        U5       5       5      nXPl        [
        R                  " TS S S2   5      nX`l
        [        U R                  S   R                  S   5      U l        / n/ nTSS S2    V	s/ s H=  oR                  U R                  [
        R                  R!                  U	5      -
  5      PM?       n	TSS S2    V	s/ s H3  oR                  [
        R                  R!                  U	5      S-
  5      PM5       n	[
        R                  " U5      U l        [
        R                  " U5      U l        [&        R(                  " [+        U R                  U-  5      [+        [
        R,                  " U R$                  S-   5      5      45      n
[
        R.                  " [
        R0                  " SU R$                  S-   45      5      U l        [        U5       HP  nTSS S2   U   XU R                  -  US-   U R                  -  2U R2                  U   U R2                  US-      24'   MR     U
R5                  5       U l        [
        R8                  " U5      (       a  [
        R                  " U5      nOUc  / n[        U5       H^  nUR                  [;        Xk   US S 2U R2                  U   U R2                  US-      24   5      R=                  5       R>                  5        M`     [
        R                  " U5      RA                  US5      nU RC                  U5      nX l"        [
        R                  RG                  [
        R                  RI                  U RD                  5      5      RJ                  U l&        U RO                  5         g s  sn	f s  sn	f )	N   r   Qsys must be a list of pairs of endogenous and exogenous variables.  Got length %s)dfk1dfk2zdfk option %s not understood   c              3   b   >#    U  H$  n[         R                  " TS SS2   U   5      v   M&     g7f)r   Nr   )npasarray).0isyss     mC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/sandbox/sysreg.py	<genexpr>SUR.__init__.<locals>.<genexpr>j   s+     JArzz#add)A,77s   ,/)(len
ValueErrorlower_dfk_Mr   column_stackrangeexogr   endogfloatshapenobsappendlinalgmatrix_rankdf_residdf_modelr   
lil_matrixintsumcumsumhstack_colstocsrsp_exoganyr   fitresidreshape_compute_sigmasigmacholeskypinvTcholsigmainv
initialize)selfr   r8   dfkMr!   r"   r)   r*   _r2   r   residss    `           r   __init__SUR.__init__\   s8   s8a<1 %'*3x0 1 199;/1 !?3!GHH	ADqD	N
 JqJJ	

3ss8$
$**Q---a01	 HKADqD	R	1RYY%:%:1%==	>	R@CADqD	J	1..q1A5	6	J

8,

8, ##S1%5t}}Q'(%* +YYryy!T]]1_)=>?
qA58AYq\ diiK1dii/JJqM$**QqS/12 3  }}66%==JJu%E]F1Xc%(4JJqM$**QqS/112 ,3 447CE%%A  ZZ'//"5F''/E
II..ryy~~JJ0  !!" 	5 	SJs   :AP
:Pc                    U R                  U R                  5      U l        U R                  U R                  5      U l        [
        R                  R                  U R                  5      U l        [
        R                  " U R                  [
        R                  " U R                  5      5      U l        S[
        R                  /0U l        SU l        g )Nparamsr   )whitenr"   wendogr2   wexogr   r'   r:   
pinv_wexogdot	transposenormalized_cov_paramsinfhistory
iterationsr>   s    r   r=   SUR.initialize   s    kk$**-[[.
))..4%'VVDOOT__-&/" BFF8,    c                 @    U R                   S   R                  U5        g )NrF   )rO   r&   )r>   rF   s     r   _update_historySUR._update_history   s    X%%f-rS   c                    U R                   nU R                  n[        R                  " XR                  5      nU R
                  (       d  UnGO'U R
                  R                  5       S:X  a|  [        R                  " US-  5      n[        U5       HB  n[        U5       H0  nU R                  U   S-   U R                  U   S-   -  S-  XVU-   '   M2     MD     UR                  X"5        O[        R                  " US-  5      n[        U5       HT  n[        U5       HB  nU[        R                  " U R                  U   S-   U R                  U   S-   5      -
  XVU-   '   MD     MV     UR                  X"5        [        R                  R                  [        R                  R                  XE-  5      5      R                  U l        XE-  $ )zB
Computes the sigma matrix and update the cholesky decomposition.
r   r   r   g      ?)r   r%   r   rK   r;   r   r   zerosr    r*   r6   maxr'   r9   r:   r<   )r>   rB   r@   r%   sigdivr   js           r   r7   SUR._compute_sigma   sc    GGyyffVXX&yyCYY__&(((1a4.C1XqA!%q!1!!3!]]1-a/!147 9C!H "  KK((1a4.C1XqA#bffT]]1-=a-?a(*',  ,C!H "  KKII..ryy~~cg/FGIIwrS   c                    U R                   nXR                  L aZ  [        R                  " [        R                  " U R
                  [        R                  " U5      5      UR                  SS5      5      $ XR                  L aF  [        R                  " U R
                  [        R                  " X"5      5      U-  R                  5       $ g)z
SUR whiten method.

Parameters
----------
X : list of arrays
    Data to be whitened.

Returns
-------
If X is the exogenous RHS of the system.
``np.dot(np.kron(cholsigmainv,np.eye(M)),np.diag(X))``

If X is the endogenous LHS of the system.
r   r   N)r%   r"   r   rK   kronr<   eyer6   r2   r   toarray)r>   Xr%   s      r   rG   
SUR.whiten   s      yy

?66"''$"3"3BFF4LA		"Q! !,,KK 1 1

4%''()*1')4 rS   c                    [         R                  " U R                  5      (       d+  U R                  U R                  U R
                  5      U l        U R                  n[         R                  " U R                  U R                  5      nU R                  U5        U =R                  S-  sl        U(       d  [        XU R                  5      nU$ U R                  S   nU(       Ga  [         R                  " [         R                  " US   US   -
  5      U:  5      (       Ga  U R                  U:  Ga  U R                   U-  R#                  US5      nU R                  U-
  n	U R                  U	5      U l        U R%                  U R                  5      U l	        U R%                  U R                   5      U l        [         R(                  R+                  U R&                  5      U l        [         R                  " U R                  [         R,                  " U R                  5      5      U l        [         R                  " U R                  U R                  5      nU R                  U5        U =R                  S-  sl        U(       aN  [         R                  " [         R                  " US   US   -
  5      U:  5      (       a  U R                  U:  a  GM  [        XU R                  5      nU$ )aB  
igls : bool
    Iterate until estimates converge if sigma is None instead of
    two-step GLS, which is the default is sigma is None.

tol : float

maxiter : int

Notes
-----
This ia naive implementation that does not exploit the block
diagonal structure. It should work for ill-conditioned `sigma`
but this is untested.
r   rF   r   )r   r3   r8   r7   r"   r!   r   rK   rJ   rH   rU   rP   
SysResultsrM   rO   absr2   r6   rG   rI   r'   r:   rL   )
r>   iglstolmaxiterr@   betasur_fitconvfittedvaluesrB   s
             r   r4   SUR.fit   s   " vvdjj!!,,TZZCDJGGvvdoot{{3T"1 T-G-GHGN||H%rvvd2hb&9:S@AA7* LL-66q<LZZ,.F,,V4DJ++djj1DKT\\2DJ iinnTZZ8DO)+LL1*3D&66$//4;;7D  &OOq O rvvd2hb&9:S@AA7* T)C)CDrS   c                     g N )r>   designs     r   predictSUR.predict       rS   )r   r0   r   r<   r*   r)   r"   r!   rO   rP   r%   rM   rJ   r8   r2   rH   rI   NN)Fgh㈵>d   )__name__
__module____qualname____firstlineno____doc__rC   r=   rU   r7   rG   r4   rt   __static_attributes__rr   rS   r   r   r      s.    GR3j.640*XrS   c                   .    \ rS rSrSrSS jrS rS rSrg)	r   i  a  
Two-Stage Least Squares for Simultaneous equations

Parameters
----------
sys : list
    [endog1, exog1, endog2, exog2,...] It will be of length 2 x M,
    where M is the number of equations endog = exog.
indep_endog : dict
    A dictionary mapping the equation to the column numbers of the
    the independent endogenous regressors in each equation.
    It is assumed that the system is entered as broken up into
    LHS and RHS. For now, the values of the dict have to be sequences.
    Note that the keys for the equations should be zero-indexed.
instruments : ndarray
    Array of the exogenous independent variables.

Notes
-----
This is unfinished, and the design should be refactored.
Estimation is done by brute force and there is no exploitation of
the structure of the system.
Nc           	      >   [        U5      S-  S:w  a  [        S[        U5      -  5      e[        USS S2   5      nX@l        US S S2   U l        USS S2   U l        USS S2    Vs/ s H"  n[
        R                  R                  U5      PM$     snU l        X0l	        0 nUR                  5        Vs/ s H  oVR                  U/ 5      PM       nU H5  nX'    H*  nXg   R                  U R                  U   S S 2U4   5        M,     M7     X l        [
        R                  " [
        R                  " SU R                  45      5      n	U H  n [!        X'   5        M     U R'                  U5      U l        g s  snf s  snf !   [#        S[%        X'   5      < SU< 35      e= f)Nr   r   r   r   z=The values of the indep_exog dict must be iterable. Got type z for converter )r   r   r   r"   r!   r   r'   r(   _Kinstrumentskeys
setdefaultr&   _indep_endogr.   r/   iter	TypeErrortyperG   rI   )
r>   r   indep_endogr   r@   rA   instr_endogeq_keyvarcol_col_maps
             r   rC   Sem2SLS.__init__  s   s8a<1 %'*3x0 1 1ADqD	N1X
1I	58AY?Y299((+Y? ' /:/?/?/AB/A!		"	%/AB!F%-#**499V+<QvX+FG . " (99RYY$''{34!FG[() ", [[-
Q @ 	CG#'(;#<f!F G Gs   %)E20E7
E<< Fc                    / nU R                   nU R                  n[        U R                  5       H  nUR	                  US5      nU R
                  U   R                  5       nU(       aN  [        U5       H?  u  p[        XR                  5      R                  5       R                  n
XSS2X5   U   4'   MA     UR                  U5        M     U$ )z\
Runs the first stage of the 2SLS.

Returns the RHS variables that include the instruments.
N)r   r   r    r   getr!   copy	enumerater   r4   rn   r&   )r>   YrI   r   r   eqinstr_eqnewRHSr   LHSyhats              r   rG   Sem2SLS.whitenN  s     ''&&.BuuRHYYr]'')F&x0EAs$4$4599;HHD371[_Q//0 1
 LL  ! rS   c                     / nU R                   nU R                  n[        U R                  5       H:  nUR	                  [        X4   X$   5      R                  5       R                  5        M<     U$ )z	
        )rI   r"   r    r   r&   r   r4   rF   )r>   deltarI   r"   r\   s        r   r4   Sem2SLS.fitd  sX     



twwALLUXux0446==>  rS   )r   r   r   r"   r!   r   rI   rw   )	ry   rz   r{   r|   r}   rC   rG   r4   r~   rr   rS   r   r   r     s    .1.h,rS   c                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )rf   in  z
Not implemented yet.
c                 F   > [         TU ]  XX45        U R                  5         g rq   )superrC   _get_results)r>   modelrF   rM   scale	__class__s        r   rC   SysResults.__init__r  s!    %	.rS   c                     g rq   rr   rQ   s    r   r   SysResults._get_resultsw  rv   rS   rr   )Ng      ?)	ry   rz   r{   r|   r}   rC   r   r~   __classcell__)r   s   @r   rf   rf   n  s    
 rS   rf   )#statsmodels.regression.linear_modelr   numpyr   statsmodels.base.modelr   scipyr   __all__r   r   rf   rr   rS   r   <module>r      sD    3  9  )
k k`j jX
' 
rS   