4 Generating Random Samples of Multidimensional Variables
Multivariate models have gained wide acceptance in the scientific community due to computational advances and the development of specialized software in this area. Natural phenomena are generally multivariate. A treatment applied to a living being, to a soil, or to a system does not affect only a single variable in isolation, but rather all variables. Moreover, the variables are related to one another, and any change in one or some of them affects the others. Thus, the generation of realizations of random vectors or matrices is a subject that cannot be ignored. In this chapter, we will provide the reader with mechanisms to generate realizations of multidimensional random variables.
4.1 Introduction
The procedures for generating multidimensional random variables are often considered difficult by most researchers. A good portion of them, however, can be carried out in Python with just a single line of code. Despite these conveniences, in these notes we will present details of some procedures for generating data from the main multivariate probabilistic models, such as the multivariate normal, the Wishart and inverse Wishart, the multivariate Student’s \(t\), and some other distributions.
One of the main characteristics of multidimensional variables is the correlation among their components. The importance of these models is practically indescribable, but we can highlight parametric inference, Bayesian inference, estimation of confidence regions, among others. In the following sections, we will address ways to generate realizations of multidimensional random variables for specific models using Python either to implement the routines or to use pre-existing routines. Our apparent waste of time, describing functions that are less efficient than those already available in Python, has as its fundamental reason allowing the reader to go beyond simply using routines previously programmed by third parties. If the reader gains, by the end of reading this material, the ability to produce their own routines and to understand how pre-existing routines work, our objective will have been achieved.
4.2 Multivariate Normal Distribution
The multivariate normal probability density function of a random vector \(\mathbf{X}\) is given by:
\[f_{\mathbf{X}}(\mathbf{x})= (2\pi)^{-\frac{p}{2}}\left|\mathbf{\Sigma}\right|^{-\frac{1}{2}} \exp \left\{-\dfrac{1}{2}(\mathbf{x} - \mathbf{\mu})^\top \mathbf{\Sigma}^{-1} (\mathbf{x}- \mathbf{\mu}) \right\} \tag{4.1}\]
where \(\mathbf{\mu}\) and \(\mathbf{\Sigma}\) are, respectively, the mean vector and the covariance matrix, symmetric and positive definite, and \(p\) is the dimension of the random vector \(\mathbf{X}\).
An important result concerns linear combinations of multivariate normal variables and will be presented in the following theorem.
Theorem 4.1 (Linear Combinations) Consider the multivariate normal random vector \(\mathbf{X}=[X_1, X_2,\cdots, X_p]^\top\) with mean \(\mathbf{\mu}\) and covariance \(\mathbf{\Sigma}\), and consider \(\mathbf{C}\) a \((p\times p)\) matrix of rank \(p\). Then the linear combination \(\mathbf{Y}=\mathbf{C}\mathbf{X}\) \((p\times 1)\) has a multivariate normal distribution with mean \(\mathbf{\mu}_{{\mathbf{Y}}}=\mathbf{C}\mathbf{\mu}\) and covariance \(\mathbf{\Sigma}_{{\mathbf{Y}}}=\mathbf{C}\mathbf{\Sigma}\mathbf{C}^\top\).
Proof. If \(\mathbf{C}\) has rank \(p\), then \(\mathbf{C}^{-1}\) exists and, therefore, \[\begin{align*} \mathbf{X}=\mathbf{C}^{-1}\mathbf{Y}. \end{align*}\]
The Jacobian of the transformation is \(J=|\mathbf{C}|^{-1}\), and the distribution of \(\mathbf{Y}\) is given by \(f_{\mathbf{Y}}(\mathbf{y})=\) \(f_{\mathbf{X}}(\mathbf{x})|J|\). If \(\mathbf{X}\) has a multivariate normal distribution, then \[\begin{align*} f_{\mathbf{Y}}(\mathbf{y}) = & (2\pi)^{-p/2} \left|\mathbf{\Sigma}\right|^{-\frac{1}{2}} \exp\left\{-\dfrac{1}{2}(\mathbf{C}^{-1}\mathbf{y}-\mathbf{\mu})^\top \mathbf{\Sigma}^{-1}(\mathbf{C}^{-1}\mathbf{y}-\mathbf{\mu})\right\}|\mathbf{C}|^{-1}\\ \\ = & (2\pi)^{-p/2} |\mathbf{C}|^{-1/2} \left|\mathbf{\Sigma}\right|^{-\frac{1}{2}}|\mathbf{C}|^{-1/2}\\ &\times \exp\left\{-\dfrac{1}{2}(\mathbf{y}-\mathbf{C}\mathbf{\mu})^\top\left(\mathbf{C}^\top\right)^{-1} \mathbf{\Sigma}^{-1}\mathbf{C}^{-1}(\mathbf{y}-\mathbf{C}\mathbf{\mu})\right\}\\ \\ = & (2\pi)^{-p/2} \left|\mathbf{C}\mathbf{\Sigma}\mathbf{C}^\top\right|^{-\frac{1}{2}} \exp\left\{-\dfrac{1}{2}(\mathbf{y}-\mathbf{C}\mathbf{\mu})^\top \left(\mathbf{C}\mathbf{\Sigma}\mathbf{C}^\top\right)^{-1}(\mathbf{y}-\mathbf{C}\mathbf{\mu})\right\}\\ \end{align*}\] which is the multivariate normal density of the random vector \(\mathbf{Y}\) with mean \(\mathbf{\mu}_{{\mathbf{Y}}}=\mathbf{C}\mathbf{\mu}\) and covariance \(\mathbf{\Sigma}_{{\mathbf{Y}}}=\mathbf{C}\mathbf{\Sigma}\mathbf{C}^\top\). Therefore, linear combinations of multivariate normal variables are multivariate normal.
Theorem 4.1 provides us with the main result for generating data from a multivariate normal. Thus, since our objective is to generate data from a multivariate normal sample with a pre-established mean vector \(\mathbf{\mu}\) and covariance matrix \(\mathbf{\Sigma}\), we should follow the following procedures. First, we must obtain the square root matrix of \(\mathbf{\Sigma}\), denoted by \(\mathbf{\Sigma}^{1/2}\). For this, we consider the spectral decomposition of the covariance matrix given by \(\mathbf{\Sigma}=\mathbf{P}\mathbf{\Lambda}\mathbf{P}^\top\). Hence, we can define the square root matrix of \(\mathbf{\Sigma}\) as \(\mathbf{\Sigma}^{1/2}=\mathbf{P}\mathbf{\Lambda}^{1/2}\mathbf{P}^\top\), where \(\mathbf{\Lambda}\) is the diagonal matrix of eigenvalues, \(\mathbf{\Lambda}^{1/2}\) is the diagonal matrix containing the square root of these elements, and \(\mathbf{P}\) is the matrix of eigenvectors, each of these vectors arranged in one of its columns. From this decomposition, we can easily observe that \(\mathbf{\Sigma}=\mathbf{\Sigma}^{1/2}\mathbf{\Sigma}^{1/2}\).
Thus, we can use the following method to generate a \(p\)-variate realization from a multivariate normal. First, we must generate a random vector \(\mathbf{Z}=[Z_1\), \(Z_2\), \(\cdots\), \(Z_p]^\top\) of \(p\) independent standard normal variables using, for example, the Box–Müller algorithm. This means that \(\mathbf{\mu}_{\mathbf{Z}} = \mathbf{0}\) and that Cov\((\mathbf{Z})=\mathbf{I}\). This vector must undergo the following linear transformation: \[ \mathbf{Y} = \mathbf{\Sigma}^{1/2}\mathbf{Z}+\mathbf{\mu}. \tag{4.2}\]
According to Theorem 4.1, the vector \(\mathbf{Y}\) has a multivariate normal distribution with mean \(\mathbf{\mu}_{\mathbf{Y}} =\mathbf{\Sigma}^{1/2} \mathbf{\mu}_{\mathbf{Z}}+\mathbf{\mu} = \mathbf{\mu}\) and covariance matrix \(\mathbf{\Sigma}^{1/2}\mathbf{I}\mathbf{\Sigma}^{1/2} = \mathbf{\Sigma}\). This will be the method we use to obtain the \(p\)-dimensional sample of size \(n\) from a multivariate normal with mean \(\mathbf{\mu}\) and covariance \(\mathbf{\Sigma}\). To obtain the square root matrix in Python, we can use the command to obtain the spectral decomposition np.linalg.svd() or alternatively the command linalg.cholesky(a,/,*,upper=False), which returns the Cholesky factor of a positive definite matrix, which is in fact a type of square root. The singular value decomposition in this case specializes to the spectral decomposition, since the matrix \(\mathbf{\Sigma}\) is symmetric. We will generate a vector of independent standard normal random variables \(\mathbf{Z}\) using the command np.random.normal(). Then transformation (4.2) is performed.
Let us illustrate and present the program for generating multivariate normal variables for a particular bivariate case \((p=2)\) with mean vector \(\mathbf{\mu}=[10, 50]^\top\) and covariance matrix given by: \[\begin{align*} \mathbf{\Sigma} = & \left[\begin{array}{cc} 4 & 1 \\ 1 & 1 \end{array}\right]. \end{align*}\]
The resulting program is given by:
In some browsers (especially Firefox), displaying plots with plt.show() in the interactive environment of this page may fail. If the plot does not appear, open the material in Google Chrome, Microsoft Edge, or Safari.
In Python, we have the numpy generator random.multivariate_normal(mean, cov, size=None, check_valid='warn', tol=1e-8) for generating data from the multivariate normal distribution. In the following script, we apply both our function and the numpy function to generate a large number of observations and compare their performance in terms of processing time. The numpy function protects the process by, for example, checking whether the covariance matrix is positive definite. In our function, we do not use any protection, although it would be possible to do so.
The numpy routine was many times faster than our implementation. This is partly due to the fact that it is compiled and, possibly, also to the method used to obtain the square root matrix. Swapping the svd square root matrix for Cholesky or the spectral decomposition could be done, and the average performance of the procedures evaluated, to define the best implementation strategy. We made a change to our implementation. We added an argument with one of three options so that the user can choose which method to use. Obtaining the square root was implemented in a separate function.
The comparison between them was presented in the following script:
Although our function is relatively fast, taking between 2 and 16 microseconds to run each bivariate observation in this case, it was still far less efficient than the numpy routine. A more detailed and separate evaluation of the effect of using the three methods for obtaining the “square root” matrix is necessary, given that the dimension is small (\(p=2\)) in the example and only one call is made to generate \(n\) multivariate random vectors. The main bottleneck in this case was the loop used, and not the method for obtaining the square root of the covariance matrix in the example. Compilation is fundamental in relation to interpretation. It is extremely simple to generate multivariate normal data using the numpy function, which frees us from having to program our own random generators. We reiterate that we did this because we want our reader and our student to be able to uncover what lies behind each method here and also to develop their programming skills by implementing sophisticated routines such as these.
4.3 Wishart and Inverse Wishart Distribution
The Wishart and inverse Wishart distributions are related to the distributions of matrices of uncorrected sums of squares and cross products \(\mathbf{W}\) obtained from samples of size \(n-1\) of the multivariate normal distribution with mean \(\mathbf{0}\). Consider \(\mathbf{X}_j=[X_1\), \(X_2\), \(\cdots\), \(X_p]^\top\) the \(j\)-th vector \((j=1\), \(2\), \(\cdots\), \(\nu)\) of a random sample of size \(\nu\) from a normal with mean \(\mathbf{0}\) and covariance \(\mathbf{\Sigma}\). Then the random matrix \[\begin{align*} \mathbf{W}=&\sum_{j=1}^{n-1} \mathbf{X}_j\mathbf{X}_j^\top \end{align*}\] has a Wishart distribution with \(n-1\) degrees of freedom and parameter \(\mathbf{\Sigma}\) (positive definite matrix).
Likewise, if we have a random sample of size \(n\) from a multivariate normal distribution with mean \(\mathbf{\mu}\) and covariance \(\mathbf{\Sigma}\), the distribution of the random matrix \[\begin{align*} \mathbf{W}=& \sum_{j=1}^n (\mathbf{X}_j-\mathbf{\bar{X}})(\mathbf{X}_j-\mathbf{\bar{X}})^\top \end{align*}\] is Wishart with \(\nu = n - 1\) degrees of freedom and parameter \(\mathbf{\Sigma}\).
The Wishart density function of a random matrix \(\mathbf{W}\) of sums of squares and cross products, denoted by \(W_p(\nu,\mathbf{\Sigma})\), is defined by:
\[f_{\mathbf{W}}(\mathbf{w}|\nu,\mathbf{\Sigma})\! =\! \dfrac{|\mathbf{\Sigma}|^{-\nu/2}|\mathbf{w}|^{(\nu-p-1)/2}} {2^{\nu p/2}\pi^{p(p-1)/4} \displaystyle \prod_{i=1}^p \Gamma\left(\frac{\nu-i+1}{2}\right)} \exp\left\{-\frac{tr\left(\mathbf{\Sigma}^{-1}\mathbf{w}\right)}{2} \right\} \tag{4.3}\] where \(\Gamma(x) = \int_0^\infty t^{x-1}e^{-x}dt\) is the gamma function.
Thus, to generate Wishart variables with parameters \(n-1\) integer degrees of freedom and positive definite matrix \(\mathbf{\Sigma}\), we can use a generator of multivariate normal random samples and obtain the sample matrix of sums of squares and cross products. This matrix will be a realization of a Wishart random variable, which is a matrix of dimension \(p \times p\). The following function can be used to obtain random realizations from a Wishart:
Another related distribution that frequently appears in multivariate inference is the inverse Wishart. Consider \(\mathbf{W}\) a random matrix \(W_p(\nu, \mathbf{\Sigma})\). Then the distribution of \(\mathbf{S} = \mathbf{W}^{-1}\), given by the density function
\[f_{\mathbf{S}}(\mathbf{s}|\nu,\mathbf{\Sigma})\! =\! \dfrac{|\mathbf{\Sigma}^{-1}|^{\nu/2}|\mathbf{s}|^{-(\nu+p+1)/2}} {2^{\nu p/2}\pi^{p(p-1)/4} \displaystyle \prod_{i=1}^p \Gamma\left(\frac{\nu-i+1}{2}\right)} \exp\left\{-\frac{tr\left(\mathbf{\Sigma}^{-1}\mathbf{s}^{-1}\right)}{2} \right\} \tag{4.4}\] is the inverse Wishart, which we will denote by \(W_p^{-1}(\nu, \mathbf{\Sigma})\).
If we want to generate a random matrix from an inverse Wishart distribution instead of a Wishart, we simply need to perform the transformation \(\mathbf{S} = \mathbf{W}^{-1}\). Thus, we will modify the previous program to generate realizations from both the Wishart and the inverse Wishart distributions. It is important to note that in our notation the parameters of the inverse Wishart are those of the Wishart. This is relevant, since some authors present the parameters of the inverse Wishart rather than those of the Wishart, and we must be attentive to this fact; otherwise, we will generate random variables with different densities.
An important aspect we need to mention concerns the need to generate Wishart or inverse Wishart variables with real degrees of freedom. For this, we can use the algorithm described by Smith and Hocking (1972). Consider the decomposition of the matrix \(\mathbf{\Sigma}\) given by \(\mathbf{\Sigma}=\mathbf{\Sigma}^{1/2}\mathbf{\Sigma}^{1/2}\), which we will use to perform a transformation on the generated random matrix that, according to properties of Wishart matrices, will be Wishart.
We must first construct a lower triangular matrix \(\mathbf{T}=[t_{ij}]\) \((i,j=1\), \(2\), \(\cdots\), \(p)\), with \(t_{ii}\sim\sqrt{\chi^2_{\nu+1-i}}\) and \(t_{ij}\sim N(0,1)\) if \(i>j\), and \(t_{ij}=0\) if \(i<j\). Then we must obtain the matrix \(\mathbf{\Gamma}=\mathbf{T}\mathbf{T}^\top\), which has distribution \(W_p(\nu, \mathbf{I})\). In this way, we obtain \(\mathbf{W}=\mathbf{\Sigma}^{1/2}\mathbf{\Gamma}\mathbf{\Sigma}^{1/2}\) with the desired distribution, that is, with distribution \(W_p(\nu,\mathbf{\Sigma})\). It is clear that with this algorithm we can generate Wishart variables with real or integer degrees of freedom.
We would like to draw attention to some facts about this function, since we used some commands and resources not mentioned up to this point. First, we used the command np.diag(np.random.chisquare(df, p)**0.5) to fill the diagonal of the matrix \(\mathbf{T}\) with realizations of chi-square random variables and then the diag method to transform the vector into a diagonal matrix.
Another interesting aspect that deserves mention is the use of the Cholesky factor instead of obtaining the square root matrix of \(\mathbf{\Sigma}\). The Cholesky factor uses the decomposition \(\mathbf{\Sigma} = \mathbf{S}\mathbf{S}^\top\), where \(\mathbf{S}\) is a lower triangular matrix. Python, through the function np.linalg.cholesky(sigma), returns the matrix \(\mathbf{S}\). Finally, if the number of variables is equal to \(1\), the Wishart distribution specializes to the chi-square and the inverse Wishart to the inverse chi-square. Thus, when \(p=1\), the algorithm will return variables \(\sigma^2 X\) and \(1/(\sigma^2 X)\) with distributions proportional to the chi-square and inverse chi-square distributions, respectively, where \(X\) is a chi-square variable with \(\nu\) degrees of freedom.
The processing for the scalar case was controlled with the use of if, since matrix operations are not applicable if the arguments are integer scalars or float (real) numbers.
4.4 Multivariate Student’s t Distribution
The family of elliptical distributions is very important in the multivariate setting and in Bayesian applications. The multivariate Student’s \(t\) distribution is a particular case of this family. This distribution is of particular interest in procedures for multiple comparison of treatments against some control evaluated in the experiment. The density function of the random vector \(\mathbf{X} = [X_1, X_2\), \(\cdots\), \(X_p]^\top\) \(\in \mathbb{R}^p\) with parameters given by the mean vector \(\mathbf{\mu} = [\mu_1, \mu_2, \cdots, \mu_p]^\top\) \(\in \mathbb{R}^p\) and symmetric positive definite matrix \(\mathbf{\Sigma}\) \((p\times p)\) is
\[\begin{align*} f_{\mathbf{X}}(\mathbf{x}) = & \dfrac{g((\mathbf{x}-\mathbf{\mu})^\top\mathbf{\Sigma}^{-1}(\mathbf{x}-\mathbf{\mu}))}{|\mathbf{\Sigma}|^{1/2}}\nonumber \\ =& \dfrac{\Gamma\left(\frac{\nu+p}{2}\right)}{(\pi\nu)^{p/2}\Gamma(\nu/2)|\mathbf{\Sigma}|^{1/2}} \left[1+\dfrac{1}{\nu}(\mathbf{x}-\mathbf{\mu})^\top\mathbf{\Sigma}^{-1}(\mathbf{x}-\mathbf{\mu})\right]^{-\frac{\nu+p}{2}} \end{align*}\] where the function \(g\) is given by \[g(z) = \dfrac{\Gamma \left( \frac{\nu+p}{2} \right)}{(\pi\nu)^{p/2} \Gamma(\nu/2)} \left(1 + \dfrac{z}{\nu} \right)^{-(\nu+p)/2}.\] The random variable \(\mathbf{X}\) has mean \(\mathbf{\mu}\) and covariance matrix \(\nu\mathbf{\Sigma}/(\nu-2)\) in the case \(\nu>2\).
If we perform the transformation \(\mathbf{Y} = \mathbf{\Sigma}^{-1/2} (\mathbf{X} - \mathbf{\mu})\), we obtain the symmetric spherical multivariate \(t\) distribution , whose density is given by:
\[ f_{\mathbf{Y}}(\mathbf{y}) = \dfrac{\Gamma\left(\frac{\nu+p}{2}\right)}{(\pi\nu)^{p/2}\Gamma(\nu/2)} \left[1+\dfrac{1}{\nu}\mathbf{y}^\top\mathbf{y}\right]^{-\frac{\nu+p}{2}}. \tag{4.5}\]
The random variable \(\mathbf{Y}\) will have a null mean vector and covariance \(\nu\mathbf{I}/(\nu-2)\), and the density will have spherical contours of equal probability.
We will present the general form for generating \(p\)-dimensional multivariate \(t\) random variables with \(\nu\) degrees of freedom and parameters \(\mathbf{\mu}\) and \(\mathbf{\Sigma}\). Let \(\mathbf{Z}\) be a random vector with distribution \(N_p(\mathbf{0}, \mathbf{I})\) and \(U\) a random variable with chi-square distribution with \(\nu\) degrees of freedom. Then the random vector \(\mathbf{Y}\), given by the transformation \[ \mathbf{Y} = \sqrt{\nu} \dfrac{\mathbf{Z}}{\sqrt{U}}, \tag{4.6}\] has a spherical multivariate \(t\) distribution with \(\nu\) degrees of freedom. The vector \(\mathbf{X}\) obtained by the linear transformation
\[ \mathbf{X} = \mathbf{\Sigma}^{1/2} \mathbf{Y}+\mathbf{\mu}, \tag{4.7}\] has an elliptical multivariate \(t\) distribution with \(\nu\) degrees of freedom and parameters \(\mathbf{\mu}\) and \(\mathbf{\Sigma}\).
We must apply transformation (4.7) \(n\) times to \(n\) different random vectors \(\mathbf{Y}\) and variables \(U\). At the end of this process, we will have a sample of size \(n\) from the desired multivariate \(t\) distribution with \(\nu\) degrees of freedom. Thus, to generate data from a multivariate \(t\) with dimension \(p\), degrees of freedom \(\nu\) (not necessarily integer), any mean vector \(\mathbf{\mu}\), and positive definite matrix \(\mathbf{\Sigma}\), we can use the following Python function, replacing in expression (4.7) the square root matrix with the Cholesky factor \(\mathbf{F}\) of \(\mathbf{\Sigma}\):
Positive real degrees of freedom can be used as an argument to the function created. In the previous example, two random samples were generated for illustration. The values \([0,\), \(0\), \(0]^\top\), \(\mathbf{I}_3\), and \(3\) were defined as default for the parameters \(\mathbf{\mu}\), \(\mathbf{\Sigma}\), and \(\nu\), respectively. We can also use the implementation provided by the function multivariate_t.rvs(mu, sigma, df n) from the scipy library to generate data from the multivariate Student’s \(t\) distribution.
4.5 Other Multivariate Distributions
There are many other multivariate distributions. We will mention only two more of them: the multivariate log-normal and the multivariate contaminated normal. Generating a random vector of dimension \(p\) with a multivariate log-normal distribution is done by taking the following vector \(\mathbf{Y} = [\exp(Z_1)\), \(\exp(Z_2)\), \(\cdots\), \(\exp(Z_p)]^\top\), where \(\mathbf{Z}\sim N_p(\mathbf{\mu}, \mathbf{\Sigma})\).
To generate realizations from the multivariate contaminated normal, we consider the simulation of a random vector \(\mathbf{X}\) whose probability density function is given by:
\[\begin{align*} f_{\mathbf{X}}(\mathbf{x}) =& \delta (2\pi)^{-p/2} |\mathbf{\Sigma}_1|^{-1/2}\exp\left\{ -\dfrac{(\mathbf{x}-\mathbf{\mu}_1)^\top\mathbf{\Sigma}_1^{-1}(\mathbf{x}-\mathbf{\mu}_1)}{2}\right\}\nonumber\\ & +(1-\delta)(2\pi)^{-p/2} |\mathbf{\Sigma}_2|^{-1/2}\exp\left\{-\dfrac{(\mathbf{x} -\mathbf{\mu}_2)^\top\mathbf{\Sigma}_2^{-1}(\mathbf{x}-\mathbf{\mu}_2)}{2}\right\} \end{align*}\] where \(\mathbf{\Sigma}_i\) is positive definite, \(i=1, 2\), and \(0\le \delta \le 1\).
If the mixture has the same mean \(\mathbf{\mu}_1\) \(=\) \(\mathbf{\mu}_2\) \(=\) \(\mathbf{\mu}\) and the covariances are merely multiples of one another \(\mathbf{\Sigma}_2\) \(=\) \(c\mathbf{\Sigma}_1\), for \(c>0\), then the contaminated multivariate normal distribution belongs to the elliptical family.
4.6 Exercises
Generate a sample of size \((n = 10)\) from a trivariate normal distribution with mean vector \(\mathbf{\mu}=[5, 10, 15]^\top\) and covariance matrix \[\begin{align*} \mathbf{\Sigma} = & \left[\begin{array}{ccc} 5 & -1 & 2 \\ -1 & 3 & -2 \\ 2 & -2 & 7 \end{array}\right]. \end{align*}\] Estimate the sample mean and covariance. Repeat this process \(1{,}000\) times and estimate the mean of the sample means and the mean of the sample covariance matrices. Do these values correspond exactly to the respective expected values? If not, present the main cause(s).
In the original text, there was a modification of the
rnormmvfunction producing thernormmv_2function, and then the average processing speeds were compared when using one of the following options for the “square root” matrix of \(\mathbf{\Sigma}\):svd,eig, orcholeskyfromlinalg. In this exercise, perform a further improvement of thernormmv_2function by implementing a function to eliminate theloopthrough the use of vectorization. Verify whether there will be an improvement in performance relative to the average processing speeds for each generated random variable, which is the \(p\)-dimensional vector. Test this both on the vectorized version ofrnormmv_2and on the originalrnormmv_2version using new values of \(p\), since in the original text only a single value of \(p\) was tested, and a small one at that, \(p=2\).We know that Wishart variables have mean \(\nu \mathbf{\Sigma}\). Present a program to verify whether the expected value, ignoring Monte Carlo error, is achieved using the functions presented in this Chapter to generate Wishart variables. Use 10,000 Monte Carlo replications. This would be a simple, though not conclusive, way of checking whether each function is performing the simulations correctly. It serves at least to indicate the presence of error, but does not guarantee correctness, which is obviously guaranteed by the underlying theory.
Implement Python functions to generate multivariate log-normal and elliptical contaminated-normal random variables.