# Constructing a stock market index using PCA

Let's say that I've got a final component and its score derived from n number of stock returns (time-series data). I want to construct a stock market index using this component (having negative and positive values). There is a good approach to to this? Also, I want that this index to have a starting value of 1,000. Thank you.

## migrated from stats.stackexchange.comJan 25 '18 at 13:13

This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.

• This is on-topic here, so this is a perfectly reasonable place to ask about it. At the same time, Quantitative Finance specializes in quantitative finance, so perhaps they would be able to provide a more illuminating answer. I only mention this to improve the chances that you get a helpful response. – Sycorax Jan 24 '18 at 17:30
• Thank you, @Sycorax. I'm new here... Can I move the post to the Quantitative Finance area, or should I delete the post here and repost there? Thanks. – Niqx Jan 24 '18 at 17:36
• If you believe this question would be more appropriate for QF (and you should make that determination based on your reading of the description in the help center for QF), you can click "flag" below your question and write a note asking the moderators to move it. On all of StackOverflow, we discourage cross-positing/re-posting questions. But please I'm not trying to tell you to move it, since it is on-topic here. – Sycorax Jan 24 '18 at 18:03

I was able to recreate a simple example of creating an index from made up stock returns using the R tidyverse. Check and see what you think.

options(tidyverse.quiet = TRUE)
library(tidyverse)
library(broom)
set.seed(42)
stocks <- tibble(
time = as.Date('2009-01-01') + 0:99,
X = rnorm(100, 0, 1),
Y = rnorm(100, 0, 2),
Z = rnorm(100, 0, 4))


This was what the fake returns looks like.

stocks %>%
gather(stock, return, -time) %>%
ggplot(aes(time, return)) +
geom_line(aes(group = stock, color = stock)) stocks %>%
gather(stock, return, -time) %>%
group_by(time) %>%
summarise(avg_ret = mean(return)) -> avg_return
avg_return %>%
ggplot(aes(time, avg_ret)) +
geom_line()


And this is the average return looks like. Now, this is how one can create an index from the PCA, treating each stock as a different variable.

stocks %>%
select(-time) %>%
as.matrix() %>%
prcomp(.) -> pca
pca_index <-
augment(pca, data = stocks) %>%
mutate(
time,
base_1000_index = (.fittedPC1*1000)/first(.fittedPC1))
pca_index %>%
as.tibble() %>%
ggplot(data = ., aes(x = time, y = base_1000_index )) +
geom_line()


And this would be the base 1000 index. You can see how I built it from in the second line of the mutate block. Now, to interpret such index is a bit difficult. The classical idea of a principal component is to to change the data such as you reduce the variability of it, by only having the directions of greater variance.

Using the first component projection o each data point, means that you are capturing the most variability of the stocks. I can't really wrap my head around what that could mean in the form of an index.

• Thank you for your answer. Based on what I have found out regarding the topic, I do not use only the PC1, but I select all components based on Kaiser rule, i.e., with the eigenvalues >1. After this, I get the scores of each components and weight them with their explained variance as a % of the total cumulative explained variance. In the end, I get a single component and based on it I want to construct the index. Your index takes also negative values, and I do not want this because I will work with logarithmic returns based on this index. – Niqx Jan 25 '18 at 11:30
• @Niqx In classic PCA, a principal component can have positive or negative loadings on the original basis vectors. In this context, this corresponds to positive or negative portfolio weights (where a negative weight corresponds to shorting an asset). Are you looking for some kind of restricted PCA where loadings cannot be ngative? – Matthew Gunn Jan 25 '18 at 17:51
• @Guilherme Marthe In securities markets, there's huge cross-sectional correlation and the first principal component (depending on the set of securities you're working with...) often ends up looking similar to the return on the market portfolio. In your example, the first PC looks pretty similar to $z$, which makes sense since it has so much more variance. – Matthew Gunn Jan 25 '18 at 17:57
• @Matthew I just want to derive an index from these loadings, and being a stock market index, it cannot have negative values. I found somewhere a sort of normalization of the loadings (putting the index in the range of, let's say, 0 and 1,000), but this is not suitable for my case as the index may exceed the cap value (1,000). I'm still struggling to find a way to derive an index from these loadings. I need the index returns to work with some VaR and systemic risk models. – Niqx Jan 25 '18 at 23:59