
    M/Ph9                         d dl mZ d dlZd dlmZ d dlmZ ddgZ G d d          Z	 G d d          Z
 G d	 d
e          ZdS )    )GLSN)LikelihoodModelResults)sparseSURSem2SLSc                   @    e Zd ZdZddZd Zd Zd Zd ZddZ	d Z
dS )r   aN  
    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                 d   
 t                    dz  dk    rt          dt                    z            |r(|                                dvrt          d|z            | _        t          dd d                   }| _        t          j        fdt          |          D                       }| _        t          j	        d d d                   }| _
        t           j
        d         j        d                    _        g g 
 fddd d         D              
fd	dd d         D              t          j	                   _        t          j	        
           _        t!          j        t%           j        |z            t%          t          j         j        dz                       f          }t          j        t          j        d j        dz   f                     _        t          |          D ]H}dd d         |         || j        z  |dz    j        z   j        |          j        |dz            f<   I|                                 _        t          j        |          rt          j	        |          }n|g }	t          |          D ]g}|	                    t7          ||         |d d  j        |          j        |dz            f                                                   j                   ht          j	        |	                              |d
          }	                     |	          }| _         t
          j!        "                    t
          j!        #                     j                             j$         _%         &                                 d S )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   ^   K   | ]'}t          j        d dd         |                   V  (dS )r   Nr
   )npasarray).0isyss     Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/sysreg.py	<genexpr>zSUR.__init__.<locals>.<genexpr>j   s;      JJArz#add)A,77JJJJJJ    c                     g | ]<}                     j        t          j                            |          z
            =S  )appendnobsr   linalgmatrix_rank)r   _df_residselfs     r   
<listcomp>z SUR.__init__.<locals>.<listcomp>u   s<    RRR1RY%:%:1%=%==	>	>RRRr   c                 z    g | ]7}                     t          j                            |          d z
            8S )r   )r   r   r   r   )r   r   df_models     r   r!   z SUR.__init__.<locals>.<listcomp>v   s:    JJJ1..q11A5	6	6JJJr   )'len
ValueErrorlower_dfk_Mr   column_stackrangeexogr   endogfloatshaper   r   r#   r   
lil_matrixintsumcumsumhstack_colstocsrsp_exoganyr   r   fitresidreshape_compute_sigmasigmar   choleskypinvTcholsigmainv
initialize)r    r   r=   dfkMr,   r-   r7   r   residsr#   r   s   ``        @@r   __init__zSUR.__init__\   s   s88a<1 %'*3xx0 1 1 1 	I99;;/11 !?3!GHHH	ADqD	NN
 JJJJqJJJJJ	
3sss8$$
$*Q--a011	 RRRRRADqD	RRRRJJJJADqD	JJJJ
8,,
8,, #S1%5%5t}Q''((%* + +Yry!T]1_)=>>??
q 	B 	BA58AYq\ AdiK1di/JqM$*QqS/12 3 3}}6%== 	0Ju%%EE]F1XX A Ac%(4JqM$*QqS/112 ,3 4 447CEE%A A A AZ''//"55F''//E
I..ry~~J0  0  ! !!" 	r   c                 v   |                      | j                  | _        |                      | j                  | _        t
          j                            | j                  | _        t          j	        | j        t          j
        | j                            | _        dt
          j        gi| _        d| _        d S )Nparamsr   )whitenr-   wendogr7   wexogr   r   r?   
pinv_wexogdot	transposenormalized_cov_paramsinfhistory
iterationsr    s    r   rB   zSUR.initialize   s    kk$*--[[..
)..44%'VDOT_--&/ &/" BF8,r   c                 F    | j         d                             |           d S )NrH   )rQ   r   )r    rH   s     r   _update_historyzSUR._update_history   s#    X%%f-----r   c                 L   | j         }| j        }t          j        ||j                  }| j        s|}n$| j                                        dk    r|t          j        |dz            }t          |          D ]>}t          |          D ],}| j	        |         dz   | j	        |         dz   z  dz  |||z   <   -?|
                    ||           nt          j        |dz            }t          |          D ]N}t          |          D ]<}|t          j        | j	        |         dz   | j	        |         dz             z
  |||z   <   =O|
                    ||           t          j                            t          j                            ||z                      j        | _        ||z  S )zR
        Computes the sigma matrix and update the cholesky decomposition.
        r   r
   r   g      ?)r)   r   r   rM   r@   r(   r'   zerosr+   r#   r;   maxr   r>   r?   rA   )r    rE   rD   r   sigdivr   js           r   r<   zSUR._compute_sigma   s    GyfVVX&&y 	CCY__&(((1a4..C1XX 9 9q 9 9A!%q!1!!3!]1-a/!147 9C!HH9 KK!(1a4..C1XX , ,q , ,A#bfT]1-=a-?a(*', ',  ,C!HH, KK!I..ry~~c#g/F/FGGI3wr   c                 b   | j         }|| j        u rSt          j        t          j        | j        t          j        |                    |                    dd                    S || j        u rBt          j        | j        t          j        ||                    |z  
                                S dS )aK  
        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   rM   kronrA   eyer;   r7   r   toarray)r    Xr   s      r   rI   z
SUR.whiten   s      y
??6"'$"3BF4LLAA		"Q! ! !$,K 1
4%%' ''()*1'))4 r   Fh㈵>d   c                    t          j        | j                  s%|                     | j        | j                  | _        | j        }t          j        | j        | j	                  }| 
                    |           | xj        dz  c_        |st          | || j                  }|S | j        d         }|rt          j        t          j        |d         |d         z
            |k              rv| j        |k     rj| j        |z                      |d          }| j        |z
  }	|                     |	          | _        |                     | j                  | _	        |                     | j                  | _        t           j                            | j                  | _        t          j        | j        t          j        | j                            | _        t          j        | j        | j	                  }| 
                    |           | xj        dz  c_        |rEt          j        t          j        |d         |d         z
            |k              r| j        |k     jt          | || j                  }|S )a  
        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   rH   r$   )r   r8   r=   r<   r-   r,   r)   rM   rL   rJ   rU   rR   
SysResultsrO   rQ   absr7   r;   rI   rK   r   r?   rN   )
r    iglstolmaxiterrD   betasur_fitconvfittedvaluesrE   s
             r   r9   zSUR.fit   s    " vdj!! 	D,,TZCCDJGvdot{33T"""1 	 tT-GHHGN|H% 	!rvd2hb&9::S@AA 	!7** L-66q<<LZ,.F,,V44DJ++dj11DKT\22DJ innTZ88DO)+L11*3 *3D&6$/4;77D  &&&OOq OO  	!rvd2hb&9::S@AA 	!7** T4)CDDr   c                     d S Nr   )r    designs     r   predictzSUR.predict       r   NN)Fra   rb   )__name__
__module____qualname____doc__rF   rB   rU   r<   rI   r9   rq   r   r   r   r   r      s        G GR3 3 3 3j  . . .  64 4 40* * * *X    r   c                   &    e Zd ZdZddZd Zd ZdS )r   ad  
    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           	      "   t          |          dz  dk    rt          dt          |          z            t          |dd d                   }|| _        |d d d         | _        |dd d         | _        d |dd d         D             | _        || _        i fd|                                D              |D ]=}||         D ]2}|                             | j        |         d d |f                    3>|| _	        t          j        t          j        d| j        f                    }|D ]E}	 t          ||                    #  t          dt          ||                   d|          xY w|                               | _        d S )	Nr
   r   r   r   c                 L    g | ]!}t           j                            |          "S r   )r   r   r   )r   r   s     r   r!   z$Sem2SLS.__init__.<locals>.<listcomp>#  s(    ???29((++???r   c                 <    g | ]}                     |g           S r   )
setdefault)r   r   instr_endogs     r   r!   z$Sem2SLS.__init__.<locals>.<listcomp>*  s)    BBB!		"	%	%BBBr   z=The values of the indep_exog dict must be iterable. Got type z for converter )r%   r&   r)   r-   r,   _Kinstrumentskeysr   _indep_endogr   r3   r4   iter	TypeErrortyperI   rK   )	r    r   indep_endogr   rD   eq_keyvarcol_col_mapr}   s	           @r   rF   zSem2SLS.__init__  s   s88a<1 %'*3xx0 1 1 1ADqD	NN1X
1I	??SAY??? ' BBBB{/?/?/A/ABBBB! 	H 	HF%f- H HF#**49V+<QQQvX+FGGGGH
 (9RY$'{3344! 	G 	GFG[())))Gi#'F(;#<#<#<#<ff!F G G G" [[--


s   2E*E2c                    g }| j         }| j        }t          | j                  D ]}|                    |d          }| j        |                                         }|rVt          |          D ]F\  }}	t          |	| j                  	                                j
        }
|
|dd||         |         f<   G|                    |           |S )zt
        Runs the first stage of the 2SLS.

        Returns the RHS variables that include the instruments.
        N)r   r   r+   r)   getr,   copy	enumerater   r9   rm   r   )r    YrK   r   r   eqinstr_eqnewRHSr   LHSyhats              r   rI   zSem2SLS.whitenN  s     '&.. 		! 		!BuuRHYr]''))F 8&x00 8 8EAcsD$45599;;HD37F111[_Q//00 LL    r   c                     g }| j         }| j        }t          | j                  D ]H}|                    t          ||         ||                                                   j                   I|S )z	
        )rK   r-   r+   r)   r   r   r9   rH   )r    deltarK   r-   r[   s        r   r9   zSem2SLS.fitd  si     

tw 	? 	?ALLU1XuQx004466=>>>>r   rs   )rt   ru   rv   rw   rF   rI   r9   r   r   r   r   r     sQ         .1. 1. 1. 1.h  ,    r   c                   *     e Zd ZdZd fd	Zd Z xZS )re   z
    Not implemented yet.
    N      ?c                 x    t                                          ||||           |                                  d S ro   )superrF   _get_results)r    modelrH   rO   scale	__class__s        r   rF   zSysResults.__init__r  s@    %u	. 	. 	.r   c                     d S ro   r   rS   s    r   r   zSysResults._get_resultsw  rr   r   )Nr   )rt   ru   rv   rw   rF   r   __classcell__)r   s   @r   re   re   n  sV              
      r   re   )#statsmodels.regression.linear_modelr   numpyr   statsmodels.base.modelr   scipyr   __all__r   r   re   r   r   r   <module>r      s    3 3 3 3 3 3     9 9 9 9 9 9       )
k k k k k k k k`j j j j j j j jX
 
 
 
 
' 
 
 
 
 
r   