Reputation: 38
How would I simulate a probability density function in MATLAB such that
fx(x)={ x/8 0<=x<=4
{ 0 Other
Upvotes: 1
Views: 1394
Reputation: 18540
Simulation from an arbitrary probability density function is done as follows:
1) Derive the inverse cumulative distribution.
2) Simulate from a uniform [0, 1] distribution.
3) Plug the uniform [0, 1] numbers into the inverse cumulative distribution.
In your situation, you have a nice easy probability density to work with, leading me to suspect that this is a homework question. Given that you haven't posted any code indicating that you've attempted to solve it yourself, I'm not going to just write out the answer for you.
Instead, why don't you have a go at deriving the inverse cumulative distribution yourself? First you'll need to get the cumulative distribution. This can be done by finding the integral of your probability density from minus infinity to x, which in your case is equivalent to the integral from 0 to x. Once you've done this, you need to find the inverse of it. The example here should be sufficient to show you how to do that for your simple case. If you get that far, then use rand(100, 1)
to simulate 100 draws from the uniform [0, 1] density, and then plug those numbers into your inverse cumulative distribution.
If you run into any problems, feel free to edit your question to add them, and leave a comment on this answer and I'll help you out. I'm around for the next hour or so.
Cheers.
UPDATE: I figure the OP's homework was probably due by now, so for completeness: The integral of the probability density, ie the cumulative distribution, is f(x) = (1/16) x^2. Note that when x = 0, f(x) = 0, and when x = 4, f(x) = 1. This demonstrates that the question has stated the domain of the probability density correctly. Next, f(x) implies an inverse CDF of g(x) = 4 * x^(1/2). Hence:
MyInverseCDF = @(x) (4 * sqrt(x));
MySimulatedDraw = MyInverseCDF(rand(100, 1));
We can visually validate that everything is working using:
hist(MySimulatedDraw);
One other thing, here is a link to another related SO question: defining-your-own-probability-distribution-function-in-matlab
Upvotes: 7