# Solving for Z when Z^T Z = A using R

First time question asker here! Thanks in advance for any suggestions! So here's the issue. I start with a matrix Z which is samples by features. I create a covariance matrix $A = Z^T Z$. Then I perform eigenvalue decomposition on A, so $A = QDQ^T$. My goal is then to solve for Z in terms of Q and D. Here's the algebra showing how I solve for z which results in:

$$Z = \sqrt(D)Q^T$$

*This post (https://math.stackexchange.com/questions/2858299/solve-xtx-a-for-x/2859023) confirms that this math checks out, at least I think.

The problem comes when I try to code the above as a toy example in R

#1. Create toy data
z <- matrix(c(1,2,3,4), nrow = 2)
print(z)

#2. Calculate symmetric matrix A
A <- t(z) %*% z

#3. Eigenvalue decomposition
ev <- eigen(A)
Q <- ev$vectors l <- ev$values
D <- diag(l)

#4. Re-multiply to obtain A again, as a sanity check
A_new <- Q %*% D %*% t(Q) # Should equal A

#5. Get expression for z
z_new = sqrt(D) %*% t(Q)  # Should equal z


A_new should equal A, and it does

A and A_new:

5 11
11 25

BUT I also think z_new should equal z, and it does not!

Z:

1 3
2 4

Z_new:

2.2108796 4.9978076
-0.3346813 0.1480529

Why doesn't Z == Z_new?! Is the math wrong? Is the code wrong? I've never been so stumped!

Ultimately, what I'm trying to do is subtract out the variance that is explained by the first principal component from the original dataset Z. So my plan is to set the first eigenvalue (entry 0,0 of the D matrix) to 0, and then multiply back out and resolve for Z. If there's an easier way to do that directly, or if my method is

Thank you all

• You're calculating something like a square root, and they are not unique. Imagine Z is a scalar: then A = Z^2, so both +Z and -Z would be solutions. With matrices there are even more solutions, e.g. you can change the signs of any the D elements without affecting the results. – user2554330 Dec 12 '20 at 10:36
• You got a reply in a comment on SO. If you have a (not the) square root $Z^{1/2}$ of $Z$ then $Z^{1/2} U$ is another square root for any orthogonal matrix $U$. – Stéphane Laurent Dec 13 '20 at 13:13
• ah, I see the issue, thank you. – kiti15237 Dec 14 '20 at 16:24

Seems like the premise for $$Z$$ does not correspond to the answer on math stackexchange that you refer to: the idea is to start from $$A$$ and decompose it into $$Z^TZ$$. Here, you define $$Z$$ first ($$Z$$ corresponds to $$X$$ in math stackexchange), then calculate $$A$$ based on $$Z$$ and then again decompose $$A$$ into $$Z^TZ$$. There's a circulatory reference here.
Nevertheless, z and z_new both satisfy the decomposition $$A=Z^TZ=Z_{new}^TZ_{new}$$, which leads to the conclusion that $$Z$$ is not unique, i.e. for a $$Z$$ that satisfies the decomposition, there is a $$Z^*=BZ$$ that also satisfies it. I'm not sure about the theory on this (if it is generally the case or not). EDIT: comment of @Stephane Laurent on OP indicates that it holds generally for a matrix root.
B = solve(z,z_new)

• Well, let's see. That would mean that $ZB=Z_n$ and $Z_nB=Z$ are both true. Solving the first for $B$ yields $Z=Z_nB^{-1}$, hence it must hold that $B=B^{-1}$, i.e. $B$ is an involutory matrix. This is the case for the matrix in your example, but does not generally hold (basically coincidence). Try for example replacing 4 with 7 in your matrix, you'll get different results. – PaulG Dec 14 '20 at 18:48
• *solving the first for $Z$ – PaulG Dec 15 '20 at 15:00