Skip to content

Instantly share code, notes, and snippets.

@0x0L
0x0L / ewm.py
Created December 22, 2016 21:06
Exponential moving average à la pandas
import numpy as np
def ewma(x, alpha, adjust=True):
q = 1 - alpha
y = x.copy()
if not adjust:
for i in range(1, len(y)):
y[i] += q * (y[i-1] - y[i])
@0x0L
0x0L / Sudoku.hs
Last active April 10, 2017 19:28
Haskell Sudoku solver
import Data.List (intersect, (\\))
import Data.Maybe (catMaybes)
digits = ['1'..'9']
blank = '0'
complexity = sum . map (length . filter (/= blank))
rows = id
@0x0L
0x0L / proj.py
Last active June 19, 2017 23:55
Projection on hyperplanes
A = np.array([
[1, 1, 1],
[0.8, 0.7, 0.5]
])
assert np.linalg.matrix_rank(A) == len(A)
Q, _ = np.linalg.qr(A.T)
P = np.eye(len(Q)) - Q @ Q.T
@0x0L
0x0L / l1_proj.py
Created July 23, 2017 14:26
l1 projection
def l1_simplex_proj(x, radius=1.0):
z = np.abs(x)
mu = np.sort(z)[::-1]
cmu = np.cumsum(mu)
f = mu * np.arange(1, 1 + len(x)) - (cmu - radius)
rho = np.where(f > 0)[0][-1]
theta = (cmu[rho] - radius) / (1.0 + rho)
y = np.sign(x) * np.maximum(z - theta, 0)
return y
@0x0L
0x0L / normalize.py
Created July 24, 2017 11:57
normalize
p = sp.stats.laplace.fit(x)
y = sp.stats.norm.ppf(sp.stats.laplace.cdf(x, *p))
z = sp.stats.laplace.ppf(sp.stats.norm.cdf(y), *p)
@0x0L
0x0L / rie.py
Last active October 15, 2019 23:00
RIE
import matplotlib.pyplot as plt
import numpy as np
import sklearn.covariance as cov
def _norm2(x):
return np.square(x.real) + np.square(x.imag)
def rie_estimator(X):
T, N = X.shape
public class LightGBMTree
{
public int[] split_feature;
public double[] threshold;
public int[] left_child;
public int[] right_child;
public double[] leaf_value;
public double Predict(double[] features)
{
def markowitz(mu, cov, hedge=None):
z = mu
K = np.linalg.inv(cov)
if hedge is not None:
Q = hedge @ K @ hedge.T
beta = np.linalg.inv(Q) @ hedge @ K @ mu
z = mu - beta @ hedge
z = K @ z
@0x0L
0x0L / gist:0def2e77518e3d137371225ee7093546
Created February 9, 2018 19:59
jupyter notebook width
from IPython.display import HTML
HTML("<style>.container { width: 95% !important; }</style>")
@0x0L
0x0L / parmap.py
Last active February 10, 2018 02:57
parmap with monitoring
def _parmap(fun, q_in, q_out, init=None):
init() if init else None
while True:
i, x = q_in.get()
ret = fun(x)
q_out.put((i, ret))
def parmap(fun, tasks, n=None, init=None):
from multiprocessing import Pool, Queue