Skip to content

Instantly share code, notes, and snippets.

View raddy's full-sized avatar

Rad raddy

  • Lagrange 3
View GitHub Profile
@raddy
raddy / step1.py
Created September 4, 2013 15:57
Step 1 SVI Quasi Explicit -- Python
def f_gradient(params,y,market_vols,tte,q):
c,d,abar = params
y = y[market_vols>0]
vols = market_vols[market_vols>0]
n = len(y)
Y1 = np.sum(y)
Y2 = np.sum(y**2)
Y3 = np.sum(np.sqrt(y**2+1))
Y4 = np.sum(y*np.sqrt(y**2+1))
var_t = vols**2*tte
@raddy
raddy / optimize_weighted.py
Created September 19, 2013 21:50
weighted convex opt svi -- just multiplying through by weights... (thx kevin)
def optimize_weighted(y,max_vi,weights,market_vols,tte,s):
# {{{
y = y[market_vols>0]
vols = market_vols[market_vols>0]
w = weights[market_vols>0]
n = len(y)
# R ends up n by 3
R = np.concatenate([np.ones(n), np.sqrt(y**2+1), y]).reshape(3,n).T
@raddy
raddy / h5example.py
Last active December 24, 2015 21:59
Example of working with those h5 files
import pandas as pd
import numpy as np
from bsm_implieds import implied_fut,implied_futs
def is_kospi(symbol_list):
s = pd.Series(symbol_list)
is_call = s.str[2:6]=='4201'
is_put = s.str[2:6]=='4301'
is_fut = s.str[2:6]=='4101'
return np.logical_or(is_call,np.logical_or(is_put,is_fut))
@raddy
raddy / simple_cython_kf.pyx
Created October 25, 2013 16:02
simplest possible kf in cython
import pandas as pd
import numpy as np
cimport numpy as np
cimport cython
from libc.math cimport exp, sqrt, pow, log, erf, abs, M_PI
ctypedef np.double_t DTYPE_t
@cython.cdivision(True)
@cython.boundscheck(False)
@raddy
raddy / simplest_mm.py
Created October 25, 2013 17:18
simple future mm
import bisect
def calc_pnl_futs(actions,observations):
pnls=[]
if observations.last_valid_index() == None:
return np.zeros(len(observations))
settle = np.mean(observations[observations.last_valid_index()])
for a,o in zip(actions,observations):
if a<0:
pnls.append(o[0]-settle)
@raddy
raddy / rf_slugs.py
Created October 29, 2013 23:57
example shitty rf
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.cross_validation import train_test_split
import pandas as pd
import numpy as np
def training_to_XY(hi,side='buy'):
X = pd.DataFrame(hi.implied_size)
if side == "buy":
X['Through2'] = hi.implied_trd-hi.AskPrice2
@raddy
raddy / compare_tcp.py
Created November 14, 2013 16:06
compare tcp pcaps
import pandas as pd
import os,sys
from tcp_pcap2h5 import open_pcap
def main(pcap1,pcap2):
open_pcap(pcap1)
open_pcap(pcap2)
h5name1 = pcap1+'.h5'
@raddy
raddy / sniper_helpers.pyx
Created November 19, 2013 17:18
cython sniper helper methods
import pandas as pd
import numpy as np
cimport numpy as np
cimport cython
from bsm_implieds import bs_tv,delta_py
ctypedef np.double_t DTYPE_t
cdef extern from "math.h":
bint isnan(double x)
@raddy
raddy / rolling_volume.pyx
Created January 23, 2014 16:43
rolling_volume in cython etc
%%cython
import numpy as np
import pandas as pd
import sys
cimport cython
cimport numpy as np
ctypedef np.double_t DTYPE_t
cdef extern from "math.h":
bint isnan(double x)
@raddy
raddy / simple_kalman_example.py
Created February 7, 2014 16:55
simplest y=mx + b kalman example
import pykalman
import numpy as np
#model of the form -- I apologize for mathjax weirndess but too lazy to fix right now
#yt = β0,t +β1,txt +νt, νt ∼ N(0, σ2 ν)
#β0,t+1 = β0,t +ξt, ξt ∼ N(0, σ2 ξ)
#β1,t+1 = β1,t +ςt, ςt ∼ N(0, σ2 ς )