3
votes

I'm using the plot the probability density function:

y = zeros(1,10000);
for j=1:10000
    r = rand(100,1);
    for i=1:100
        y(j) = y(j) + r(i) - 0.5;
    end
    y(j) = y(j)/sqrt(100);
end
[n,x] = hist(y,100);
plot(x,n/10000/diff(x(1:2)));
hold on;

However I'd also like to print the theoretical too. The best I seem to have managed is the following:

plot(x,normpdf(x,0,1),'r');

But this doesn't follow the actual at all. What am I missing here? Here's what my plots look like at the moment. The blue is the actual and the red is the theoretical.

enter image description here

1
Your theoretical sigma is clearly wrong, try for example plot(x,normpdf(x,0,1/pi),'r');Maksim Gorkiy
@ClockworkOrkwork that worked. Of course my sigma was wrong...thanks! :) I can't quite accept your comment as an answer though... ;)codedude

1 Answers

1
votes

Your y's aren't coming from a uniform distribution; They're coming from a distribution of a sum of i.i.d (independent identically distributed) random variables of uniform distribution with mean 0 and variance 1/12. The sum approaches normal distribution as the number of summed variables (100 in your case) gets large. Using your code, I was able to achieve a very nice fit with normpdf, with the correct variance being 1/12 (sigma is the square root of this number):

y2=normpdf(x,0,sqrt(1/12));
plot(x,y2,'r');

BTW, your matlab code can be made simpler and more readable replacing the first 8 lines with:

r=rand(100,10000)-0.5;
y=sum(r)/sqrt(100);