Skip to content

Instantly share code, notes, and snippets.

@mgei
Created February 8, 2021 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgei/ea3116c6f2447cc928dcaa2fd5943729 to your computer and use it in GitHub Desktop.
Save mgei/ea3116c6f2447cc928dcaa2fd5943729 to your computer and use it in GitHub Desktop.
# What is PCA?
# - PCA is a form of multi-dimensional scaling.
# - It transforms the data into a lower dimensional space while keeping the maxiumum of information.
# Book: Multi-Dimensional Diversification
# get data https://www.msci.com/end-of-day-data-search
library(tidyverse)
library(tidyquant)
library(readxl)
msci <- read_excel("historyIndex_msci.xls", skip = 6, col_names = T)
msci <- msci %>% mutate(Date = as.Date(Date))
msci <- msci %>% gather(Index, Value, -Date)
msci %>% group_by(Index) %>% filter(!is.na(Value)) %>% summarise(start = min(Date)) %>%
arrange(desc(start))
msci <- msci %>% filter(Date >= "1997-01-01")
msci %>% ggplot(aes(x = Date, y = Value, color = Index)) + geom_line() +
theme_tq()
returns <- msci %>% group_by(Index) %>% tq_transmute(select = Value,
mutate_fun = periodReturn,
period = "monthly",
type = "arithmetic")
returns %>% filter(Date >= "2018-01-01") %>% ggplot(aes(x = Date, y = monthly.returns, fill = Index)) +
geom_bar(stat = "identity") +
facet_wrap(~Index) +
theme_tq()
correlations <- returns %>% spread(Index, monthly.returns) %>% select(-Date) %>% cor()
returns %>% spread(Index, monthly.returns) %>% select(-Date) %>% chart.Correlation()
## PCA
pca <- returns %>% spread(Index, monthly.returns) %>% select(-Date) %>% as.matrix() %>%
prcomp(scale. = T, center = T)
pca %>% summary()
library(factoextra)
pca %>% fviz_eig()
pca %>% fviz_pca_ind()
pca$rotation
eigenvectors <- returns %>% spread(Index, monthly.returns) %>% select(-Date) %>% as.matrix() %>%
cor() %>% eigen()
eigenvectors$vectors
pca$sdev^2
eigenvectors$values
t(pca$rotation[,1])%*%pca$rotation[,4]
pca %>% fviz_pca_var(repel = T)
# Varimax rotation
pca$rotation[,1:3] %>% varimax()
returns %>% tq_portfolio(assets_col = Index, weights = rep(1/6,6), returns_col = monthly.returns) %>%
tq_performance(Ra = portfolio.returns, performance_fun = table.AnnualizedReturns) * 100
returns %>% tq_portfolio(assets_col = Index, weights = c(1, 1, 0, 1, 1, 0)/4, returns_col = monthly.returns) %>%
tq_performance(Ra = portfolio.returns, performance_fun = table.AnnualizedReturns) * 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment