Skip to content

Instantly share code, notes, and snippets.

View raddy's full-sized avatar

Rad raddy

  • Lagrange 3
View GitHub Profile
@raddy
raddy / convex_inner_svi.py
Created June 21, 2014 14:58
Convex Inner Problem For SVI (As described by Zeliade)
def optimize(y,max_vi,market_vols,tte,s,weights):
y = y[market_vols>0]
vols = market_vols[market_vols>0]
w = weights[market_vols>0]
n = len(y)
R = np.concatenate([np.ones(n), np.sqrt(y**2+1), y]).reshape(3,n).T
W = np.diag(w)
@raddy
raddy / bs_symbolic.py
Created November 17, 2014 15:14
Black Scholes Sympy Greeks
import numpy as np
import sympy as sy
from sympy.statistics import Normal as syNormal
sy.init_printing()
#spot, strike, vol, days till expiry, interest rate, call or put (1,-1)
S, K, vol, dte, r,cp = sy.symbols('S,K,vol,dte,r,cp')
T = dte / 260.
N = syNormal(0.0, 1.0)
@raddy
raddy / spline1.py
Created December 3, 2014 14:47
cvxopt spline example a bit more flexible
from cvxopt import lapack, solvers, matrix, mul
from cvxopt.modeling import op, variable, max, sum
#def const_spline(n=12):
# """ Adapted from example on CVXOPT site -- Changing as little as possible """
#t = matrix(v.index.values[::4])
#y = matrix(v.values[::4])
t, y = data['t'], data['y']
@raddy
raddy / py-const-spline.ipynb
Created December 16, 2014 16:21
py constrained spline ipynb example
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@raddy
raddy / mech_plot.py
Created February 27, 2015 18:37
mechanical markets style python plots
import seaborn
import pandas as pd
import numpy as np
import matplotlib.patches as mpatches
#create random trade pnls
n = 5000
sigma = .1
pnl1 = pd.DataFrame(np.random.randn(n,15)) * sigma
@raddy
raddy / mech_plot2.py
Created March 1, 2015 15:29
mechanical markets style plots updated with better line widths
import seaborn
import pandas as pd
import numpy as np
import matplotlib.patches as mpatches
#create random trade pnls
n = 5000
sigma = .1
pnl1 = pd.DataFrame(np.random.randn(n,15)) * sigma
@raddy
raddy / index_rep_example.py
Created August 31, 2015 13:48
secrets secrets
import numpy as np
import numpy.random as rnd
from cvxopt import matrix
from cvxopt import blas
from cvxopt import solvers
from cvxopt.modeling import op
class IndexReplication(object):
def __init__(self):
self.G = None # number of groups undergoes mutation
@raddy
raddy / pcf.py
Created August 31, 2015 13:50
pcf ex
import pandas as pd
from scipy import stats, linalg
def partial_corr(C):
"""
Returns the sample linear partial correlation coefficients between pairs of variables in C, controlling
for the remaining variables in C.
Parameters
@raddy
raddy / ab_kf.py
Created August 31, 2015 13:50
ab kf
def fit_ab_kf(single_cumulative_returns,dex_cumulative_returns):
n_steps = len(dex_cumulative_returns)
A = np.matrix([[1,0],[0,1]])
# Transition matrix is dependent on previous B0/B1 with no inherent cross dependency
C = map(lambda x: np.matrix(np.append(1,x[0])),np.reshape(dex_cumulative_returns,(n_steps,1,1)))
# Observation matrix is tensor of [1,x_t] (of length equal to our observations)
# Initial hidden state means are 0 for intercept and 1 for slope
@raddy
raddy / ou_kf.py
Created August 31, 2015 13:55
ou kf
#model of the form -- I apologize for mathjax weirndess but too lazy to fix right now
#yt = a_tx_t + νt ∼ N(0, σ2 ν)
#at = a_t-1 + b_t-1 + 1/2 c_t-1
#bt = b_t-1 + c_t-1
#ct = c_t-1 + ut ~ N(0,..)