Just a heads up, I'm not going to go through all the mathematical caveats of using this approach.
Let $\Sigma$ be your covariance matrix, and $X$ a random vector of daily returns. So
$$\text{Var}(X) = \Sigma.$$
You have a bug in your code. In your code you call it pxCov
, but you probably meant to use cov()
insted of cor()
. Check out the documentation to princomp().
It expects the covariance matrix by default. This will change things.
Also, princomp()
is interpreting your covariance matrix as a data frame. You should run the following commands instead of your last two lines:
pxCov=cov(pxList)
pc=princomp(covmat=pxCov)
Now the math stuff. All the stuff you want to do, that you mentioned in the comments, can be done by taking expectations, variances, or covariances of linear combinations of your random return vector. The weights will be constructed from the loadings or eigenvectors, and calculation of variances will be simplified using the eigenvalues or standard deviation terms corresponding to the loadings.
Let $w$ be a weight (column) vector. Assume for now that the sum of its entires is $1$. Each element denotes the share of your portfolio sitting in that stock. Basic properties tell us that
$$\text{Var}(w'X) = w'\Sigma w,$$
where the apostrophe denotes the transpose.
We can take the spectral decomposition of covariance matrices (positive definite and symmetric):
$$\Sigma = \sum_{i=1}^4 \lambda_i v_i v_i'$$
with the set of loadings or eigenvectors, $\{v_i\}$ being orthonormal. In your code, these are pc$loadings
. The lambdas that correspond with these are pc$sdev^2
. This function orders everything in decreasing order, so $\lambda_1 > \cdots > \lambda_4$.
When you say PCA, you are probably trying to use the $v_i$s to construct a ''good'' $w$. There is no single way to do this. If you want to minimize your volatility without paying any mind to your expected returns, you would set $w = v_4$. Then your portfolio variance/risk would be
$$\text{Var}(wX) = v_4' \left[ \sum_{i=1}^4 \lambda_i v_i v_i'\right] v_4 = \lambda_4 v_4'v_4 v_4'v_4 = \lambda_4,$$
because of the orthonormality of the loading vectors.
If you want to minimize some other objective function, then you can do that too. For example, if your expected return is $w'\mu$, then you might want to minimize $w'\Sigma w - m w'\mu$. I have no idea what's commonly done in practice, though.
If you want to see how much each variance each loading explains, run plot(pc$sdev^2/sum(pc$sdev^2))
. If you want 90\% you need some mix of the first two loadings. So we need to find some $0 < \alpha < 1$ such that $w = \alpha v_1 + (1-\alpha)v_2$. Using stuff like orthogonality that I showed you earlier, you can get
$$\text{Var}(wX) = \alpha^2\lambda_1 + (1-\alpha)^2\lambda_2 \overset{\text{set}}{=} .9$$
So you have to use the quadratic formula to find $\alpha$ in order to get your $w$. You could also do a mix of the last three loadings, too. I'm not sure what's done commonly in practice here, either.
If you want to find the "exposure" of your new portfolio, $w'X$, to the returns of the SPY, $e_1'X$, which I take to mean as covariance, then you can use the bilinearity of $\text{Cov}(\cdot,\cdot)$
$$\text{Cov}(w'X, e_1'X) = w'\Sigma e_1$$
where $e_1' = (1, 0, 0, 0)$.