Simulating Multivariate Distributions PDF 4
Simulating Multivariate Distributions PDF 4
Md Aktar Ul Karim
Statistical Simulation Course
Symbiosis Statistical Institute, Pune
Contents
1 Introduction 2
7 Excercises 20
1. Introduction
Multivariate distributions are extensions of univariate distributions where we analyze multiple random vari-
ables simultaneously. These distributions model the relationships between multiple variables and are commonly
used in fields like statistics, finance, and biological modeling.
Examples of multivariate distributions:
• Multivariate t-Distribution
• Practical examples in R
A multivariate distribution extends the concept of a univariate distribution (distribution of a single random
variable) to multiple random variables. It describes the probability structure for two or more random variables
taken together.
For example, if X1 , X2 , . . . , Xn are random variables, their joint distribution specifies the probabilities of
different outcomes for these variables considered as a whole.
• Joint Distribution: Describes how random variables interact with each other.
• Conditional Distribution: The distribution of some variables given specific values of others.
2
• Covariance and Correlation: Measures of how variables are related.
The joint distribution of random variables describes the probability structure for a set of variables X1 , X2 , ..., Xn .
For discrete random variables, it is represented by a joint probability mass function (PMF):
P (X1 = x1 , X2 = x2 , ..., Xn = xn ).
For continuous random variables, it is represented by a joint probability density function (PDF):
f (x1 , x2 , ..., xn ).
For a set of random variables X ∈ Rn , the joint distribution defines the probabilities of different outcomes for
all variables considered together.
Example:
1. Consider two random variables X1 and X2 which can take values from the set {1, 2, 3}. We want to derive
their joint probability mass function (PMF).
To compute the joint probability for any specific pair of values X1 = 2 and X2 = 3:
1
P (X1 = 2, X2 = 3) =
9
Similarly, we can compute joint probability for any other pair and all combinations of X1 and X2 have
equal probability.
This joint distribution describes the likelihood of any pair of X1 and X2 occurring simultaneously. As all
pairs have equal probability, these variables are uniformly distributed.
3
2. Let X and Y be continuous random variables with the joint probability density function (PDF):
2, if 0 ≤ x ≤ 1, 0 ≤ y ≤ 1, x + y ≤ 1
fX,Y (x, y) =
0, otherwise
• Find P (X ≤ 21 , Y ≤ 12 ).
By looking at the joint PDF fX,Y (x, y) ≥ 0 for all x, y is already proved.
Now we will compute the integral:
1 Z 1−x 1 1 1 !
x2
Z Z Z
1 1
2 dy dx = 2(1−x) dx = 2 (1 − x) dx =2 x− =2 1− −0 =2 =1
0 0 0 0 2 0 2 2
The marginal distribution of a random variable provides the probability distribution of that variable inde-
pendently from other variables in a multivariate setting. It is obtained by summing or integrating over the
values of the other random variables in the joint distribution.
For two continuous random variables X1 and X2 , the marginal distribution of X1 is obtained by integrating
out X2 from the joint probability density function (PDF) and is given by:
Z ∞
fX1 (x1 ) = f (x1 , x2 ) dx2
−∞
4
For two discrete random variables X1 and X2 , the marginal probability mass function (PMF) of X1 is obtained
by summing over all possible values of X2 :
X
P (X1 = x1 ) = P (X1 = x1 , X2 = x2 )
x2
Marginal distributions describe the probability of each variable on its own, ignoring the presence of other
variables.
Example:
1. Continuous Case: Let us consider the joint PDF of two random variables X1 and X2 as follows:
f (x1 , x2 ) = 6x1 x2 , 0 ≤ x1 ≤ 1, 0 ≤ x2 ≤ 1
The joint PDF is valid as the integral over the entire domain is equal to 1.
Z 1Z 1
6x1 x2 dx1 dx2 = 1
0 0
2. Discrete Case: Suppose the joint PMF of two discrete random variables X1 and X2 as follows:
X2 \X1 1 2 3
1 0.1 0.05 0.05
P (X1 = x1 , X2 = x2 ) =
2 0.2 0.1 0.1
3 0.05 0.1 0.25
5
To derive the marginal PMF of X1 , we sum over all possible values of X2 :
Similarly, we can derive the marginal PMF of X2 (left for the readers).
• For continuous cases, the marginal distribution is derived by integrating the joint PDF over the other
variable.
• For discrete cases, the marginal distribution is found by summing the joint PMF over the other variable.
The conditional distribution describes the probability of one random variable given the specific values of
other random variables. This allows us to understand how one variable behaves when the value of another is
fixed.
For continuous random variables, the conditional probability density function (PDF) of X1 given X2 = x2
is expressed as:
f (X1 = x1 , X2 = x2 )
f (X1 = x1 |X2 = x2 ) = ,
f (X2 = x2 )
where:
6
For discrete random variables, the conditional probability mass function (PMF) of X1 given X2 = x2 is
expressed as:
P (X1 = x1 , X2 = x2 )
P (X1 = x1 |X2 = x2 ) = ,
P (X2 = x2 )
where:
Example:
1. Continuous Case: Suppose X1 and X2 are continuous random variables with the joint PDF:
f (x1 , x2 ) = 6x1 x2 , 0 ≤ x1 ≤ 1, 0 ≤ x2 ≤ 1
To derive the conditional distribution of X1 given X2 = 0.5, first we have to derive the marginal PDF.
Now we compute the conditional PDF f (X1 |X2 = 0.5) by using the conditional distribution formula.
f (X1 = x1 , X2 = 0.5)
f (X1 = x1 |X2 = 0.5) =
fX2 (0.5)
3x1
f (X1 = x1 |X2 = 0.5) = = 2x1 , 0 ≤ x1 ≤ 1
1.5
Hence, the conditional distribution of X1 given X2 = 0.5 is f (X1 |X2 = 0.5) = 2x1 , which is a valid PDF
over [0, 1].
7
2. Discrete Case: Now we take the same discrete joint probability distribution table for X1 and X2 as in
the marginal case:
X2 \X1 1 2 3
1 0.1 0.05 0.05
P (X1 = x1 , X2 = x2 ) =
2 0.2 0.1 0.1
3 0.05 0.1 0.25
The marginal PMF of X2 can be written as:
Now, we use the conditional probability formula to derive the conditional PMF:
P (X1 = x1 , X2 = 2)
P (X1 = x1 |X2 = 2) =
P (X2 = 2)
P (X1 = 1, X2 = 2) = 0.2
P (X1 = 2, X2 = 2) = 0.1
P (X1 = 3, X2 = 2) = 0.1
The marginal probability P (X2 = 2) = 0.4. Now, the conditional probabilities are given by:
0.2
P (X1 = 1|X2 = 2) = = 0.5
0.4
0.1
P (X1 = 2|X2 = 2) = = 0.25
0.4
0.1
P (X1 = 3|X2 = 2) = = 0.25
0.4
8
2.5. Covariance
The variance-covariance between two random variables X1 and X2 measures how much the variables change
together. It is defined as:
Cov (X1 , X2 ) = E [(X1 − E [X1 ]) (X2 − E [X2 ])]
• If Cov (X1 , X2 ) < 0, one variable tends to increase when the other decreases.
2.6. Correlation
The correlation between two variables is a normalized measure of covariance that gives the strength and
direction of a linear relationship. It is defined as:
Cov(X1 , X2 )
Corr (X1 , X2 ) = p
V ar(X1 )V ar(X2 )
The correlation matrix is derived from the covariance matrix by normalizing each element.
Example:
9
1. Consider a discrete joint probability distribution for X1 and X2 given by the table:
X2 \X1 1 2 3
1 0.1 0.05 0.05
2 0.2 0.1 0.1
3 0.05 0.1 0.25
Sol: First, we have to compute E[X1 ] and E[X2 ], and for that we need to compute the marginal PMF
of X1 and X2 . Marginal distribution of X1 :
Marginal distribution of X2 :
10
Now we derive expectation of X1 and X2
X
E[X1 ] = x1 P (X1 = x1 ) = 1 × 0.35 + 2 × 0.25 + 3 × 0.4 = 0.35 + 0.5 + 1.2 = 2.05
x1
X
E[X2 ] = x2 P (X2 = x2 ) = 1 × 0.2 + 2 × 0.4 + 3 × 0.4 = 0.2 + 0.8 + 1.2 = 2.2
x2
E[X1 X2 ] = 1×1×0.1+1×2×0.2+1×3×0.05+2×1×0.05+2×2×0.1+2×3×0.1+3×1×0.05+3×2×0.1+3×3×0.25
E[X1 X2 ] = 0.1 + 0.4 + 0.15 + 0.1 + 0.4 + 0.6 + 0.15 + 0.6 + 2.25 = 4.75
For X1 , we have already computed E[X1 ] = 2.05. Now we need to compute E[X12 ].
X
E[X12 ] = x21 P (X1 = x1 )
x1
E[X12 ] = 12 × 0.35 + 22 × 0.25 + 32 × 0.4 = 0.35 + 1 × 0.25 + 9 × 0.4 = 0.35 + 1 + 3.6 = 4.95
So:
V ar(X1 ) = E[X12 ] − (E[X1 ])2 = 4.95 − (2.05)2 = 4.95 − 4.2025 = 0.7475
11
Using the marginal probabilities:
E[X22 ] = 12 × 0.2 + 22 × 0.4 + 32 × 0.4 = 0.2 + 4 × 0.4 + 9 × 0.4 = 0.2 + 1.6 + 3.6 = 5.4
So:
V ar(X2 ) = E[X22 ] − (E[X2 ])2 = 5.4 − (2.2)2 = 5.4 − 4.84 = 0.56
Now that we have the covariance and variances. From this we can compute the correlation:
Cov(X1 , X2 )
ρ(X1 , X2 ) = p p
V ar(X1 ) · V ar(X2 )
0.24
ρ(X1 , X2 ) = √ √
0.7475 · 0.56
Finally, the correlation is:
0.24
ρ(X1 , X2 ) = ≈ 0.3708
0.6471
The correlation between X1 and X2 is approximately 0.37. This indicates a moderate positive linear
relationship between X1 and X2 . The closer the correlation is to 1, the stronger the positive relationship,
while values closer to 0 indicate weaker relationships.
This is a joint PDF over the range [0, 1] for both X1 and X2 .
We integrate the joint distribution over the other variable to compute the marginal distribution.
Marginal PDF of X1 :
Z 1 Z 1 Z 1
fX1 (x1 ) = f (x1 , x2 ) dx2 = 2(1 − x1 )(1 − x2 ) dx2 = 2(1 − x1 ) (1 − x2 ) dx2
0 0 0
x22 1
R1 h i
1
The integral 0 (1 − x 2 ) dx 2 = x2 − 2 0 =1− 2 = 12 .
fX1 (x1 ) = 1 − x1 , 0 ≤ x1 ≤ 1
Marginal PDF of X2 :
12
Similarly, we can compute the marginal PDF of X2 :
Z 1 Z 1 Z 1
fX2 (x2 ) = f (x1 , x2 ) dx1 = 2(1 − x1 )(1 − x2 ) dx1 = 2(1 − x2 ) (1 − x1 ) dx1
0 0 0
R1 1
The integral 0 (1 − x1 ) dx1 = 1 − 2 = 21 .
We now need to calculate the expectations E[X1 ], E[X2 ], E[X12 ], E[X22 ], and E[X1 X2 ].
1 1 1 1
x2 x3
Z Z Z
1 1 1
E[X1 ] = x1 fX1 (x1 ) dx1 = x1 (1 − x1 ) dx1 = (x1 − x21 ) dx1 = 1− 1 = − =
0 0 0 2 3 0 2 3 6
1 1 1 1
x22 x32
Z Z Z
1 1 1
E[X2 ] = x2 fX2 (x2 ) dx2 = x2 (1 − x2 ) dx2 = (x2 − x22 ) dx2 = − = − =
0 0 0 2 3 0 2 3 6
1 1 1 1
x3 x4
Z Z Z
1 1 1
E[X12 ] = x21 fX1 (x1 ) dx1 = x21 (1 − x1 ) dx1 = (x21 − x31 ) dx1 = 1− 1 = − =
0 0 0 3 4 0 3 4 12
1 1 1 1
x3 x4
Z Z Z
1 1 1
E[X22 ] = x22 fX2 (x2 ) dx2 = x22 (1 − x2 ) dx2 = (x22 − x32 ) dx2 = 2− 2 = − =
0 0 0 3 4 0 3 4 12
Z 1Z 1 Z 1Z 1
E[X1 X2 ] = x1 x2 f (x1 , x2 ) dx1 dx2 = x1 x2 · 2(1 − x1 )(1 − x2 ) dx1 dx2
0 0 0 0
We have already calculated both integrals earlier, and both are 16 , so:
1 1 1
E[X1 X2 ] = 2 × × =
6 6 18
13
Now, the correlation is given by:
Cov(X1 , X2 )
ρ(X1 , X2 ) = p p
V ar(X1 ) V ar(X2 )
1 1 1
V ar(X2 ) = E[X22 ] − (E[X2 ])2 = − =
12 36 18
The multivariate normal distribution is one of the most important multivariate distributions. A random
vector X = (X1 , X2 , ..., Xn ) follows a multivariate normal distribution with mean vector µ ∈ Rn and covariance
matrix Σ ∈ Rn×n , denoted by X ∼ N (µ, Σ), if its PDF is:
1 1 T −1
f (X) = exp − (X − µ) Σ (X − µ)
(2π)n/2 |Σ|1/2 2
where:
Properties
• Marginal Distributions: The marginal distribution of any subset of the variables is also multivariate
normal.
• Conditional Distributions: The conditional distribution of one subset of variables given another is also
multivariate normal.
14
Cholesky Decomposition The Cholesky decomposition of the covariance matrix Σ is key to simulating
multivariate normal distributions. If Σ = LLT , where L is a lower triangular matrix, then we can simulate
X ∼ N (µ, Σ) by:
X = µ + LZ
The multivariate t-distribution generalizes the univariate t-distribution to multiple variables. It is useful
when the variables follow a normal distribution but the variance is unknown. If X follows a multivariate
t-distribution with degrees of freedom ν, mean µ, and scale matrix Σ, we write:
X ∼ t(ν, µ, Σ).
The PDF is similar to the multivariate normal distribution but includes an additional term for ν.
It extends the gamma distribution to multiple variables. It models positive random variables with a de-
pendency structure. The multivariate gamma distribution has applications in reliability analysis and survival
models.
It models the joint behavior of time-to-event variables. It is used in survival analysis and reliability engi-
neering, where we are interested in the time until multiple events occur (e.g., component failures).
• Independence
Two random variables X1 and X2 are independent if:
or equivalently, their joint PDF can be factorized into the product of their marginal PDFs:
For multivariate cases, independence indicates that all pairs of random variables are uncorrelated, and
their covariance matrix is diagonal.
15
• The covariance matrix describes the linear dependence between all pairs of variables, while the corre-
lation matrix standardizes this dependence.
The diagonal entries of the covariance matrix represent variances, while the off-diagonal entries represent
covariances.
Understanding the covariance or correlation structure is crucial for modeling multiple variables’ joint
behavior and designing simulation algorithms for multivariate distributions.
To simulate samples from a multivariate normal distribution we directly use mvtnorm packages.
# Load the package
library(mvtnorm)
16
6.2. Multivariate t-distribution
In multivariate t-distributions, the samples are not drawn purely based on a mean vector and a covariance
matrix.
The multivariate t-distribution introduces an additional parameter, degrees of freedom (df ), which controls
the heaviness of the tails of the distribution.
• When the degrees of freedom are very large, the multivariate t-distribution approaches a multivariate
normal distribution.
• For small degrees of freedom (e.g., less than 10), the tails become much heavier, increasing the likelihood
of generating extreme values.
• This parameter effectively adjusts the scale of the covariance matrix based on how much variability
(beyond normal distribution assumptions) is expected.
Z
t=µ+ p ,
W/ν
Where:
17
• Z is a sample from the multivariate normal distribution.
Figure 2: Tail part comparison of multivariate normal distribution and multivariate t-distribution
18
tail_t = get_tail_values(X_t)
plot(X_normal, col = ifelse(tail_normal, "red", "black"), main = "Multivariate Normal (tail in red)", pch = 2
# Set parameters
mu = c(1, 2) # Mean vector
Sigma = matrix(c(1, 0.8, 0.8, 1), 2, 2) # Covariance matrix
n = 10000 # Number of samples
df = 5 # Degrees of freedom for the t-distribution
19
X_t = rmvt(n, sigma = Sigma, df = df, delta = mu)
#Visualization
# Plot multivariate t-distribution tail
plot(X_t, col = ifelse(tail_t, "red", "black"),
main = "Multivariate t-distribution (tail in red)", pch = 20)
In the exercise, we will discuss the generation of simulated data from the other multivariate
distributions.
7. Excercises
1. Given a multivariate normal distribution X ∼ N (µ, Σ), simulate conditional distribution of X1 given
X2 = 5, where:
1 2 0.5
µ = ,Σ =
2 0.5 1
Sol.
Conditional distribution of X1 given X2 = 5 is normally distributed with:
Σ12
µ1|2 = µ1 + (X2 − µ2 )
Σ22
Σ212
Σ1|2 = Σ11 −
Σ22
Now we simulate values and plot the distribution.
# Plot
hist(X1_given_X2, main="Conditional Distribution", breaks=30)
20
Figure 4: Conditional distribution
2. Simulate 1000 samples from a bivariate normal distribution with mean vector
4
µ=
−2
# Given parameters
mean_vector = c(4, -2)
cov_matrix = matrix(c(2, 0.6, 0.6, 1.5), nrow = 2)
21
# Estimate sample mean and sample variance
sample_mean_X1 = mean(DF$X1)
sample_mean_X2 = mean(DF$X2)
sample_var_X1 = var(DF$X1)
sample_var_X2 = var(DF$X2)
3. Simulate 10000 samples from a bivariate normal distribution with the following parameters: Mean vector
1
µ=
2
Covariance matrix
1 0.5
Σ= .
0.5 1
Sol.: R code
# Packages
library(MASS)
22
cov_matrix = matrix(c(1, 0.5, 0.5, 1), nrow = 2)
hist(X1, main = "Histogram of X1", xlab = "X1", col = "lightblue", border = "black", freq = FALSE)
hist(X2, main = "Histogram of X2", xlab = "X2", col = "lightgreen", border = "black", freq = FALSE)
# Scatter plot
plot(X1, X2, main = "Scatter Plot of X1 vs X2", xlab = "X1", ylab = "X2", col = "blue", pch = 19)
Figure 5: Histogram plot of the marginal distributions and Scatter plot of the bivariate normal distributions
23
4. Simulate 500 samples from a trivariate normal distribution with the following parameters: Mean vector
0
µ= 1
−1
Covariance matrix
1 0.4 −0.2
Σ = 0.4
1 0.5
−0.2 0.5 1
Sol
24
Figure 7: scatter plots of X1 , X2 , X3 with X2
25
Results
26
# Load the MASS package
library(MASS)
# Parameter values
mean_vector = c(0, 1, -1)
cov_matrix = matrix(c(1, 0.4, -0.2,
0.4, 1, 0.5,
-0.2, 0.5, 1), nrow = 3)
plot(X1, X1, main = "X1 vs X1", xlab = "X1", ylab = "X1", col = "blue", pch = 19)
plot(X1, X2, main = "X1 vs X2", xlab = "X1", ylab = "X2", col = "red", pch = 19)
plot(X1, X3, main = "X1 vs X3", xlab = "X1", ylab = "X3", col = "green", pch = 19)
plot(X2, X1, main = "X2 vs X1", xlab = "X2", ylab = "X1", col = "red", pch = 19)
plot(X2, X2, main = "X2 vs X2", xlab = "X2", ylab = "X2", col = "blue", pch = 19)
plot(X2, X3, main = "X2 vs X3", xlab = "X2", ylab = "X3", col = "orange", pch = 19)
plot(X3, X1, main = "X3 vs X1", xlab = "X3", ylab = "X1", col = "green", pch = 19)
plot(X3, X2, main = "X3 vs X2", xlab = "X3", ylab = "X2", col = "orange", pch = 19)
plot(X3, X3, main = "X3 vs X3", xlab = "X3", ylab = "X3", col = "blue", pch = 19)
# Covariance matrix
sample_cov_matrix = cov(samples)
cat("Estimated Covariance Matrix:\n")
print(sample_cov_matrix)
# Correlation matrix
sample_corr_matrix = cor(samples)
cat("Estimated Correlation Matrix:\n")
print(sample_corr_matrix)
5. Simulate 1000 samples from a bivariate t-distribution with 5 degrees of freedom, mean vector
2
µ=
3
27
, and covariance matrix
1 0.6
Σ= .
0.6 1.5
Sol.
28
Figure 11: Scatter plot of two variables X1 and X2 following bivariate t- distributions
Results
# Parameters
mean_vector = c(2, 3)
29
cov_matrix = matrix(c(1, 0.6, 0.6, 1.5), nrow = 2)
df = 5 # Degrees of freedom
6. Simulate 500 samples from a trivariate gamma distribution. Let the shape parameters for the gamma
distributions be
k1 = 2, k2 = 3, k3 = 4,
(c) Estimate the sample mean and variance for each variable.
Sol.
30
Figure 12: Histograms of the marginal distributions of multivariate Gamma distributions
Results:
• Sample Mean:
2.093228, 4.470578, 8.312582
• Sample Variance:
2.081435, 6.920623, 16.16606
31
Figure 13: Scatter plot of three variables X1 , X2 , and X3 following multivariate Gamma distributions
# Parameters
shape_params = c(2, 3, 4)
scale_params= c(1, 1.5, 2)
32
bg = c("red", "blue", "green")[unclass(samples)])
7. Simulate 1000 samples from a bivariate exponential distribution using a copula-based approach. The
marginal distributions are exponential with rate parameters λ1 = 0.5 and λ2 = 1.
Sol.
Results:
• Sample Mean:
2.06757, 0.9578588
33
• Sample Correlation: 0.646122
Figure 15: Scatter plot of two variables X1 , and X2 following multivariate Exponential distributions
# Parameters
lambda1 = 0.5
lambda2 = 1
rho = 0.7 # Copula correlation
34
# Estimate sample mean and correlation
sample_mean = colMeans(samples)
sample_cor = cor(samples[, 1], samples[, 2])
35