Skip to content

Instantly share code, notes, and snippets.

@elephann
elephann / timeseries_cnn.py
Created December 20, 2018 19:37 — forked from jkleint/timeseries_cnn.py
Example of using Keras to implement a 1D convolutional neural network (CNN) for timeseries prediction.
#!/usr/bin/env python
"""
Example of using Keras to implement a 1D convolutional neural network (CNN) for timeseries prediction.
"""
from __future__ import print_function, division
import numpy as np
from keras.layers import Convolution1D, Dense, MaxPooling1D, Flatten
from keras.models import Sequential
@elephann
elephann / fm
Created October 5, 2018 07:52 — forked from rshowcase/fm
Fama-MacBeth Entire Procedure
# In my portfolio, I show how the popular Fama-MacBeth (1973) procedure is constructed in R.
# The procedure is used to estimate risk premia and determine the validity of asset pricing models.
# Google shows that the original paper has currently over 9000 citations (Mar 2015), making the methodology one of the most
# influential papers in asset pricing studies. It's used by thousands of finance students each year, but I'm unable to find a
# complete description of it from the web.
#
# While the methodology is not statistically too complex (although the different standard errors can get complex),
# it can pose some serious data management challenges to students and researchers.
#
# The goal of the methodology is to estimate risk premia in the financial markets. While newer, more sophisticated methods for
@elephann
elephann / multi-ts-lstm.py
Created November 10, 2017 19:16 — forked from lukovkin/multi-ts-lstm.py
Time series prediction with multiple sequences input - LSTM - 1
# Time Series Testing
import keras.callbacks
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dense, Dropout
from keras.layers.recurrent import LSTM
# Call back to capture losses
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
@elephann
elephann / tscv.R
Created July 27, 2017 09:12 — forked from robjhyndman/tscv.R
Time series cross-validation diagrams
par(mar=c(0,0,0,0))
plot(0,0,xlim=c(0,28),ylim=c(0,1),
xaxt="n",yaxt="n",bty="n",xlab="",ylab="",type="n")
i <- 1
for(j in 1:20)
{
test <- (6+j):26
train <- 1:(5+j)
arrows(0,1-j/20,27,1-j/20,0.05)
points(train,rep(1-j/20,length(train)),pch=19,col="blue")
@elephann
elephann / 200day.R
Created July 19, 2017 07:39 — forked from geoquant/200day.R
backtesting in R
library(quantmod)
library(PerformanceAnalytics)
library(TTR)
###############################################################
### User Input ################################################
###############################################################
getSymbols("SPY", from = "1900-01-01", to="2012-06-01")
close <- SPY$SPY.Close
@elephann
elephann / xlsxToR.r
Last active August 29, 2015 14:21 — forked from byzheng/xlsxToR.r
#' Read xlsx files
#'
#' @param file The path to xlsx file
#' @param keep_sheets A vector of sheet name
#' @param header Whether include the head in the sheet
#' @param empty_row Whether to remove the empty rows
#' @export
xlsxToR <- function(file, keep_sheets = NULL, header = TRUE, empty_row = TRUE)
{
suppressWarnings(file.remove(tempdir()))
@elephann
elephann / xlsxToR.r
Last active August 29, 2015 14:21 — forked from schaunwheeler/xlsxToR.r
# The MIT License (MIT)
#
# Copyright (c) 2012 Schaun Jacob Wheeler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@elephann
elephann / xlsxToR.r
Last active August 29, 2015 14:21 — forked from byzheng/xlsxToR.r
#' Read xlsx files
#'
#' @param file The path to xlsx file
#' @param keep_sheets A vector of sheet name
#' @param header Whether include the head in the sheet
#' @param empty_row Whether to remove the empty rows
#' @export
xlsxToR <- function(file, keep_sheets = NULL, header = TRUE, empty_row = TRUE)
{
suppressWarnings(file.remove(tempdir()))