Skip to content

Instantly share code, notes, and snippets.

View twiecki's full-sized avatar

Thomas Wiecki twiecki

View GitHub Profile
import scipy.cluster.hierarchy as sch
import random
import numpy as np
import pandas as pd
def getIVP(cov, **kargs):
# Compute the inverse-variance portfolio
ivp = 1. / np.diag(cov)
ivp /= ivp.sum()
return ivp
@twiecki
twiecki / custom_rolling.py
Created July 11, 2016 13:43
Rolling window and rolling apply with flexible lookback and resample behavior that also support parallelism.
import pandas as pd
from pandas.tseries.offsets import *
def rolling_window(data, resample='1BM', lookback=12 * BMonthEnd()):
dts = data.resample(resample).index
if lookback == 'aggregate':
for dt in dts:
yield data.loc[:dt]
else:
class BayesianModel(object):
samples = 2000
def __init__(self, cache_model=True):
self.cached_model = None
self.cached_start = None
self.cached_sampler = None
self.shared_vars = {}
def cache_model(self, **inputs):
self.shared_vars = self._create_shared_vars(**inputs)
def initialize(context):
fetch_csv('URL',
date_format='%d/%m/%y',
universe_func=my_universe)
def my_universe(context, fetcher_data):
my_stocks = set(fetcher_data['sid'])
# log the size of the universe for debugging
context.count = len(my_stocks)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@twiecki
twiecki / bayesian_neural_network.ipynb
Last active February 22, 2022 01:28
Bayesian Neural Network in PyMC3
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@twiecki
twiecki / abstract
Created January 15, 2016 11:00
Thomas Wiecki Strata Hadoop World London 2016 submission -- accepted
All that glitters is not gold: Comparing backtest and out-of-sample performance of 800.000 trading algorithms
“Past performance is no guarantee of future returns”. This cautionary message will certainly match the experience of many
investors. When automated trading strategies are developed and evaluated using backtests on historical pricing data, there
is always a tendency, intentional or not, to overfit to the past. As a result, strategies that show fantastic performance on
historical data often flounder when deployed with real capital.
Quantopian is an online platform that allows users to develop, backtest, and trade algorithmic investing strategies. By
pooling all strategies developed on our platform we constructed a huge and unique data set of over 800.000 trading algorith
ms. Although we do not have access to source code, we have returns and portfolio allocations as well as the time the
@twiecki
twiecki / table.html
Last active September 15, 2015 14:07
<html>
<table id="comparetable" class="clean">
<tr>
<td class="blank"> </td>
<th>Machine Learning</th>
<th>Frequentist<br>Statistics</th>
<th>Probabilistic<br>Programming</th>
</tr>
<tr>
<td class="rowTitle">Prediction</td>
@twiecki
twiecki / ppc.ipynb
Last active August 29, 2015 14:25
Example NB to run a PPC test using new random() methods.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@twiecki
twiecki / exmachina.py
Last active October 10, 2021 05:22
PEP8 version of Ex-Machina bluebook decryption code
# After seeing Ex Machina I tweeted:
# "So disappointed in #ExMachina -- the #Python wasn't PEP8 compatible..."
# https://twitter.com/twiecki/status/599615426922938368
# For the original code in the movie as well as what it does (it's kinda neat), see:
# http://moviecode.tumblr.com/post/119171520870/in-the-movie-ex-machina-which-is-really-great
# To put my code where my mouth is, I decided to make the code PEP8 compatible which you can see below.
# I also did some minor cosmetic changes like changing i = i + 1 to i += 1
# Other ideas for improvements? Comment below.