36 views (last 30 days)

Show older comments

I had to plot the PDF of X = U1 — U2, where U1 and U2 are uniform random variables . When Iam trying with the code the following error is coming. Please let me know what Iam doing wrong.

>> rand('state',0)

>> x = zeros(1000,1);

>> for i=1:1000

x(i,1) = randn(1,1)-randn(1,1) ;

end

>> y = pdf(x,1000) ;

Error using pdf (line 64)

Requires the first input to be the name of a distribution.

John D'Errico
on 3 Apr 2021

Edited: John D'Errico
on 3 Apr 2021

What are you doing wrong? Um, pretty much everything? Sorry, but true. ;) However, you do seem to have made some credible effort, and you did try to use functions that were in the correct field of study.

You want to find the pdf of the difference between two uniform random variables. So then why are you using randn, which produces a GAUSSIAN (normal) random variable? Generate a UNIFORM random variate using rand, not randn.

Next, that is not what the function pdf does, i.e., take a set of values and produce a pdf.

I said pretty much everything was wrong, but you did subtract two numbers that were sampled from distributions, so in terms of a difference, you were spot on there. You were heded in the rght direction. :) (Hey, what can I say?) :)

So how might you plot the pdf of a difference of two uniform variables? Simple seems best.

N = 1e6; % note the large sample size.

X1 = rand(N,1); % UNIFORM samples

X2 = rand(N,1);

histogram(X1-X2,'normalization','pdf')

Is that correct? Well, theoretically, one would expect the solution to be a triangle distribution, with peak at 0, and extremes at -1 and 1. That is clearly what we see. A sum of more terms would gradually start to look more like a normal distribution, the law of large numbers tells us that. Using the symbolic toolbox, we could probably spend some time and generate an analytical solution for the pdf, using an appropriate convolution.

Paul
on 3 Apr 2021

Can you clarify this statement: "A sum of more terms would gradually start to look more like a normal distribution, the law of large numbers tells us that."

What more terms would be added to make the pdf of the sum look normal? Are there any constraint on these terms? As I understand the LLN, it makes statements about the convergence of the sample mean, but not about the distribution of the sample mean.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!