Histcounts not giving me the same answers as my histogram

32 views (last 30 days)
Hi all, I plotted a histogram, and normalized it as a probability. I wanted to get those probabilities through code instead of looking at the bins manually, but when I use histcounts it doesn't seem to go correctly. I just get 100% probabilities. I need to keep the bins separate as individual histograms. I'm trying to create a mapping from delta_h (also referred to as EFH) to IFH through MC simulation
%% Monte Carlo Simulations for IFH
n = 100000;
g = 32.2; %ft/s^2
Cd = 0.94;
af = [0.01 0.95 0.99]; %Flow Areas
time = 3600; % Time duration
l_int = 25; %Length of room [ft]
w_int = 25; %Width of room [ft]
A_int = l_int*w_int;
% Randomly generate flood heights for each flood height discretization bin
delta_h = [rand(n,1)*3, rand(n,1)+3, rand(n,1)+4, rand(n,1)+5, rand(n,1)+6, rand(n,1)+7, rand(n,1)+8, rand(n,1)+9, rand(n,1)+10, rand(n,1)+11, rand(n,1)+12, rand(n,1)+13, rand(n,1)+14, (rand(n,1)*5)+15] ;
figure
histogram(delta_h,'BinEdges',[0 3],'Normalization','probability') %Histogram for EFH bin 1
hold on
for i=3:14
histogram(delta_h,'BinEdges',i:(i+1),'Normalization','probability') %Histogram for next set of bins
end
histogram(delta_h,'BinEdges',[15 20],'Normalization','probability') % Histogram for last bin
title('Histogram of EFH')
xlabel('External Flood Height (ft)')
ylabel('Probability')
grid on
% Converting EFH to IFH
IFH_cf=[];
IFH_pf=[];
IFH_nf=[];
for i = 1:14
IFH_cf = [IFH_cf (Cd.*sqrt(2.*g.*delta_h(:,i)).*af(3))*time/A_int]; % Take the rows, make it a column and stack next to what we have
IFH_pf = [IFH_pf (Cd.*sqrt(2.*g.*delta_h(:,i)).*af(2))*time/A_int];
IFH_nf = [IFH_nf (Cd.*sqrt(2.*g.*delta_h(:,i)).*af(1))*time/A_int];
end
% Replacing all IFH that are greater than EFH with the maximum EFH.
IFH_cf = min(IFH_cf,delta_h);
IFH_pf = min(IFH_pf,delta_h);
IFH_nf = min(IFH_nf,delta_h);
%% Plotting Histogram of IFH Separately
figure
histogram(IFH_cf,'BinEdges',[0 3],'Normalization','probability') %Histogram for EFH 0 to 3
hold on
for i=3:14
histogram(IFH_cf,'BinEdges',i:(i+1),'Normalization','probability')
end
histogram(IFH_cf,'BinEdges',[15 20],'Normalization','probability')
title('Histogram of IFH for Complete Failure')
xlabel('Internal Flood Height (ft)')
ylabel('Probability')
grid on
figure
histogram(IFH_pf,'BinEdges',[0 3],'Normalization','probability') %Histogram for EFH 0 to 3
hold on
for i=3:14
histogram(IFH_pf,'BinEdges',i:(i+1),'Normalization','probability')
end
histogram(IFH_pf,'BinEdges',[15 20],'Normalization','probability')
title('Histogram of IFH for Partial Failure')
xlabel('Internal Flood Height (ft)')
ylabel('Probability')
grid on
figure
histogram(IFH_nf,'BinEdges',[0 3],'Normalization','probability') %Histogram for EFH 0 to 3
hold on
for i=3:14
histogram(IFH_nf,'BinEdges',i:(i+1),'Normalization','probability')
end
histogram(IFH_nf,'BinEdges',[15 20],'Normalization','probability')
title('Histogram of IFH for No Failure')
xlabel('Internal Flood Height (ft)')
ylabel('Probability')
grid on
%% Getting Histcount
IFH_cf_histcount = [];
IFH_pf_histcount = [];
IFH_nf_histcount = [];
h_int_edges = [0 3 4 5 6 7 8 9 10 11 12 13 14 15 20];
for i = 1:14
IFH_cf_histcount = [IFH_cf_histcount; histcounts(IFH_cf(:,i),'BinEdges',h_int_edges,'Normalization','probability')];
IFH_pf_histcount = [IFH_pf_histcount; histcounts(IFH_pf(:,i),'BinEdges',h_int_edges,'Normalization','probability')];
IFH_nf_histcount = [IFH_nf_histcount; histcounts(IFH_nf(:,i),'BinEdges',h_int_edges,'Normalization','probability')];
end

Accepted Answer

Image Analyst
Image Analyst on 9 Apr 2022
BinEdges should be a whole vector of lots of numbers, not just two numbers. If you have just two numbers you only get one bin inbetween the two numbers, not a bunch of bins.
Maybe
edges = 15 : 0.5 : 20;
or something like that. histcounts() and histogram() will give the same counts if they both use the same edges.
  4 Comments
Joy Shen
Joy Shen on 10 Apr 2022
Ah right, you're saying that the bins shouldn't be in increments of 1? I guess I'm having conceptual difficulty. I'm trying to find the probability of IFH_cf being 0 to 3, 3 to 4 etc. If I do the bins as 0:0.5:3 which histcount value should I take? The sum is 1. Shouldn't the normalization in the histcount make sure that they don't all add to 1?
Sorry I'm pretty new to Matlab!
Image Analyst
Image Analyst on 10 Apr 2022
You can set the bin edges to whatever you want. The sum will be the number of elements in your array that you're taking the histogram of. If you want bins from minus infinity to 0, 0 to 3, 3 to 4, 5 to 5, and 5 to infinity you can do
edges = [-inf, 0, 3, 4, 5, inf];
[counts, edges2] = histcounts(data, edges);
If you want to normalize so that the sum is 1 you can do
[counts, edges2] = histcounts(data, edges, 'Normalization', 'probability');
See the documentation for other normalization options.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!