---
title: "Completion of Assignment 2: Eigennumbers and Eigenvectors"
author: "Ahmed MUSTAFA <br> (& Google Gemini)"
date: "October 15, 2026"
format:
html:
toc: true
toc-location: left
theme: united
highlight-style: tango
embed-resources: true
code-fold: show
code-tools: true
---
## Small Exercise: Variance-Covariance Matrix and Eigen-Decomposition
::: {.callout-note}
### Exercise Overview & Objective
Here, we analyze the joint return behavior of two investment assets, **Asset X** and **Asset Z**, under three different economic phases: **Expansion**, **Normal growth**, and **Recession**. We construct the variance-covariance matrix using vectors and matrix multiplication, and then calculate its eigenvalues and eigenvectors to understand the main direction of portfolio risk.
:::
### 1. Defining Probabilities and Asset Returns Across Economic Phases
We first set up:
- The probability vector $\mathbf{p}$ for the three economic phases (summing to 1).
- The return matrix $\mathbf{R}$, where each row represents an economic phase and each column represents an asset's return in percent.
$$\mathbf{p} = \begin{pmatrix} p_{\text{Expansion}} \\ p_{\text{Normal}} \\ p_{\text{Recession}} \end{pmatrix} = \begin{pmatrix} 0.25 \\ 0.45 \\ 0.30 \end{pmatrix}$$
$$\mathbf{R} = \begin{pmatrix} 12 & 21 \\ 10 & 14 \\ 8 & 9 \end{pmatrix}$$
```{r}
#| label: setup-matrix-data
# Probabilities of the three economic phases (as decimals)
p <- c(Expansion = 0.25, Normal = 0.45, Recession = 0.30)
# Asset returns (%) across each economic phase
R <- matrix(
c(12, 21,
10, 14,
8, 9),
nrow = 3,
byrow = TRUE,
dimnames = list(
c("Expansion", "Normal", "Recession"),
c("Asset_X", "Asset_Z")
)
)
p
R
```
---
### 2. Calculating the Variance-Covariance Matrix
To measure how each asset varies on its own and how both move together:
1. **Expected return ($\boldsymbol{\mu}$):** Multiply each phase's return by its likelihood.
2. **Deviation matrix ($\mathbf{\tilde{R}}$):** Subtract the expected return from the returns in each economic phase.
3. **Variance-Covariance matrix ($\mathbf{\Sigma}$):** Weight the squared deviations and cross-products by their probabilities using matrix multiplication.
```{r}
#| label: compute-covariance-matrix
# 1. Expected return for each asset
mu <- t(R) %*% p
colnames(mu) <- "Expected_Return"
# 2. Difference between returns in each phase and the expected return
ones_vec <- rep(1, length(p))
Deviation <- R - (ones_vec %*% t(mu))
# 3. Probability weight matrix along the diagonal
P_mat <- diag(p)
# 4. Variance-Covariance matrix
Variance_Covariance <- t(Deviation) %*% P_mat %*% Deviation
dimnames(Variance_Covariance) <- list(colnames(R), colnames(R))
mu
Deviation
Variance_Covariance
```
::: {.callout-tip}
### What the Covariance Matrix Shows
- **Variance of Asset X:** $2.19$ (standard deviation $\approx 1.48\%$)
- **Variance of Asset Z:** $19.69$ (standard deviation $\approx 4.44\%$)
- **Covariance between X and Z:** $6.525$
Asset Z exhibits much wider swings across economic phases than Asset X, and the positive covariance shows that both assets move in the same direction. Both experience a gain during expansions and a drop during recessions.
:::
---
### 3. Eigen-Decomposition
The eigenvalues ($\lambda$) and eigenvectors ($\mathbf{v}$) of the Variance-Covariance matrix ($\mathbf{\Sigma}$) satisfy the fundamental equation:
$$\mathbf{\Sigma} \mathbf{v} = \lambda \mathbf{v} \quad \Longleftrightarrow \quad (\mathbf{\Sigma} - \lambda \mathbf{I}) \mathbf{v} = \mathbf{0}$$
Non-trivial solutions exist when the characteristic polynomial equals zero:
$$\det(\mathbf{\Sigma} - \lambda \mathbf{I}) = 0$$
For a symmetric Variance-Covariance matrix, this yields the spectral decomposition:
$$\mathbf{\Sigma} = \mathbf{V} \mathbf{\Lambda} \mathbf{V}^T$$
where $\mathbf{\Lambda} = \begin{pmatrix} \lambda_1 & 0 \\ 0 & \lambda_2 \end{pmatrix}$ is the diagonal matrix of eigenvalues, and $\mathbf{V} = \begin{pmatrix} \mathbf{v}_1 & \mathbf{v}_2 \end{pmatrix}$ is the orthogonal matrix of eigenvectors.
The proportion of total portfolio risk explained by each component $i$ is calculated as:
$$\text{Proportion}_i = \frac{\lambda_i}{\sum_{j} \lambda_j} \times 100\%$$
Eigenvalues tell us how much total risk is concentrated in each dimension, while eigenvectors give the direction (the asset combination) representing that risk.
```{r}
#| label: eigen-decomposition
# Calculate eigenvalues and eigenvectors
eig_decomp <- eigen(Variance_Covariance, symmetric = TRUE)
lambda_vals <- eig_decomp$values
V_vecs <- eig_decomp$vectors
dimnames(V_vecs) <- list(colnames(R), c("Component_1", "Component_2"))
lambda_vals
V_vecs
# Percentage of total risk explained by each component
prop_variance <- (lambda_vals / sum(lambda_vals)) * 100
names(prop_variance) <- c("Component_1", "Component_2")
round(prop_variance, 2)
```
::: {.callout-note}
### Practical Interpretation
The first eigenvalue accounts for **`r round(prop_variance[1], 2)`%** of the total variance. Because this first component captures virtually all joint movement, diversification between Asset X and Asset Z across these three economic phases is limited. A broad economic downturn hurts both assets simultaneously.
:::
---
### 4. Mathematical Checks
We verify our results using basic matrix properties:
1. **Total Variance Check:** The sum of eigenvalues must equal the sum of the diagonal elements (the trace) of the Variance-Covariance matrix:
$$\sum_{i=1}^k \lambda_i = \text{tr}(\mathbf{\Sigma})$$
2. **Determinant Check:** The product of eigenvalues must equal the determinant of the Variance-Covariance matrix:
$$\prod_{i=1}^k \lambda_i = \det(\mathbf{\Sigma})$$
3. **Definition Check:** The matrix equation must hold with zero residual error:
$$\mathbf{\Sigma} \mathbf{v}_1 = \lambda_1 \mathbf{v}_1$$
```{r}
#| label: verification-checks
# 1. Definition check: Variance_Covariance %*% v1 equals lambda1 * v1
v1 <- V_vecs[, 1]
lambda1 <- lambda_vals[1]
diff_norm <- max(abs((Variance_Covariance %*% v1) - (lambda1 * v1)))
# 2. Total variance: sum of eigenvalues vs trace
sum_lambdas <- sum(lambda_vals)
matrix_trace <- sum(diag(Variance_Covariance))
# 3. Determinant: product of eigenvalues vs determinant
prod_lambdas <- prod(lambda_vals)
matrix_det <- det(Variance_Covariance)
# Summary table comparing both methods
verification_matrix <- matrix(
c(sum_lambdas, matrix_trace,
prod_lambdas, matrix_det,
diff_norm, 0),
nrow = 3,
byrow = TRUE,
dimnames = list(
c("Total Variance (Trace)", "Determinant", "Difference ||Av - lv||"),
c("From_Eigenvalues", "From_Covariance_Matrix")
)
)
verification_matrix
```
::: {.callout-important}
### Summary
The checks confirm our calculations match exactly:
- Total variance is $21.8775$ under both methods.
- The determinant is $0.54$ under both methods.
- The difference between the matrix product and the scalar-eigenvector product is practically zero, proving the calculation is exact.
:::