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 / 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 / main.cpp
Created March 8, 2015 15:01
PCR in C++
#include "pcr.hpp"
#include <iostream>
int main(int argc, char * argv [])
{
DNA dna({DNA::Base::A, DNA::Base::C, DNA::Base::G, DNA::Base::T, DNA::Base::C, DNA::Base::T});
DNA::container_t ret = pcr(dna, 1, 3, 10);
@goldsborough
goldsborough / range.cpp
Created April 2, 2015 01:10
Python range() in C++
class range
{
public:
typedef std::vector<long>::iterator iterator;
typedef std::vector<long>::const_iterator const_iterator;
range(long end)
@goldsborough
goldsborough / calculate.py
Created August 15, 2015 22:36
Djikstra's two-stack algorithm for equation parsing
#include <map>
#include <stack>
#include <string>
#include <iostream>
#include <functional>
double calculate(const std::string& equation)
{
static std::string chars("+-*/");
@goldsborough
goldsborough / polynomial.py
Created August 18, 2015 18:28
Polynomial parser and calculator using Horner's rule
import re
from collections import namedtuple
def split_equation(equation):
match = re.match(r'^\s*\w+\((\w+)\)\s*=\s*(.*)$', equation)
return match.groups()
def find_terms(equation, var='x'):