
    M/Ph?                        d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ d$d
Zd$dZd$dZd$dZd%dZd&dZ G d de
          Z G d de
          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Zd Zd!Z G d" d#          ZdS )'a  Treatment effect estimators

follows largely Stata's teffects in Stata 13 manual

Created on Tue Jun  9 22:45:23 2015

Author: Josef Perktold
License: BSD-3

currently available

                     ATE        POM_0        POM_1
res_ipw       230.688598  3172.774059  3403.462658
res_aipw     -230.989201  3403.355253  3172.366052
res_aipw_wls -227.195618  3403.250651  3176.055033
res_ra       -239.639211  3403.242272  3163.603060
res_ipwra    -229.967078  3403.335639  3173.368561


Lots of todos, just the beginning, but most effects are available but not
standard errors, and no code structure that has a useful pattern

see https://github.com/statsmodels/statsmodels/issues/2443

Note: script requires cattaneo2 data file from Stata 14, hardcoded file path
could be loaded with webuse

    N)Substitution)
block_diag)WLS)GMM)ContrastResults)indentTc                     ||z  }d|z
  d|z
  z  }|r.||                                 z  }||                                 z  }||z
  }||z  | z
  S )zmoment condition for average treatment effect

    This does not include a moment condition for potential outcome mean (POM).

          ?mean)paramsendogtindprobweightedw1w0wdiffs           g/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/treatment/treatment_effects.py_mom_ater   '   sa     +B
t)T		"B 
bggii
bggiiGE5=6!!    c                     ||z  }d|z
  d|z
  z  }|r.||                                 z  }||                                 z  }t          j        ||z  | d         z
  ||z  | d         z
  f          S )zamoment conditions for average treatment means (POM)

    moment conditions are POM0 and POM1
    r
   r      )r   npcolumn_stack)r   r   r   r   r   r   r   s          r   _mom_atmr   8   sx    
 +B
t)T		"B 
bggii
bggii?EBJ2EBJ4JKLLLr   c                     ||z  d|z
  d|z
  z  z   }t          j        d|z
  |f          }|||                    |           z
  z  dddf         |z  }|S )z}
    moment condition for average treatment mean based on OLS dummy regression

    moment conditions are POM0 and POM1

    r   N)r   r   dot)r   r   r   r   r   w	treat_indmoms           r   _mom_olsr"   F   si     	tqv!d(++AT4 011I	f---.4
89
DCJr   c                     ||z  d|z
  d|z
  z  z   }t          j        |t          j        t          |                    f          }|||                    |           z
  z  dddf         |z  }|S )z
    moment condition for average treatment mean based on OLS dummy regression

    first moment is ATE
    second moment is POM0  (control)

    r   N)r   r   oneslenr   )tmr   r   r   r   r   r    r!   s           r   _mom_ols_ter'   U   su     	tqv!d(++Arws4yy'9'9 :;;I	b)))*AAAtG
4y
@CJr   c                     ||n|j         }|                    | |          }|j        |z
  }|||z  }|d d d f         |z  }|S N)exogpredictr   )r   modelr*   scalefittedresidr!   s          r   
_mom_olsexr0   e   s[    #44D]]64((FK& E
4.4
CJr   c                 ,   ||z  }d|z
  d|z
  z  }|
||z  }||z  }|r.||                                 z  }||                                 z  }||z
  }| |z                                   | |z                                   | |z                                   fS )zKaverage treatment effect based on basic inverse propensity weighting.

    r
   r   )r   r   r   r   probtr   r   r   s           r   ate_ipwr3   o   s     +B
t)T		"B
e
e 
bggii
bggiiGEEM!!EBJ#4#4#6#68I8I8K8KKKr   c                   *     e Zd ZdZd fd	Zd Z xZS )_TEGMMGeneric1Y  GMM class to get cov_params for treatment effects

    This combines moment conditions for the selection/treatment model and the
    outcome model to get the standard errors for the treatment effect that
    takes the first step estimation of the treatment model into account.

    this also matches standard errors of ATE and POM in Stata

    Fc                    t                                          |d d            || _        || _        || _        | j                            |           | j        j        g | j        _        |rd| _	        n#t          |j        j        j                  | _	        |r | j                                        | _        d S d | _        d S )Nr   )super__init__results_selectmom_outcomeexclude_tmoms__dict__updatedataxnamesk_selectr%   r,   param_namesr+   r   )selfr   
res_selectr;   r<   kwargs	__class__s         r   r9   z_TEGMMGeneric1.__init__   s    d+++(&*V$$$ 9#!DI  	CDMM
 0 5 ABBDM 	+3355DIIIDIIIr   c                    t          |          | j        z
  }|d |         }||d          }| j        j        j        }| j        r| j        }n| j        j                            |          }g }|                     || j        ||d          }|	                    |           | j        s4| j        j        
                    |          }	|	                    |	           t          j        |          }
|
S )NT)r   )r%   rA   r:   r,   r   r<   r   r+   r;   append	score_obsr   r   )rC   r   	k_outcomer&   p_tmr   r   	moms_listmom_omom_tmomss              r   momcondz_TEGMMGeneric1.momcond   s    KK$-/	JYJijj!"(. 	;9DD&,44T::D	  TZtd KK! 	$'-77==EU###y))r   )F__name__
__module____qualname____doc__r9   rP   __classcell__rF   s   @r   r5   r5      sV              2      r   r5   c                   (     e Zd ZdZ fdZd Z xZS )_TEGMMr6   c                     t                                          |d d            || _        || _        | j        j        g | j        _        d S d S r)   )r8   r9   r:   r;   r?   r@   )rC   r   rD   r;   rF   s       r   r9   z_TEGMM.__init__   sT    d+++(& 9#!DI $#r   c                 .   |d d         }|dd          }| j         j        j        }| j         j                            |          }|                     || j        ||          }t          j        || j         j                            |          f          }|S )N   )r:   r,   r   r+   r;   r   r   rI   )rC   r   r&   rK   r   r   momtrO   s           r   rP   z_TEGMM.momcond   s    BQBZabbz"(."(0066DJd;; $ 3 9 C CD I I K L Lr   rQ   rW   s   @r   rY   rY      sQ         " " " " "	 	 	 	 	 	 	r   rY   c                       e Zd ZdZd ZdS )_IPWGMMa GMM for aipw treatment effect and potential outcome

    uses unweighted outcome regression
    c                    | j         }|j        }|j        }|j        j        }| j        }|d d         }|dd          }t          j        |j        	                    |                    }	t          j
        |	dd          }	|	}
|dk    rd }n=|dv r|
}n6|dv rd|
z
  }n,t          |t          j                  r|}nt          d          ||
z  d|z
  d|
z
  z  z   }|||z  }t          j        |t          j        t!          |                    f          }|||                    |          z
  z  d d d f         |z  }|j                            |          }t          j        ||f          }|S )	Nr\   {Gz?Gz?allr   treatedr   	untreatedcontrolr   !incorrect option for effect_group)teffr:   	treatment
model_poolr   effect_groupr   asarrayr,   r+   clip
isinstancendarray
ValueErrorr   r$   r%   r   rI   )rC   r   rarD   r   r   rn   r&   psprob_selr   r2   r   r    mm
mom_selectrO   s                    r   rP   z_IPWGMM.momcond   s   Y&
|#(BQBZABBZ:j.66r::;;78T4005  EE^++EE888HEEbj11 	BEE@AAA4K1t8D11
 JAOT273t99+=+=$>??	59==,,,-qqq$w7)C%//33
J/00r   NrR   rS   rT   rU   rP    r   r   r_   r_      s-         
' ' ' ' 'r   r_   c                       e Zd ZdZd ZdS )_AIPWGMMr`   c                    | j         }|j        }|j        }|d         }t          j        t          |                    dk    }||         }|j        j        j        j	        d         }|d         }|d|dz            }	||dz   d|z  dz            }
|d|z  dz   d          }|j        j        }|j
        j        }|j        }|j        }t          j        |j                            |                    }t          j        |dd          }||          }||         }t          j        ||f          }|                    |	|          }t#          |	|          }|                    |
|          }t#          |
|          }t%          ||          }|j        }t          j        ||          ||         f          }||z
  d|z
  z  d|z
  z  }||z
  |z  |z  }||z   }||z   }||z
  }||z
  }||z
  } t          j        || f          }|j                            |          }!t          j        |!|          |!|         fd          }!t          j        |||!f          }"|"S )Nr   r   r\   rb   rc   r,   axisrk   
treat_maskr:   r   aranger%   results0r,   r*   shaperesults1exog_groupedendog_groupedro   r+   rp   concatenater0   r   rl   r   rI   )#rC   r   rt   r   rD   ppommaskkpmp0p1ru   mod0mod1r*   r   rv   prob0prob1r   fitted0mom0fitted1mom1r;   r   correct0correct1tmean0tmean1aterw   mpomrx   rO   s#                                      r   rP   z_AIPWGMM.momcond  s|   Y]
&
ayyV%%*K"(+AYAacE]AaC!AIAaCEFF^{ {  :j.66r::;;78T400*%$~uen-- ,,r4(("D))),,r4(("D))) t,, |~tZK0$z2BCDDGOD1QX>GOt+d28#8#vo2X}_b$Z((  %//33
^Z%<%/
%;%=CDF F F
 K<==r   Nry   rz   r   r   r|   r|     s-         
9 9 9 9 9r   r|   c                       e Zd ZdZd ZdS )_AIPWWLSGMMzc GMM for aipw-wls treatment effect and potential outcome

    uses weighted outcome regression
    c                 2   | j         }|j        }|j        }|d         }t          j        t          |                    dk    }||         }|j        j        j        j	        d         }|d         }|d|dz            }	||dz   d|z  dz            }
|dd          }|j        j        }|j
        j        }|j        }|j        }t          j        |j                            |                    }t          j        |dd          }||          }||         }t          j        ||f          }d}d|z
  d|z
  z  d|z
  d|z
  z  dz
  z  }d}||z  ||z  dz
  z  }|                    |	|          }t#          |	|          |d d d f         z  }|                    |
|          }t#          |
|          |d d d f         z  }t%          ||          }|j        }t          j        ||          ||         f          }||z
  d|z
  z  d|z
  z  }||z
  |z  |z  }||z   }||z   }||z
  } | |z
  }!||z
  }"t          j        |!|"f          }!|j                            |          }#t          j        |#|          |#|         fd          }#t          j        |!||#f          }$|$S )	Nr   r   r\   MbP?+?r~   r   r   )%rC   r   rt   r   rD   r   r   r   r   r   r   ru   r   r   r*   r   rv   r   r   r   r   ww0ww1r   r   r   r   r;   r   r   r   r   r   rw   r   rx   rO   s%                                        r   rP   z_AIPWWLSGMM.momcondX  s   Y]
&
ayyV%%*K"(+AYAacE]AaC!AIBCC[{ {   :j.66r::;;78UE22*%$~uen--4xAI&1t8E	*BQ*FGUldUlQ./ ,,r4(("D)))C4L8,,r4(("D)))C4L8 t,, |~tZK0$z2BCDDGOD1QX>GOt+d28#8#vo2X}_b$Z((  %//33
^Z%<%/
%;%=CDF F F
 K<==r   Nry   rz   r   r   r   r   R  s2         
A A A A Ar   r   c                       e Zd ZdZd ZdS )_RAGMMzqGMM for regression adjustment treatment effect and potential outcome

    uses unweighted outcome regression
    c                    | j         }|d         }t          j        t          |                    dk    }||         }|j        j        j        j        d         }|d         }|d|dz            }|| d          }|j        j        }	|j        j        }
|j	        }|	
                    ||          }t          ||	          }|

                    ||          }t          ||
          }t          ||          }||z
  |z
  }||z
  }t          j        ||f          }| j        .|| j        | j                                        z  d d d f         z  }t          j        ||f          }|S )Nr   r   r~   )rk   r   r   r%   r   r,   r*   r   r   r   r+   r0   r   r   r2   r   )rC   r   rt   r   r   r   r   r   r   r   r   r*   r   r   r   r   momoutrw   r   rO   s                       r   rP   z_RAGMM.momcond  s\   YayyV%%*K"(+AYAacE]QBCC[{ { ,,r4(("D))),,r4(("D)))D$''w#~_b$Z((:!4:
 1 11111d7;;BF|,,r   Nry   rz   r   r   r   r     s-         
    r   r   c                       e Zd ZdZd ZdS )	_IPWRAGMMz: GMM for ipwra treatment effect and potential outcome
    c                    | j         }|j        }|j        }|d         }t          j        t          |                    dk    }||         }|j        j        j        j	        d         }|d         }|d|dz            }	||dz   d|z  dz            }
|dd          }|j        j        }|j
        j        }|j        }t          j        t          |                    }d||                                 d <   t          j        |j                            |                    }t          j        |dd          }||          }||         }| j        }|dk    rdd|z
  z  }d|z  }d}nl|dv r%|d|z
  z  }||z  }||                                z  }nC|d	v r0d|z
  d|z
  z  }d|z
  |z  }d|z
  }||                                z  }nt'          d
          |                    |	|          }t)          |	|          |d d d f         z  }|                    |
|          }t)          |
|          |d d d f         z  }t+          ||          }||z
  |z
  |z  }||z
  |z  }t          j        ||f          }|j                            |          }t          j        ||          ||         fd          }t          j        |||f          }|S )Nr   r   r\   r   r   r   rd   re   rg   rj   r~   r   )rk   r   r:   r   r   r%   r   r,   r*   r   r   r   zerossumro   r+   rp   rn   r   rs   r0   r   r   rI   r   ) rC   r   rt   r   rD   r   r   r   r   r   r   ru   r   r   r*   r   rv   r   r   rn   r   r   sindr   r   r   r   r;   rw   r   rx   rO   s                                    r   rP   z_IPWRAGMM.momcond  s   Y]
&
ayyV%%*K"(+AYAacE]AaC!AIBCC[{ {  xJ((#$jnn  :j.66r::;;78UE22*%$(5  a%iBUBDD^++!e)$BB$))++%DD888e)E	*Be)u$BHDDIIKKDD@AAA ,,r4(("D)))Bqqq$wK7,,r4(("D)))Bqqq$wK7 t,, "$,$$&_b$Z((  %//33
^Z%<%/
%;%=CDF F F
 K<==r   Nry   rz   r   r   r   r     s2         D D D D Dr   r   c                   "     e Zd ZdZ fdZ xZS )TreatmentEffectResultsa	  
    Results class for treatment effect estimation

    Parameters
    ----------
    teff : instance of TreatmentEffect class
    results_gmm : instance of GMMResults class
    method : string
        Method and estimator of treatment effect.
    kwds: dict
        Other keywords with additional information.

    Notes
    -----
    This class is a subclass of ContrastResults and inherits methods like
    summary, summary_frame and conf_int. Attributes correspond to a z-test
    given by ``GMMResults.t_test``.
    c                    t                                                       t          |j                  }t	          j        d|f          }d|d<   d|d<   ddg|dd df<   |                    |          }| j                            |j                   || _	        || _
        || _        | j                            |           g d| _        d S )N   r   )r   r   )r   r   r\   )ATEPOM0POM1)r8   r9   r%   r   r   r   t_testr=   r>   rk   results_gmmmethodc_names)	rC   rk   r   r   kwdsk_paramsconstraintsttrF   s	           r   r9   zTreatmentEffectResults.__init__#  s    {)**h8}--DDVArrE,,R[)))	&T"""...r   )rR   rS   rT   rU   r9   rV   rW   s   @r   r   r     sB         &/ / / / / / / / /r   r   a  Parameters
----------
return_results : bool
    If True, then a results instance is returned.
    If False, just ATE, POM0 and POM1 are returned.
effect_group : {"all", 0, 1}
    ``effectgroup`` determines for which population the effects are
    estimated.
    If effect_group is "all", then sample average treatment effect and
    potential outcomes are returned
    If effect_group is 1 or "treated", then effects on treated are
    returned.
    If effect_group is 0, "treated" or "control", then effects on
    untreated, i.e. control group, are returned.
disp : bool
    Indicates whether the scipy optimizer should display the
    optimization results

Returns
-------
TreatmentEffectsResults instance or tuple (ATE, POM0, POM1)
aA  Parameters
----------
return_results : bool
    If True, then a results instance is returned.
    If False, just ATE, POM0 and POM1 are returned.
disp : bool
    Indicates whether the scipy optimizer should display the
    optimization results

Returns
-------
TreatmentEffectsResults instance or tuple (ATE, POM0, POM1)
c                   :   e Zd ZdZddZedd            Zdd
Z e e	e
d                    dd            Z e e	ed                    dd            Z e e	ed                    dd            Z e e	e
d                    dd            ZdS )TreatmentEffecta  
    Estimate average treatment effect under conditional independence

    .. versionadded:: 0.14.0

    This class estimates treatment effect and potential outcome using 5
    different methods, ipw, ra, aipw, aipw-wls, ipw-ra.
    Standard errors and inference are based on the joint GMM representation of
    selection or treatment model, outcome model and effect functions.

    Parameters
    ----------
    model : instance of a model class
        The model class should contain endog and exog for the outcome model.
    treatment : ndarray
        indicator array for observations with treatment (1) or without (0)
    results_select : results instance
        The results instance for the treatment or selection model.
    _cov_type : "HC0"
        Internal keyword. The keyword oes not affect GMMResults which always
        corresponds to HC0 standard errors.
    kwds : keyword arguments
        currently not used

    Notes
    -----
    The outcome model is currently limited to a linear model based on OLS.
    Other outcome models, like Logit and Poisson, will become available in
    future.

    See `Treatment Effect notebook
    <../examples/notebooks/generated/treatment_effect.html>`__
    for an overview.

    NHC0c                    | j                             |           t          j        |          | _        |dk    x| _        }| || _        |                                | _        || _	        |j
        }|j        }|j        d         | _        || _        |                    ||          ||                    }	|	                    |          | _        |                    ||         ||                   }
|
                    |          | _        t          j        |	j        |
j        fd          | _        t          j        |	j
        |
j
        fd          | _        d S )Nr   r   cov_typer   )r=   r>   r   ro   rl   r   r:   r+   prob_selectrm   r   r*   r   nobs	_cov_typerF   fitr   r   r   r   r   )rC   r,   rl   r:   r   r   r   r   r*   r   r   s              r   r9   zTreatmentEffect.__init__  s8    	T"""I..(1Q7*%"0D-5577DzKN	" uj[143DEE)44uZ0$z2BCC)44 NDIty+AJJJ^TZ,D1MMMr   olsc                     t           )z>create models from data

        not yet implemented

        )NotImplementedError)clsr   r*   rl   r,   r   s         r   	from_datazTreatmentEffect.from_data  s
     "!r   Trd   Fc                 $   | j         j        }| j        }| j        }|dk    rd}nC|dv r|}d}n:|dv rd|z
  }d}n.t	          |t
          j                  r|}d}nt          d          t          |||d	|
          }|s|S t          || j
        d| |          }	t          j        |dd         | j
        j        f          }
|	                    |
t          j        t          |
                    dd|dd          }t!          | |d|
|          }|S )a  Inverse Probability Weighted treatment effect estimation.

        Parameters
        ----------
        return_results : bool
            If True, then a results instance is returned.
            If False, just ATE, POM0 and POM1 are returned.
        effect_group : {"all", 0, 1}
            ``effectgroup`` determines for which population the effects are
            estimated.
            If effect_group is "all", then sample average treatment effect and
            potential outcomes are returned.
            If effect_group is 1 or "treated", then effects on treated are
            returned.
            If effect_group is 0, "treated" or "control", then effects on
            untreated, i.e. control group, are returned.
        disp : bool
            Indicates whether the scipy optimizer should display the
            optimization results

        Returns
        -------
        TreatmentEffectsResults instance or tuple (ATE, POM0, POM1)

        See Also
        --------
        TreatmentEffectsResults
        rd   Nre   r   rg   r   userrj   T)r   r2   rk   rn   r\   nm  maxiterdispstart_paramsinv_weightsoptim_method
optim_argsr   IPWr   rn   )rm   r   rl   r   rq   r   rr   rs   r3   r_   r:   r   r   r   eyer%   r   )rC   return_resultsrn   r   r   r   r   r2   res_ipwgmmr   res_gmmress                r   ipwzTreatmentEffect.ipw  sp   : %~5  EE^++ELL888HELLbj11 	B E!LL@AAA%td%HHH 	N eT0$T#/1 1 1~wrr{'+':'A'C D D''|&(fS->->&?&?'+15t%D%D"#	    %T7E2>2>' ' ' 
r   z        )params_returnsc                    t          j        t          | j                            }d|| j                                         d<   |dk    rd}nC|dv r|}d}n:|dv rd|z
  }d}n.t          |t           j                  r|}d}nt          d          | j        }|||	                                z  }nd}| j
                            |          |z  	                                }| j                            |          |z  	                                }	|s|	|z
  ||	fS | j        j        }
t          |
| j        d| |	          }t          j        |	|z
  |g| j
        j        | j        j        f          }|                    |t          j        t          |                    d
d|dd          }t+          | |d||          }|S )z
        Regression Adjustment treatment effect estimation.
        
%(params_returns)s
        See Also
        --------
        TreatmentEffectsResults
        r   Nrd   re   rg   r   r   rj   )rk   r2   r   r   r   r   r   r   )r   r   r%   rl   r   rq   rr   rs   r   r   r   r+   r   rm   r   r   r:   r   r   r   r   r   )rC   r   rn   r   r   r2   r*   cwpom0pom1r   mod_gmmr   r   r   s                  r   rt   zTreatmentEffect.ra  s
    xDN++,,'(dn  """##$5  EE^++ELL888HELLbj11 	B E!LL@AAA  %**,,&BBB%%d++b06688%%d++b06688 	+$;d**% 3T$& & &~D[$M M 	'" # #
 ++<*,&\1B1B*C*C+/594)H)H&'	      %T7E2>2>' ' ' 
r   c                 Z   | j         }| j        }| j        }| j        j        }| j        j        d||dk             z
  z                                  |z  }| j        j        ||dk             z                                  |z  }| j        	                    |          
                                |z   }	| j        	                    |          
                                |z   }
|
|	z
  }|s||	|
fS | j        j        }t          j        ||	g          }t          || j        d|           }t          j        || j        j        | j        j        | j        j        f          }|                    |t          j        t)          |                    dd|dd          }t+          | |d	|d
          }|S )z
        ATE and POM from double robust augmented inverse probability weighting
        
%(params_returns)s
        See Also
        --------
        TreatmentEffectsResults

        r   r   Nrk   r   r   r   r   r   rd   r   )r   r   rl   rm   r*   r   r/   r   r   r+   r   r   r   ro   r|   r:   r   r   r   r   r%   r   )rC   r   r   r   r   r   r*   r   r   r   r   r   r   p2_aipw	mag_aipw1r   r   r   s                     r   aipwzTreatmentEffect.aipw,  s    y~#M'1tDAI+>?DDFFMM'4	?;@@BBTI&&t,,1133h>&&t,,1133h>vo 	'&&%*c6]++UD$7DIII	~M $-"6&'( ) ) --%s<0011#'66     %T7E2>27' ' ' 
r   c                    | j         }| j        }| j        j        }| j        j        }| j        }| j        }||z  ||z  dz
  z  }	t          ||         ||         |	|                   }
|
                    d          }|	                    |          
                                }d|z
  d|z
  z  d|z
  d|z
  z  dz
  z  }t          ||          ||          ||                    }|                    d          }|	                    |          
                                }|| _        || _        |j        d||dk             z
  z                                  |z  }|j        ||dk             z                                  |z  }||z   }||z   }||z
  }|s|||fS t          j        ||g                                          }t%          || j        d|           }t          j        ||j        |j        | j        j        f          }|                    |t          j        t/          |                    dd	|d
d          }t1          | |d|d          }|S )ah  
        ATE and POM from double robust augmented inverse probability weighting.

        This uses weighted outcome regression, while `aipw` uses unweighted
        outcome regression.
        Option for effect on treated or on untreated is not available.
        
%(params_returns)s
        See Also
        --------
        TreatmentEffectsResults

        r   weightsHC1r   r   Nr   r   r   r   r   r   rd   r   )r   r   rm   r   r*   rl   r   r   r   r+   r   results_ipwwls0results_ipwwls1r/   r   r   ro   squeezer   r:   r   r   r   r%   r   )rC   r   r   r   r   r   r*   r   r   r   r   result1
mean1_ipw2r   r   result0
mean0_ipw2r   r   r   r   r   p2_aipw_wlsr   r   r   r   s                              r   aipw_wlszTreatmentEffect.aipw_wlsX  s    y%#~_
TkTD[1_-5$d:&6z?, , ,((E(**__T**//11
4xAH%!d(q4x)@1)DE5*%tZK'8
{+- - -((E(**__T**//11
&&MQdai%89>>@@4GMT$!)_5::<<tCh&h&vo 	'&&j#v//7799 eT%8$#') ) )~NN&	'( ) )
 ++%s<0011#'66    %T7E2>27' ' ' 
r   c                    | j         }| j        j        }| j        j        }| j        }||          }||         }	|dk    rdd|z
  z  }
d|	z  }|}nN|dv r|d|z
  z  }
|	|	z  }||         }d}n2|dv rd|z
  d|z
  z  }
d|	z
  |	z  }||          }d}nt          d          t          ||          ||          |
          }|                    d	          }|                    |          	                                }t          ||         ||         |          }|                    d	          }|                    |          	                                }|s||z
  ||fS t          || j        d
| |          }t          j        ||z
  |g|j        |j        t          j        | j        j                  f          }|                    |t          j        t#          |                    dd|dd          }t%          | |d||          }|S )z
        ATE and POM from inverse probability weighted regression adjustment.

        
%(params_returns)s
        See Also
        --------
        TreatmentEffectsResults

        rd   r   re   rg   r   rj   r   r   r   Nr   r   i  r   r   r   r   )r   rm   r   r*   r   rs   r   r   r+   r   r   r:   r   r   r   ro   r   r%   r   )rC   r   rn   r   r   r   r*   r   r   r   r   r   exogtr   r   mean0_ipwrar   r   mean1_ipwrar   r   r   r   s                          r   ipw_razTreatmentEffect.ipw_ra  s    _
%#j[!Z 5  a%iBUBEE^++!e)$BB$ELL888e)E	*Be)u$B*%ELL@AAA5*%tZK'8  ((E(**ooe,,11335$d:&6  ((E(**ooe,,1133 	G,k;FF E4#64)57 7 7~;&4NNJt*122	'   ++%s<0011#'66    %T7E2>2>' ' ' 
r   )Nr   )r   )Trd   F)TF)rR   rS   rT   rU   r9   classmethodr   r   r   r   doc_params_returnsrt   doc_params_returns2r   r   r   rz   r   r   r   r   ]  sm       " "HN N N N> " " " ["D D D DL \(:G!D!DEEE9 9 9 FE9v \(;W!E!EFFF) ) ) GF)V \(;W!E!EFFFA A A GFAF \(:G!D!DEEEF F F FEF F Fr   r   )T)NNN)TN) rU   numpyr   statsmodels.compat.pandasr   scipy.linalgr   #statsmodels.regression.linear_modelr   "statsmodels.sandbox.regression.gmmr   statsmodels.stats.contrastr   statsmodels.tools.docstringr   r   r   r"   r'   r0   r3   r5   rY   r_   r|   r   r   r   r   r  r  r   rz   r   r   <module>r     s   :     2 2 2 2 2 2 # # # # # # 3 3 3 3 3 3 2 2 2 2 2 2 6 6 6 6 6 6 . . . . . ." " " ""M M M M          L L L L(9 9 9 9 9S 9 9 9x    S   B- - - - -n - - -`? ? ? ? ?~ ? ? ?DG G G G G. G G GT% % % % %^ % % %PH H H H H H H HV#/ #/ #/ #/ #/_ #/ #/ #/L 0  F F F F F F F F F Fr   