Skip to content

Instantly share code, notes, and snippets.

View goldsborough's full-sized avatar
🔨
Fixing things

Peter Goldsborough goldsborough

🔨
Fixing things
View GitHub Profile
@goldsborough
goldsborough / forward.py
Created September 3, 2014 09:19
Forward algorithm
states = ("Sad","Happy")
observations = ("Crying","Laughing","Eating")
stateM = {'Sad': 0.3, 'Happy': 0.7}
transM = {'Sad': {'Sad': 0.3, 'Happy': 0.7}, 'Happy': {'Sad': 0.4, 'Happy': 0.6}}
observM = {'Sad': {'Crying': 0.7, 'Laughing': 0.1, 'Eating': 0.2}, 'Happy': {'Crying': 0.1, 'Laughing': 0.6, 'Eating': 0.3}}
observS = ["Laughing","Eating","Laughing"]
bayes = lambda a,b: ((a/(a+b)),(b/(a+b)))
@goldsborough
goldsborough / viterby.py
Created September 3, 2014 09:20
Viterbi algorithm
def viterbi(states,observations,stateM,transM,observM,observSeq):
probab = [{}]
path = {}
# Initialize states
for s in states:
# first probability is the initial state times the
# first observation
probab[0][s] = stateM[s] * observM[s][observSeq[0]]
path[s] = [s] # first destination
@goldsborough
goldsborough / HMM.cpp
Created September 3, 2014 09:22
Hidden Markov Model algorithms
//
// main.cpp
// HMM
//
// Created by Peter Goldsborough on 01/07/14.
// Copyright (c) 2014 Peter Goldsborough. All rights reserved.
//
#include <iostream>
#include <vector>
@goldsborough
goldsborough / database.py
Created September 3, 2014 10:07
Python Sqlite3 wrapper
###########################################################################
#
## @file database.py
#
###########################################################################
import sqlite3
###########################################################################
#
@goldsborough
goldsborough / postgres.py
Created September 3, 2014 10:09
Python psycopg2 wrapper
###########################################################################
#
## @file postgres.py
#
###########################################################################
import psycopg2
###########################################################################
#
@goldsborough
goldsborough / mini_midi.ino
Last active August 29, 2015 14:06
Mini Midi
/*
The MIT License (MIT)
Copyright (c) 2014 Peter Goldsborough
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@goldsborough
goldsborough / statistics.py
Created December 3, 2014 21:02
Statistical functions
import math
def average(values):
return sum(values)/len(values)
def median(values):
values.sort()
@goldsborough
goldsborough / medianfilter.cpp
Last active August 29, 2015 14:13
1-D Median Filter
std::vector<double> medianFilter_(const std::vector<double>& array, unsigned long window)
{
std::vector<double> filtered;
filtered.reserve(array.size());
unsigned long middle = window / 2;
std::vector<double>::const_iterator itr, end, first, last, firstMid, lastMid;
@goldsborough
goldsborough / sm.cpp
Created January 20, 2015 22:01
Square-and-multiply algorithm for efficient exponentiation
long squareAndMultiply(long base, unsigned short power)
{
// x^0 = 1
if (! power) return 1;
// x^1 = x
if (power == 1) return base;
// if power is odd
if (power % 2)
@goldsborough
goldsborough / rsa.py
Last active July 25, 2019 07:16
Simple RSA
import random
import math
from collections import namedtuple
###########################################################
##
## Simple class to generate RSA keys and then encrypt or
## decrypt an integer.
##