# Probability question in Mat

My teacher give me this question: Using MATLAB, generate 10000 Random Vectors of size 500 with the PDF of Gamma distribution. Find the PDF of maximum and minimum of the generated Random vectors. (Use Matlab Commands ‘makedist’ and ‘random’ to generate Data)

I write it in matlab, but cant understand how to " Find the PDF of maximum and minimum of the generated Random vectors."

pd=makedist('gamma')

R=random(pd,[500,1,1000])

• I have shown two programming structures for doing such a simulation, using R. If this is mainly a question about specific Matlab code, there may be a better forum for your question. // Simulation can show histograms of min and mix of a gamma samples which suggest the shapes of the respective solutions. If you want analytic derivations of the two distributions, you should ask that question here--separately without talking about simulations and software. – BruceET Apr 3 at 23:28

Here are two program structures for doing this. I will illustrate in R (because it is excellent, free software which I have at hand, while Matlab is excellent, expensive software which I don't have at hand).

You don't say which gamma distribution, so I use shape parameter $$3$$ and rate parameter $$0.1$$ to illustrate.

For loop. Make each of the $$m = 10,000$$ vectors one at a time. Record the min 'v' and max w of each one. Then make histograms of the $$m$$ vs and of the $$m$$ ws to illustrate distributions.

set.seed(403)  # for reproducibility
m = 10^4;  n = 500;  v = w = numeric(m)
# 'v' initialized as vec of m 0's, ith element changed at each passage thru loop
# 'w' similarly
for (i in 1:m) {
x = rgamma(n, 3, 0.1)
v[i] = min(x);  w[i] = max(x)  }
summary(v);  summary(w)

#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.  # for v
# 0.104   1.563   2.133   2.163   2.735   5.310
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.  # for w
# 77.04   99.94  108.15  110.64  118.62  203.68

par(mfrow=c(1,2))  # enable 2 panels per figure
hist(v, prob=T, col="skyblue2")
hist(w, prob=T, col="skyblue2")
par(mfrow=c(1,1))


Huge matrix: Put all $$mn = 10^4(500) = 5,000,000$$ gamma observations into an $$m \times n$$ matrix MAT, each sample of 500 in a row. Then put row minima into vector v and row maxima into w. Starting with the same seed for the random number generator, this method gives the same results as above.

set.seed(403);  m = 10^4;  n = 500
x = rgamma(m*n, 3, 0.1)
MAT = matrix(x, nrow=m, byrow=T)
v = apply(MAT, 1, min)   # apply function 'max' to dim-1 (rows) of 'MAT'
w = apply(MAT, 1, max)
summary(v);  summary(w)

# Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
# 0.104   1.563   2.133   2.163   2.735   5.310
# Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
# 77.04   99.94  108.15  110.64  118.62  203.68


The resulting figure is exactly the same as the one shown above.

Note: Neither program is exactly optimized for R, but I hope the simpler methods used should carry over to Matlab with minimum difficulty.