0

I need to plot a probability density function of a uniformly distributed matrix from

U = rand (1,1000)

but I can't use the ksdensity function. I've tried this:

term = 1000;
U = rand (1,term);
x=0:0.001:1;
for j = 2:term;
    u_height(j) = u_height(j-1)+((abs(x(j)-U(j))<0.01/2)/0.01)/term;
    n_height(j) = n_height(j-1)+((abs(x(j)-N(j))<0.01/2)/0.01)/term;
end

but it's not plotting correctly

user2064077
  • 1
  • 1
  • 1

1 Answers1

2

The function ksdensity should work fine. You need to specify that that the range of the PDF is finite, and use a box kernel.

u = rand(10000,1);

ksdensity(u, 'Support', [0 1], 'kernel', 'box');

Additionally you can approximate the pdf using histc

u = rand(10000,1);
bins = 0:.05:1;
counts = hist(u, bins);
p = counts ./ trapz(bins, counts);

plot(bins, p);
slayton
  • 20,123
  • 10
  • 60
  • 89
  • Why do you recommend `box`? (opposed to keeping the default setting or using `epanechnikov`) – Dennis Jaheruddin Feb 12 '13 at 14:40
  • @DennisJaheruddin both are fine actually, but after messing around with the various options I felt that a box kernel produced an estimate that more closely resembled a uniform distribution. – slayton Feb 12 '13 at 14:44
  • while this is close to what I was looking for, it's not precise enough, even with a 10,000 size vector. – user2064077 Feb 13 '13 at 10:48
  • @user2064077 remember that this is an estimate of the underlying distribution. The precision is going to depend on the data set size and quality. – slayton Feb 13 '13 at 12:19