Theoretical graph of the probability density function

I am using a graph of a 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 would also like to print theoretical data. It seems that the best thing I understood is:

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

      

But this is not true. What am I missing here? This is how my plots look like. Blue is actual and red is theoretical.

enter image description here

+3


source to share


1 answer


Your y does not come from an even distribution; They start from the distribution of the sum of iid (independent equally distributed) random variables of uniform distribution with mean 0 and variance 1/12. The sum approaches a normal distribution as the number of variables being added (100 in your case) becomes large. Using your code, I was able to achieve a very good fit with normpdf, with the correct variance of 1/12 (sigma is the square root of that number):

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

      



BTW, your matlab code can be made simpler and clearer by replacing the first 8 lines:

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

      

+1


source







All Articles