
    M/Php              
       l   d Z ddlZddlmZ ddlmZ ddlmc m	Z	 d&dZ
d&dZd&dZed	k    rddlmZ d
dgd         ZdZdZ ej        dej                            e          z            Z ej        ed          Zedk    r&dedz  z   eej                            e          z  z   Zed
k    r#dez   eej                            e          z  z   Z ed            e ej        ee          d                     ed ej        ee                      ed ej        ee                      ed ej        ee                      ej         eed          \  Z!Z"Z# ej$        ee"          \  Z%Z& ej$        ee#          \  Z'Z( ed e	j)        e%dz  ez  e'dz  ez  de!dz  ez  z   ej*                              ed e
ee                      ed e
eed                      eeedd          \  Z+\  Z,Z-Z.Z/Z0Z1 ed e+            ed!e12                                            eeed"d          \  Z+\  Z,Z-Z.Z/Z0Z1 ed"            ed e+            ed!e12                                            ej        e          Z3 ej        e          Z4e3edz
   ej5        g d#          z  6                    e7                   Z8e4edz
   ej5        g d#          z  6                    e7                   Z9 eeee8e9fd          \  Z+\  Z,Z-Z.Z/Z0Z1 ed$            ed e+            ed!e12                                           dZ:e:rUddl;m<Z=  e=j>        eed%            ej?        ee          @                                ZA e=j>        eeAjB                   dS dS 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                    t          |          }t          |           |k    st          d          t          j        |t                    }t          j        | t                    } t          j        | |f          } t          |          |          } t          |           |           } t          |          |          }t          j        |          t          j        |          z
  t          j        |          z
  }|                                |z  }	|r.t          j	        dt          j
        d|	z            z
            }
|
S |	S )Cmutual information of two random variables estimated with kde

    +both data arrays need to have the same size      ?)len
ValueErrornpasarrayfloatvstackr   logsumsqrtexp)yxnormednobsyxkde_xkde_ykde_yxmi_obsmi	mi_normeds              m/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/distributions/mv_measures.pymutualinfo_kder       s    q66Dq66T>>FGGG

1eA

1eA	Aa5		BLOOAELOOAE\"b!!FVF^^bfUmm+bfUmm;F		B GBR011		    c                 2   t          |          }t          j        |t                    }t          j        | t                    }  t	          |j                  |j                  } t	          | j                  |j                  }t          j        |          t          j        |          z
  }t          |          |k    rt          d          |                                }|r.t          j	        dt          j
        d|z            z
            }|S |S )r   zWrong number of observationsr   r	   )r
   r   r   r   r   Tr   r   meanr   r   )	r   r   r   r   r   r   r   r   r   s	            r   mutualinfo_kde_2sampler%   4   s     q66D

1eA

1eALac""ELac""E VE]]RVE]]*F
6{{d7888	B GBR011		r!   c                    t          |          }t          |           |k    st          d          t          j        |t                    }t          j        | t                    } |dk    rt          j        |           }t          j        |          }t          j        d|z            }t          j        ddd|z            }|dz
  |z                      t                    }	dt          j
        |j                  z   }
|
dxx         dz  cc<   ||	         |
z   }||	         |
z   }n5t          j        |          dk    r|}|}nt          |          d	k    r|\  }}t          j        ||
          \  }}t          j        | |
          \  }}t          j        | |||f
          \  }}}|dz  |z  }|dz  |z  }|dz  |z  }|t          j        |dz             t          j        |          dddf         z
  t          j        |          z
  z  }|                                }|r6t          j        dt          j        d|z            z
            }||||||f|fS |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   linspaceastypeintonesshapesize	histogramhistogram2dr   r   r   )r   r   r+   r   r   ysxsqbin_sqr	quantilesquantile_indexshiftbinsybinsxfxfyfyxpyxpxpyr   r   r   s                         r   mutualinfo_binnedrC   J   sJ    q66Dq66T>>FGGG

1eA

1eA v~~WQZZWQZZ72d7##K1bk22	69,44S99rwy///aF>"U*>"U*	!		
d))q..u QU+++IBQU+++IBq!5%.AAAC
(T/C	b4B	b4B BF3u9%%r

111T6(::RVBZZGHF	B GBR011	3Bu5v==	r!   __main__linear	quadraticr(      r)      )prependcorrelation)r   r(   pearsonr	spearmanr
kendalltau   r*   
mutualinfor   gV瞯<)logbasezmutualinfo_kde normedzmutualinfo_kde       F)r   zmutualinfo_binned normedzmutualinfo_binned       r'   )r   g      ?g?g333333?g      ?r(   r8   o)T)C__doc__numpyr   scipyr   scipy.statsr   statsmodels.sandbox.infotheosandboxinfotheor    r%   rC   __name__statsmodels.apiapismfuntyper   sigr,   randomrandnr   add_constantexogr   printcorrcoefrK   rL   rM   r4   pxyr<   r;   r3   rA   binsx_rB   binsy_rO   er   pyx2py2px2binsy2binsx2r   r   r5   r6   arrayr.   r/   bybxdoplotmatplotlib.pyplotpyplotpltplotOLSfitolsresfittedvalues r!   r   <module>r{      s,   ,           $ $ $ $ $ $ / / / / / / / / /   ,   ,? ? ? ?D z      %a(GD
C")//$'''((A2?1d+++D+1HsRY__T2222(EC")//$////	E-	E+"+a

4
 !!!	E*nenQq))***	E+uq++,,,	E,((1--...&q333Cae,,,JBae,,,JB	E,++BrE$J2d
,1#b&+,=rtM M M N N N 
E
!>>!A#6#6777	E
!>>!Ae#D#D#DEEE  Aq666 8I/c3	E
$i000	E
$fjjll333 ! Avd;;; 8I/c3	E&MMM	E
$i000	E
$fjjll333	B	B	d1fhbh;;;<<<DDSII	JB	d1fhbh;;;<<<DDSII	JB  A2wt<<< 8I/c3	E+	E
$i000	E
$fjjll333F )''''''As4$$&&F'(((((o f) )r!   