# Week3-4 - Matrix Algebra

## 3.8 - Matrix Albegra: Review part 1

• Here we note A a matrix nxm,
• n: number of rows
• m: number of columns
• In R, to create a matrix:
matA = matrix(data=c(1,2,3,4,5,6),nrow=2,ncol=3)
• To create a vector:
xvec = c(1,2,3)
• To convert:
xvec.mat = as.matrix(xvec)
• To transpose a matrix or vector:
t(mat)
t(xvec)
• A matrix A is symetric if $A=A^T$.

## 3.9 - Matrix Albegra: Review part 2

• Matrix multiplication is not commutative: $A \cdot B \neq B \cdot A$
• Matrix multiplication in R:
 A %*% B
• nxn identity matrix in R is created with:
diag(n)
• In R to inverse a matrix we use:
invA = solve(A)
• For a 2×2 matrix we can compute the inverse as:

$A^{-1} = \frac{1}{det(A)} \begin{bmatrix} a_{22} & -a_{12} \\ -a_{21} & a_{11} \end{bmatrix}, ~~ \text{assuming: } det(A) = a_{11}a_{22} - a_{12}a_{21} \neq 0$

• If we have a vector of 1 called one, then we can use: $one^T x = \sum\limits_{i=1}^n x_i$
• In R we have the function crossprod(x,y) = $x^Ty$

## 4.1 - Matrix Algebra: Portfolio Math

• We consider 3 assets with return rates $R_i$ normally distributed.
• The portfolio is normally distributed too. We can compute its mean and variance already.
• We then note: $R = \begin{bmatrix} R_A \\ R_B \\ R_C \end{bmatrix}$, $\mu = \begin{bmatrix} \mu_A \\ \mu_B \\ \mu_C \end{bmatrix}$, $\mathbf{1} = \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}$, $x = \begin{bmatrix} x_A \\ x_B \\ x_C \end{bmatrix}$
• And finally $\Sigma = \begin{bmatrix} \sigma_A^2 & \sigma_{AB} & \sigma_{AC} \\ \sigma_{AB} & \sigma_B^2 & \sigma_{BC} \\ \sigma_{AC} & \sigma_{BC} & \sigma_C^2 \end{bmatrix}$
• Portfolio weights sum to 1 so we have : $\mathbf{1}^Tx = 1$

### Portfolio return

• $R_{p,x} = x^T R = R^T x$

### Portfolio expected return

• $\mu_{p,x} = x^T\mu = \mu^T x$

### Portfolio variance

• $\sigma_{p,x}^2 = x^T \Sigma x$

### Covariance Between 2 portfolio returns

• 2 portfolio with weights x and y,
• The covariance between the 2 is: $Cov(R_{p,x},R_{p,y}) = x^T \Sigma y = y^T \Sigma x$

## 4.2 - Matrix Algebra: Bivariate Normal

• We note: $\mathbf{X} = \begin{bmatrix} X \\ Y \end{bmatrix}, ~ x = \begin{bmatrix} x \\ y \end{bmatrix}, ~ \mu = \begin{bmatrix} \mu_X \\ \mu_Y \end{bmatrix}, \Sigma = \begin{bmatrix} \sigma_X^2 & \sigma_{XY} \\ \sigma_{XY} & \sigma_Y^2 \end{bmatrix}$
• Then we can write the bivariate normal distribution as:

$f(x) = \frac{1}{2\pi det(\Sigma)^\frac 12} e^{- \frac 12 (x - \mu)^T \Sigma^{-1} (x-\mu)}$

• Where $det(\Sigma) = \sigma_X^2 \sigma_Y^2 (1 - \rho_{XY}^2)$
• We use the shorthand notation: $\mathbf{X} \sim N(\mu,\Sigma)$