- #1
Dustinsfl
- 2,281
- 5
How do I estimate the pdf from a random variable \(X\) where \(X = U_1 - U_2\) and \(U_i\) are uniform random variables?
In the code below, I used unifrnd(-5, 5, 1000, 1) which generated a 1000x1 vector of uniform random number between -5 and 5.
How do I estimate the PDF for X?
In the code below, I used unifrnd(-5, 5, 1000, 1) which generated a 1000x1 vector of uniform random number between -5 and 5.
How do I estimate the PDF for X?
Code:
rng;
X = unifrnd(-5, 5, 1000, 1) - unifrnd(-5, 5, 1000, 1);
bincenters = (-5:0.5:5);
bins = length(bincenters);
h = zeros(bins, 1); % pre-allocating h
for i = 1:length(X)
for k = 1:bins
if X(i) > bincenters(k) - 0.5/2 && X(i) <= bincenters(k) + 0.5/2
h(k, 1) = h(k, 1) + 1;
end
end
end
pxest = h/(1000*0.5);
bar(bincenters, pxest)