Matlab estimate PDF from random variable X

In summary, the conversation discusses how to estimate the probability density function (PDF) for a random variable \(X\) where \(X = U_1 - U_2\) and \(U_i\) are uniform random variables. The code provided uses the function unifrnd to generate a 1000x1 vector of uniform random numbers between -5 and 5, and then calculates the PDF using the kernel density estimation method. The conversation also mentions the option of using the symmetric triangular distribution function with support (-10, 10).
  • #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?

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
  • #2
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".

.
 
  • #3
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.
 
  • #4
dwsmith said:
I just doing a problem out of a book on stochastic processes.

What is the exact wording of the problem.

.
 
  • #5


To estimate the PDF for X, we can use the approach of binning the data and counting the number of observations falling within each bin. In this case, we have two uniform random variables, U1 and U2, and we want to estimate the PDF for their difference, X = U1 - U2.

First, we generate a large number of random values for U1 and U2 using the unifrnd function in MATLAB. In this code, we generate 1000 values for each variable, but this can be adjusted as needed.

Next, we create a vector of bin centers, which will serve as the boundaries for our bins. In this code, we use a bin width of 0.5, but this can also be adjusted based on the range of values for X.

We then create a loop to go through each observation of X and count the number of values falling within each bin. This is done by comparing each value of X to the bin boundaries and incrementing the corresponding bin count.

Finally, we divide the bin counts by the total number of observations (1000) and the bin width (0.5) to obtain an estimate of the PDF for X. This is then plotted as a bar graph using the bar function in MATLAB.

It is important to note that this is just one method for estimating the PDF from a random variable in MATLAB. Other methods, such as kernel density estimation, can also be used depending on the specific needs of the analysis.
 

Related to Matlab estimate PDF from random variable X

1. How do I estimate the probability density function (PDF) of a random variable in Matlab?

To estimate the PDF of a random variable X in Matlab, you can use the built-in function ksdensity(X). This function uses kernel smoothing to estimate the PDF based on the data points in X. You can also specify additional parameters, such as the type of kernel and bandwidth, to customize the estimation process.

2. Can I estimate the PDF of a continuous random variable in Matlab?

Yes, you can estimate the PDF of both discrete and continuous random variables in Matlab. For continuous random variables, the ksdensity(X) function will return a smooth curve representing the estimated PDF. For discrete random variables, it will return a step function.

3. Is it possible to plot the estimated PDF in Matlab?

Yes, after using the ksdensity(X) function to estimate the PDF, you can use the plot() function to plot the resulting curve. You can also use other plotting functions, such as histogram(X), to visualize the PDF.

4. How accurate is the estimated PDF in Matlab?

The accuracy of the estimated PDF depends on various factors, such as the size and distribution of the data, the type of kernel used, and the bandwidth chosen. Generally, a larger sample size and a suitable kernel and bandwidth can result in a more accurate estimation of the PDF.

5. Are there any other methods to estimate the PDF of a random variable in Matlab?

Yes, there are other methods in Matlab that can be used to estimate the PDF of a random variable, such as the fitdist(X, 'kernel') function, which fits a parametric distribution to the data in X using a specified kernel. You can also use the histcounts() function to create a histogram of the data and then normalize it to obtain an estimate of the PDF.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
44
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
3
Views
792
  • Calculus and Beyond Homework Help
Replies
1
Views
663
  • Set Theory, Logic, Probability, Statistics
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Back
Top