Skip to content

Instantly share code, notes, and snippets.


Erik Bernhardsson erikbern

View GitHub Profile
erikbern / gist:7cd899651aff9b656e9f
Last active Aug 29, 2015
Stupid graph visualizations
View gist:7cd899651aff9b656e9f
def coin():
while True:
yield random.randint(0, 1)
def dice_optimal():
x, y, z = 0, 1, 1
for c in coin():
x, y, z = x+c*z, y+c*z, 2*z
a, b = x*6//z, y*6//z

The cities of Beijing and Buenos Aires are almost antipodes, i.e. they are situated almost opposite of each other on the globe.

An interesting attribute of this is that travelling between the two cities can be done through any point of earth and the trip will still be roughly the same distance. This D3 visualization demonstrates this principle: drag the map around to change perspective, click the map to set a "midpoint" to travel through.

Code for this visualization was stolen from all over the web without any deeper understanding of D3 – in particular this one and this one.

IIRC this is in a theme in Wong Kar-Wai's movie Happy Together.

erikbern /
Last active Jan 15, 2021
MCMC for simple marketing data
import pymc, pymc.graph
import matplotlib.pyplot as plt
import numpy as np
import sys
channels = [
('A', 2292.04, 9),
('B', 1276.85, 2),
('C', 139.59, 3),
('D', 954.98, 5)
erikbern /
Last active Jan 17, 2021
Installing TensorFlow on EC2
# Note – this is not a bash script (some of the steps require reboot)
# I named it .sh just so Github does correct syntax highlighting.
# This is also available as an AMI in us-east-1 (virginia): ami-cf5028a5
# The CUDA part is mostly based on this excellent blog post:
# Install various packages
sudo apt-get update
erikbern /
Last active Oct 19, 2016
Install TensorFlow on EC2
# Start from ami-763a311e
# Install various packages
sudo apt-get update
sudo apt-get upgrade -y # choose “install package maintainers version”
sudo apt-get install -y build-essential python-pip python-dev git python-numpy swig python-dev default-jdk zip zlib1g-dev
# Install Bazel
git clone
cd bazel
# Simple models of two competing firms bidding on employees
# Each employee is described in terms of k factors
# Company X knows about the x first factors
# Company Y knows about the y first factors
import numpy
from matplotlib import pyplot
n = 10000
k, x, y = 10,
import pymc3, numpy, sys, seaborn, re
def get_dist(fn):
y = [0, 0, 0, 0, 0]
for line in open(fn):
num = re.split('\D', line)[0]
y[int(num) - 1] += 1
print fn, 'can not parse:', line
import os, sys, re
patterns = [
# <img src=';bg=T&#038;fg=000000&#038;s=0' alt='\mathcal{O}(n)' title='\mathcal{O}(n)' class='latex' />
(r'<img.*?title=\'(.*?)\' class=\'latex\' />',
'$$ \\1 $$ ',
# [<img class=" size-full wp-image-1722 aligncenter" src="" alt="avg" width="512" height="512" />](
from google.transit import gtfs_realtime_pb2
import urllib
for feed_id in [1, 2, 11]:
feed = gtfs_realtime_pb2.FeedMessage()
response = urllib.urlopen('' % (os.environ['MTA_KEY'], feed_id))
print feed