# How to find (and plot) a probability distribution function?

I'm working on my biometrics course, and I have to plot a pdf (I think it means probability density / distribution function). Here is a sample pdf graph : Introduction to Biometrics page 5 , figure 2.a

I have the data for genuine and impostor scores, in MATLAB. So I have two vectors, which contain hundreds of data (values of which are between 0 and 1). But I have no idea how to pdf-plot them.

I tried probplot(data) function of MATLAB, but it gaves an increasing function, whereas I was expecting a bell-curve shape. See the image :

I also see that there is a pdf function of MATLAB, but it asks you to use which kind of pdf. Maybe normal distribution is the one for me, but I'm confused because it also asks for the mean and st.dev. values. Shouldn't the function compute these for me?

In the end, how can I get a PDF plot like in the link ?

Thanks for any help !

## Edit:

ksdensity plot for the genuine scores:

## Edit 2:

Output for normpdf(scores , mean(scores) , std(scores)); :

## Edit 3:

This is the actual graph I'm trying to achieve : (x-axis values are not important)

## Edit 4:

vector_to_pdfplot = genuine_scores;
plot(min(vector_to_pdfplot) : ((max(vector_to_pdfplot) - min(vector_to_pdfplot))/1000) : max(vector_to_pdfplot) , normpdf(min(vector_to_pdfplot) : ((max(vector_to_pdfplot) - min(vector_to_pdfplot))/1000) : max(vector_to_pdfplot) , mean(vector_to_pdfplot) , std(vector_to_pdfplot) ));


-
The probplot function compares your data with the normal cdf (hence the increasing function with range [0,1]). To plot the pdf, you may find this of use: mathworks.co.uk/matlabcentral/newsreader/view_thread/155832 –  George Tomlinson Mar 8 '14 at 13:05
@GeorgeTomlinson thanks! But I don't get this : why do we compute a mean value ? I just need the probability for the value of a variable selected from the set. There is no "mean" value I'm interested in. I hope I could explain my problem. –  halilpazarlama Mar 8 '14 at 13:40
I don't know why it takes the mean as an argument, but I don't see that it's a problem to compute this. This is utter speculation, but maybe supplying the mean as an argument means that it's possible not only to plot a pdf of a given set of values in the way you want to, but also to plot a pdf based on a sample of values from a population whose mean is known. –  George Tomlinson Mar 8 '14 at 14:09
No it is not. Ok let me paste the output of normpdf. –  halilpazarlama Mar 8 '14 at 14:10
Are you using the data values as the first argument? I don't think this is what you're supposed to do. See the link in my first comment or my answer for details. –  George Tomlinson Mar 8 '14 at 14:20

Try hist(data) to get a histogram.

You can also try

[f,xi] = ksdensity(data)
plot(xi,f)


to get a smoothed histogram.

You can restrict the domain (since the default is the real number line):

support = (0:.01:1)'
[f] = ksdensity(data,support)
plot(support,f)

-
Thanks ! +1. This sure prints a much prettier graph. But I'm confused with one thing. It shows a positive probability of data being > 1. Whereas the data is normalized between 0 and 1. –  halilpazarlama Mar 8 '14 at 13:08
Can you add the graph? –  Dimitriy V. Masterov Mar 8 '14 at 13:15
The height does not tell you the probability. The area under the pdf does. This means the height can exceed one. If you're getting positive density for x above one, you can use the support option to restrict it to [0,1]. –  Dimitriy V. Masterov Mar 8 '14 at 13:24
I attached the ksdensity output. It sure makes sense, except being greater than 1. –  halilpazarlama Mar 8 '14 at 13:37
I added the code showing how to restrict the domain. –  Dimitriy V. Masterov Mar 10 '14 at 17:22

I suggest adapting this code I've taken from the link I gave in my first comment.

>> A = rand(700,1);
>> MAX = max(A);
>> STD = std(A);
>> MAX = max(A);
>> MIN = min(A);
>> STEP = (MAX - MIN) / 1000;
>> PDF = normpdf(MIN:STEP:MAX, M, S);
>> plot(MIN:STEP:MAX, PDF);


In your case the distribution isn't random, but I imagine you can apply the principles used here.

In particular, the first argument should be a combination of the maximum, minimun and a step size which depends upon the maximum, minimum and one other number (1000 in the example). I'm afraid I can't offer any help on how to choose the number in your case.

EDIT: maybe just modifying your code so that it's in this format will solve the problem:

x = -100:0.1:100;
plot(x,normpdf(x,0,20),'-')


What's the difference? The difference is that defining your x values beforehand alters MATLAB's scaling:

When you call plot with ONE argument, it plots those numbers on the y axis, using the index numbers of those values for the x axis. If you wanted the x axis scaled properly, you had to provide them in the first place. Thus...

x = -100:0.1:100;
plot(x,normpdf(x,0,20),'-')


produces very different results to

plot(normpdf((-100:0.1:100),0,20))


contrary to what one might expect.

-
Thanks! +1, but again something strange. It does not go back to zero. Posting the output of this to the answer. –  halilpazarlama Mar 8 '14 at 14:26
No problems. Thanks for the +1. Possibly just a scaling issue: you may need to find out how to force MATLAB to set the minimum x label to 0. However, it may help to try using a number other than 1000 (in fact, it may be necessary to do so): perhaps a number 30% larger than the number of data values, although this is a guess, based on the fact that 1000 is comparably larger than 700 (I know it's not 30% larger). This link may also help, although it may not make any difference, but I thought I'd post it in case: stackoverflow.com/questions/3820240/… –  George Tomlinson Mar 8 '14 at 14:38
Oh, sorry, you mean at the right hand end. It could be to do with the 1000. It could be the choice of mean and variance. It could be something else. I don't know I'm afraid. If you can adjust the x axis so that it ranges from 0 to 1, the plot may (or may not) look better, but it still may not be perfect. The accepted answer on this link does imply that incorrect scaling of the x axis label values can produce weird results, so possibly you need to give some thought to how to find the best x axis label values : stackoverflow.com/questions/3820240/… –  George Tomlinson Mar 8 '14 at 14:53
Please see the edit to my answer: it may solve your problem. Another possible solution is that the mean and s.d. you supply need to be different. We still don't know whether using the sample mean and s.d. is correct and if not, what they should be instead. I'm afraid I don't know the answers to these questions at present. –  George Tomlinson Mar 8 '14 at 15:10
I think normpdf is plotting the actual normal pdf corresponding to the mean and standard deviation you supply for the range you supply, which isn't what you want. You want to plot the possible data values against the probability of it being less than a given value, so I don't think normpdf is what you need, in fact. –  George Tomlinson Mar 8 '14 at 15:31