This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model mpg cyl disp hp drat wt qsec vs am gear carb | |
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4 | |
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4 | |
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 | |
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 | |
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2 | |
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1 | |
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4 | |
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2 | |
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
index_returns <- function(years = 10, tickers = c("SPX Index","SXXP Index", "UKX Index", "HSI Index", "TPX Index","AS51 Index")){ | |
require(Rblpapi) | |
con <- blpConnect() | |
opt <- c("periodicitySelection"="MONTHLY") | |
dataset = bdh(securities = tickers, | |
fields = "PX_LAST", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mix_it_up <- function(names, topics){ | |
for(i in 1:length(names)){ | |
name = sample(names , 1, replace = FALSE) | |
topic = sample(topics, 1, replace = FALSE) | |
print(paste0(name, " : ", topic)) | |
names = names[names != name] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
market_regime <- function(full_data, window=24, cut_off_perc=0.80, lookback=24){ | |
rows = nrow(full_data) | |
kritz = NULL | |
for (i in window:(rows-1)){ | |
start_row <- i-window+1 | |
end_row <- i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rolling_mahalanobis <- function(dataset, width = 24){ | |
mahalanobis <- function(dataset){ | |
N <- nrow(dataset) | |
y_t <- last(dataset) | |
mu <- colMeans(dataset[1:(N-1), ]) | |
y_t_minus_mu <- y_t- mu | |
Sigma_1 <- ginv(cov(dataset[1:(N-1), ])) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mahalanobis <- function(dataset){ | |
N <- nrow(dataset) | |
y_t <- last(dataset) | |
mu <- colMeans(dataset[1:(N-1), ]) | |
y_t_minus_mu <- y_t- mu | |
Sigma_1 <- ginv(cov(dataset[1:(N-1), ])) | |
d_t <- y_t_minus_mu %*% Sigma_1 %*% t(y_t_minus_mu) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rolling_market_regime <- function(datareturns, window=48, cutoff=0.75){ | |
#datareturns <- coredata(datareturns) | |
Nreturns <- dim(datareturns)[1] | |
degsFreedom <- dim(datareturns)[2] | |
kritz <- matrix(NA, ncol=1, nrow=Nreturns) # make this a vector ?? | |
for(i in (1+window):Nreturns){ | |
start_row <- i-window |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculating VaR conditional on market regime ['calm', 'turbulent' or 'full period'] | |
# | |
# Regime = Kritzman's measure of turbulence | |
# | |
# | |
# | |
# | |
d.turbulence.VaR.data <- function(datareturns, wgts){ | |
ans <- matrix(0, ncol = 1, nrow = dim(datareturns)[1]) | |
datareturns.cov <- cov(datareturns) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Pull 13F holdings over 1% of portfolio AuM | |
# | |
# link = 'copy link address' to the information table .xml file | |
# | |
# requires XML, RCurl, dplyr, magrittr | |
# | |
require(XML) | |
require(RCurl) | |
require(dplyr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# e.g. FamaFrenchDownload("2018-08-31") | |
# | |
# ignore the warning message | |
# | |
requires xts | |
requires lubridate | |
FamaFrenchDownload <- function(last_date){ |
NewerOlder