Skip to content

Instantly share code, notes, and snippets.

arima.simulation <- function(linear=1, exponential=0, seasonal=0, season=25, random=1, ahead=10) {
sim.data <- (1:100 * linear/100) + exponential*exp( (1:100/100))/exp(1) + seasonal*sin(1:100 * 2 * pi / season ) + random*rnorm(100);
sims <- ts(sim.data, frequency=25)
sim.model <- auto.arima(sims);
sim.past.future <- c(as.vector(fitted(sim.model)),as.vector(forecast(sim.model, h=ahead)$mean));
ylimits = c( min(sim.past.future), max(sim.past.future))
plot(sim.data, pch=20, col=rgb(0,0,1, alpha=0.5), ylim=ylimits, xlim=c(0,length(sim.past.future)));
lines(seq_along(sim.past.future), sim.past.future, col=rgb(0,1,0, alpha=0.75));
return(sim.model);
@olooney
olooney / operators.js
Created May 28, 2011 20:13
javascript operators wrapped in functions so you can refer to them by name
op = {
"!!": function(x) { return !!x; },
"!": function(x) { return !x; },
"&&": function(x, y) { return x && y; },
"||": function(x, y) { return x || y; },
"~": function(x) { return ~x; },
"negative": function(x) { return -x; },
"+": function(x, y) { return x + y; },
@olooney
olooney / addEventListener.js
Created June 15, 2011 15:39
cross-browser event listeners
// observable is a DOM node, document, window, etc.
// eventName is the string event name, say "load" or "click"
// handler is the callback function.
function addEventListener(observable, eventName, handler) {
if ( observable.addEventListener ) {
observable.addEventListener(eventName, handler, false);
} else if ( observable.attachEvent ) {
observable.attachEvent("on" + eventName, handler);
} else {
// error handling?
@olooney
olooney / sizes.cpp
Created August 8, 2011 18:59
Show the sizes of C++ built-in types (useful for 32/64 bit systems)
#include <iostream>
#define SIZE(type) std::cout << #type ": " << sizeof(type) << " bytes\n";
int main(int argc, char** argv ) {
SIZE(char);
SIZE(short);
SIZE(int);
SIZE(long);
SIZE(float);
@olooney
olooney / pkcs7.py
Created December 19, 2011 17:15
padding/unpadding per PKCS7 or PKCS5.
# padding/unpadding per PKCS7 or PKCS5.
class PKCS7(object):
def __init__(self, block_size):
self.block_size = block_size
def pad(self, string):
padding_number = self.block_size - len(string) % self.block_size
if padding_number == self.block_size:
return string
@olooney
olooney / p2.py
Created February 22, 2012 15:42
examples of python class customization, generic descriptors
import math
class Magnitude(object):
'''A descriptor that can be added to any N-dimensional point to provide a read/writable magnitude property.'''
def __init__(self, *attributes):
self.attributes = attributes
def __get__(self, instance, owner):
sum_of_squares = 0.0
@olooney
olooney / encrypt-decrypt.bash
Created February 29, 2012 22:33
Use openSSL to simply encrypt a file
# snippet from http://www.madboa.com/geek/openssl/#encrypt-simple
# encrypt file.txt to file.enc using 256-bit AES in CBC mode
openssl enc -aes-256-cbc -salt -in file.txt -out file.enc
To decrypt file.enc you or the file’s recipient will need to remember the cipher and the passphrase.
# decrypt binary file.enc
openssl enc -d -aes-256-cbc -in file.enc
def pipe(inputCursor, outputCursor, outputTablename):
'''
Read rows from the inputCursor and immediately INSERT them into the
outputTablename using the supplied outputCursor. There are faster
mechanisms like copy_from() for particular databases but this is extremely
portable: not only do the inputCursor and outputCursor not have to be for
the same database, they can be for different database engines without
problem.
'''
import poplib, email, getpass
M = poplib.POP3_SSL('pop.gmail.com', 995)
username = raw_input("gmail username: ")
M.user(username)
M.pass_(getpass.getpass('%s@gmail.com password: ' % username))
status, emails, octets = M.list()
if status.startswith('+OK'):
print 'new emails:', len(emails)
"""Solves the 8-queens problem (and its N-queens generalization.)"""
# It is clear that since no two queens can occupy the same column, and there are
# as many columns as queens to place, then each column must contain one and only one
# queen. The only question is, on which row to place each queen?
# A "left-board" is a way to represent a partial solution to the n-queens problem.
# It is simply a list of integers, each indicating the row occupied by the queen
# in each column. Because it is a partial solution, it can have fewer than 8 elements,
# or even be empty. For example, [0, 2] indicates a that the queen in the left-most