|
| 1 | +from collections import defaultdict |
| 2 | +def compute_distribution(v): |
| 3 | + """ |
| 4 | + v: vector de valores enteros |
| 5 | + devuelve un diccionario con la probabilidad de cada valor |
| 6 | + computado como la frecuencia de ocurrencia |
| 7 | + """ |
| 8 | + d= defaultdict(int) |
| 9 | + for e in v: d[e]+=1 |
| 10 | + s= float(sum(d.values())) |
| 11 | + return dict((k, v/s) for k, v in d.items()) |
| 12 | +def entropy(y): |
| 13 | + """ |
| 14 | + Computa la entropia de un vector discreto |
| 15 | + """ |
| 16 | + # P(Y) |
| 17 | + Py= compute_distribution(y) |
| 18 | + res=0.0 |
| 19 | + for k, v in Py.items(): |
| 20 | + res+=v*np.log2(v) |
| 21 | + return -res |
| 22 | + |
| 23 | +def conditional_entropy(x, y): |
| 24 | + """ |
| 25 | + x: vector de numeros reales |
| 26 | + y: vector de numeros enteros |
| 27 | + devuelve H(Y|X) |
| 28 | + """ |
| 29 | + # discretizacion de X |
| 30 | + #print(int(x.size/10)) |
| 31 | + # https://stats.stackexchange.com/questions/179674/number-of-bins-when-computing-mutual-information#:~:text=3%20Answers&text=There%20is%20no%20best%20number,on%20histograms%20have%20been%20proposed. |
| 32 | + # Choosing number of bins |
| 33 | + #hx, bx= histogram(x, bins=int(x.size/10), density=True) |
| 34 | + hx, bx= np.histogram(x, bins=int(np.sqrt(x.size/5)),density=True) |
| 35 | + |
| 36 | + Py= compute_distribution(y) |
| 37 | + Px= compute_distribution(np.digitize(x,bx)) |
| 38 | + |
| 39 | + res= 0 |
| 40 | + for ey in set(y): |
| 41 | + # P(X | Y) |
| 42 | + x1= x[y==ey] |
| 43 | + condPxy= compute_distribution(np.digitize(x1,bx)) |
| 44 | + |
| 45 | + for k, v in condPxy.items(): |
| 46 | + res+= (v*Py[ey]*(np.log2(Px[k]) - np.log2(v*Py[ey]))) |
| 47 | + return res |
| 48 | + |
| 49 | +def mutual_information(x,y): |
| 50 | + return entropy(y) - conditional_entropy(x,y) |
| 51 | + |
| 52 | +def mutual_information_ufunc(x, y, dim='time'): |
| 53 | + return xr.apply_ufunc( |
| 54 | + mutual_information, |
| 55 | + x, |
| 56 | + y, |
| 57 | + input_core_dims=[[dim], [dim]], |
| 58 | + dask="parallelized", |
| 59 | + output_dtypes=[float], |
| 60 | + ) |
| 61 | + |
| 62 | + mi = mutual_information_ufunc(x.chunk({'time': -1}),y.chunk({'time': -1})) |
0 commit comments