Skip to content

Instantly share code, notes, and snippets.

@0x0L
0x0L / pandas_sherman.py
Last active March 2, 2018 19:41
Sherman-Morrison rolling regression
import pandas as pd
def pd_move_regress(X, Y, w, fit_intercept=True):
assert (X.index == Y.index).all()
B, A = move_regress(X.values, Y.values, w, fit_intercept=fit_intercept)
idx = pd.MultiIndex.from_product([X.columns, Y.columns])
B = pd.DataFrame(B.reshape(len(B), -1), index=X.index, columns=idx)
A = pd.DataFrame(A, index=X.index, columns=Y.columns)
return B, A
from math import log, pi
import numba
import scipy.optimize as opt
@numba.jit(nopython=True, nogil=True)
def _precompute_dp(x, y):
T = 1.0 * x.shape[0]
@0x0L
0x0L / tf.py
Created October 25, 2018 06:26
tf.py
# coding: utf-8
# In[ ]:
import numpy as np
import pandas as pd
import xarray as xr
@0x0L
0x0L / fig2summary.py
Created January 21, 2019 16:47
mpl figure 2 tf image summary
import io
def tf_summary_image(figure):
figure.canvas.draw()
w, h = figure.canvas.get_width_height()
with io.BytesIO() as buffer:
figure.savefig(buffer, format='png')
image_str = buffer.getvalue()
class MergeSequence(keras.utils.Sequence):
def __init__(self, *seqs):
lens = [len(s) for s in seqs]
length = sum(lengths)
indices = np.zeros((length, 2), dtype='int')
for n, i in zip(lens, np.cumsum(lens))[:-1]:
indices[i:] += [1, n]
self.indices = indices
from collections import defaultdict
import numpy as np
import pandas as pd
import tensorflow as tf
import xarray as xr
tf.enable_eager_execution()
@0x0L
0x0L / str_accessors.py
Last active May 24, 2019 17:46
xarray string accessors
import numpy as np
import pandas as pd
import xarray as xr
@xr.register_dataarray_accessor('str')
class StringAccessor:
def __init__(self, xarray_obj):
self._obj = xarray_obj
self._data = xarray_obj.data
@0x0L
0x0L / pandas.cs
Last active October 5, 2021 20:14
Pandas EWM C#
public static class Pandas
{
public static double[] Ewma(
double[] vals, double com,
bool adjust=true, bool ignore_na=false, int minp=0)
{
int N = vals.Length;
var output = new double[N];
if (N == 0)
@0x0L
0x0L / np_diag.py
Created December 12, 2019 19:44
Numpy diagonal views
def diag(A, k=0):
s = slice(
max(k, -A.shape[1] * k),
max(0, (A.shape[1] - k) * A.shape[1]),
A.shape[1] + 1
)
return A.ravel()[s]
@0x0L
0x0L / RMT autocorrelations.ipynb
Created January 12, 2020 21:41
RMT autocorrelations
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.