Skip to content

Instantly share code, notes, and snippets.

@caub
caub / bordereffects.md
Last active August 29, 2015 14:00
Border effects, the importance of padding method

padding in green without padding, in red with symmetric padding

% generate a  noisy signal
y = flipud( 3*sin(0.14*(1:128)')-1/20*(1:128)'+ (((1:128)'-64)/20).^2 + randn(128,1));
w=[-21;14;39;54;59;54;39;14;-21]/231; % http://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter#Tables_of_selected_convolution_coefficients

z = zeros(length(y)+10,1); % bigger
z(5:end-4) = y; 

z(1:5) = flipud(z(6:10)); % symmetric padding

@caub
caub / normalize.m
Last active August 29, 2015 14:01
normalizes data
% classic case, feature are by column
function [Xnorm, Xmean, Xsigma] = normalizeFeature(X)
Xmean = mean(X);
Xnorm = bsxfun(@minus, X, Xmean);
Xsigma = sqrt(sum(Xnorm.^2)/(size(Xnorm,1)-1));
Ynorm = bsxfun(@rdivide, Xnorm, Xsigma);
% all this is equivalent to zscore(X) :)
% for example collaborative filtering sets, classes are in row, and users in col
@caub
caub / LDA.md
Last active August 29, 2015 14:01

lda

X =[-1 -1; -2 -1;-3 -2;1 1;2 1; 3 2];
y = [1 1 1 2 2 2];
m = lda(X,y);
m.predict([-0.8 -1]) %1
gscatter(X(:,1),X(:,2),y','rb','v^',[],'off');
hold on
subtract = @(X) X(:,2) - X(:,1);

ezplot(@(x,y) subtract(m.decision_function([x y])))

@caub
caub / mountain_goats.py
Last active August 29, 2015 14:01
web scraping
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException,ElementNotVisibleException
import time
import requests
import json
import urlparse
browser = webdriver.Firefox()
@caub
caub / rsi.m
Last active August 29, 2015 14:01
% largely inspired from http://www.mathworks.fr/matlabcentral/fileexchange/37932-automated-trading-with-matlab-2012
% small script to generate signals from rsi indicator, and calculate the return
function [s,r,sh,mar] = rsi(price,M,N,thresh,scaling,cost)
% returns the signals array, the returns array (profit and loss), the
% sharpe ratio, the MAR ratio
if nargin==0
% fill params for you..
M = 22; % Moving Average for rsi
N = 14; % rsi loopback
@caub
caub / Triangulations.md
Last active August 29, 2015 14:02
triangulations

triangulations

The positions of green nodes are initially set, other nodes are guessed incrementally from neighbors distance. A noise is added to distances, up to N(0, 1.7^2) to simulate measurement error, greater values can make the nodes positions diverge importantly
To fix this stability problem, it's possible to avoid 'flat' triangles (where the 3 points are almost collinear)
Other perspective: make it work in 3D, or more, with the intersection of (n+1) n-spheres, compare with more robust optimizations in litterature
Another perspective, average the result of multiple different estimations for a node positions, to eliminate the noise better

@caub
caub / indic.py
Last active August 29, 2015 14:08
from scipy import *
def sma(v, k):
weights = repeat(1.0,k)/k
return convolve(v, weights, 'valid')
def ema(v, k):
weights = exp(linspace(-1,0,k))
weights /= weights.sum()
#print 'we: %s' % weights
#print 'c: %s' % convolve(v, weights)
@caub
caub / conv.js
Last active August 29, 2015 14:15
convolution product in js
function conv(a, b, mode){
var dl = b.length-1
var aa=a.concat(repeat(0, dl)) //0-padding
var c=[]
for(var i=0;i<aa.length;i++){
c[i]=0;
for(var m=0;m<aa.length;m++)
if (0<=i-m && i-m<b.length)
c[i]+=aa[m]*b[i-m];
@caub
caub / SEChat.java
Last active November 2, 2015 16:16
package ws;
import java.lang.reflect.Type;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.concurrent.Future;
@caub
caub / SEChat.js
Last active September 20, 2016 18:21
var cheerio = require('cheerio');
var Promise = require("bluebird");
var request = Promise.promisifyAll(require('request'));
var WebSocket = require('ws');
var email = "SE email",
password = "SE pw",
roomid = 17;
var j = request.jar()