U
    Kvf7                     @   s   d Z ddl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mZmZ ddlmZmZ G dd	 d	eZd
d Zi Zdd ed< dd ed< dd ed< ejed< eed< dddZdd ZdddZG dd deZdS ) aM  
Quantile regression model

Model parameters are estimated using iterated reweighted least squares. The
asymptotic covariance matrix estimated using kernel density estimation.

Author: Vincent Arel-Bundock
License: BSD-3
Created: 2013-03-19

The original IRLS function was written for Matlab by Shapour Mohammadi,
University of Tehran, 2008 (shmohammadi@gmail.com), with some lines based on
code written by James P. Lesage in Applied Econometrics Using MATLAB(1999).PP.
73-4.  Translated to python with permission from original author by Christian
Prinoth (christian at prinoth dot name).
    N)pinv)norm)cache_readonly)RegressionModelRegressionResultsRegressionResultsWrapper)ConvergenceWarningIterationLimitWarningc                       s2   e Zd ZdZ fddZdd ZdddZ  ZS )QuantRega  Quantile Regression

    Estimate a quantile regression model using iterative reweighted least
    squares.

    Parameters
    ----------
    endog : array or dataframe
        endogenous/response variable
    exog : array or dataframe
        exogenous/explanatory variable(s)

    Notes
    -----
    The Least Absolute Deviation (LAD) estimator is a special case where
    quantile is set to 0.5 (q argument of the fit method).

    The asymptotic covariance matrix is estimated following the procedure in
    Greene (2008, p.407-408), using either the logistic or gaussian kernels
    (kernel argument of the fit method).

    References
    ----------
    General:

    * Birkes, D. and Y. Dodge(1993). Alternative Methods of Regression, John Wiley and Sons.
    * Green,W. H. (2008). Econometric Analysis. Sixth Edition. International Student Edition.
    * Koenker, R. (2005). Quantile Regression. New York: Cambridge University Press.
    * LeSage, J. P.(1999). Applied Econometrics Using MATLAB,

    Kernels (used by the fit method):

    * Green (2008) Table 14.2

    Bandwidth selection (used by the fit method):

    * Bofinger, E. (1975). Estimation of a density function using order statistics. Australian Journal of Statistics 17: 1-17.
    * Chamberlain, G. (1994). Quantile regression, censoring, and the structure of wages. In Advances in Econometrics, Vol. 1: Sixth World Congress, ed. C. A. Sims, 171-209. Cambridge: Cambridge University Press.
    * Hall, P., and S. Sheather. (1988). On the distribution of the Studentized quantile. Journal of the Royal Statistical Society, Series B 50: 381-391.

    Keywords: Least Absolute Deviation(LAD) Regression, Quantile Regression,
    Regression, Robust Estimation.
    c                    s$   |  | tt| j||f| d S N)Z_check_kwargssuperr
   __init__)selfendogexogkwargs	__class__ N/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/regression/quantile_regression.pyr   M   s    
zQuantReg.__init__c                 C   s   |S )zE
        QuantReg model whitener does nothing: returns data.
        r   )r   datar   r   r   whitenQ   s    zQuantReg.whiten      ?robustepa	hsheather  ư>c           !      K   s  |dks|dkrt ddddddg}||krBt d	d
| nt| }|dkrXt}n$|dkrft}n|dkrtt}nt d| j}	| j}
| j}t	j
| j}|| _t| j| j | _| j| j | _d}|
}t	|
jd }d}d}tg g d}||k rH||krH|sH|d7 }|}t	|j|
}t	|j|	}t	t||}|	t	|
| }t	|dk }|| dkd d d ||< t	|dk || d| | }t	|}|
|ddt	jf  }t	t	|| }|d | |d t	||  |dkr|d dkrtddD ]4}t	||d |  krd}t dt!  qqq||krjt dt"| d t# |	t	|
| }t$%|dt$%|d }|||}t&t	'|	|d t()|| t()||   }d ||  t	*|||  }|d!krZt	|dk|| d d| | d }tt	|
j|
}t	|
j|t	jddf  |
}|| | }n>|d"krd | d | d|  tt	|
j|
 }nt d#t+| ||d$} || _,|| _-d | | _.|| _/|| _0t1| S )%a  
        Solve by Iterative Weighted Least Squares

        Parameters
        ----------
        q : float
            Quantile must be strictly between 0 and 1
        vcov : str, method used to calculate the variance-covariance matrix
            of the parameters. Default is ``robust``:

            - robust : heteroskedasticity robust standard errors (as suggested
              in Greene 6th edition)
            - iid : iid errors (as in Stata 12)

        kernel : str, kernel to use in the kernel density estimation for the
            asymptotic covariance matrix:

            - epa: Epanechnikov
            - cos: Cosine
            - gau: Gaussian
            - par: Parzene

        bandwidth : str, Bandwidth selection method in kernel density
            estimation for asymptotic covariance estimate (full
            references in QuantReg docstring):

            - hsheather: Hall-Sheather (1988)
            - bofinger: Bofinger (1975)
            - chamberlain: Chamberlain (1994)
        r      z"q must be strictly between 0 and 1biwcosr   gauparzkernel must be one of z, r   bofingerchamberlainz;bandwidth must be in 'hsheather', 'bofinger', 'chamberlain'
   F)paramsmser      Nr&   r'   i,  d   TzConvergence cycle detectedzMaximum number of iterations (z
) reached.K      gq=
ףp?      ?r   Ziidzvcov must be 'robust' or 'iid')Znormalized_cov_params)2	Exceptionjoinkernelshall_sheatherr#   r$   r   r   nobsnpZlinalgZmatrix_rankZrankfloatZ
k_constantZdf_modelZdf_residZonesshapedictdotTr   abswhereZnewaxismaxappendZmeanrangeallwarningswarnr   strr	   statsscoreatpercentileminZstdr   ppfsumQuantRegResultsqZ
iterationssparsity	bandwidthhistoryr   )!r   rG   ZvcovkernelrI   Zmax_iterZp_tolr   Z
kern_namesr   r   r1   Z	exog_rankZn_iterZxstarbetaZdiffcyclerJ   Zbeta0ZxtxZxtyresidmaskiieZiqrehZfhat0dZxtxiZxtdxZlfitr   r   r   fitW   s    !





& 
,
zQuantReg.fit)r   r   r   r   r   r   )__name__
__module____qualname____doc__r   r   rT   __classcell__r   r   r   r   r
       s   ,    r
   c              	   C   sb   t t | dkdd| d   dt | d   ddt |  d  d }d|t | dk< |S )	Nr   gUUUUUU?g       @r(      r   g      @r   r2   r9   r8   )uzr   r   r   _parzen   s
    0r^   c                 C   s,   dd| d  d  t t | dkdd S )Ng      ?r   r(   r   r[   r\   r   r   r   <lambda>       r`   r   c                 C   s,   t t | dkdt dt j |   dS )Nr   r   r(   r   )r2   r9   r8   r    pir_   r   r   r   r`      ra   r    c                 C   s(   dd| d   t t | dkdd S )Ng      ?r   r(   r   r[   r_   r   r   r   r`      ra   r   r!   r"   皙?c                 C   sZ   t |}dt |d  }d|d  d }| d t d|d  d  || d  }|S )Ng      ?g       @r,   gUUUUUUտgUUUUUU?gUUUUUU?)r   rD   pdf)nrG   alphar]   numdenrR   r   r   r   r0      s
    
*r0   c                 C   sN   dt dt | d  }dt |d  d d }| d || d  }|S )Ng      @r(      r   gɿg?)r   rd   rD   )re   rG   rg   rh   rR   r   r   r   r#      s    r#   c                 C   s(   t d|d  t|d|  |   S )Nr   r(   )r   rD   r2   sqrt)re   rG   rf   r   r   r   r$     s    r$   c                   @   s   e Zd ZdZedd Zdd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zd&d$d%Zd"S )'rF   z'Results instance for the QuantReg modelc                 C   s   | j }| jj}| j}t|dk d| | || }t|}|t||d  }t|dk d| | || }t|}dt	|t	|  S )Nr   r   r)   )
rG   modelr   rN   r2   r9   r8   rA   rB   rE   )r   rG   r   rQ   Zeredr   r   r   	prsquared  s    

zQuantRegResults.prsquaredc                 C   s   dS )Nr,   r   r   r   r   r   scale  s    zQuantRegResults.scalec                 C   s   t jS r   r2   nanrm   r   r   r   bic  s    zQuantRegResults.bicc                 C   s   t jS r   ro   rm   r   r   r   aic   s    zQuantRegResults.aicc                 C   s   t jS r   ro   rm   r   r   r   llf$  s    zQuantRegResults.llfc                 C   s   t jS r   ro   rm   r   r   r   rsquared(  s    zQuantRegResults.rsquaredc                 C   s   t jS r   ro   rm   r   r   r   rsquared_adj,  s    zQuantRegResults.rsquared_adjc                 C   s   t jS r   ro   rm   r   r   r   r'   0  s    zQuantRegResults.msec                 C   s   t jS r   ro   rm   r   r   r   	mse_model4  s    zQuantRegResults.mse_modelc                 C   s   t jS r   ro   rm   r   r   r   	mse_total8  s    zQuantRegResults.mse_totalc                 C   s   t jS r   ro   rm   r   r   r   centered_tss<  s    zQuantRegResults.centered_tssc                 C   s   t jS r   ro   rm   r   r   r   uncentered_tss@  s    zQuantRegResults.uncentered_tssc                 C   s   t d S r   NotImplementedErrorrm   r   r   r   HC0_seD  s    zQuantRegResults.HC0_sec                 C   s   t d S r   rz   rm   r   r   r   HC1_seH  s    zQuantRegResults.HC1_sec                 C   s   t d S r   rz   rm   r   r   r   HC2_seL  s    zQuantRegResults.HC2_sec                 C   s   t d S r   rz   rm   r   r   r   HC3_seP  s    zQuantRegResults.HC3_seNrc   c                 C   s<  | j }| j}ddddgfddg}dd| j gfd	d| j gfd
d| j gfdddg}|dkrn| jjjd d }ddlm	}	 |	 }
|
j
| |||||d |
j| |||| jd g }|d dk rd}|d7 }|d7 }|d7 }||d  }|| n8|dkr(d}|d7 }|d7 }|d7 }|| }|| |r8|
| |
S ) a[  Summarize the Regression Results

        Parameters
        ----------
        yname : str, optional
            Default is `y`
        xname : list[str], optional
            Names for the exogenous variables. Default is `var_##` for ## in
            the number of regressors. Must match the number of parameters
            in the model
        title : str, optional
            Title for the top table. If not None, then this replaces the
            default title
        alpha : float
            significance level for the confidence intervals

        Returns
        -------
        smry : Summary instance
            this holds the summary tables and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary.Summary : class to hold summary results
        )zDep. Variable:N)zModel:NzMethod:zLeast Squares)zDate:N)zTime:NzPseudo R-squared:z%#8.4gz
Bandwidth:z	Sparsity:)zNo. Observations:N)zDf Residuals:N)z	Df Model:NN zRegression Resultsr   )Summary)ZgleftZgrightynamexnametitle)r   r   rf   use_tg|=z6The smallest eigenvalue is %6.3g. This might indicate zthat there are
z5strong multicollinearity problems or that the design zmatrix is singular.r   z1The condition number is large, %6.3g. This might zindicate that there are
z,strong multicollinearity or other numerical z	problems.)Z	eigenvalsZcondition_numberrl   rI   rH   rk   r   rU   Zstatsmodels.iolib.summaryr   Zadd_table_2colsZadd_table_paramsr   r;   Zadd_extra_txt)r   r   r   r   rf   ZeigvalsZcondnoZtop_leftZ	top_rightr   ZsmryZetextZwstrr   r   r   summaryT  sZ    
  


zQuantRegResults.summary)NNNrc   )rU   rV   rW   rX   r   rl   rn   rq   rr   rs   rt   ru   r'   rv   rw   rx   ry   r|   r}   r~   r   r   r   r   r   r   rF   	  sB   














rF   )rc   )rc   )rX   Znumpyr2   r>   Zscipy.statsrA   Znumpy.linalgr   r   Zstatsmodels.tools.decoratorsr   Z#statsmodels.regression.linear_modelr   r   r   Zstatsmodels.tools.sm_exceptionsr   r	   r
   r^   r/   rd   r0   r#   r$   rF   r   r   r   r   <module>   s*    C


