MATLAB Matlab estimate PDF from random variable X

AI Thread Summary
To estimate the probability density function (PDF) of the random variable \(X = U_1 - U_2\), where \(U_i\) are uniform random variables, the discussion highlights a MATLAB code snippet that generates 1000 samples from a uniform distribution between -5 and 5. The code calculates the histogram of \(X\) by counting occurrences in specified bins and normalizing the counts to estimate the PDF. However, it is noted that the distribution of \(X\) is known to be a symmetric triangular distribution with support ranging from -10 to 10, eliminating the need for estimation. For those seeking alternative methods, suggestions include kernel density estimation and using MATLAB's `ksdensity` function. The context of the inquiry stems from a problem in a stochastic processes textbook.
Dustinsfl
Messages
2,217
Reaction score
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?

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)
 
Physics news on Phys.org
dwsmith said:
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?

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)

What are you really trying to do?

There is no need to "estimate" this distribution you know it, it is the symmetric triangular distribution with support (-10,10).

Otherwise google "kernel density estimation", or "matlab ksdensity".

.
 
zzephod said:
What are you really trying to do?

There is no need to "estimate" this distribution you know it, it is the symmetric triangular distribution with support (-10,10).

Otherwise google "kernel density estimation", or "matlab ksdensity".

.

I just doing a problem out of a book on stochastic processes.
 
dwsmith said:
I just doing a problem out of a book on stochastic processes.

What is the exact wording of the problem.

.
 

Similar threads

Replies
44
Views
6K
Replies
8
Views
2K
Replies
4
Views
1K
Replies
4
Views
2K
Replies
8
Views
2K
Back
Top