Coursera Computational Finance and Financial Econometrics Week 4 Assignment 3

Let X and Y be distributed bivariate normal with muX=0.05, muY=0.025, sigX=0.10, sigY=0.05
a) rhoXY = 0.9
b) rhoXY = -0.9
c) rhoXY = 0

Using R package mvtnorm function rmvnorm(), simulate 100 observations from the bivariate distribution with rhoXY equal to
a), b) and c).

Using the plot() function create a scatterplot of the observations and comment on the direction and strength of the linear
association.

Using the function pmvnorm(), compute the joint probability Pr(X < 0,Y < 0).

library(mvtnorm)

#
# |------------------------------------------------------------------------------------------|
# | I N T E R N A L F U N C T I O N S |
# |------------------------------------------------------------------------------------------|
CalcSigmaMtx <- function(rho.xy, sig.x, sig.y) {
    # ---- Assert simulate from bivariate normal with rho
    sig.xy = rho.xy * sig.x * sig.y
    matrix(c(sig.x^2, sig.xy, sig.xy, sig.y^2), 2, 2, byrow = TRUE)
}

GenerateBiNormMtx <- function(rho.xy, n = 100, seed = 123, mu.x, mu.y, sig.x, 
    sig.y, sigma.xy) {
    # ---- Assert use the rmvnorm() function to simulate from bivariate normal
    set.seed(seed)
    xy.vals = rmvnorm(n, mean = c(mu.x, mu.y), sigma = sigma.xy)
}

#
# |------------------------------------------------------------------------------------------|
# | M A I N P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# ---- Assert THREE (3) plots ONE per chart
layout(matrix(1:1, 1, 1, byrow = TRUE))

muX = 0.05
sigX = 0.1
muY = 0.025
sigY = 0.05

# ---- Assert generate random values for rhoXY = 0.9
sigmaXY <- CalcSigmaMtx(rho.xy = 0.9, sigX, sigX)
sigmaXY
##       [,1]  [,2]
## [1,] 0.010 0.009
## [2,] 0.009 0.010
valXY <- GenerateBiNormMtx(rho.xy = 0.9, n = 100, seed = 123, muX, muY, sigX, 
    sigY, sigmaXY)
head(valXY)
##          [,1]       [,2]
## [1,] -0.03522 -0.0649601
## [2,]  0.04414  0.0345417
## [3,]  0.16897  0.0868786
## [4,]  0.03752 -0.0007032
## [5,]  0.01042 -0.0487659
## [6,]  0.19293  0.1122699

# ---- Assert scatterplot
plot(valXY[, 1], valXY[, 2], pch = 16, cex = 2, col = "blue", xlab = "x", ylab = "y")
title("Bivariate normal: rho=0.9")
abline(h = muY, v = muX)

plot of chunk unnamed-chunk-1


# ---- Assert compute area under bivariate standard normal distribution
# Find P( -00 < X < 0 and -00 < Y < 0)
pmvnorm(lower = c(-Inf, -Inf), upper = c(0, 0), mean = c(muX, muY), sigma = sigmaXY)
## [1] 0.2782
## attr(,"error")
## [1] 1e-15
## attr(,"msg")
## [1] "Normal Completion"

# ---- Assert generate random values for rhoXY = -0.9
sigma2XY <- CalcSigmaMtx(rho.xy = -0.9, sigX, sigX)
sigma2XY
##        [,1]   [,2]
## [1,]  0.010 -0.009
## [2,] -0.009  0.010
val2XY <- GenerateBiNormMtx(rho.xy = -0.9, n = 100, seed = 231, muX, muY, sigX, 
    sigY, sigma2XY)
head(val2XY)
##          [,1]     [,2]
## [1,] -0.05566  0.14982
## [2,] -0.08840  0.05607
## [3,] -0.13079  0.23512
## [4,]  0.03426  0.07166
## [5,]  0.05503 -0.02188
## [6,]  0.05082  0.04154

# ---- Assert scatterplot
plot(val2XY[, 1], val2XY[, 2], pch = 16, cex = 2, col = "blue", xlab = "x", 
    ylab = "y")
title("Bivariate normal: rho=-0.9")
abline(h = muY, v = muX)

plot of chunk unnamed-chunk-1


# ---- Assert compute area under bivariate standard normal distribution
# Find P( -00 < X < 0 and -00 < Y < 0)
pmvnorm(lower = c(-Inf, -Inf), upper = c(0, 0), mean = c(muX, muY), sigma = sigma2XY)
## [1] 0.003488
## attr(,"error")
## [1] 1e-15
## attr(,"msg")
## [1] "Normal Completion"

# ---- Assert generate random values for rhoXY = 0
sigma3XY <- CalcSigmaMtx(rho.xy = 0, sigX, sigX)
sigma3XY
##      [,1] [,2]
## [1,] 0.01 0.00
## [2,] 0.00 0.01
val3XY <- GenerateBiNormMtx(rho.xy = 0, n = 100, seed = 321, muX, muY, sigX, 
    sigY, sigma3XY)
head(val3XY)
##          [,1]     [,2]
## [1,]  0.22049 -0.10535
## [2,] -0.02120 -0.06687
## [3,]  0.02220  0.13833
## [4,]  0.03804  0.10678
## [5,]  0.03760 -0.09243
## [6,]  0.07682  0.01615

# ---- Assert scatterplot
plot(val3XY[, 1], val3XY[, 2], pch = 16, cex = 2, col = "blue", xlab = "x", 
    ylab = "y")
title("Bivariate normal: rho=0")
abline(h = muY, v = muX)

plot of chunk unnamed-chunk-1


# ---- Assert compute area under bivariate standard normal distribution
# Find P( -00 < X < 0 and -00 < Y < 0)
pmvnorm(lower = c(-Inf, -Inf), upper = c(0, 0), mean = c(muX, muY), sigma = sigma3XY)
## [1] 0.1238
## attr(,"error")
## [1] 1e-15
## attr(,"msg")
## [1] "Normal Completion"

#
# |------------------------------------------------------------------------------------------|
# | E N D O F S C R I P T |
# |------------------------------------------------------------------------------------------|