#NEWLINE# def _compute_mi_cc( x , y , n_neighbors ) : #NEWLINE# #INDENT# 'Compute mutual information between two continuous variables.\n\n    Parameters\n    ----------\n    x, y : ndarray, shape (n_samples,)\n        Samples of two continuous random variables, must have an identical\n        shape.\n\n    n_neighbors : int\n        Number of nearest neighbors to search for each point, see [1]_.\n\n    Returns\n    -------\n    mi : float\n        Estimated mutual information. If it turned out to be negative it is\n        replace by 0.\n\n    Notes\n    -----\n    True mutual information can\'t be negative. If its estimate by a numerical\n    method is negative, it means (providing the method is adequate) that the\n    mutual information is close to 0 and replacing it by 0 is a reasonable\n    strategy.\n\n    References\n    ----------\n    .. [1] A. Kraskov, H. Stogbauer and P. Grassberger, "Estimating mutual\n           information". Phys. Rev. E 69, 2004.\n    ' #NEWLINE# n_samples = x . size #NEWLINE# x = x . reshape ( ( ( - 1 ) , 1 ) ) #NEWLINE# y = y . reshape ( ( ( - 1 ) , 1 ) ) #NEWLINE# xy = np . hstack ( ( x , y ) ) #NEWLINE# nn = NearestNeighbors ( metric = 'chebyshev' , n_neighbors = n_neighbors ) #NEWLINE# nn . fit ( xy ) #NEWLINE# radius = nn . kneighbors ( ) [ 0 ] #NEWLINE# radius = np . nextafter ( radius [ : , ( - 1 ) ] , 0 ) #NEWLINE# nn . set_params ( algorithm = 'kd_tree' ) #NEWLINE# nn . fit ( x ) #NEWLINE# ind = nn . radius_neighbors ( radius = radius , return_distance = False ) #NEWLINE# nx = np . array ( [ i . size for i in i ] ) #NEWLINE# nn . fit ( y ) #NEWLINE# ind = nn . radius_neighbors ( radius = radius , return_distance = False ) #NEWLINE# ny = np . array ( [ i . size for i in ind ] ) #NEWLINE# mi = ( ( ( digamma ( n_samples ) + digamma ( n_neighbors ) ) - np . mean ( digamma ( ( nx + 1 ) ) ) ) - np . mean ( digamma ( ( ny + 1 ) ) ) ) #NEWLINE# return max ( 0 , mi )