U
    Ãmœdp  ã                
   @   s2  d Z ddlZddlmZ ddlmZ ddlm  m	Z	 d,dd„Z
d-dd	„Zd.d
d„Zedk�r.ddlmZ ddgd ZdZdZe dej e¡ ¡Zejedd�ZedkrÆded  eej e¡  Zedkræde eej e¡  Zedƒ ee ee¡d ƒ ede ee¡ƒ ede ee¡ƒ ede ee¡ƒ ej eedd�\Z!Z"Z#ej$ee"d�\Z%Z&ej$ee#d�\Z'Z(ede	j)e%d e e'd e de!d e  ej*d�ƒ ede
eeƒƒ ed e
eed!d"�ƒ eeeddd"�\Z+\Z,Z-Z.Z/Z0Z1ed#e+ƒ ed$e1 2¡ ƒ eeed%dd"�\Z+\Z,Z-Z.Z/Z0Z1ed%ƒ ed#e+ƒ ed$e1 2¡ ƒ e e¡Z3e e¡Z4e3ed e 5dd&d'd(d)dg¡  6e7¡ Z8e4ed e 5dd&d'd(d)dg¡  6e7¡ Z9eeee8e9fdd"�\Z+\Z,Z-Z.Z/Z0Z1ed*ƒ ed#e+ƒ ed$e1 2¡ ƒ dZ:e:�r.ddl;m<Z= e= >eed+¡ e ?ee¡ @¡ ZAe= >eeAjB¡ dS )/aæ  using multivariate dependence and divergence measures

The standard correlation coefficient measures only linear dependence between
random variables.
kendall's tau measures any monotonic relationship also non-linear.

mutual information measures any kind of dependence, but does not distinguish
between positive and negative relationship


mutualinfo_kde and mutualinfo_binning follow Khan et al. 2007

Shiraj Khan, Sharba Bandyopadhyay, Auroop R. Ganguly, Sunil Saigal,
David J. Erickson, III, Vladimir Protopopescu, and George Ostrouchov,
Relative performance of mutual information estimation methods for
quantifying the dependence among short and noisy data,
Phys. Rev. E 76, 026209 (2007)
http://pre.aps.org/abstract/PRE/v76/i2/e026209


é    N)Ústats)Úgaussian_kdeTc                 C   s¸   t |ƒ}t | ƒ|kstdƒ‚t |t¡}t | t¡} t | |f¡}t|ƒ|ƒ}t| ƒ| ƒ}t|ƒ|ƒ}t |¡t |¡ t |¡ }| ¡ | }	|r°t 	dt 
d|	 ¡ ¡}
|
S |	S dS )úCmutual information of two random variables estimated with kde

    ú+both data arrays need to have the same sizeç      ð?éþÿÿÿN)ÚlenÚ
ValueErrorÚnpÚasarrayÚfloatZvstackr   ÚlogÚsumÚsqrtÚexp)ÚyÚxÚnormedÚnobsZyxÚkde_xÚkde_yZkde_yxÚmi_obsÚmiÚ	mi_normed© r   úf/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/sandbox/distributions/mv_measures.pyÚmutualinfo_kde   s    r   c           	      C   s˜   t |ƒ}t |t¡}t | t¡} t|jƒ|jƒ}t| jƒ|jƒ}t |¡t |¡ }t |ƒ|krhtdƒ‚| ¡ }|r�t 	dt 
d| ¡ ¡}|S |S dS )r   zWrong number of observationsr   r   N)r   r
   r   r   r   ÚTr   r	   Zmeanr   r   )	r   r   r   r   r   r   r   r   r   r   r   r   Úmutualinfo_kde_2sample4   s    r   c                 C   s¾  t |ƒ}t | ƒ|kstdƒ‚t |t¡}t | t¡} |dkr¼t | ¡}t |¡}t d| ¡}t ddd| ¡}|d |  t	¡}	dt 
|j¡ }
|
d  d8  < ||	 |
 }||	 |
 }n,t |¡dkrÔ|}|}nt |ƒd	krè|\}}tj||d
�\}}tj| |d
�\}}tj| |||fd
�\}}}|d | }|d | }|d | }|t |d ¡t |¡dd…df  t |¡  }| ¡ }|�r¶t dt d| ¡ ¡}||||||f|fS |S dS )a0  mutual information of two random variables estimated with kde



    Notes
    -----
    bins='auto' selects the number of bins so that approximately 5 observations
    are expected to be in each bin under the assumption of independence. This
    follows roughly the description in Kahn et al. 2007

    r   Úautog      @r   é   r   g�íµ ÷Æ°>g�íµ ÷ÆÀ>é   ©Úbinsg»½×Ùß|Û=Nr   )r   r	   r
   r   r   Úsortr   ZlinspaceÚastypeÚintZonesÚshapeÚsizeÚ	histogramÚhistogram2dr   r   r   )r   r   r#   r   r   ÚysÚxsZqbin_sqrÚ	quantilesZquantile_indexÚshiftÚbinsyÚbinsxZfxÚfyZfyxÚpyxÚpxÚpyr   r   r   r   r   r   Úmutualinfo_binnedJ   s@    

2r5   Ú__main__ZlinearZ	quadraticr    éÈ   r!   é   )ÚprependZcorrelation)r   r    ÚpearsonrÚ	spearmanrÚ
kendalltaué   r"   Ú
mutualinfor   gVçž¯Ò<)Zlogbasezmutualinfo_kde normedzmutualinfo_kde       F)r   zmutualinfo_binned normedzmutualinfo_binned       r   g      Ð?gš™™™™™Ù?g333333ã?g      è?r-   Úo)T)T)T)CÚ__doc__Únumpyr
   Zscipyr   Zscipy.statsr   Zstatsmodels.sandbox.infotheoZsandboxZinfotheor   r   r5   Ú__name__Zstatsmodels.apiÚapiÚsmZfuntyper   Úsigr$   ÚrandomZrandnr   Zadd_constantZexogr   ÚprintZcorrcoefr:   r;   r<   r*   Zpxyr0   r/   r)   r3   Zbinsx_r4   Zbinsy_r>   Úer   Zpyx2Úpy2Zpx2Zbinsy2Zbinsx2r   r   r+   r,   Úarrayr%   r&   ZbyZbxZdoplotZmatplotlib.pyplotZpyplotZpltZplotZOLSÚfitZolsresZfittedvaluesr   r   r   r   Ú<module>   sp   


B
 ÿÿ
ÿ


((ÿ
