I have a 2D-histogram for two vectors, s1 and s2, generated using the hist3 function in Matlab:
[hist2D, binC] = hist3([s1' s2']);
I am normalizing it by making its total volume equal to unity, in the following manner:
L = binC{1}(2) - binC{1}(1);
B = binC{2}(2) - binC{2}(1);
totalVolume = sum(sum(hist2D.*L*B));
prob2D = hist2D/totalVolume;
Question: Is this correct way to normalize a 2D-histogram?
I have also normalized the 1D-histograms for s1 and s2, as shown below.
[hist1, binCentres1] = hist(s1);
binWidth1 = binCentres1(2) - binCentres1(1);
prob1 = hist1 / (sum(hist1) * binWidth1);
%same for s2
Question: How do I get the marginal (1D-)histograms from the normalized 2D-histogram?
I have tried doing this in the following manner:
prob1M = sum(prob2D, 2); %extract marginal for s1
prob2M = sum(prob2D, 1); %extract marginal for s2
If I were doing this correctly, I'd expect prob1 to be equal to prob1M. I seem to be on the right track because the bar graphs (below) look similar, but scaled on the vertical axis. Maybe I'm doing the normalizations wrong?
I tried normalizing prob1M as well, using:
prob1M = prob1M / (sum(prob1M) * binWidth1);
Question: Is normalization still necessary if you get the marginals from a normalized 2D-histogram? Why / Why not?
After normalization, the area of prob1 and prob1M is equal to 1, where area is calculated as:
area = sum(binWidth1.*prob1)
%same for prob1M
However, prob1 and prob1M (and prob2 and prob2M) are still slightly different:
prob1 = 0.9412 0.4412 0.3235 0.3235 0.2941 0.3235 0.3235 0.4118 0.4706 1.1471
prob1M = 0.9706 0.4706 0.3824 0.3235 0.3235 0.3235 0.3530 0.3824 0.4412 1.0295
Thank you for your suggestions.