The problem of ksdensity plot in Matlab

1k Views Asked by At

I want to verify that random numbers generated by exprnd match the exponential distribution. I used Matlab's ksdensity function – here's the code:

R = exprnd(10,1000,1);
[f,xi]=ksdensity(R);

x = 0.01:0.01:60; 
y = 0.1.*exp(-x./10); 
plot(xi,f,x,y); 
legend('exprnd','exp')

But the resulting plot shows that they do not match.Matlab plot

I checked and no random number is below zero, however the plot of ksdensity shows some points below zero that have probability. Why is that?

1

There are 1 best solutions below

0
On

By default ksdensity assumes that the support of the distribution is unbounded, i.e., a two-sided distribution. You can specify positive support via the 'Support' parameter:

[f,xi] = ksdensity(R,'Support','positive');