How to generate data based on uninvertible probability distribution function?

I want to generate data based on given probability distribution.
f(y) = alpha*exp(-y/beta1)+(1-alpha)*exp(-y/beta2)
I tried to use the way as follows. I used inverse function and reversed a probability distribution function to get the inverse result.
syms y
f(y) = alpha*exp(-y/beta1)+(1-alpha)*exp(-y/beta2);
g = finverse(f);
This method doesn't work since "Functional inverse cannot be found. " However, I find another code written by a programmer. This code can generate the data series perfectly. But I cannot understand its thought. Could you help me explain it?
m = [beta1;beta2]; % the two means
t = 1 + (randn(n,1)<alpha); % randomly select 1st or 2nd mean
X = m(t) .* -log(rand(n,1)); % generate exponentials with selected mean

 Accepted Answer

a. If your f(y) is meant to be the cumulative probability distribution over a y support from zero to infinity, it is backwards from the usual convention. For y = 0 it should be zero and at infinity it should be one. To get that, subtract your expression from 1. (It certainly isn't a density distribution.)
b. I have serious doubts that the code you describe will give the desired distribution. I think you need to use matlab's 'fzero' function for taking the inverse combined with the use of 'rand' to obtain what you want.

3 Comments

Thank you so much Roger.
It is abundantly clear to me now for using 1 to subtract the CDF function to make sure its validity.
But I still get the error ‘Functional inverse cannot be found.’
a) Is it because one value of f(y) can correspond to two or more y, which makes Matlab confused by the ambiguity?
b) Can probability density function be an alternative to be applied in the sample generation? If so, what is the proper coding way?
Provided beta1 and beta2 are positive and alpha lies between 0 and 1, then a given value of f(y) can only correspond to at most a single y value, and therefore an inverse should exist. The message that "Functional inverse cannot be found" is undoubtedly due to Matlab's inability to find a symbolic expression for that inverse. It is an inability in mathematical theory. I personally can't find a symbolic inverse to f(y), and I tried. That is why you should be using 'fzero' to numerically evaluate an inverse rather than a symbolic inverse.
Thanks Roger. Thanks for your attentive explanation. It is very helpful.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!