Skip to content

Instantly share code, notes, and snippets.

@tpott
tpott / value_iteration
Created March 13, 2013 20:51
One of these has a semantic error that is fixed in the other. Can you find which one??
def value_iteration_A(probs, gamma, rewards, oldV):
newV = oldV.copy()
nstates, nactions = probs.shape[0], probs.shape[-1]
for s in range(nstates):
weightedV = np.zeros([nactions], dtype='float64')
for a in range(nactions):
weightedV[a] = np.dot(probs[s,:,a], oldV)
newV[s] = rewards[s] + gamma * weightedV.max()
return newV
@tpott
tpott / gist:5210987
Last active December 15, 2015 05:39
hmm training
# Created from these lecture notes:
# http://cseweb.ucsd.edu/classes/wi13/cse150-a/lecture/lecture11.pdf
# http://cseweb.ucsd.edu/classes/wi13/cse150-a/lecture/lecture12.pdf
# http://cseweb.ucsd.edu/classes/wi13/cse150-a/lecture/lecture13.pdf
# Dependencies:
# init_{a,b,pi} := the initial estimated matrices, can be random
# compute_forward := calculates the alpha matrix
# compute_reverse := calculates the beta matrix
# compute := calculates each of the matrices for the E-step
@tpott
tpott / gist:5401472
Created April 17, 2013 03:02
javascript callbacks
// this first version uses a function callback to pass in the object reference...
for (var i = 0; i < files.length; i++) { // WORKS
var setFile = function(file) {
return function(err, data) {
if (err) throw err;
file[1] = data;
};
};
fs.readFile(files[i][2], 'utf8', setFile(files[i]));
@tpott
tpott / gist:5454257
Created April 24, 2013 18:16
server/objects/movable.js check for collisions
Movable.prototype.doICollide = function(collidables) {
var myPosition = this.position;
var myNewPos = this.position.clone().add(this.velocity);
// check if i collide with anything!
for (var id in collidables) {
if (collision(myPosition, myNewPos, collidables[id])) {
return true;
}
}
@tpott
tpott / gist:5970249
Created July 10, 2013 20:55
estimated collision for given binary length of an ID
# collision.py
# needed for python 2.x
from __future__ import division
# permutation
def perm(x, n):
"x balls in n buckets"
def mult_l(acc, head):
return acc * head
@tpott
tpott / find_dup_files.py
Created November 25, 2013 03:12
Finds duplicate files in a specified directory. First checks file size, and then the md5.
#! /usr/bin/env python
# find_dup_files.py
# Trevor Pottinger
# Sun Nov 10 21:55:45 PST 2013
import hashlib
import sys
import os
from os.path import join, getsize
# nhands.py
# Trevor Pottinger
# Fri Mar 14 22:23:19 PDT 2014
from random import random
def counts2deck(ccounts):
"""Converts the card counts to a list of sorted cards"""
deck = []
for letter in ccounts:
@tpott
tpott / vidcopy.py
Created March 13, 2014 17:52
video processing, take one
# vidcopy.py
# Trevor Pottinger
# Thu Mar 6 17:18:22 PST 2014
import numpy as np
# lets try reading in a file and then writing it out
from moviepy.video.io.ffmpeg_reader import FFMPEG_VideoReader as reader
from moviepy.video.io.ffmpeg_writer import FFMPEG_VideoWriter as writer
@tpott
tpott / nhands.py
Last active August 29, 2015 13:57
# nhands.py
# Trevor Pottinger
# Fri Mar 14 22:23:19 PDT 2014
import time
from random import random
def my_cmp(x,y):
if x[1] > y[1]:
return -1
@tpott
tpott / self_parsing.go
Created May 26, 2014 16:48
simple go parser
// self_parsing.go
// Mon May 26 09:47:10 PDT 2014
package main
import (
"os"
"fmt"
"reflect"
"go/ast"