MATLAB for Statisticians
Posted on Dec 04, 2012 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
-
There are generally two ways to generate uniform and normal random numbers in MATLAB. One ways is to use [functions]
randn
(for standard normal distribution) andrand
(for standard uniform distribution). The other way is to use [functions]unirnd
andnormrnd
(there are also functions end with "rnd" for generating random numbers from other distributions). The difference between these two different kinds of functions is that the first group of functions generate only "standard" distributions while the second group of functions generate distribution with any parameters. So the group of functions end with "rnd" is recommended to use. In old versions (2010a and earlier) of MATLAB, these random number generating functions end with "rnd" might cause problems in parallel computing. Then you want to use these two basic functionsrand
andrandn
, and use all kinds of technics to generate random variables you want. -
randsample
generates a random sample from a given collection of data with or without replacement. It also support weighted sampling with replacement, but does not support weighted sampling without replacement. There is another very similar but more powerful function calleddatasample
which support weighted sampling (both with and without replacement). Note that both these two functions can generate random permutations which was frequently done usingrandperm
in MATLAB of older versions. For old versions (before 2011b) of MATLAB, you can userandi
to generate random indexes and extract corresponding elements of arrays. -
When MATLAB starts, it set the random number generator to the default one and set the seed to be the default seed. So if you run a simulation, reboot MATLAB and run the same simulation, you will get the same results.
-
tabular
counts the frequency of observations. -
iqr
calculates the interquartile range of given data. There is a similar function calledIQR
in R.