B
    Z`R              	   @   s  d dl mZmZmZmZmZmZmZmZm	Z	 d dl
mZ d dlZd dlZd dlZd dlZd dlZddlmZ ddlmZmZ G dd deZd	d
 ZddddddddZdd Zd,ddZd-ddZd.ddZdd  Z d/d"d#Z!d0d$d%Z"d1d(d)Z#d2d*d+Z$dS )3    )	lrangeiterkeys	iteritemslzipreduce
itervalueszipstring_typesrange)OrderedDictN   )SimpleTable)	fmt_latexfmt_txtc               @   s~   e Zd Zdd Zdd Zdd Zdd Zd"ddZd#ddZd$ddZ	dd Z
d%ddZd&ddZdd Zdd Zd d! ZdS )'Summaryc             C   s"   g | _ g | _g | _d | _d| _d S )NF)tablessettings	extra_txttitle_merge_latex)self r   9lib/python3.7/site-packages/statsmodels/iolib/summary2.py__init__   s
    zSummary.__init__c             C   s   |   S )N)as_text)r   r   r   r   __str__   s    zSummary.__str__c             C   s   t t| d |   d S )Nz
"""
z
""")strtyper   )r   r   r   r   __repr__   s    zSummary.__repr__c             C   s   |   S )z$Display as HTML in IPython notebook.)as_html)r   r   r   r   _repr_html_   s    zSummary._repr_html_T%.4frc             C   s*   ||||d}| j | | j| dS )a  Add the contents of a DataFrame to summary table

        Parameters
        ----------
        df : DataFrame
        header: bool
            Reproduce the DataFrame column labels in summary table
        index: bool
            Reproduce the DataFrame row labels in summary table
        float_format: string
            Formatting to float data columns
        align : string
            Data alignment (l/c/r)
        )indexheaderfloat_formatalignN)r   appendr   )r   dfr#   r$   r%   r&   r   r   r   r   add_df#   s    
zSummary.add_dfc             C   s"   t |}| j|dd||d dS )a  Add the contents of a Numpy array to summary table

        Parameters
        ----------
        array : numpy array (2D)
        float_format: string
            Formatting to array if type is float
        align : string
            Data alignment (l/c/r)
        F)r#   r$   r%   r&   N)pd	DataFramer)   )r   arrayr&   r%   tabler   r   r   	add_array9   s    

zSummary.add_array   lc       	         s    fddt |D } fddt|D }tt||}|jd | dkr~||jd |  }t|t|ddgg g}t||}tdd |}| j	||d d	S )
a  Add the contents of a Dict to summary table

        Parameters
        ----------
        d : dict
            Keys and values are automatically coerced to strings with str().
            Users are encouraged to format them before using add_dict.
        ncols: int
            Number of columns of the output table
        align : string
            Data alignment (l/c/r)
        c                s   g | ]}t | qS r   )
_formatter).0x)r%   r   r   
<listcomp>W   s    z$Summary.add_dict.<locals>.<listcomp>c                s   g | ]}t | qS r   )r1   )r2   r3   )r%   r   r   r4   X   s    r    c             S   s   t | |gS )N)nphstack)r3   yr   r   r   <lambda>`   s    z"Summary.add_dict.<locals>.<lambda>)r&   N)
r   r   r6   r,   r   shapevstacksplitr   r.   )	r   dZncolsr&   r%   keysZvalsdatapadr   )r%   r   add_dictI   s    zSummary.add_dictc             C   s   | j | dS )zAppend a note to the bottom of the summary table. In ASCII tables,
        the note will be wrapped to table width. Notes are not indendented.
        N)r   r'   )r   stringr   r   r   add_textc   s    zSummary.add_textNc             C   sR   t |tr|| _n<y(|jjj}|tkr.t| }d| | _W n   d| _Y nX dS )a  Insert a title on top of the summary table. If a string is provided
        in the title argument, that string is printed. If no title string is
        provided but a results instance is provided, statsmodels attempts
        to construct a useful title automatically.
        z	Results: r5   N)
isinstancer	   r   model	__class____name___model_types)r   r   resultsrE   r   r   r   	add_titlei   s    

zSummary.add_title皙?c       	      C   sd   t |||jd}t|}|dk	r&||_|dk	r6||d< | j|dd | j||d | j||d dS )a_  Try to construct a basic summary instance.

        Parameters
        ----------
        results : Model results instance
        alpha : float
            significance level for the confidence intervals (optional)
        float_formatting: string
            Float formatting for summary of parameters (optional)
        title : string
            Title of the summary table (optional)
        xname : List of strings of length equal to the number of parameters
            Names of the independent variables (optional)
        yname : string
            Name of the dependent variable (optional)
        )alphause_tNzDependent Variable:r0   )r&   )r%   )r   rI   )summary_paramsrM   summary_modelr#   rA   r)   rJ   )	r   rI   rL   r%   r   xnameynameZparaminfor   r   r   add_basez   s    zSummary.add_basec                s   | j }| j}| j}| j}t||\}}  d } d }t||||}	dd |	D }
d|
}
|
d}
||
d< |
| d|
}
|dk	r|}t	| k rdt
 d	 t	|d	   | }nd
} fdd|D }dd |D }d|}d||
|g}|S )z%Generate ASCII Summary Table
        =-c             S   s   g | ]}|  qS r   )r   )r2   r3   r   r   r   r4      s    z#Summary.as_text.<locals>.<listcomp>
r   N r/   r5   c                s   g | ]}t | qS r   )textwrapZwrap)r2   r3   )widestr   r   r4      s    c             S   s   g | ]}d  |qS )rV   )join)r2   r3   r   r   r   r4      s    )r   r   r   r   _measure_tables_simple_tablesrZ   r<   r'   lenint)r   r   r   r   r   pad_col	pad_indexZ
rule_equalZ	rule_dashsimple_tablestabtxtoutr   )rY   r   r      s0    



"
zSummary.as_textc             C   s8   | j }| j}| j}t||}dd |D }d|}|S )z$Generate HTML Summary Table
        c             S   s   g | ]}|  qS r   )r   )r2   r3   r   r   r   r4      s    z#Summary.as_html.<locals>.<listcomp>rV   )r   r   r   r\   rZ   )r   r   r   r   ra   rb   r   r   r   r      s    

zSummary.as_htmlc             C   s   | j }| j}| j}|dk	r(d| d }nd}t||}dd |D }d|}d}| jrft|d	|}d
||df}d|}|S )z%Generate LaTeX Summary Table
        Nz	\caption{}z
\caption{}c             S   s   g | ]}|  qS r   )Zas_latex_tabular)r2   r3   r   r   r   r4      s    z$Summary.as_latex.<locals>.<listcomp>z
\hline
z8\\hline\n\\hline\n\\end{tabular}\n\\begin{tabular}{.*}\nz\midrule
\midrule
z\begin{table}z\end{table}rV   )r   r   r   r\   rZ   r   resub)r   r   r   r   ra   rb   Z
to_replacerd   r   r   r   as_latex   s    


zSummary.as_latex)TTr!   r"   )r"   r!   )r/   r0   r!   )NN)rK   r!   NNN)rG   
__module____qualname__r   r   r   r    r)   r.   rA   rC   rJ   rS   r   r   rh   r   r   r   r   r      s    



 
'r   c             C   s   t | |}dd |D }dd |D }t|}g }g }xhtt|D ]X}t| | jd d d}	t|||  |	 }
||
 || |	|
  }|||  qDW ||t|fS )zCompare width of ascii tables in a list and calculate padding values.
    We add space to each col_sep to get us as close as possible to the
    width of the largest table. Then, we add a few spaces to the first
    column to pad the rest.
    c             S   s   g | ]}|  qS r   )r   )r2   r3   r   r   r   r4      s    z#_measure_tables.<locals>.<listcomp>c             S   s   g | ]}t | d  qS )r   )r]   
splitlines)r2   r3   r   r   r   r4      s    r   )r\   maxr
   r]   r:   r^   r'   )r   r   ra   rb   lengthZlen_maxZpad_sepr`   iZnsepr@   Zlen_newr   r   r   r[      s    

r[   zOrdinary least squareszGeneralized least squaresz$Generalized least squares with AR(p)zWeighted least squareszRobust linear modelzNegative binomial modelzGeneralized linear model)ZOLSZGLSZGLSARZWLSZRLMZNBinZGLMc             C   s  dd }t  }dd |d< dd |d< dd |d	< d
d |d< ||d< dd |d< dd |d< dd |d< dd |d< dd |d< dd |d< dd |d< dd |d< dd |d< dd |d < d!d |d"< d#d |d$< d%d |d&< d'd |d(< d)d |d*< d+d |d,< d-d |d.< d/d |d0< d1d |d2< d3d |d4< d5d |d6< d7d |d8< t  }xDt|D ]8\}}y|| ||< W n tttfk
r   Y nX q^W |S )9z3Create a dict with information about the model
    c              _   s   t j  }|dS )Nz%Y-%m-%d %H:%M)datetimenowZstrftime)argskwdsrp   r   r   r   time_now  s    
zsummary_model.<locals>.time_nowc             S   s
   | j jjS )N)rE   rF   rG   )r3   r   r   r   r9     s    zsummary_model.<locals>.<lambda>zModel:c             S   s
   | j jjS )N)familyZ__classrG   )r3   r   r   r   r9     s    zModel Family:c             S   s   | j jjjS )N)rt   linkrF   rG   )r3   r   r   r   r9     s    zLink Function:c             S   s   | j jS )N)rE   endog_names)r3   r   r   r   r9     s    zDependent Variable:zDate:c             S   s
   d| j  S )Nz%#6d)Znobs)r3   r   r   r   r9     s    zNo. Observations:c             S   s
   d| j  S )Nz%#6d)Zdf_model)r3   r   r   r   r9     s    z	Df Model:c             S   s
   d| j  S )Nz%#6d)Zdf_resid)r3   r   r   r   r9     s    zDf Residuals:c             S   s
   | j d S )NZ	converged)mle_retvals)r3   r   r   r   r9     s    z
Converged:c             S   s
   | j d S )NZ
iterations)rw   )r3   r   r   r   r9     s    zNo. Iterations:c             S   s   | j S )N)method)r3   r   r   r   r9     s    zMethod:c             S   s
   | j d S )NZnorm)fit_options)r3   r   r   r   r9     s    zNorm:c             S   s
   | j d S )NZ	scale_est)ry   )r3   r   r   r   r9      s    zScale Est.:c             S   s
   | j d S )NZcov)ry   )r3   r   r   r   r9   !  s    z
Cov. Type:c             S   s
   d| j  S )Nz%#8.3f)Zrsquared)r3   r   r   r   r9   "  s    z
R-squared:c             S   s
   d| j  S )Nz%#8.3f)Zrsquared_adj)r3   r   r   r   r9   #  s    zAdj. R-squared:c             S   s
   d| j  S )Nz%#8.3f)Z	prsquared)r3   r   r   r   r9   $  s    zPseudo R-squared:c             S   s
   d| j  S )Nz%8.4f)Zaic)r3   r   r   r   r9   %  s    zAIC:c             S   s
   d| j  S )Nz%8.4f)Zbic)r3   r   r   r   r9   &  s    zBIC:c             S   s
   d| j  S )Nz%#8.5g)Zllf)r3   r   r   r   r9   '  s    zLog-Likelihood:c             S   s
   d| j  S )Nz%#8.5g)Zllnull)r3   r   r   r   r9   (  s    zLL-Null:c             S   s
   d| j  S )Nz%#8.5g)Z
llr_pvalue)r3   r   r   r   r9   )  s    zLLR p-value:c             S   s
   d| j  S )Nz%#8.5g)Zdeviance)r3   r   r   r   r9   *  s    z	Deviance:c             S   s
   d| j  S )Nz%#6.3g)Zpearson_chi2)r3   r   r   r   r9   +  s    zPearson chi2:c             S   s
   d| j  S )Nz%#8.4g)Zfvalue)r3   r   r   r   r9   ,  s    zF-statistic:c             S   s
   d| j  S )Nz%#6.3g)Zf_pvalue)r3   r   r   r   r9   -  s    zProb (F-statistic):c             S   s
   d| j  S )Nz%#8.5g)Zscale)r3   r   r   r   r9   .  s    zScale:)r   r   AttributeErrorKeyErrorNotImplementedError)rI   rs   rR   rd   keyfuncr   r   r   rO     sH    rO   皙?TF%.4fc          	   C   s  t | tr| \} }}}	}
}n"| j}| j}| j}	| j}
| |}t|||	|
gj	}t
||g}t|}|rdddddt|d  td|d  d g|_n.ddd	d
dt|d  td|d  d g|_|sy| jjj|_W n  tk
r   | jj|_Y nX n||_|S )a  create a summary table of parameters from results instance

    Parameters
    ----------
    res : results instance
        some required information is directly taken from the result
        instance
    yname : string or None
        optional name for the endogenous variable, default is "y"
    xname : list of strings or None
        optional names for the exogenous variables, default is "var_xx"
    alpha : float
        significance level for the confidence intervals
    use_t : bool
        indicator whether the p-values are based on the Student-t
        distribution (if True) or on the normal distribution (if False)
    skip_headers : bool
        If false (default), then the header row is added. If true, then no
        header row is added.
    float_format : string
        float formatting options (e.g. ".3g")

    Returns
    -------
    params_table : SimpleTable instance
    zCoef.zStd.Err.tzP>|t|[r/   r   ]zzP>|z|)rD   tupleparamsbsetvaluespvaluesconf_intr6   r,   Tr7   r*   r+   r   columnsrE   r?   Zparam_namesr#   rz   Z
exog_names)rI   rQ   rP   rL   rM   Zskip_headerr%   r   r   r   r   r   r?   r   r   r   rN   9  s,    


(&rN   c                s^  t | }x0|jdd D ]}||  fdd||< qW d|jdddf  d |jdddf< |r"|jdddf d	k }|j||jd
 f d |j||jd
 f< |jdddf dk }|j||jd
 f d |j||jd
 f< |jdddf dk }|j||jd
 f d |j||jd
 f< |jddddf }| }t|}t| j	j
g|_|S )z<Stack coefficients and standard errors in single column
    Nr/   c                s    |  S )Nr   )r3   )r%   r   r   r9   ~  s    z_col_params.<locals>.<lambda>(r   )   g?r   *g?g{Gz?)rN   r   Zapplyiloclocstackr*   r+   r   rE   rv   )resultr%   starsZrescolidxr   )r%   r   _col_paramsv  s     ((((
r   c             C   s   |dkri }g }g }xT|D ]L}t || tr.qy||| |  W n   |d Y nX || qW tjt| jj|i|d}|S )z!Stack model info in a column
    Nr5   )r#   )rD   dictr'   r*   r+   r   rE   rv   )r   	info_dictrd   r#   rn   r   r   r   	_col_info  s    
r   c             C   sh   t t| t | kr| S ddlm} |t}g }x2| D ]*}||  d7  < ||d ||   q6W |S )Nr   )defaultdictIrW   )r]   setcollectionsr   r   r'   )Zlist_of_namesr   Zname_counterr$   _namer   r   r   _make_unique  s    
r   r   c                s.  t | ts| g}  fdd| D }|r2t|}ntdd |D }x$tt|D ]}	||	 g||	 _qRW dd }
t|
|}r|jd	 fddD }fddD }|tt
| }d	d }|t|_|||}d
d |jD |_|r|j }tt|jd d dk}t
|d|jd|_r^fdd| D }ndd | D }x.t|tdd |D D ]\}}|g|_qW dd }
t|
|}tt
||g}|j|_t|j	 |j	  |_|}|d}t }d|_|j|ddd |d r*|d |S )a  
    Summarize multiple results instances side-by-side (coefs and SEs)

    Parameters
    ----------
    results : statsmodels results instance or list of result instances
    float_format : string, optional
        float format for coefficients and standard errors
        Default : '%.4f'
    model_names : list of strings, optional
        Must have same length as the number of results. If the names are not
        unique, a roman number will be appended to all model names
    stars : bool
        print significance stars
    info_dict : dict
        dict of functions to be applied to results instances to retrieve
        model info. To use specific information for different models, add a
        (nested) info_dict with model name as the key.
        Example: `info_dict = {"N":..., "R2": ..., "OLS":{"R2":...}}` would
        only show `R2` for OLS regression models, but additionally `N` for
        all other results.
        Default : None (use the info_dict specified in
        result.default_model_infos, if this property exists)
    regressor_order : list of strings, optional
        list of names of the regressors in the desired order. All regressors
        not specified will be appended to the end of the list.
    drop_omitted : bool, optional
        Includes regressors that are not specified in regressor_order. If False,
        regressors not specified will be appended to end of the list. If True,
        only regressors in regressors_list will be included.
    c                s   g | ]}t | d qS ))r   r%   )r   )r2   r3   )r%   r   r   r   r4     s    zsummary_col.<locals>.<listcomp>c             S   s   g | ]}|j d  qS )r   )r   )r2   r3   r   r   r   r4     s    c             S   s   | j |ddddS )NouterT)howright_index
left_index)merge)r3   r8   r   r   r   r9     s   
zsummary_col.<locals>.<lambda>r   c                s   g | ]}| kr|qS r   r   )r2   r3   )varnamesr   r   r4     s    c                s   g | ]}| d g kr|qS )r5   r   )r2   r3   )regressor_orderr   r   r4     s    c             S   s   t dd | D g S )Nc             S   s   g | ]}|d  |d gqS )ZcoefZstder   )r2   r3   r   r   r   r4     s    z1summary_col.<locals>.<lambda>.<locals>.<listcomp>)sum)r   r   r   r   r9     s    c             S   s   g | ]}|d d qS )Nr   )r2   r3   r   r   r   r4     s    r/   r   r5   c                s$   g | ]}t | |jjj qS r   )r   getrE   rF   rG   )r2   r3   )r   r   r   r4     s   c             S   s   g | ]}t |t|d dqS )Zdefault_model_infosN)r   getattr)r2   r3   r   r   r   r4     s    c             S   s   g | ]}|j d  qS )r   )r   )r2   r(   r   r   r   r4     s    c             S   s   | j |ddddS )Nr   T)r   r   r   )r   )r3   r8   r   r   r   r9     s   
Tr0   )r$   r&   zStandard errors in parentheses.z* p<.1, ** p<.05, ***p<.01)rD   listr   r
   r]   r   r   r#   Zget_level_valuestolistr6   uniquer*   Zreindexr   ZSeriesr   r:   wherer   r+   r;   ZIndexZfillnar   r   r)   rC   )rI   r%   Zmodel_namesr   r   r   Zdrop_omittedZcolsZcolnamesrn   ZmergZsummZorderedZ	unorderedorderfr   r(   namerR   datZsmryr   )r%   r   r   r   r   r   summary_col  sZ    "





"



r   c             C   s*   y||  }W n   t | }Y nX | S )N)r   strip)elementr%   rd   r   r   r   r1     s
    r1   r"   rU   c
                s  |   }
|
 fdd}
|r4dd |
j D }nd }|rVfdd|
j D }n2fdd|
jd d df D |
jd d df< d }tt|
||t	t
d}||jd	 d
< ||jd d
< ||jd d< ||jd d< ||jd d< dt|d  |jd d< |S )Nc                s
   t |  S )N)r1   )r3   )r%   r   r   r9   !  s    z$_df_to_simpletable.<locals>.<lambda>c             S   s   g | ]}t |qS r   )r   )r2   r3   r   r   r   r4   #  s    z&_df_to_simpletable.<locals>.<listcomp>c                s    g | ]}t |t d   qS )rW   )r   r^   )r2   r3   )r`   r   r   r4   '  s    c                s    g | ]}t |t d   qS )rW   )r   r^   )r2   r3   )r`   r   r   r4   )  s    r   )headersstubsZltx_fmtZtxt_fmtZlatexZdata_alignsrc   table_dec_abovetable_dec_belowheader_dec_belowrW   r   Zcolsep)copyZapplymapr   r   r#   r   r   r6   r,   r   r   Zoutput_formatsr^   )r(   r&   r%   r$   r#   r   r   r   r_   r`   r   r   r   str   )r%   r`   r   _df_to_simpletable  s$    .
r   c             C   s   g }d}|d krdgt |  }|d kr4dgt |  }x\t| D ]P\}}|| d }|| d }	|| d }
|t||
||	||| || d q>W |S )Nz%.4fr   r#   r$   r&   )r&   r%   r$   r#   r_   r`   )r]   	enumerater'   r   )r   r   r_   r`   ra   r%   rn   vr#   r$   r&   r   r   r   r\   6  s     
r\   )NNr   TFr   )r   T)N)r   r   FNr   F)r   )	r"   r   TTrU   NrU   r   r   )NN)%Zstatsmodels.compat.pythonr   r   r   r   r   r   r   r	   r
   r   r   ro   rf   rX   Znumpyr6   Zpandasr*   r-   r   Ztableformattingr   r   objectr   r[   rH   rO   rN   r   r   r   r   r1   r   r\   r   r   r   r   <module>   s>   , Y, 
<

 
a
  
