
    M/Ph8              
       D   d Z ddlmZ ddlZddlmZ dgZddlm	Z	 d Z
d Z G d	 d
e	          Z G d d          Z G d de          Z G d de          Z G d de          Zedk    r2ddlmc mZ ddlZddlm	Z	 ddlmZ ej        j                                        Zej        dd         Zej        dd         Z ej         edee!d          Z" ej#        e"          Z$e$%                    ddg          &                                Z'eddg         (                    e!          )                    dd          Z ej*        ed          Z e
ed                   Z+ed         Z, eeee+e,ddgd          Z-e-.                    d !          Z/e-.                    d"d#$          Z0e-.                    d%d#$          Z1e-.                    d"d&$          Z2e-.                    d%d&$          Z3e-.                    d%d'$          Z4 ej5        g d(          Z6e6j7        d         Z8 ej9        e6          Z: ej5        g d)          Z; ej9        e;          Z<e6dddf         e:k    =                    e!          Z>e;dddf         e<k    =                    e!          Z?d*Z@ ejA        d+          ZB ejA        d,          ZCejD        eBe>z  eCe?z  f         ZE ejF        eEeEjG                  e@ ejH        e8          z  z   ZI eJeI            eJejK        L                    eI                     ejK        M                    eI          \  ZNZO ejF        eOd-eNz  eOz  jG                  ZPejK        Q                    eI          ZR ejF        eOeNeOz  jG                  ZS eJ ejT         ejU        eSeIz
                                  eJ ejT         ejU         ejF        ePeI           ejH        e8          z
                                 eO ejA        eN          z  ZV eJ ejT         ejU         ejF        eVeVjG                  ePz
                                  ej5        eBeCe@g          ZW ejX        e6e;f          ZY eeYeW          \  ZZZ[Z\ eJ ejT         ejU        eZeIz
                                  eJ ejT         ejU        e[ePz
                                  eJ ejT         ejU        e\eVz
                                  eejF        e>ejK        Q                     ejF        e>jG        e>                    e>jG        g          Z] ejH        e8          e]z
  Z^ eJ ejT         ejU         ejF        e^e6                                         dS dS ).z
Sandbox Panel Estimators

References
-----------

Baltagi, Badi H. `Econometric Analysis of Panel Data.` 4th ed. Wiley, 2008.
    )reduceN)GLS
PanelModel)Panelc                    i }t          j        t          |                     }t          t          |                     D ]G}| |         |vr*|                    | |         t          |          i           || |                  ||<   H|S )z
    Returns unique numeric values for groups without sorting.

    Examples
    --------
    >>> X = np.array(['a','a','b','c','b','c'])
    >>> group(X)
    >>> g
    array([ 0.,  0.,  1.,  2.,  1.,  2.])
    )npzeroslenrangeupdate)X	uniq_dictgroupis       b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/panel/panelmod.pyr   r      s     IHSVVE3q66]] # #ty  adS^^4555QqT?aL    c                 0   | j         dk    r| dddf         } | j        \  }}|d         t          j        |          z  }t	          |          D ]k}| dd||dz   f         }t          j        |          }||         ||k                        t                    z  }|t          j        ||j	                  z  }lt          j
                            |          \  }	}
t          j        |
d|	z  |
z  j	                  }|
t          j        |	          z  }|||fS )aG  calculate error covariance matrix for random effects model

    Parameters
    ----------
    groups : ndarray, (nobs, nre) or (nobs,)
        array of group/category observations
    sigma : ndarray, (nre+1,)
        array of standard deviations of random effects,
        last element is the standard deviation of the
        idiosyncratic error

    Returns
    -------
    omega : ndarray, (nobs, nobs)
        covariance matrix of error
    omegainv : ndarray, (nobs, nobs)
        inverse covariance matrix of error
    omegainvsqrt : ndarray, (nobs, nobs)
        squareroot inverse covariance matrix of error
        such that omega = omegainvsqrt * omegainvsqrt.T

    Notes
    -----
    This does not use sparse matrices and constructs nobs by nobs
    matrices. Also, omegainvsqrt is not sparse, i.e. elements are non-zero
       N)ndimshaper   eyer   uniqueastypefloatdotTlinalgeighsqrt)groupssigmasnobsnreomegaigrr   	groupuniqdummygrevevecomegainvomegainvhalfs                r   repanel_covr-   '   s   8 {a$ID#2Jrvd||#ESzz - -qqqSU{#Ie$$	+)!3 ; ;E B BB"&'),,,y~~e$$HBvdQrTD[O,,H#L(L((r   c                       e Zd ZdS )	PanelDataN__name__
__module____qualname__ r   r   r/   r/   S           Dr   r/   c                   F    e Zd ZdZ	 	 ddZd Zd ZddZdd	Zd
 Z	d Z
dS )r   a  
    An abstract statistical model class for panel (longitudinal) datasets.

    Parameters
    ----------
    endog : array_like or str
        If a pandas object is used then endog should be the name of the
        endogenous variable as a string.
#    exog
#    panel_arr
#    time_arr
    panel_data : pandas.Panel object

    Notes
    -----
    If a pandas object is supplied it is assumed that the major_axis is time
    and that the minor_axis has the panel variable.
    Nc                 B    ||                      ||||||           d S d S N)
initialize)selfendogexogpaneltimextnamesequation
panel_datas           r   __init__zPanelModel.__init__i   s4    
 OOE4gxHHHHH r   c                    |                     d          }|d         | _        |dd         }|d         | _        |d         | _        |                    d          dk    }	d|	v r:t          j        |	dk              d         d         }
|                    |
d           |	| _        || _	        t          j
        t          j        |                    | _        t          j        |          }|| _        t          j        |          | _        t          j        |          | _        t          j        |          | _        t          j        |          | _        dS )zG
        Initialize plain array model.

        See PanelModel
         r   r   NTcons)split
endog_name
panel_name	time_namevarr   whereinsert_cons_index
exog_namessqueezeasarrayr;   r<   r=   r>   r   	paneluniqtimeuniq)r:   r;   r<   r=   r>   r?   r@   namesrN   novar
cons_indexs              r   r9   zPanelModel.initializez   s    s##(122Y
!!*  q 5==%1*--a03Jj&111 $Z
5 1 122
z$	Z&&
Jt$$	5))	$r   c                 J   || _         ||         j        }t          j        |          | _        |.|j                                        }|                    |           |                    |          j        | _	        || _
        || _        |j        | _        |j        | _        d S r8   )rA   valuesr   rO   r;   columnstolistremovefilterItemsr<   
_exog_name_endog_name
major_axis_timeseries
minor_axis_panelseries)r:   rA   rG   	exog_namer;   s        r   initialize_pandaszPanelModel.initialize_pandas   s    $:&-Z&&
"*1133IZ(((**955<	#%%0&1r   onewayFc                    |dk    r| j         }| j        }n'|dk    r| j        }| j        }nt	          d|z            t          |||dddf         t          |          t          |          t          |dddf                   |           ||dddf         k                        t                    }|j	        dk    r6t          j        ||          |                    d          dddf         z  }n+t          j        ||          |                    d          z  }|du r|du r|S |du r|du r||                    d          fS |du r|du r||                    d          |fS |du r|du r||fS dS dS )z[
        Get group means of X by time or by panel.

        index default is panel
        rd   r>   zindex %s not understoodNr   FT)r=   rQ   r>   rR   
ValueErrorprintr
   r   r   r   r   r   sum)	r:   r   indexcountsdummiesYuniqdummymeans	            r   _group_meanzPanelModel._group_mean   s    H
A>DDf__	A=DD6>???atAAAdF|SVVSYYD4L8I8I	 	 	 d111T6l"**5116A::6%??599Q<<$#77DD6%??599Q<</DU??w%//Kt^^5 0 01%%t^^41u,,u__D; _r   c                 ^   |r|                                 }|                                 }|r|dvrt          d|z            |dk    r,t          | j        | j                                                  S |dk    r|                     ||          S |dk    r|                     ||          S dS )ab  
        method : LSDV, demeaned, MLE, GLS, BE, FE, optional
        model :
                between
                fixed
                random
                pooled
                [gmm]
        effects :
                oneway
                time
                twoway
        femethod : demeaned (only one implemented)
                   WLS
        remethod :
                swar -
                amemiya
                nerlove
                walhus


        Notes
        -----
        This is unfinished.  None of the method arguments work yet.
        Only oneway effects should work.
        )lsdvdemeanedmleglsbefez%s not a valid methodpooledbetweenfixedN)lowerrf   r   r;   r<   fit	_fit_btwn
_fit_fixed)r:   modelmethodeffectss       r   r|   zPanelModel.fit   s    6  	$\\^^F 	?f %8 8 8 4v=>>> Htz49--11333I>>&'222G??67333 r   c                     |dk    r9|                      | j        |          }|                      | j        |          }nt          d|z            t	          ||                                          }|S )Ntwowayri   z1%s effects is not valid for the between estimator)rp   r;   r<   rf   r   r|   )r:   r   r   r;   r<   befits         r   r}   zPanelModel._fit_btwn  s    h$$TZw$??E##DIW#==DD )+23 4 4 4E4  $$&&r   c                    | j         }| j        }d}|dv r|dk    rd}d}|                     ||d          \  }}|                     ||          }|                    t                    }|t          j        ||          z
  }|t          j        ||d	          z
  }|s|d
k    rg|                     |d
d          \  }}	|                     |d
          }|t          j        ||	          z
  }|t          j        |	j        |          z
  }t          ||d d | j
         f                                                   }
|
S )NF)rd   twowaysr   Trd   )ri   rj   r   r   )axisr>   )ri   rk   )r;   r<   rp   r   intr   repeatr   r   r   rM   r|   )r:   r   r   r;   r<   demeantwice
endog_meanrj   	exog_meanrk   fefits              r   r~   zPanelModel._fit_fixed)  sh   
y***)##""!%!1!1%w "2 " "J((W(==I]]3''FBIj&999E")IvA>>>>D 	7'V++"&"2"25 #3 # #J((V(<<IBF:w777E"&I666DE44#3"3 345599;;r   )NNNNNNN)rd   FF)NNrd   )r1   r2   r3   __doc__rB   r9   rc   rp   r|   r}   r~   r4   r   r   r   r   V   s         $ @D48I I I I"( ( (|2 2 2    @)4 )4 )4 )4l	 	 	    r   c                       e Zd ZdS )SURPanelNr0   r4   r   r   r   r   E  r5   r   r   c                       e Zd ZdS )SEMPanelNr0   r4   r   r   r   r   H  r5   r   r   c                       e Zd ZdS )DynamicPanelNr0   r4   r   r   r   r   K  r5   r   r   __main__i
investmentF)usemaskyearfirmvaluecapitalr      )prependzinvest value capital)r?   r@   rx   )r   ry   rd   )r   r   rz   r>   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   g      ?g       @g      @r   )_r   	functoolsr   numpyr   #statsmodels.regression.linear_modelr   __all__pandasr   r   r-   r/   r   r   r   r   r1   numpy.lib.recfunctionslibrecfunctionsnprfstatsmodels.apiapismdatasetsgrunfeldloaddatar;   r<   fullexogappend_fieldsr   	panel_arr	DataFramepanel_df	set_indexto_panelpanel_pandaviewreshapeadd_constantr=   r   	panel_modr|   	panel_olspanel_bepanel_fe	panel_bet	panel_fet	panel_fe2arrayr!   r   r#   r   r'   periods
perioduniqr   r(   dummypesigmar    sigmagrsigmapec_dummyallr   r   r   r%   rg   r   choleskyr   r)   r*   r+   inv	omegainv2	omegacompmaxabsr,   sigmas2column_stackgroups2omega_	omegainv_omegainvhalf_PgrQgrr4   r   r   <module>r      s              3 3 3 3 3 3.        &() () ()X	 	 	 	 	 	 	 	j j j j j j j j^	 	 	 	 	z 	 	 		 	 	 	 	z 	 	 		 	 	 	 	: 	 	 	 z)))))))))MMM      ;$$&&DJttEy#H""8\5%  I  v	**H$$ff%566??AAK WY'(--e44<<RBBD2?4///DE(6"##EFD
5$tfV_+- - -I H--I}}9h}??H}}7H}==HIv>>IGV<<IGY??I" RX'''((F<?D	&!!Ibh((())G7##Jaaaf~*22599Gqqqv*,44U;;GEbgbkkGbgbkkG
 uWW_ggo56H BF8XZ((5&"&,,+>>E	E%LLL	E")

U
#
#$$$y~~e$$HBrvdQrTD[O,,H	e$$Itb4i]++I	E&"&	E)**
+
+,,, 
E&"&x..=>>
?
?@@@#L	E&"&|LN;;hFGG
H
HIII bh%011Gbovw/00G'2{7G'D'D$FI}	E&"&''
(
()))	E&"&	H,--
.
.///	E&"&455
6
6777 &IMM&"&G4455giA B BC
"&,,
C 
E&"&sF++,,
-
-.....W r   