Skip to content

Instantly share code, notes, and snippets.

View tschm's full-sized avatar
🦧
Be sure to wear some flowers in your hair

Thomas Schmelzer tschm

🦧
Be sure to wear some flowers in your hair
  • Stanford & Team Q @ ADIA
  • Palo Alto & Abu Dhabi, United Arab Emirates
  • X @Thomster78
View GitHub Profile
def formatter(str="{:0.2f}"):
return lambda x: str.format(x)
def to_latex(frame, filename, na_rep="", float_format=lambda number: "{:0.2f}".format(number)):
with open(filename, "w") as file:
frame.to_latex(buf=file, na_rep=na_rep, float_format=float_format, escape=False)
@tschm
tschm / Script.sh
Created April 27, 2016 13:32
Bash script to install a tagged version of a git repository
rm -rf pybank
# clone the entire github repository. This is kind of inefficient as we delete the .git folder in a moment
git clone git@github.com:lobnek/pybank.git
cd pybank
VERSION=$1
if [ "$VERSION" == "" ]
then
def monthlytable(nav, year=1900):
r = nav.pct_change().dropna()
# Works better in the first month
# Compute all the intramonth-returns, instead of reapplying some monthly resampling of the NAV
return_monthly = r.groupby([lambda x: x.year, lambda x: x.month]).apply(lambda x: (1 + x).prod() - 1.0)
frame = return_monthly.unstack(level=1).rename(columns=lambda x: calendar.month_abbr[x])
a = (frame + 1.0).prod(axis=1) - 1.0
frame["STDev"] = np.sqrt(12) * frame.std(axis=1)
# make sure that you don't include the column for the STDev in your computation
frame["YTD"] = a
@tschm
tschm / mail.py
Created December 7, 2015 09:30
Log handler based on Mailgun
import requests
import logging
class MailHandler(logging.Handler):
def emit(self, record):
send_message(subject=self.__subject, text=record, toAdr=self.__toAdr, fromAdr=self.__fromAdr,
mailgun=self.__mailgun, mailgunkey=self.__mailgunkey)
def __init__(self, level, format, subject, toAdr, fromAdr, mailgun, mailgunkey):
super().__init__()
@tschm
tschm / drawdown.py
Created November 17, 2015 09:07
Compute the drawdown for a price time series...
import numpy as np
import pandas as pd
def drawdown(price):
high_water_mark = np.empty(len(price.index))
moving_max_value = 0
for i, value in enumerate(price.values):
moving_max_value = max(moving_max_value, value)
high_water_mark[i] = moving_max_value
@tschm
tschm / PandasEngine.py
Last active August 29, 2015 14:23
Little engine for Python
import logging
import pandas as pd
class Engine(object):
def __init__(self, engine):
self.__engine = engine
def read(self, table, index_col):
logging.info('SELECT * FROM {0}'.format(table))