ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
getRand.m File Reference

Go to the source code of this file.

Functions

function getRand (in ndim, in scale)
 Generate and return a random positive-definite (correlation or covariance) matrix using the Gram method.
More...
 

Function Documentation

◆ getRand()

function getRand ( in  ndim,
in  scale 
)

Generate and return a random positive-definite (correlation or covariance) matrix using the Gram method.

The Gram method of generating random positive-definite square matrices is based on the observation that every real positive definite matrix \(M\) has a Cholesky factorization

\begin{equation} M = LL* \end{equation}

where \(L\) is a uniquely defined lower triangular matrix with positive diagonal entries.
Therefore, \(M\) can be constructed from a given random \(L\).
The Gram method is fast, however, the resulting matrix \(M\) does not possess any particular structure.
because it uses the Cholesky factorization of the distribution covariance matrix.

Parameters
[in]ndim: The input positive scalar MATLAB whole number(integer), representing the rank of the matrix (the number of dimensions) of shape (ndim, ndim).
(optional. It must be present if and only if the input scale argument is missing or is a scalar.)
[in]scale: The input scalar or contiguous vector of size ndim of type real, representing the scale of the matrix (e.g., the standard deviation of a covariance matrix) along each dimension.
(optional. default = 1. It can be present if and only if it is a scalar or is a vector of size ndim.)
Returns
rand : The output matrix of shape (1:ndim, 1:ndim) of type MATLAB double, containing a random positive-definite matrix.
If the optional input scale is missing, then the output rand is a correlation matrix.


Possible calling interfaces

rand(1:ndim, 1:ndim) = pm.stats.dist.cov.getRand(ndim)
rand(1:ndim, 1:ndim) = pm.stats.dist.cov.getRand(ndim, scale(1:ndim))
function getRand(in ndim, in scale)
Generate and return a random positive-definite (correlation or covariance) matrix using the Gram meth...
Warning
The condition all([0 < scale]) must hold for the corresponding input arguments.


Example usage

1cd(fileparts(mfilename('fullpath'))); % Change working directory to source code directory.
2addpath('../../../../../'); % Add the ParaMonte library root directory to the search path.
3
4pm.matlab.show()
5pm.matlab.show('pm.stats.dist.cov.getRand(1)')
6pm.matlab.show( pm.stats.dist.cov.getRand(1) )
7
8pm.matlab.show()
9pm.matlab.show('pm.stats.dist.cov.getRand(3)')
10pm.matlab.show( pm.stats.dist.cov.getRand(3) )
11
12pm.matlab.show()
13pm.matlab.show('pm.stats.dist.cov.getRand(3, 5)')
14pm.matlab.show( pm.stats.dist.cov.getRand(3, 5) )
15
16pm.matlab.show()
17pm.matlab.show('pm.stats.dist.cov.getRand(3, [1, 3, 5])')
18pm.matlab.show( pm.stats.dist.cov.getRand(3, [1, 3, 5]) )
function root()
Return a scalar MATLAB string containing the root directory of the ParaMonte library package.

Example output
1
2pm.stats.dist.cov.getRand(1)
3 1
4
5pm.stats.dist.cov.getRand(3)
6 1.000000000000000 -0.577854797832945 -0.954443147379482
7 -0.577854797832945 1.000000000000000 0.560035903241402
8 -0.954443147379482 0.560035903241402 1.000000000000000
9
10pm.stats.dist.cov.getRand(3, 5)
11 25.000000000000000 -0.754798913402097 5.406846000538978
12 -0.754798913402097 25.000000000000007 -18.585362635411862
13 5.406846000538978 -18.585362635411862 24.999999999999996
14
15pm.stats.dist.cov.getRand(3, [1, 3, 5])
16 1.000000000000000 -1.375032468175924 -0.072602829102421
17 -1.375032468175924 8.999999999999998 5.176622456153680
18 -0.014520565820484 5.176622456153680 25.000000000000000


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Fatemeh Bagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.
Amir Shahmoradi, July 6 2024, 7:07 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.