Skip to content

Instantly share code, notes, and snippets.

@paultsw
paultsw / fit_gp_sklearn.py
Created August 21, 2017 16:56
Gaussian process regression for 1d signals with sklearn
"""
Fit a Gaussian process to a signal using SKLearn.
"""
import numpy as np
from sklearn import gaussian_process
from sklearn.gaussian_process import kernels as K
import matplotlib.pyplot as plt
from scipy.signal import resample
import argparse
@paultsw
paultsw / causal_conv1d.py
Created August 7, 2017 22:56
A simple way of computing a causal conv1d using a pad+shift on the whole sequence. Implemented as a PyTorch module. (Currently untested with no CUDA support.)
import torch
import torch.nn as nn
from torch.autograd import Variable
__CUDA__ = torch.cuda.is_available()
class CausalConv1d(nn.Module):
"""
A causal 1D convolution.
"""
@paultsw
paultsw / percent_identity.py
Created July 26, 2017 15:25
Compute percent identity between two bases.
"""
Tensorflow ops for computing percent-identity between two sequences.
"""
import tensorflow as tf
def sparsify_seq(dseq, maxtime, pad_value=0):
"""
Convert a dense rank-2 sequence tensor `dseq` to a sparse tensor, applying masking to
pad values to avoid spuriously high edit distances when comparing two sparsified
tensors.
@paultsw
paultsw / gaussian_noise_wrapper.py
Created May 30, 2017 18:30
LSTM Wrapper with Gaussian Noise added to Input
class GaussianNoiseWrapper(tf.contrib.rnn.RNNCell):
"""
Wrapper for RNNCells to add gaussian noise to the input with specified mean,
stdv before each call of the internal RNNCell.
(The structure of this wrapper class follows the design of wrappers in tf.contrib.rnn
such as InputProjectionWrapper, DropoutWrapper, OutputProjectionWrapper, etc.)
"""
def __init__(self, cell, mean=0.0, stddev=0.1):
"""
"""
Binomial asset price dynamics model class definiton.
"""
class BinomialModel:
"""
Initialize with the following parameters:
R <- interest rate on cash account investment;
u <- up movement percentage (d := 1/u);
@paultsw
paultsw / median.js
Created September 14, 2015 16:23
Algorithm to recursively compute median of the union of two arrays.
function median(A,B,x,y) {
/*
Function that takes sorted arrays A and B and their medians x and y
and returns the median of A.concat(B).
x is assumed to be the median of A and y is assumed to be the median
of B.
Arrays are assumed to be the same length. (Arrays of differing length
may be handled with a different recursion case.)
@paultsw
paultsw / cluster.js
Last active September 14, 2015 16:21
function dist(p,q) {
/*
Euclidean distance function between two points.
*/
return Math.sqrt((p.x - q.x)^2, (p.y - q.y)^2);
}
function average(datapts) {
/*
Takes an array of datapoints, where each
@paultsw
paultsw / boundingSphere.hs
Last active February 26, 2019 02:29
Implementations of Ritter's sphere bound algorithm
boundingSphere :: [(Float,Float,Float)] -> ((Float,Float,Float),Float) --takes a list of points in 3D to a pair (center,radius)
boundingSphere points =
case points of --induction on number of points: 0,1,2, or 3+
[] -> ((0,0,0),0)
p:[] -> (p,0)
(p1,p2,p3):(q1,q2,q3):[] -> (((p1+q1)/2,(p2+q2)/2,(p3+q3)/2),dist (p1,p2,p3) (q1,q2,q3))
p:pts -> let
y@(y1,y2,y3) = head $ filter (\pt -> (dist pt p) - 0.1 < (maximum $ map (dist p) pts)) pts
z@(z1,z2,z3) = head $ filter (\pt -> (dist pt y) - 0.1 < (maximum $ map (dist y) pts)) pts
initSphere@(ctr,rad) = (((y1+z1)/2, (y2+z2)/2, (y3+z3)/2), dist y z / 2)