## Matrix decomposition example#

Let N be the number of possible simulated paths, T be the number of time periods, and P be the number of parameters to estimate. For now, let’s suppose that N = 4, T=2 and P=2. Below is an example with T-by-N matrix. The rows indicates different time periods, and the columns indicate different simulated paths $$n \in \{1,2,3,4\}$$. For example,

$$A = \begin{bmatrix} a_{11}\theta + b_{11}\rho & a_{12}\theta + b_{12}\rho & a_{13}\theta + b_{13}\rho & a_{14}\theta + b_{14}\rho \newline \beta(a_{21}\theta + b_{21}\rho) & \beta(a_{22}\theta + b_{22}\rho) & \beta(a_{23}\theta + b_{23}\rho) & \beta(a_{24}\theta + b_{24}\rho) \end{bmatrix} \newline$$ When n =1, $$\begin{bmatrix} 1 & \beta \end{bmatrix} \begin{bmatrix} a_{11} & b_{11} \newline a_{21} & b_{21} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix}$$ When n =2, $$\begin{bmatrix} 1 & \beta \end{bmatrix} \begin{bmatrix} a_{12} & b_{12} \newline a_{22} & b_{22} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix}$$ When n =3, $$\begin{bmatrix} 1 & \beta \end{bmatrix} \begin{bmatrix} a_{13} & b_{13} \newline a_{23} & b_{23} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix}$$ When n =4, $$\begin{bmatrix} 1 & \beta \end{bmatrix} \begin{bmatrix} a_{14} & b_{14} \newline a_{24} & b_{24} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix}$$

We can decompose matrix A into three parts: a row vector of discount factors, a T-by-P matrix of average coefficients, and a column vector of parameters. To obtain the T-by-P matrix, I factored the T-by-N matrix by taking the average of coefficients for each parameter from N simulated paths, thus removing the N dimension. $$\begin{bmatrix} 1 & \beta \end{bmatrix} \frac{1}{4} \begin{bmatrix} a_{11}+a_{12} + a_{13} + a_{14} & b_{11}+b_{12} + b_{13} + b_{14} \newline a_{21}+a_{22} + a_{23} + a_{24} & b_{21}+b_{22} + b_{23} + b_{24} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix}$$

## Julia codes#

N=4
T=2
P=2

using SymPy
# assign values for coeffs
a11, a12, a13, a14 = 1,2,3,4
a21, a22, a23, a24 = 5,6,7,8
b11, b12, b13, b14 = -1, -2, -3, -4
b21, b22, b23, b24 = -5, -6, -7, -8

θ, ρ = symbols("θ, ρ")
# generate A matrix
A = [a11*θ + b11*ρ a12*θ + b12*ρ a13*θ + b13*ρ a14*θ + b14*ρ;
a21*θ + b21*ρ a22*θ + b22*ρ a23*θ + b23*ρ a24*θ + b24*ρ]

# Ψ contains above written metrices for each n
Ψ=Array{Any}(undef, T, P, N)
for n in 1:N
Ψ[:, :, n]=
hcat(
[A[t, n].coeff(θ) for t in 1:T],
[A[t, n].coeff(ρ) for t in 1:T]
)
end
β=0.95
βm= [β^t for t in 1:T]
W = transpose(βm) .* 1/N * reduce(+, Ψ[:,:, n] for n in 1:N )

1/N * reduce(+, Ψ[:,:, n] for n in 1:N )


Output:

2×2 Matrix{Sym}:
2.50000000000000  -2.50000000000000
6.50000000000000  -6.50000000000000

W


Output:

1×2 Matrix{Sym}:
8.24125000000000  -8.24125000000000