This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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 = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import argparse | |
import re | |
import json | |
import string | |
parser = argparse.ArgumentParser( | |
description="Print a Markdown table of contents for a Jupyter notebook." | |
) |

NewerOlder