<s> import os <EOL> from matplotlib import pylab <EOL> import numpy as np <EOL> from scipy . stats import norm , entropy <EOL> from utils import CHART_DIR <EOL> def mutual_info ( x , y , bins = <NUM_LIT:10> ) : <EOL> counts_xy , bins_x , bins_y = np . histogram2d ( x , y , bins = ( bins , bins ) ) <EOL> counts_x , bins = np . histogram ( x , bins = bins ) <EOL> counts_y , bins = np . histogram ( y , bins = bins ) <EOL> counts_xy += <NUM_LIT:1> <EOL> counts_x += <NUM_LIT:1> <EOL> counts_y += <NUM_LIT:1> <EOL> P_xy = counts_xy / np . sum ( counts_xy , dtype = float ) <EOL> P_x = counts_x / np . sum ( counts_x , dtype = float ) <EOL> P_y = counts_y / np . sum ( counts_y , dtype = float ) <EOL> I_xy = np . sum ( P_xy * np . log2 ( P_xy / ( P_x . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) * P_y ) ) ) <EOL> return I_xy / ( entropy ( counts_x ) + entropy ( counts_y ) ) <EOL> def plot_entropy ( ) : <EOL> pylab . clf ( ) <EOL> pylab . figure ( num = None , figsize = ( <NUM_LIT:5> , <NUM_LIT:4> ) ) <EOL> title = "<STR_LIT>" <EOL> pylab . title ( title ) <EOL> pylab . xlabel ( "<STR_LIT>" ) <EOL> pylab . ylabel ( "<STR_LIT>" ) <EOL> pylab . xlim ( xmin = <NUM_LIT:0> , xmax = <NUM_LIT> ) <EOL> x = np . arange ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> ) <EOL> y = - x * np . log2 ( x ) - ( <NUM_LIT:1> - x ) * np . log2 ( <NUM_LIT:1> - x ) <EOL> pylab . plot ( x , y ) <EOL> pylab . autoscale ( tight = True ) <EOL> pylab . grid ( True ) <EOL> filename = "<STR_LIT>" <EOL> pylab . savefig ( os . path . join ( CHART_DIR , filename ) , bbox_inches = "<STR_LIT>" ) <EOL> def _plot_mi_func ( x , y ) : <EOL> mi = mutual_info ( x , y ) <EOL> title = "<STR_LIT>" % mi <EOL> pylab . scatter ( x , y ) <EOL> pylab . title ( title ) <EOL> pylab . xlabel ( "<STR_LIT>" ) <EOL> pylab . ylabel ( "<STR_LIT>" ) <EOL> def plot_mi_demo ( ) : <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> pylab . clf ( ) <EOL> pylab . figure ( num = None , figsize = ( <NUM_LIT:8> , <NUM_LIT:8> ) ) <EOL> x = np . arange ( <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT> ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> y = <NUM_LIT:0.5> * x + norm . rvs ( <NUM_LIT:1> , scale = <NUM_LIT> , size = len ( x ) ) <EOL> _plot_mi_func ( x , y ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> y = <NUM_LIT:0.5> * x + norm . rvs ( <NUM_LIT:1> , scale = <NUM_LIT> , size = len ( x ) ) <EOL> _plot_mi_func ( x , y ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> y = <NUM_LIT:0.5> * x + norm . rvs ( <NUM_LIT:1> , scale = <NUM_LIT:1> , size = len ( x ) ) <EOL> _plot_mi_func ( x , y ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> y = norm . rvs ( <NUM_LIT:1> , scale = <NUM_LIT:10> , size = len ( x ) ) <EOL> _plot_mi_func ( x , y ) <EOL> pylab . autoscale ( tight = True ) <EOL> pylab . grid ( True ) <EOL> filename = "<STR_LIT>" <EOL> pylab . savefig ( os . path . join ( CHART_DIR , filename ) , bbox_inches = "<STR_LIT>" ) <EOL> pylab . clf ( ) <EOL> pylab . figure ( num = None , figsize = ( <NUM_LIT:8> , <NUM_LIT:8> ) ) <EOL> x = np . arange ( - <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT> ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> y = <NUM_LIT:0.5> * x ** <NUM_LIT:2> + norm . rvs ( <NUM_LIT:1> , scale = <NUM_LIT> , size = len ( x ) ) <EOL> _plot_mi_func ( x , y ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> y = <NUM_LIT:0.5> * x ** <NUM_LIT:2> + norm . rvs ( <NUM_LIT:1> , scale = <NUM_LIT> , size = len ( x ) ) <EOL> _plot_mi_func ( x , y ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> y = <NUM_LIT:0.5> * x ** <NUM_LIT:2> + norm . rvs ( <NUM_LIT:1> , scale = <NUM_LIT:1> , size = len ( x ) ) <EOL> _plot_mi_func ( x , y ) <EOL> pylab . subplot ( <NUM_LIT> ) <EOL> y = <NUM_LIT:0.5> * x ** <NUM_LIT:2> + norm . rvs ( <NUM_LIT:1> , scale = <NUM_LIT:10> , size = len ( x ) ) <EOL> _plot_mi_func ( x , y ) <EOL> pylab . autoscale ( tight = True ) <EOL> pylab . grid ( True ) <EOL> filename = "<STR_LIT>" <EOL> pylab . savefig ( os . path . join ( CHART_DIR , filename ) , bbox_inches = "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> plot_entropy ( ) <EOL> plot_mi_demo ( ) </s>
