Skip to content

Instantly share code, notes, and snippets.

View zachmayer's full-sized avatar

Zach Deane-Mayer zachmayer

View GitHub Profile
#!/usr/bin/env python
# encoding: utf-8
#Constants.R
#Created by Kenneth J. Shackleton on 14 June 2011.
#Copyright (c) 2011 Ringo Limited.
#All rights reserved.
#R Port by Zach Mayer on 4 December 2011
@zachmayer
zachmayer / openblas.sh
Last active March 14, 2022 03:20
Mac BLAS
#Option 1 - install openblas with homebrew and link to CRAN installed R
brew tap homebrew/science
brew install openblas
ln -sf /usr/local/Cellar/openblas/0.2.12/lib/libopenblas.dylib /Library/Frameworks/R.framework/Resources/lib/libRblas.dylib
#Option 2 - install r with openblas through homebrew
brew tap homebrew/science
brew install r --with-openblas
@zachmayer
zachmayer / Recessions.R
Created August 9, 2011 16:33
Forecasting Recessions
#Code to re-create John Hussman's Recession warning index
#http://www.hussmanfunds.com/wmc/wmc110801.htm
#R code by Zach Mayer
rm(list = ls(all = TRUE)) #CLEAR WORKSPACE
library(quantmod)
#################################################
# 1. Credit spreads
#################################################
@zachmayer
zachmayer / Kaggle Code.R
Created April 29, 2011 16:35
Cross validate RFE
#Directory
setwd('~/wherever')
#Load Required Packages
library('caret')
library('glmnet')
library('ipred')
library('e1071')
library('caTools')
@zachmayer
zachmayer / Chart 1.R
Created September 15, 2011 21:32
Stock Correlations
#Load Data
rm(list = ls(all = TRUE))
library(quantmod)
library(PerformanceAnalytics)
symbols <- c('XLE','XLV','XLI','XLU','XLP','IYZ','XLK','XLY','XLF','XLB','GLD','SLV','EFA','EEM','FXA','FXE','FXY','HYG','LQD')
getSymbols(symbols,from='2007-01-01')
getSymbols('SPY',from='2007-01-01')
@zachmayer
zachmayer / multiclass.R
Created July 6, 2012 16:46
Multi Class Error Metrics
#Multi-Class Summary Function
#Based on caret:::twoClassSummary
require(compiler)
multiClassSummary <- cmpfun(function (data, lev = NULL, model = NULL){
#Load Libraries
require(Metrics)
require(caret)
#Check data
@zachmayer
zachmayer / Build Model.R
Created August 22, 2011 14:50
Recession forecasting-Me
#################################################
# Build a model
#################################################
#Choose Differencing window
#Hussman uses 6 months
Diff <- 6
Data$PAYEMS <- (Data$PAYEMS-Lag(Data$PAYEMS,Diff))/Lag(Data$PAYEMS,Diff)
Data$UNRATE <- (Data$UNRATE-Lag(Data$UNRATE,Diff))/Lag(Data$UNRATE,Diff)
Data$SP500 <- (Data$SP500-Lag(Data$SP500,Diff))/Lag(Data$SP500,Diff)
@zachmayer
zachmayer / AttentionWithContext.py
Created September 13, 2017 15:52 — forked from cbaziotis/AttentionWithContext.py
Keras Layer that implements an Attention mechanism, with a context/query vector, for temporal data. Supports Masking. Follows the work of Yang et al. [https://www.cs.cmu.edu/~diyiy/docs/naacl16.pdf] "Hierarchical Attention Networks for Document Classification"
def dot_product(x, kernel):
"""
Wrapper for dot product operation, in order to be compatible with both
Theano and Tensorflow
Args:
x (): input
kernel (): weights
Returns:
"""
if K.backend() == 'tensorflow':
@zachmayer
zachmayer / 1.parDEoptim.R
Created January 23, 2012 14:28
parDEoptim
setClass("parDEoptim", representation(optim="list", member="list"))
parDEoptim <- function (fn, lower, upper, n=1, control = DEoptim.control(), .packages=NULL, ...) {
library("DEoptim")
library("foreach")
segSizes <- (upper-lower)/n
segments <- lapply(1:n, function(x){
lower+segSizes*(x-1)
})
@zachmayer
zachmayer / GetNews.R
Last active May 31, 2017 21:01
Get News
getNews <- function(symbol, number){
# Warn about length
if (number>300) {
warning("May only get 300 stories from google")
}
# load libraries
library(XML); library(plyr); library(stringr); library(lubridate);
library(xts); library(RDSTK)