The question refers to @whuber's algorithm to draw a random variable with a given covariance structure to a given set of random variables: https://stats.stackexchange.com/a/313138/3277
The algorithm does exactly what it is supposed to do.
However, I don't grasp yet how and why it does so. Can anyone please clarify WHY the proposal by @whuber actually works.
In particular,
1) one line of his code reads
y.dual <- with(svd(y), (n-1)*u %*% diag(ifelse(d > 0, 1/d, 0)) %*% t(v))
where the inner matrix is a diagonal matrix containing the inverses of all singular values of y.
u contains the left-singular vectors of y. v contains the right-singular values of y.
So what is y.dual ?
2) the final step in calculating z is
z <- y.dual %*% rho + sigma*e
Why do we have to add sigma*e here?
And, frankly, why does y.dual %*% rho + sigma*e
produce what we are looking for?
I would be grateful for a clarification / explanation of this procedure.
y.dual
is (proportional to) a pseudo-inverse ofy
(which you can check by inspectingzapsmall(t(y.dual) %*% y)
provided the columns ofy
are linearly independent) and (2) implements the generalization of the formula for $X_{Y;\rho}$ from ordinary to multiple regression. $\endgroup$center = TRUE
iny <- scale(y, center=FALSE)
. In the original answer, the example codes generate Y withrnorm
, which has zero means so it works. When I generate Y withrbeta
, which follows a beta distribution with a mean greater than 0, the codes fail. After some debugging, usingcenter=TRUE
will solve the problem. $\endgroup$