3

I couldn't find a function in matlab that implement gets mean and standard deviation of normal distribution and plot its PDF and CDF.

I am afraid the two functions I have implemented bellow are missing something, since I get maximal value for pdfNormal which is greater than 1.

function plotNormPDF(u,s,color)
    mu = u; 
    sigma = s; 
    x = (mu - 5 * sigma) : (sigma / 100) : (mu + 5 * sigma); 
    pdfNormal = normpdf(x, mu, sigma);
    string = 'the maximal pdfNormal is';
    string = sprintf('%s :%d', string,max(pdfNormal));
    disp(string)
    plot(x, pdfNormal/max(pdfNormal),color); 
end

And for the CDF norm

function plotNormCDF(u,s,color)
    mu = u; 
    sigma = s; 
    x = (mu -  5*sigma) : (sigma / 100) : (mu + 5*sigma); 
    pdfNormal = normpdf(x, mu, sigma);
    plot(x,cumsum(pdfNormal)./max(cumsum(pdfNormal)),color)
end

Here is an example for using both:

plotNormCDF(0.2, 0.1,'r')
plotNormPDF(0.2, 0.1,'r')

enter image description here

enter image description here

mkl
  • 90,588
  • 15
  • 125
  • 265
0x90
  • 39,472
  • 36
  • 165
  • 245
  • 5
    It's fine if the maximal value of the pdf is greater than 1: the density under the curve needs to integrate to 1. Consider this: take a single point on the pdf and set its value to 1 million. The area under this point is still 0, and so the area under the pdf is unaffected. Alternatively, consider a uniform distribution on [0,.5]: to integrate to one, the pdf equals 2 everywhere in the support. For more, reference the following whuber comment and the links he provides: http://stats.stackexchange.com/questions/47714/how-to-explain-what-density-is-and-the-interpretation-of-the-curves-height-to-n – David Marx Nov 30 '13 at 13:38
  • Also see http://stats.stackexchange.com/questions/4220/a-probability-distribution-value-exceeding-1-is-ok – Glen_b Nov 30 '13 at 14:59
  • 1
    "unlike a probability, a probability density function can take on values greater than one" in the Wikipedia page that you refer to. – A. Donda Nov 30 '13 at 15:10

2 Answers2

5

You don't need all that code, look how simpler it is:

mu = 0.2; sigma = 0.1;
x = linspace (mu-4*sigma, mu+4*sigma);
plot(x, normpdf (x,mu,sigma))
plot(x, normcdf (x,mu,sigma))
juliohm
  • 3,691
  • 2
  • 18
  • 22
3

Your function plotNormPDF is correct except that you should not divide by the maximum. As David Marx wrote, there is no upper constraint on the values that a probability density function can attain, only a constraint regarding its integral over the range of possible values.

function plotNormPDF(u,s,color)
mu = u;
sigma = s;
x = (mu - 5 * sigma) : (sigma / 100) : (mu + 5 * sigma);
pdfNormal = normpdf(x, mu, sigma);
string = 'the maximal pdfNormal is';
string = sprintf('%s :%d', string,max(pdfNormal));
disp(string)
plot(x, pdfNormal,color);
end

Your function plotNormCDF is correct in principle, but probably not very precise because it approximates an integral by a cumulative sum. Better to use the function normcdf. Normalization of the maximum to 1 here is neither necessary nor does it have an effect.

function plotNormCDF(u,s,color)
mu = u;
sigma = s;
x = (mu -  5*sigma) : (sigma / 100) : (mu + 5*sigma);
cdfNormal = normcdf(x, mu, sigma);
plot(x,cdfNormal,color)
end
A. Donda
  • 8,381
  • 2
  • 20
  • 49