
    M/Ph                     v    d Z ddlZddlmZmZ ddlmZ d Zd Z	d Z
 G d d	          Z G d
 de          ZdS )a1  Panel data analysis for short T and large N

Created on Sat Dec 17 19:32:00 2011

Author: Josef Perktold
License: BSD-3


starting from scratch before looking at references again
just a stub to get the basic structure for group handling
target outsource as much as possible for reuse

Notes
-----

this is the basic version using a loop over individuals which will be more
widely applicable. Depending on the special cases, there will be faster
implementations possible (sparse, kroneker, ...)

the only two group specific methods or get_within_cov and whiten

    N)OLSGLS)GroupSortedc                 d    d} |            D ]"}| |         }|t          j        ||          z  }#|S )zIsum outerproduct dot(x_i, x_i.T) over individuals

    loop version

    r   )npouter)x
group_itermomgx_gs        e/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/panel/panel_short.pysum_outer_product_loopr      sD     CZ\\ " "drxS!!!J    c                 f    |                      d|d          }t          j        ||j                  S )zsum outerproduct dot(x_i, x_i.T) over individuals

    where x_i is (nobs_i, 1), and result is (nobs_i, nobs_i)

    reshape-dot version, for x.ndim=1 only

    F)order)reshaper   dotT)r	   n_groupsxrss      r   sum_outer_product_balancedr   ,   s/     ))B)
,
,C6#sur   c                     g } |            D ]2}| |         }|                     t          j        ||                     3t          j        |          S )zAapply linear transform for each individual

    loop version
    )appendr   r   concatenate)r	   	transformr
   x_newr   r   s         r   whiten_individuals_loopr    >   sX     EZ\\ - -dRVIs++,,,,>%   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )ShortPanelGLS2  Short Panel with general intertemporal within correlation

    assumes data is stacked by individuals, panel is balanced and
    within correlation structure is identical across individuals.

    It looks like this can just inherit GLS and overwrite whiten
    c                 l    || _         || _        t          |          | _        | j        j        | _        d S N)endogexogr   groupr   )selfr&   r'   r(   s       r   __init__zShortPanelGLS2.__init__X   s/    
	 ''

+r   c                 r    t          | j        | j                                                  | _        | j        S r%   r   r&   r'   fit
res_pooledr)   s    r   fit_olszShortPanelGLS2.fit_ols_   s+    dj$)4488::r   c                 J    t          || j        j                  }|| j        z  S r%   r   r(   r
   r   r)   residr   s      r   get_within_covzShortPanelGLS2.get_within_covc   #    $UDJ,ABBT]""r   c                 <    t          ||| j        j                  }|S r%   r    r(   r
   r)   r	   cholsigmainv_iwxs       r   whiten_groupszShortPanelGLS2.whiten_groupsh       $Q
8MNN	r   c                    |                                  }|                     |j                  }t          j                            t          j                            |                    j        | _        | 	                    | j
        | j                  }| 	                    | j        | j                  }t          ||                                          | _        | j        S r%   )r0   r5   r4   r   linalgcholeskypinvr   r:   r<   r&   r'   r   r-   res1r)   r.   sigma_iwendogwexogs        r   r-   zShortPanelGLS2.fitm   s    \\^^
%%j&677 i001H1HIIK##DJ0CDD""49d.ABB&&**,,	yr   N)	__name__
__module____qualname____doc__r*   r0   r5   r<   r-    r   r   r"   r"   O   si         , , ,  # # #
  
    r   r"   c                   J     e Zd ZdZd fd	Zd Zd Zd Zd Zd Z	dd
Z
 xZS )ShortPanelGLSr#   Nc                    t          |          | _        | j        j        | _        t          |          | j        z  }|!t	          j        t          |                    }t          j                            t          j        	                    |                    j
        | _        t          | j        |                               ||d            d S )N)sigma)r   r(   r   lenr   eyeintr?   r@   rA   r   r:   super	__class__r*   )r)   r&   r'   r(   rD   nobs_irT   s         r   r*   zShortPanelGLS.__init__   s     ''

+Udm+ ?fS[[))G i001H1HIIK 	dnd##,,UD,EEEEEr   c                 J    t          || j        j                  }|| j        z  S r%   r2   r3   s      r   r5   zShortPanelGLS.get_within_cov   r6   r   c                 <    t          ||| j        j                  }|S r%   r8   r9   s       r   r<   zShortPanelGLS.whiten_groups   r=   r   c                 r    t          | j        | j                                                  | _        | j        S r%   r,   r/   s    r   _fit_olszShortPanelGLS._fit_ols   s+    dj$)4488::r   c                    |                                  }|                     |j                  }t          j                            t          j                            |                    j        | _        | 	                    | j
        | j                  }| 	                    | j        | j                  }t          ||                                          | _        | j        S r%   )rY   r5   r4   r   r?   r@   rA   r   r:   r<   r&   r'   r   r-   rB   rC   s        r   _fit_oldzShortPanelGLS._fit_old   s    ]]__
%%j&677 i001H1HIIK##DJ0CDD""49d.ABB&&**,,	yr   c                 F    t          || j        | j        j                  }|S r%   )r    r:   r(   r
   )r)   r	   r;   s      r   whitenzShortPanelGLS.whiten   s     $Q(;TZ=RSS	r      c                 8   |dk     rt          d          ddl}|                    t                    | _        t          |          D ]}t          | d          r| `|                                 }| j        d         	                    |j
                   ||dz
  k    s{|| _        |                     |j                  }t          j                            t          j                            |                    j        | _        |                                  |S )a#  
        Perform an iterative two-step procedure to estimate the GLS model.

        Parameters
        ----------
        maxiter : int, optional
            the number of iterations

        Notes
        -----
        maxiter=1: returns the estimated based on given weights
        maxiter=2: performs a second estimation with the updated weights,
                   this is 2-step estimation
        maxiter>2: iteratively estimate and update the weights

        TODO: possible extension stop iteration if change in parameter
            estimates is smaller than x_tol

        Repeated calls to fit_iterative, will do one redundant pinv_wexog
        calculation. Calling fit_iterative(maxiter) once does not do any
        redundant recalculations (whitening or calculating pinv_wexog).
           zmaxiter needs to be at least 1r   N
pinv_wexogself_params)
ValueErrorcollectionsdefaultdictlisthistoryrangehasattrra   r-   r   paramsresults_oldr5   r4   r   r?   r@   rA   r   r:   
initialize)r)   maxiterrd   iresultsrD   s         r   fit_iterativezShortPanelGLS.fit_iterative   s   : Q;;=>>>"..t44w 	" 	"At\** $O hhjjGL'..w~>>>	>>#*  --gm<<&(i&8&89P9P&Q&Q&S# !!! r   r%   )r^   )rG   rH   rI   rJ   r*   r5   r<   rY   r[   r]   rp   __classcell__)rT   s   @r   rM   rM   w   s         F F F F F F"# # #
  
  
    9 9 9 9 9 9 9 9r   rM   )rJ   numpyr   #statsmodels.regression.linear_modelr   r   statsmodels.tools.grouputilsr   r   r   r    r"   rM   rK   r   r   <module>ru      s    .     8 8 8 8 8 8 8 8 4 4 4 4 4 4  	 	 	$! ! !"& & & & & & & &Pr r r r rC r r r r rr   