Star
Star

Reputation: 2299

Draw random numbers from the Gumbel distribution in Matlab

Question: I would like your help to draw random numbers from the Gumbel distribution with scale mu and location beta in Matlab.

I want to use the definition of the Gumbel distribution provided by Wikipedia (see the PDF and CDF definitions on the right of the page).

Notice: The package evrnd in Matlab, described here, cannot be used (or maybe can be used with some modifications?) because it considers flipped signs.

Let me explain better this last point.

Let's fix the scale to 0 and the location to 1.

Now, following Wikipedia and other textbooks (for example, here p.42) the Gumbel PDF is

exp(-x)*exp(-exp(-x))

In Matlab though it seems that evrnd considers random draws from the following PDF:

exp(x)*exp(-exp(x))

You can see that in Matlab -x is replaced with x.

Any idea on what is the best way to proceed?

Upvotes: 2

Views: 1258

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112729

According to the Wikipedia, the inverse cumulative distribution function is

Q(p) = mu - beta*log(-log(p))

From this function, the inverse transformation method can be applied. Thus

sz = [1 1e6]; % desired size for result array
mu = 1; % location parameter
beta = 2.5; % scale parameter
result = mu - beta*log(-log(rand(sz)))

gives result with i.i.d. Gumbel-distributed numbers. Plotting the histogram for these example values gives

>> histogram(result, 51)

enter image description here


If you want to use the evrnd function (Statistics Toolbox), you only need to change the sign of the output. According to the documentation,

R = evrnd(mu,sigma,[m,n,...])

The version used here is suitable for modeling minima; the mirror image of this distribution can be used to model maxima by negating R.

Thus, use

result = -evrnd(mu, beta, sz);

Upvotes: 5

Related Questions