Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theoretical Plot of Probability Density Function

Tags:

matlab

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

like image 662
codedude Avatar asked May 19 '26 03:05

codedude


1 Answers

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);
like image 103
kishkash Avatar answered May 22 '26 00:05

kishkash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!