U
    ÃmœdMT  ã                   @   s<  d dl mZmZ d dlmZ d dl mZ d dlmZmZ d dl	Z
d dlZerZd dlmZ ndd„ 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 d dlmZmZmZmZ dZe
  e¡d Z!dd„ Z"d?dd„Z#d@dd„Z$dAdd„Z%dBdd„Z&dCdd„Z'dDdd„Z(e#e
j)e
j*e
j+e
j,e$e
j-e(e%e'e&dœZ.d d!„ Z/d"d#„ Z0dEd$d%„Z1d&Z2d'Z3d(d)„ e3D ƒZ4e2e5e4ƒ Z6G d*d+„ d+ƒZ7ee7j8ƒZ9e9 :d,edd-d.gƒ¡ e9 :d/g ¡ e9 :d0d1gd2gfd3gd4gfg¡ ee;e9ƒƒdFd5d5d6d7ed8d9œee
j<ej=ej>f ee; e?e?e@e?eeeAe@f  e?ej>d:œ	d;d<„ƒZBG d=d>„ d>ƒZCdS )Gé    )ÚAppenderÚis_numeric_dtype)ÚSP_LT_19)ÚPD_LT_2)ÚSequenceÚUnionN©Úis_categorical_dtypec                 C   s   t | tjƒS ©N)Ú
isinstanceÚpdZCategoricalDtype©Údtype© r   ú[/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/stats/descriptivestats.pyr	      s    r	   )Ústats)ÚSimpleTable)Újarque_bera)Úcache_readonly)Ú	DocstringÚ	Parameter)Ú
array_likeÚ	bool_likeÚ
float_likeÚint_like)	é   é   é
   é   é2   éK   éZ   é_   éc   g      Y@c                 C   s   |   ¡ |  ¡  S r
   )ÚmaxÚmin©Údfr   r   r   Úpd_ptp    s    r(   c                 C   s   dt  | ¡ j|d�S )Nr   ©Úaxis)ÚnpÚisnanÚsum)Úxr*   r   r   r   Únancount$   s    r/   c                 C   s   t j| |d�t j| |d� S ©Nr)   )r+   ÚnanmaxÚnanmin©Zarrr*   r   r   r   Únanptp(   s    r4   c                 C   s   t j| d |d�S )Né   r)   )r+   Znansumr3   r   r   r   Únanuss,   s    r6   c                 C   s   t j| t|d�S r0   )r+   ÚnanpercentileÚPERCENTILESr3   r   r   r   r7   0   s    r7   c                 C   s   t j| |dd�S ©NZomit)r*   Z
nan_policy)r   Úkurtosisr3   r   r   r   Únankurtosis4   s    r;   c                 C   s   t j| |dd�S r9   )r   Úskewr3   r   r   r   Únanskewness8   s    r=   )ZobsÚmeanÚstdr$   r%   ZptpÚvarr<   Zussr:   Úpercentilesc                 C   s.   zt  | ¡}W n tk
r(   tj}Y nX |S )zi
    wrapper for scipy.stats.kurtosis that returns nan instead of raising Error

    missing options
    )r   r:   Ú
ValueErrorr+   Únan©ÚaÚresr   r   r   Ú	_kurtosisK   s
    rG   c                 C   s.   zt  | ¡}W n tk
r(   tj}Y nX |S )ze
    wrapper for scipy.stats.skew that returns nan instead of raising Error

    missing options
    )r   r<   rB   r+   rC   rD   r   r   r   Ú_skewX   s
    rH   c                 C   s†   t  | ¡} t  | |k¡}t  | |k ¡}|| d }zt t||ƒ|| d¡j}W n, tk
r|   t t||ƒ|| d¡}Y nX ||fS )a8  
    Signs test

    Parameters
    ----------
    samp : array_like
        1d array. The sample for which you want to perform the sign test.
    mu0 : float
        See Notes for the definition of the sign test. mu0 is 0 by
        default, but it is common to set it to the median.

    Returns
    -------
    M
    p-value

    Notes
    -----
    The signs test returns

    M = (N(+) - N(-))/2

    where N(+) is the number of values above `mu0`, N(-) is the number of
    values below.  Values equal to `mu0` are discarded.

    The p-value for M is calculated using the binomial distribution
    and can be interpreted the same as for a t-test. The test-statistic
    is distributed Binom(min(N(+), N(-)), n_trials, .5) where n_trials
    equals N(+) + N(-).

    See Also
    --------
    scipy.stats.wilcoxon
    g       @ç      à?)	r+   Úasarrayr-   r   Z	binomtestr%   ZpvalueÚAttributeErrorZ
binom_test)ZsampZmu0ÚposÚnegÚMÚpr   r   r   Ú	sign_teste   s    #
rP   )ÚnobsÚmissingr>   Ústd_errÚcir?   ÚiqrÚ
iqr_normalÚmadÚ
mad_normalÚcoef_varÚranger$   r%   r<   r:   r   ÚmodeÚmedianrA   )rQ   rR   ÚdistinctÚtopÚfreqc                 C   s   g | ]}|t kr|‘qS r   )ÚNUMERIC_STATISTICS©Ú.0Ústatr   r   r   Ú
<listcomp>«   s     rd   c                   @   sà   e Zd ZdZdddgZeZeZe	Z
ddddded	d
œeejejejf ee eeeeeeeef  edœdd„Zejejdœdd„Zeejdœdd„ƒZeejdœdd„ƒZeejdœdd„ƒZedœdd„Zedœdd„ZdS )ÚDescriptiona  
    Extended descriptive statistics for data

    Parameters
    ----------
    data : array_like
        Data to describe. Must be convertible to a pandas DataFrame.
    stats : Sequence[str], optional
        Statistics to include. If not provided the full set of statistics is
        computed. This list may evolve across versions to reflect best
        practices. Supported options are:
        "nobs", "missing", "mean", "std_err", "ci", "ci", "std", "iqr",
        "iqr_normal", "mad", "mad_normal", "coef_var", "range", "max",
        "min", "skew", "kurtosis", "jarque_bera", "mode", "freq",
        "median", "percentiles", "distinct", "top", and "freq". See Notes for
        details.
    numeric : bool, default True
        Whether to include numeric columns in the descriptive statistics.
    categorical : bool, default True
        Whether to include categorical columns in the descriptive statistics.
    alpha : float, default 0.05
        A number between 0 and 1 representing the size used to compute the
        confidence interval, which has coverage 1 - alpha.
    use_t : bool, default False
        Use the Student's t distribution to construct confidence intervals.
    percentiles : sequence[float]
        A distinct sequence of floating point values all between 0 and 100.
        The default percentiles are 1, 5, 10, 25, 50, 75, 90, 95, 99.
    ntop : int, default 5
        The number of top categorical labels to report. Default is

    Attributes
    ----------
    numeric_statistics
        The list of supported statistics for numeric data
    categorical_statistics
        The list of supported statistics for categorical data
    default_statistics
        The default list of statistics

    See Also
    --------
    pandas.DataFrame.describe
        Basic descriptive statistics
    describe
        A simplified version that returns a DataFrame

    Notes
    -----
    The selectable statistics include:

    * "nobs" - Number of observations
    * "missing" - Number of missing observations
    * "mean" - Mean
    * "std_err" - Standard Error of the mean assuming no correlation
    * "ci" - Confidence interval with coverage (1 - alpha) using the normal or
      t. This option creates two entries in any tables: lower_ci and upper_ci.
    * "std" - Standard Deviation
    * "iqr" - Interquartile range
    * "iqr_normal" - Interquartile range relative to a Normal
    * "mad" - Mean absolute deviation
    * "mad_normal" - Mean absolute deviation relative to a Normal
    * "coef_var" - Coefficient of variation
    * "range" - Range between the maximum and the minimum
    * "max" - The maximum
    * "min" - The minimum
    * "skew" - The skewness defined as the standardized 3rd central moment
    * "kurtosis" - The kurtosis defined as the standardized 4th central moment
    * "jarque_bera" - The Jarque-Bera test statistic for normality based on
      the skewness and kurtosis. This option creates two entries, jarque_bera
      and jarque_beta_pval.
    * "mode" - The mode of the data. This option creates two entries in all tables,
      mode and mode_freq which is the empirical frequency of the modal value.
    * "median" - The median of the data.
    * "percentiles" - The percentiles. Values included depend on the input value of
      ``percentiles``.
    * "distinct" - The number of distinct categories in a categorical.
    * "top" - The mode common categories. Labeled top_n for n in 1, 2, ..., ``ntop``.
    * "freq" - The frequency of the common categories. Labeled freq_n for n in 1,
      2, ..., ``ntop``.
    rQ   rR   r]   NTçš™™™™™©?Fr   ©ÚnumericÚcategoricalÚalphaÚuse_trA   Úntop©Údatar   rh   ri   rj   rk   rA   rl   c             	   C   sê  |}	t |tjtjfƒs$t|ddd�}	|	jdkr8t |¡}t|dƒ}t|dƒ}g }
d}|rh|
 tj	¡ d}|r’|
 d¡ ||dkr„d	nd7 }|d7 }|s¢|s¢t
d
ƒ‚t |¡ |
¡| _| jjd dkrÔt
d|› d�ƒ‚dd„ | jjD ƒ| _dd„ | jjD ƒ| _|d k	�r.dd„ |D ƒ}|�r.t
d |¡› d�ƒ‚|d k�r@ttƒnt|ƒ| _t|dƒ| _d| jk| _d| jk| _| j�r¢| jd  k�r–t| jƒk �r¢n nt
dƒ‚ddgddgddgdd„ td| jd ƒD ƒdd„ td| jd ƒD ƒd œ}|D ]H}|| jk�rî| j |¡}| jd |… ||  | j|d d …  | _�qît|d!dd"d#�| _t | j¡| _t | j¡jd | jjd k�r€t
d$ƒ‚t | jd%k¡�s¤t | jdk¡�r¬t
d&ƒ‚t |d'ƒ| _!d|  k �rÐdk �sÚn t
d(ƒ‚t|d)ƒ| _"d S )*Nrn   r5   )Úmaxdimr   rh   ri   Ú Úcategoryzand z4At least one of numeric and categorical must be Truer   z
Selecting z results in an empty DataFramec                 S   s   g | ]}t |ƒ‘qS r   )r   ©rb   Údtr   r   r   rd   /  s     z(Description.__init__.<locals>.<listcomp>c                 S   s   g | ]}t |ƒ‘qS r   r   rr   r   r   r   rd   0  s    c                 S   s   g | ]}|t kr|‘qS r   )ÚDEFAULT_STATISTICSra   r   r   r   rd   5  s      z, z are not known statisticsrl   r^   r_   z"top must be a non-negative integerr[   Ú	mode_freqÚupper_ciÚlower_cir   Újarque_bera_pvalc                 S   s   g | ]}d |› �‘qS ©Ztop_r   ©rb   Úir   r   r   rd   H  s     c                 S   s   g | ]}d |› �‘qS ©Zfreq_r   rz   r   r   r   rd   I  s     )r[   rT   r   r^   r_   rA   Úd)ro   r   zpercentiles must be distinctéd   z.percentiles must be strictly between 0 and 100rj   z&alpha must be strictly between 0 and 1rk   )#r   r   ÚSeriesÚ	DataFramer   Úndimr   Úappendr+   ÚnumberrB   Zselect_dtypesÚ_dataÚshapeZdtypesÚ_is_numericÚ_is_cat_likeÚjoinÚlistrt   Ú_statsr   Ú_ntopÚ_compute_topÚ_compute_freqr-   rZ   ÚindexÚ_percentilesÚsortÚuniqueÚanyr   Ú_alphaÚ_use_t)Úselfrn   r   rh   ri   rj   rk   rA   rl   Zdata_arrÚincludeZ	col_typesZundefZreplacementsÚkeyÚidxr   r   r   Ú__init__	  s�    




ÿ
ÿÿ
ÿÿ,ûÿþÿ   ÿ $zDescription.__init__)r'   Úreturnc                    s   ˆ j ‡ fdd„| jD ƒ S )Nc                    s   g | ]}|ˆ j kr|‘qS r   ©rŽ   )rb   Úsr&   r   r   rd   c  s     
 z(Description._reorder.<locals>.<listcomp>)ÚlocrŠ   )r•   r'   r   r&   r   Ú_reorderb  s    zDescription._reorder)rš   c                 C   sT   | j }| j}|jd dkr|S |jd dkr0|S tj||gdd�}|  || jj ¡S )zœ
        Descriptive statistics for both numeric and categorical data

        Returns
        -------
        DataFrame
            The statistics
        r   r   r)   )rh   ri   r…   r   Úconcatrž   r„   Úcolumns)r•   rh   ri   r'   r   r   r   Úframee  s    
zDescription.framec           "         s¶  ˆj jdd…ˆjf }|j}|j\}}| ¡ }| ¡ }| ¡ }||  ¡  ¡ }| 	¡ }	|	j|dk  |j|dk d   < ˆj
r t |d ¡ dˆjd  ¡}
ntj dˆjd  ¡}
dd„ }| |¡j}|jdk�r^t|tjƒ�rtj|d td	�}tj|d tjd	�}nPg }g }|jD ],}|j| }| |d ¡ | |d ¡ �qt |¡}t |¡}nt d¡ }}|dk}t |jd tj¡}|| |j|  ||< |}zNdd
l m!} | 	¡ }|D ]0}||| j"ƒ�r¼||  #t$¡ %tj¡||< �q¼W n t&k
�r   Y nX |jd dk�r.| 'd¡| 'd¡ }n|}dd„ ‰ |j‡ fdd„dd�j}| 	¡ }tj|j|dk< || }tj(tj)|tjd	�|jd  |d�|jd | ||	||
|	  ||
|	  ||||t*|ƒ| +¡ | ,¡ |d |d |t -tj ddg¡¡ |t .dtj/ ¡ |d |d tj(||d�tj(||d�| 0¡ dœ}‡fdd„| 1¡ D ƒ}tjt2| 3¡ ƒ|t2| 4¡ ƒd�}dˆj5k�r~|S |jd dk�r¦| 'ˆj6d ¡ #t¡}ntjˆj6d td�}t 7t 8d|j ¡d|j k¡�rðdd„ |jD ƒ|_nœd}d}|j} |�r:|d9 }t 8||j ¡}t 7t -|¡dk¡�rþd }�qþt 8||  ¡|d  } d!t9t:|d ƒƒd › d"�}!d#|!› d$�‰‡fd%d„| D ƒ|_ˆj5|j ;¡  ˆ_5ˆ <tj=||gdd&�¡S )'zž
        Descriptive statistics for numeric data

        Returns
        -------
        DataFrame
            The statistics of the numeric columns
        Nr   rI   r   g      ð?r5   c                 S   s˜   t | jtjƒr| jn| jj}|  ¡ j|d�}tr4i nddi}tj|f|Ž}t 	|d ¡rlt
|d ƒ|d fS |d jd dkrŒdd„ |D ƒS tjtjfS )Nr   ZkeepdimsTr   r   c                 S   s   g | ]}t |ƒ‘qS r   )Úfloat©rb   Úvalr   r   r   rd   ™  s     z6Description.numeric.<locals>._mode.<locals>.<listcomp>)r   r   r+   Znumpy_dtypeÚdropnaZto_numpyr   r   r[   Zisscalarr¢   r…   rC   )Zserr   Zser_no_missingÚkwargsZmode_resr   r   r   Ú_mode�  s    z"Description.numeric.<locals>._moder   )Úis_extension_array_dtypeg      è?g      Ð?c                 S   s,   t  | ¡}|jd dk r$t jfd S t|ƒS )Nr   r5   é   )r+   rJ   r…   rC   r   )ÚcrE   r   r   r   Ú_safe_jarque_beraÂ  s    
z.Description.numeric.<locals>._safe_jarque_berac                    s   t ˆ |  ¡ ƒƒS r
   )r‰   r¥   )r.   )r«   r   r   Ú<lambda>É  ó    z%Description.numeric.<locals>.<lambda>Úexpand)Zresult_typer›   é   )rQ   rR   r>   rS   rv   rw   r?   rU   rW   rY   rZ   r$   r%   r<   r:   rV   rX   r   rx   r[   ru   r\   c                    s    i | ]\}}|ˆ j kr||“qS r   ©rŠ   ©rb   ÚkÚv©r•   r   r   Ú
<dictcomp>é  s     
  z'Description.numeric.<locals>.<dictcomp>)r    rŽ   rA   r~   )rŽ   r   c                 S   s   g | ]}t d | ƒ› d�‘qS )r~   ú%)Úint©rb   r˜   r   r   r   rd   ö  s     z'Description.numeric.<locals>.<listcomp>Tr   Fz0.Úfz{0:z}%c                    s   g | ]}ˆ   |¡‘qS r   )Úformatr£   )Úoutputr   r   rd     s     r)   )>r„   r�   r†   r    r…   r?   Úcountr>   ÚabsÚcopyr”   r   ÚtZppfr“   ZnormÚapplyÚTÚsizer   r   r€   r+   rJ   r¢   Úint64rŽ   r‚   Z
atleast_1dÚemptyÚfullrC   Zpandas.api.typesr¨   r   ÚastypeÚobjectÚfillnaÚImportErrorZquantiler   Úonesr(   r$   r%   ÚdiffÚsqrtÚpir\   Úitemsr‰   ÚvaluesÚkeysrŠ   r�   ÚallÚfloorÚlenÚstrÚtolistrž   rŸ   )"r•   r'   ÚcolsÚ_r²   r?   r¼   r>   rW   rS   Úqr§   Zmode_valuesr[   Zmode_countsr˜   r¤   r�   ru   Z_dfr¨   ÚcolrU   ZjbZnan_meanrY   ÚresultsÚfinalÚ
results_dfÚpercZdupeÚscalerŽ   Úfmtr   )r«   r»   r•   r   rh   x  sÎ    

$ 


"
 ÿ ÿ

è
  
ÿ"zDescription.numericc                    s  ˆj jdd…dd„ ˆjD ƒf ‰ ˆ jd }ˆ j}‡ fdd„ˆ D ƒ‰tj‡fdd„ˆD ƒtjd�}i }i }ˆD ]¤}ˆ| }|jd	 ˆj	kr¶|j
dˆj	… ||< t |jdd
… ¡||< qlt|j
ƒ}|dgˆj	t|ƒ  7 }|||< t|ƒ}	|	tjgˆj	t|	ƒ  7 }	t |	¡||< qldd„ tdˆj	d ƒD ƒ}
tj|d|
|d�}dd„ tdˆj	d ƒD ƒ}
tj|d|
|d�}tjtj|tjd�ˆ jd	  |d�ˆ jd	 ˆ  ¡  |dœ}‡fdd„| ¡ D ƒ}tjt| ¡ ƒ|t| ¡ ƒdd�}ˆj�rötj||gd	d�}ˆj�rtj||gd	d�}ˆ |¡S )z¦
        Descriptive statistics for categorical data

        Returns
        -------
        DataFrame
            The statistics of the categorical columns
        Nc                 S   s   g | ]}|‘qS r   r   ©rb   rÙ   r   r   r   rd     s     z+Description.categorical.<locals>.<listcomp>r   c                    s   i | ]}|ˆ | j d d�“qS )T)Ú	normalize)Zvalue_countsrà   r&   r   r   rµ     s      z+Description.categorical.<locals>.<dictcomp>c                    s   i | ]}|ˆ | j d  “qS )r   )r…   rà   )Úvcr   r   rµ     s      r   r   r   c                 S   s   g | ]}d |› �‘qS ry   r   rz   r   r   r   rd   *  s     rÇ   )r   rŽ   r    c                 S   s   g | ]}d |› �‘qS r|   r   rz   r   r   r   rd   ,  s     r›   )rQ   rR   r]   c                    s    i | ]\}}|ˆ j kr||“qS r   r°   r±   r´   r   r   rµ   6  s     
  )r    rŽ   r   r)   )r„   r�   r‡   r…   r    r   r   r+   rÃ   r‹   rŽ   rJ   Zilocr‰   rÓ   rC   rZ   r€   rÊ   r¼   rÎ   rÏ   rÐ   rŒ   rŸ   r�   rž   )r•   r²   rÖ   r]   r^   r_   rÙ   Zsingler¤   Zfreq_valrŽ   Ztop_dfZfreq_dfrÚ   rÛ   rÜ   r   )r'   r•   râ   r   ri   
  sX     
 ÿ
 ÿû

üzDescription.categoricalc              	   C   s�   | j  t¡}| d¡}dd„ |jD ƒ}dd„ |jD ƒ}g }| ¡ D ]\}}| dd„ |D ƒ¡ qBdd„ }t|||dd	d
|dœidgt	|ƒ d�S )z¸
        Summary table of the descriptive statistics

        Returns
        -------
        SimpleTable
            A table instance supporting export to text, csv and LaTeX
        rp   c                 S   s   g | ]}t |ƒ‘qS r   ©rÔ   rà   r   r   r   rd   O  s     z'Description.summary.<locals>.<listcomp>c                 S   s   g | ]}t |ƒ‘qS r   rã   r¸   r   r   r   rd   P  s     c                 S   s   g | ]}|‘qS r   r   )rb   r³   r   r   r   rd   S  s     c                 S   s.   t | tƒr| S | d | kr&tt| ƒƒS | d›S )Nr   z0.4g)r   rÔ   r·   )r³   r   r   r   Ú
_formatterU  s
    
z'Description.summary.<locals>._formatterzDescriptive StatisticsZ	data_fmtsz%s)r   r   r   )ÚheaderÚstubsÚtitleZtxt_fmtZ	datatypes)
r¡   rÆ   rÇ   rÈ   r    rŽ   Ziterrowsr‚   r   rÓ   )r•   r'   rÖ   ræ   rn   r×   Úrowrä   r   r   r   ÚsummaryD  s     	
úzDescription.summaryc                 C   s   t |  ¡  ¡ ƒS r
   )rÔ   ré   Zas_textr´   r   r   r   Ú__str__e  s    zDescription.__str__)N) Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_int_fmtr`   Znumeric_statisticsÚCATEGORICAL_STATISTICSZcategorical_statisticsrt   Zdefault_statisticsr8   r   r+   Úndarrayr   r   r€   r   rÔ   Úboolr¢   r·   r™   rž   r   r¡   rh   ri   r   ré   rê   r   r   r   r   re   ±   sB   R
 ýööY 9!re   ZReturnsr€   zDescriptive statisticsZ
AttributeszSee Also)zpandas.DataFrame.describeNzBasic descriptive statistics)re   Nz;Descriptive statistics class with additional output optionsTrf   Fr   rg   )	rn   r   rh   ri   rj   rk   rA   rl   rš   c             
   C   s   t | |||||||d�jS )Nrg   )re   r¡   rm   r   r   r   Údescribe}  s    ørò   c                   @   s   e Zd ZdZdd„ ZdS )ÚDescribez
    Removed.
    c                 C   s   t dƒ‚d S )NzDescribe has been removed)ÚNotImplementedError)r•   Zdatasetr   r   r   r™   š  s    zDescribe.__init__N)rë   rì   rí   rî   r™   r   r   r   r   ró   •  s   ró   )r   )r   )r   )r   )r   )r   )r   )N)DZstatsmodels.compat.pandasr   r   Zstatsmodels.compat.scipyr   r   Útypingr   r   Únumpyr+   Zpandasr   Zpandas.core.dtypes.commonr	   Zscipyr   Zstatsmodels.iolib.tabler   Zstatsmodels.stats.stattoolsr   Zstatsmodels.tools.decoratorsr   Zstatsmodels.tools.docstringr   r   Zstatsmodels.tools.validationr   r   r   r   r8   ÚarrayZ	QUANTILESr(   r/   r4   r6   r7   r;   r=   ZnanmeanZnanstdr1   r2   ZnanvarÚMISSINGrG   rH   rP   r`   rï   Z_additionalÚtuplert   re   rî   ZdsZreplace_blockrÔ   rð   r   r€   rñ   r¢   r·   rò   ró   r   r   r   r   Ú<module>   sª   





õ
/ÿ   ;
 ÿþþûþ
 þ÷ö