ó
áp7]c        	   @  s  d  Z  d d l m Z d d l m Z m Z d d l m Z d d l Z	 d g Z
 d d l m Z d „  Z d	 „  Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d k rd d l Z d d l m Z d d l j Z d d l j j Z e j j j d e  ƒ Z! e! j" d  Z" e! j# d  Z$ e j% e$ d e" e& d e  ƒZ' e j( e' ƒ Z) e) j* d d g ƒ j+ ƒ  Z, e$ d d g j- e& ƒ j. d d ƒ Z# e j/ e# d e  ƒZ# e e$ d ƒ Z0 e$ d Z1 e e" e# e0 e1 d d d g d d  ƒZ2 e2 j3 d! d" ƒ Z4 e2 j3 d! d# d$ d% ƒ Z5 e2 j3 d! d& d$ d% ƒ Z6 e2 j3 d! d# d$ d' ƒ Z7 e2 j3 d! d& d$ d' ƒ Z8 e2 j3 d! d& d$ d( ƒ Z9 e	 j: d) d) d) d* d* d d d g ƒ Z; e; j< d) Z= e	 j> e; ƒ Z? e	 j: d) d* d d* d d) d* d g ƒ Z@ e	 j> e@ ƒ ZA e; d d … d f e? k jC e& ƒ ZD e@ d d … d f eA k jC e& ƒ ZE d+ ZF e	 jG d, ƒ ZH e	 jG d- ƒ ZI e	 jJ eH eD eI eE f ZK e	 jL eK eK jM ƒ eF e	 jN e= ƒ ZO eP eO ƒ eP e	 jQ jR eO ƒ ƒ e	 jQ jS eO ƒ \ ZT ZU e	 jL eU d* eT eU jM ƒ ZV e	 jQ jW eO ƒ ZX e	 jL eU eT eU jM ƒ ZY eP e	 jZ e	 j[ eY eO ƒ ƒ ƒ eP e	 jZ e	 j[ e	 jL eV eO ƒ e	 jN e= ƒ ƒ ƒ ƒ eU e	 jG eT ƒ Z\ eP e	 jZ e	 j[ e	 jL e\ e\ jM ƒ eV ƒ ƒ ƒ e	 j: eH eI eF g ƒ Z] e	 j^ e; e@ f ƒ Z_ e e_ e] ƒ \ Z` Za Zb eP e	 jZ e	 j[ e` eO ƒ ƒ ƒ eP e	 jZ e	 j[ ea eV ƒ ƒ ƒ eP e	 jZ e	 j[ eb e\ ƒ ƒ ƒ e e	 jL eD e	 jQ jW e	 jL eD jM eD ƒ ƒ eD jM g ƒ Zc e	 jN e= ƒ ec Zd eP e	 jZ e	 j[ e	 jL ed e; ƒ ƒ ƒ ƒ n  d S(.   s   
Sandbox Panel Estimators

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

Baltagi, Badi H. `Econometric Analysis of Panel Data.` 4th ed. Wiley, 2008.
iÿÿÿÿ(   t   print_function(   t   ranget   reduce(   t   GLSNt
   PanelModel(   t   Panelc         C  s   i  } t  j t |  ƒ ƒ } x] t t |  ƒ ƒ D]I } |  | | k re | j i t | ƒ |  | 6ƒ n  | |  | | | <q. W| S(   sÓ   
    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.])
    (   t   npt   zerost   lenR   t   update(   t   Xt	   uniq_dictt   groupt   i(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyR      s    !c         C  s  |  j  d k r( |  d d … d f }  n  |  j \ } } | d t j | ƒ } xy t | ƒ D]k } |  d d … | | d … f } t j | ƒ } | | | | k j t ƒ } | t j	 | | j
 ƒ 7} q[ Wt j j | ƒ \ }	 }
 t j	 |
 d |	 |
 j
 ƒ } |
 t j |	 ƒ } | | | f S(   s=  calculate error covariance matrix for random effects model

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

    Returns
    -------
    omega : array, (nobs, nobs)
        covariance matrix of error
    omegainv : array, (nobs, nobs)
        inverse covariance matrix of error
    omegainvsqrt : array, (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
    i   Niÿÿÿÿ(   t   ndimt   Nonet   shapeR   t   eyeR   t   uniquet   astypet   floatt   dott   Tt   linalgt   eight   sqrt(   t   groupst   sigmast   nobst   nret   omegat   igrR   t	   groupuniqt   dummygrt   evt   evect   omegainvt   omegainvhalf(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyt   repanel_cov&   s     t	   PanelDatac           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyR'   R   s   c           B  st   e  Z d  Z d	 d	 d	 d	 d	 d	 d	 d „ Z d „  Z d „  Z d e e d „ Z d	 d	 d d „ Z	 d „  Z
 d „  Z RS(
   sà  
    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.
    c         C  s/   | d  k r+ |  j | | | | | | ƒ n  d  S(   N(   R   t
   initialize(   t   selft   endogt   exogt   panelt   timet   xtnamest   equationt
   panel_data(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyt   __init__h   s    c         C  s"  | j  d ƒ } | d |  _ | d } | d |  _ | d |  _ | j d ƒ d k }	 t |	 k r‘ t j |	 d k ƒ d d }
 | j |
 d ƒ n  |	 |  _	 | |  _
 t j t j | ƒ ƒ |  _ t j | ƒ } | |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ d S(   sG   
        Initialize plain array model.

        See PanelModel
        t    i    i   t   consN(   t   splitt
   endog_namet
   panel_namet	   time_namet   vart   TrueR   t   wheret   insertt   _cons_indext
   exog_namest   squeezet   asarrayR,   R-   R.   R/   R   t	   paneluniqt   timeuniq(   R+   R,   R-   R.   R/   R0   R1   t   namesR?   t   novart
   cons_index(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyR*   y   s$    

			c         C  s–   | |  _  | | j } t j | ƒ |  _ | d  k rS | j j ƒ  } | j | ƒ n  | j	 | ƒ j |  _
 | |  _ | |  _ | j |  _ | j |  _ d  S(   N(   R2   t   valuesR   R@   R,   R   t   columnst   tolistt   removet   filterItemsR-   t
   _exog_namet   _endog_namet
   major_axist   _timeseriest
   minor_axist   _panelseries(   R+   R2   R7   t	   exog_nameR,   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyt   initialize_pandas·   s    			t   onewayc   	   
   C  sÀ  | d k r! |  j  } |  j } n1 | d k rB |  j } |  j } n t d | ƒ ‚ t | | | d d … d f t | ƒ t | ƒ t | d d … d f ƒ | ƒ | | d d … d f k j t	 ƒ } | j
 d k rt j | | ƒ | j d ƒ d d … d f } n t j | | ƒ | j d ƒ } | t k rA| t k rA| S| t k rl| t k rl| | j d ƒ f S| t k rš| t k rš| | j d ƒ | f S| t k r¼| t k r¼| | f Sd S(   s[   
        Get group means of X by time or by panel.

        index default is panel
        RT   R/   s   index %s not understoodNi   (   R.   RB   R/   RC   t
   ValueErrort   printR   R   R   R   R   R   R   t   sumt   FalseR;   (	   R+   R
   t   indext   countst   dummiest   Yt   uniqt   dummyt   mean(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyt   _group_meanÇ   s*    		G%2c         C  s§   | r | j  ƒ  } n  | j  ƒ  } | rF | d k rF t d | ƒ ‚ n  | d k rk t |  j |  j ƒ j ƒ  S| d	 k r‡ |  j | | ƒ S| d
 k r£ |  j | | ƒ Sd S(   sb  
        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.
        t   lsdvt   demeanedt   mlet   glst   bet   fes   %s not a valid methodt   pooledt   betweent   fixedN(   Ra   Rb   Rc   Rd   Re   Rf   (   t   lowerRU   R   R,   R-   t   fitt	   _fit_btwnt
   _fit_fixed(   R+   t   modelt   methodt   effects(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyRk   ç   s    		c         C  sh   | d k r? |  j  |  j d | ƒ} |  j  |  j d | ƒ} n t d | ƒ ‚ t | | ƒ j ƒ  } | S(   Nt   twowayRY   s1   %s effects is not valid for the between estimator(   R`   R,   R-   RU   R   Rk   (   R+   Ro   Rp   R,   R-   t   befit(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyRl     s    
c         C  s`  |  j  } |  j } t } | d	 k r¹ | d k r? t } d } n  |  j | d | d t ƒ\ } } |  j | d | ƒ} | j t ƒ } | t j | | ƒ } | t j | | d d ƒ} n  | sË | d k r3|  j | d d d t ƒ\ } }	 |  j | d d ƒ} | t j	 | |	 ƒ } | t j	 |	 j
 | ƒ } n  t | | d  d  … |  j f ƒ j ƒ  }
 |
 S(
   NRT   t   twowaysRY   RZ   t   axisi    R/   R[   (   RT   Rs   (   R,   R-   RX   R;   R`   R   t   intR   t   repeatR   R   R   R>   Rk   (   R+   Ro   Rp   R,   R-   t   demeantwicet
   endog_meanRZ   t	   exog_meanR[   t   fefit(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyRm   (  s*    			)N(   R(   R)   t   __doc__R   R3   R*   RS   RX   R`   Rk   Rl   Rm   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyR   U   s   	>	 6	t   SURPanelc           B  s   e  Z RS(    (   R(   R)   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyR|   D  s   t   SEMPanelc           B  s   e  Z RS(    (   R(   R)   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyR}   G  s   t   DynamicPanelc           B  s   e  Z RS(    (   R(   R)   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyR~   J  s   t   __main__t	   as_pandasiìÿÿÿt
   investmentt   usemaskt   yeart   firmt   valuet   capitali   t   prependR0   R1   s   invest value capitalRn   Rg   Rh   Rp   RT   Ri   R/   Rs   i    i   g      ð?g       @g      @(e   R{   t
   __future__R    t   statsmodels.compat.pythonR   R   t#   statsmodels.regression.linear_modelR   t   numpyR   t   __all__t   pandasR   R   R&   R'   t   objectR   R|   R}   R~   R(   t   statsmodels.apit   apit   smt   numpy.lib.recfunctionst   libt   recfunctionst   nprft   datasetst   grunfeldt   loadRX   t   dataR,   R-   t   fullexogt   append_fieldsR   t	   panel_arrt	   DataFramet   panel_dft	   set_indext   to_panelt   panel_pandat   viewt   reshapet   add_constantR.   Rƒ   t	   panel_modRk   t	   panel_olst   panel_bet   panel_fet	   panel_bett	   panel_fett	   panel_fe2t   arrayR   R   R   R   R    t   periodst
   perioduniqR   R   R!   t   dummypet   sigmaR   t   sigmagrt   sigmapet   c_t   dummyallR   R   R   R   RV   R   t   choleskyR   R"   R#   R$   t   invt	   omegainv2t	   omegacompt   maxt   absR%   t   sigmas2t   column_stackt   groups2t   omega_t	   omegainv_t   omegainvhalf_t   Pgrt   Qgr(    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/panel/panelmod.pyt   <module>   sŠ   			,ï	%
	''%%&
 5/   -