Skip to content

Instantly share code, notes, and snippets.

View eigenfoo's full-sized avatar
Probably drinking coffee

George Ho eigenfoo

Probably drinking coffee
View GitHub Profile
@eigenfoo
eigenfoo / trajectory.py
Created December 15, 2019 20:04
PyMC3 `pymc3/step_methods/hmc/trajectory.py` as of dc9fd725
from collections import namedtuple
import theano
import theano.tensor as tt
import numpy as np
from pymc3.theanof import join_nonshared_inputs, gradient, CallableTensor, floatX
Hamiltonian = namedtuple("Hamiltonian", "logp, dlogp, pot")
class FunctionToGenerator(ast.NodeTransformer):
"""
This subclass traverses the AST of the user-written, decorated,
model specification and transforms it into a generator for the
model. Subclassing in this way is the idiomatic way to transform
an AST.
Specifically:
1. Add `yield` keywords to all assignments
class Model:
""" pm.Model decorator. """
def __init__(self, func):
self.func = func
# Introspect wrapped function, instead of the decorator class.
functools.update_wrapper(self, func)
# Uncompile wrapped function.
@Model
def linear_regression(x):
scale = tfd.HalfCauchy(0, 1)
coefs = tfd.Normal(tf.zeros(x.shape[1]), 1)
predictions = tfd.Normal(tf.linalg.matvec(x, coefs), scale)
return predictions
linear_regression.model_generator(tf.zeros([3, 10])) # Shape is irrelevant here
def linear_regression(x):
scale = yield tfd.HalfCauchy(0, 1)
coefs = yield tfd.Normal(tf.zeros(x.shape[1]), 1, )
predictions = yield tfd.Normal(tf.linalg.matvec(x, coefs), scale)
return predictions
@eigenfoo
eigenfoo / beta_bernoulli_bandit.py
Last active March 1, 2021 22:57
Solving Bernoulli multi-armed bandit with Thompson sampling
def make_bandits(params):
def pull(arm, size=None):
while True:
# Bernoulli distributed rewards
reward = np.random.binomial(n=1, p=params[arm], size=size)
yield reward
return pull, len(params)
@eigenfoo
eigenfoo / client.py
Last active January 30, 2022 23:34
""" Stream data from the WebSocket and update the Beta posterior parameters online. """
import tornado.ioloop
import tornado.websocket
class WebSocketClient:
def __init__(self, io_loop):
self.connection = None
self.io_loop = io_loop
@eigenfoo
eigenfoo / server.py
Last active January 30, 2022 23:34
""" Every 100ms, sample from a Bernoulli and write the value to a WebSocket. """
import random
import tornado.ioloop
import tornado.web
import tornado.websocket
class WebSocketServer(tornado.websocket.WebSocketHandler):
"""Simple WebSocket handler to serve clients."""
@eigenfoo
eigenfoo / gamma_poisson_bandit.py
Last active March 6, 2022 17:25
Solving Poisson multi-armed bandit with Thompson sampling
def make_bandits(params):
def pull(arm, size=None):
while True:
# Poisson distributed rewards
reward = np.random.poisson(lam=params[arm], size=size)
yield reward
return pull, len(params)
@eigenfoo
eigenfoo / ipynb_toc.py
Created June 24, 2019 08:37
Print a Markdown table of contents for a Jupyter notebook.
#!/bin/python
import argparse
import re
import json
import string
parser = argparse.ArgumentParser(
description="Print a Markdown table of contents for a Jupyter notebook."
)