Sample from multivariate exponential distribution
Show older comments
I'd like to generate random vectors according to a multivariate exponential distribution, that is with a pdf f: R^n->R given by

for appropriate normalisation constant c_e. I wondered if there was built in functionality to do this, if not how would I go about doing this manually?
1 Comment
the cyclist
on 3 Feb 2017
Are the x variables independent, or correlated?
Answers (1)
Roger Stafford
on 4 Feb 2017
You can make use of the ‘gammaincinv’ function for this problem:
X = randn(m,n);
X = X.*repmat(gammaincinv(rand(m,1),n)./sqrt(sum(X.^2,2)),1,n);
The resulting array X has m rows, each consisting of n coordinates in R^n space with the requested distribution.
(I’m assuming here that ‘gammaincinv’ will accept the scalar n for its second argument. If not, the n will have to be repeated m times using ‘repmat’. I have also assumed that the n coordinate variables are to be statistically independent.)
3 Comments
Roger Stafford
on 7 Feb 2017
I should probably have given an explanation for the above answer.
For each element of X = randn(m,n) the pdf is 1/sqrt(2*pi)*exp(-x^2/2). Since they are regarded as independent, the n-dimensional pdf of each i-th row in X is the product of its elements’ separate pdf’s:
1/sqrt(2*pi)^n*prod(exp(-x(i,:)^2/2)) =
1/sqrt(2*pi)^n*exp(-sum(x(i,:)^2)/2)
and is therefore only a function of r, the distance from the origin in R^n. That means that each row of
X./sqrt(sum(X.^2,2))
is located entirely on the unit n-sphere surface and is uniformly distributed throughout that surface.
The cumulative distribution function cdf of the solid sphere of radius R, as per your request, is to be:
cdf(r<=R) = K*integral from 0 to R of (exp(-r)*S*r^(n-1) dr
= gammainc(R,n)
where S is the surface measure of a unit n-sphere and where K is the appropriate normalizing constant. To generate this distribution using the rand function in the usual way, we need to use the inverse of gammainc:
gammaincinv(rand,n)
All of this finally gives us the second line in the given answer:
X = X.*repmat(gammaincinv(rand(m,1),n)./sqrt(sum(X.^2,2)),1,n);
ayoub bouayaben
on 22 Apr 2021
@Roger Stafford hello, can you explain me please how can i modify the code that you mentioned before if i want to sample a vector of N elements from a gaussian distribution which is given by :
p(y) = exp(-1/2 *(y - a).' * C* ( y - a) ) with C is the cov matrix with y in R^n
Thank you for your help !
the cyclist
on 23 Apr 2021
Categories
Find more on Exponential Distribution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!