Azzera filtri
Azzera filtri

Histogram plot and Gaussian

17 visualizzazioni (ultimi 30 giorni)
PaVi90
PaVi90 il 25 Feb 2015
Risposto: Ayush Anand il 5 Apr 2024
Hi, I know this question has been quite popular and asked before, but based on the answers that I found online I am still not able to fix my problem. I have two populations (vectors) of N elements each, and they are both normally distributed (actually, generated with a Monte Carlo method which gives an almost-normal distribution, as it should). So far no problem.
Now, I would like to plot not only these two distributions as histograms, but also overimpose to them their related Gaussian distribution (not normalized with respect to the total number of elements N but keeping the "ratio" of the original histogram, that is keeping the graph with the frequencies).
My code is something like this:
% After generating R1 and R2 vectors...
% First, plot the histograms (with 50 "containers" each)
hist(R1,50);
hist(R2,50);
% Then, I retrieve the normal pdf and multiply each element of it
% in order to "denormalize". Multiply by N times the number of
% containers of the histogram
X = linspace(0,10000,7000);
Y1 = normpdf(X,mean(R1),std(R1)).*(N*50);
Y2 = normpdf(X,mean(R2),std(R2)).*(N*50);
plot(X,Y1,'r');
plot(X,Y2,'r');
hold off;
But unfortunately, the output looks like this (the Red lines, identifying the associated Gaussian curve, do not seem to fit properly the histogram distribution plot...):
What could my problem be?
Thanks a lot in advance for your valuable help!
Paolo

Risposte (1)

Ayush Anand
Ayush Anand il 5 Apr 2024
Hi,
When you multiply the PDF by N*50, you're assuming each bin of the histogram represents an equal division of the probability, which is not accurate. The correct scaling factor should consider the bin width of the histogram, not just the number of bins, to ensure the areas under the histogram and the Gaussian curve are comparable.
Here's how you can modify the scaling factor to correctly plot the histograms and their corresponding Gaussian distributions:
binWidth1 = (max(R1) - min(R1)) / numBins;
binWidth2 = (max(R2) - min(R2)) / numBins;
% Generate points for the Gaussian PDF and scale correctly
X = linspace(min([R1;R2]), max([R1;R2]), 1000); % Adjusted range for both R1 and R2
Y1 = normpdf(X, mean(R1), std(R1)) * N * binWidth1; % Correct scaling
Y2 = normpdf(X, mean(R2), std(R2)) * N * binWidth2;
Alternatively, you could normalize the histogram so that the area (or integral) under the histogram equals 1. You can do this by using the "histogram" function for plotting histograms(instead of "hist"), with a name-value pair of "Normalization" and "pdf":
histogram(R1, numBins, 'Normalization', 'pdf');
Hope this helps!

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by