Is randn() in Matlab actually using an inverse normal distribution to generate the random numbers from normal distributions?
This manual page says:
NormalTransform: Transformation algorithm used by randn(s, ...) to
generate normal pseudorandom values. Possible values are 'Ziggurat',
'Polar', or 'Inversion'.
You specifically asked about inversion, so I'm assuming you're already familiar with it. Here are links in case you want to know more about the Ziggurat or polar methods.
Related
I am new to this, and I'd like to know whether there's a function or any way I can generate n independent normal random variables in Matlab?
randn will produce independent random variates from a Normal distribution.
In general, you have to trust that the sequence of variates produced by a pseudo-random number generator are statistically independent. Smart people who are experts in designing RNGs have worked hard to try to achieve that.
You can use these two below,
randn
which will give you normally distributed random numbers.
rand
which will give you uniformly distributed random numbers.
I want to generate multiple(1000) random multidimensional(10000) normal distributions with different densities in matlab.
I know how to generate a multivariate normal distribution using mvnrnd() function but I was wondering if I could do it in a automated way. Maybe have the mean and sigma generated by rand function then feed it into the mvnrnd() function somehow?
Also would it be possible to generate some(300) of these in a hyper spherical formation where the means are distributed spherically in the multidimensional space. They should have the same density and spread/sigma.
Thank you for your input in advance.
The pdf for the multivariate normal distribution in MATLAB is mvnpdf(...). What about the case where multiple variables are uniformly distributed: Is there a function to describe their joint distribution analogous to the multivariate normal distribution? If there is no such function, is there a trick to handle this case?
The simplest way how several variables can be uniformly distributed is if they are mutually independent; in that case you simply have a uniform distribution over the hypercube in the space spanned by the variables. In order to get samples from this distribution, you just separately generate samples for each of the variables.
The point where a "trick" might be necessary is if you have dependencies between the variables even though the marginal distribution for each of them is still uniform. In this case you have to describe the dependency structure, and I'm not aware of any standard way to do this (the way dependencies between normally distributed variables are described by a correlation matrix).
Of course such distributions exist: For two dimensions, one possibility would be to have a joint distribution that looks like a solution to the "eight rooks" problem:
Another one actually derives from the introductory Matlab example, the magic square:
Both of these examples are discrete distributions, but can be produced at arbitrary granularity, or simply interpreted as piecewise constant continuous distributions.
As you can see there are many possibilities for a multivariate distribution each of whose marginal distributions are uniform. The question you have to answer for yourself is what kind of dependencies, if any, you are interested in?
If I'm understanding the question properly, we want to calculate the pdf of a multivariate uniform distribution. By definition, the pdf is constant for all values in the support the distribution. Thus to calculate the pdf all that is required is to calculate the norming constant, which is given by the inverse of the integral of the support. That is to say, the pdf is given by
f(x) = 1 / integral(A)
where A is the support set, and x is an element in A. If an analytic solution to integral(A) is not available, then a numerical integrator can be employed.
Suppose to generate a n-dim normal random number with distribution N(u, diag(sigma_1^2, ..., sigma_n^2) in Matlab, where u is a vertical vector.
There are two ways.
randn(n,1).*[sigma_1, ..., sigma_n]' + u;
mvnrnd(u', diag(sigma_1^2, ..., sigma_n^2))';
I think they are both correct. But I wonder if there is some preference of one over the other based on some reasons? I ask this question, because I saw another person always choose the first way, while I choose the second without having thought about it yet.
Thanks and regards!
They are equivalent methods. Personally, I would prefer the second option because it's one function that can be used to generate this sort of data for arbitrarily-shaped arrays. If all of a sudden you wanted a whole matrix of Gaussian values, you can get that more easily from the second function call, without doing any calls to reshape(). I also think the second example is easier to read because it relies on a built-in of Matlab's that has been ubiquitous for a long time.
I suppose that if n is large, one could argue that it's inefficient to actually form diag(sigma_1^2, ..., sigma_n^2). But if you're needing to make random draws from a matrix that large, then Matlab is already the wrong tool for the job and you should use Boost::Probability in C++, or perhaps SciPy / scikits.statsmodels in Python.
If there are correlations between the random variables then the covariance matrix is not anymore diagonal. In such case you may use mvnrnd or use randn with Cholesky decompistion as following.
U = chol(SIGMA);
x = U'*randn(n,1);
Whenever possible, use basic functions instead of using toolbox functions. Basic function are faster and portable.
Is it possible to define your own probability density function in MATLAB or Octave and use it
for generating random numbers?
MATLAB and Octave have default functions like rand, randn built in to draw points at random from a uniform, or normal distributions but there seems to be no documentation of how to define my very own proability density function.
Sampling from an arbitrary random distribution is not always trivial. For well known distributions there are tricks to implement them and most of them are implemented in stats toolbox as Oli said.
If your distribution of interest is of difficult form, there are many sampling algorithms that may help you, such as, rejection sampling, slice sampling, Metropolis–Hastings algorithm.
If your distribution is discrete, or can be approximated by a discrete distribution fairly well, then you can just do multinomial sampling using randsamp.
If you have the stats toolbox, you can use random(), as it has a lot of useful PDFs built-in.
I've had to do that a few times recently, and it's not exactly an easy thing to accomplish. My favorite technique was to use Inverse transform sampling.
The idea is quite simple:
create a cdf
use a uniform random number generator.
identify the RV that maps to your cdf value.