Skip to content

Instantly share code, notes, and snippets.

View rodmorley's full-sized avatar

Rod M rodmorley

  • Malta & Monaco
View GitHub Profile
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
@rodmorley
rodmorley / bbg_download
Created May 8, 2023 14:57
Data download from bbg into xts returns
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",
@rodmorley
rodmorley / mix_it_up
Created May 8, 2023 13:24
Randomly allocate topics to students
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]
@rodmorley
rodmorley / market_regime
Created August 17, 2022 08:18
Calcualate the market regime (calm v turbulent) as per Krtizman.
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
@rodmorley
rodmorley / rolling_mahalanobis
Created August 17, 2021 14:38
Rolling Mahalanobis distance
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), ]))
@rodmorley
rodmorley / mahalanobis
Created August 17, 2021 14:34
Mahalanobis distance for time series
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)
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
# 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)
@rodmorley
rodmorley / SEC 13F Holdings Report
Last active October 5, 2018 13:22
SEC 13F Holdings Report
#
# 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)
@rodmorley
rodmorley / FamaFrenchFactorsDownload
Last active February 24, 2020 16:42
Fama French 6 Factors Data Download
#
# e.g. FamaFrenchDownload("2018-08-31")
#
# ignore the warning message
#
requires xts
requires lubridate
FamaFrenchDownload <- function(last_date){