Let me start by restating that this is a terrible MI estimation procedure. Please don't use this in any serious business, unless you are absolutely sure you know what you are doing.
You are making two critical mistakes in your codeYou are making two critical mistakes in your code:
Your scale doesn't match because you didn't do the integration properly. Mutual information is defined as in the paper is for probability, not probability density. But your kernel density estimator gives you back probability density which integrates to one, but does not sum to one on the points on a grid. If you want to use the sum formula as in the paper, you need to convert the density back to probability.
Your scale doesn't match because you didn't do the integration properly. Mutual information is defined as in the paper is for probability, not probability density. But your kernel density estimator gives you back probability density which integrates to one, but does not sum to one on the points on a grid. If you want to use the sum formula as in the paper, you need to convert the density back to probability.You are adding the probabilities only at the points where your data live, and counting multiple times on those regions. You should be integrating over the support of the entire distribution! An easy way to do this would be to make a uniform grid to integrate over. I think you should carefully review the definition of mutual information. (You need to add the probabilities, not the point wise MI estimates over the data.)
You are adding the probabilities only at the points where your data live, and counting multiple times on those regions. You should be integrating over the support of the entire distribution! An easy way to do this would be to make a uniform grid to integrate over. I think you should carefully review the definition of mutual information. (You need to add the probabilities, not the point wise MI estimates over the data.)
This is not a code-review site, but a couple of small things on your code.
- never use reserved matlab function names as variables:
sum
is one of them - You don't need
inv
, so don't use it. Use\
and/
instead - try not to use
i
as a looping variable.i
is for imaginary numbers in MATLAB (so isj
) - hint: MATLAB has a KDE for 1D which you can verify your code against:
ksdensity
EDIT: I misinterpreted the code. I was wrong. OP is replacing expectation with sum over samples, so there's no problem using density nor having a grid.