How to find PDF, CDF and the probability at a certain point from histogram ?

Suppose that I have an array of random numbers, e.g.
X=randn(1,1000);
hist(X,100);
Using the histogram ,I want to find and plot the PDF and CDF of X, and for a constant value a, I also want to find the probabilities P(X=a) and P(X<=a).
Any help would be appreciated. Thank you.

 Risposta accettata

Is this homework? Hint: just take the counts return argument from hist() and normalize the counts array by the total number of counts. Then get cdf by using cumsum() on the normalized counts.

7 Commenti

No it is not a homework. Thanks.
Is the following correct?
%for PDF
a=hist(X,100);
plot(a./sum(a));
%for CDF
plot(cumsum(a./sum(a)))
but how can I use the same X-axis values?
Looks pretty close to me. This is how I'd do it.
[counts, binCenters] = hist(X, 100);
pdf = counts ./ sum(counts(:));
cdf = cumsum(pdf);
subplot(1,2,1);
plot(binCenters, pdf, 'b-', 'LineWidth', 3);
title('PDF', 'FontSize', 25);
xlabel('X Value', 'FontSize', 25);
yxlabel('PDF Percentage', 'FontSize', 25);
grid on;
subplot(1,2,2);
plot(binCenters, cdf, 'r-', 'LineWidth', 3);
title('CDF', 'FontSize', 25);
xlabel('X Value', 'FontSize', 25);
yxlabel('CDF Percentage', 'FontSize', 25);
grid on;
Thanks a lot. just a question; let say I want to know the probability P(X=1.2) or P(X<1.2), how can I find these probabilities?
P(X=1.2) = 0 because chances are it will never be exactly 1.2
P(X<1.2) = cdf(1.2) so you need to find the index that corresponds to 1.2
index = find(binCenters<1.2, 1, 'last');
p = cdf(index);
may
may il 25 Set 2013
Modificato: may il 25 Set 2013
just a question; when I change the number of bins in hist function, I get different PDF's, the shape is the same but the numbers on Y axis are different! any help would be appreciated.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by