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 / 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."""
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
@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
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.
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
@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")
@eigenfoo
eigenfoo / setup.py
Created September 9, 2019 00:58
Template setup.py file
""" Template setup.py file """
from os.path import dirname, realpath, join, exists
from re import search, M
from setuptools import setup, find_packages
# TODO: add package details
NAME = ""
MAINTAINER = ""
@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."
)
@eigenfoo
eigenfoo / blog.png
Last active June 17, 2019 23:50
Personal website and blog
blog.png