Celeb Glow
general | April 17, 2026

The problem of ksdensity plot in Matlab

$\begingroup$

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?

$\endgroup$ 1

1 Answer

$\begingroup$

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');
$\endgroup$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy