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
import redis | |
import threading | |
class Listener(threading.Thread): | |
def __init__(self, r, channels): | |
threading.Thread.__init__(self) | |
self.redis = r | |
self.pubsub = self.redis.pubsub() | |
self.pubsub.subscribe(channels) | |
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
import time | |
import redis | |
import urllib | |
import threading | |
import simplejson | |
class Generator(threading.Thread): | |
def __init__(self, generator, output_channel, _redis=None): | |
threading.Thread.__init__(self) | |
self.generator = generator |
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
import matplotlib | |
import matplotlib.pyplot as plt | |
# Iris petal length/width scatterplot (greatest class correlation) | |
# Dataset: http://archive.ics.uci.edu/ml/datasets/Iris | |
# Output: https://imgur.com/9TWhn | |
def data(): | |
lists = [line.strip().split(",") for line in open('flowerdata.txt', 'r').readlines()] | |
return [map(float, l[:4]) for l in lists], [l[-1] for l in lists] |
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
import csv | |
import pylab as p | |
data = [r for r in csv.reader(open('piratebay.csv', 'rb'))] | |
def categoryratio(c): | |
return sum([ int(r[3]) for r in data if r[1][0] == str(c) ]) | |
# Adapted from # http://scienceoss.com/bar-plot-with-custom-axis-labels/ | |
fig = p.figure() |
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
heights = ["high", "medium", "short"] | |
dist = [0.2, 0.6, 0.2] | |
generator = position_generator(dist) | |
n = 10000 | |
count = 0 | |
high = 0 | |
med = 0 | |
low = 0 |
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 profit(supply, demand, sale_price=0.40, production_cost=0.25, welfare_price=0.10, lost=0.15): | |
operative_cost = production_cost * supply | |
if supply >= demand: | |
excedent = supply - demand | |
sale_income = demand * sale_price | |
welfare_gain = excedent * welfare_price | |
result = sale_income + welfare_gain - operative_cost | |
else: | |
not_baked_loss = lost * (demand - supply) | |
sale_income = supply * sale_price |
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
demand_dist = (0.30, 0.45, 0.25) # [0]high, [1]medium, [2]low | |
positions = ( 36, 48, 60, 72, 84, 96) | |
high_demand = (0.05, 0.10, 0.25, 0.30, 0.20, 0.10) | |
med_demand = (0.10, 0.20, 0.30, 0.25, 0.10, 0.05) | |
low_demand = (0.15, 0.25, 0.35, 0.15, 0.05, 0.05) | |
demand_type = position_generator(demand_dist) | |
# be careful with order! | |
demand_amount = [ |
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
import sys | |
import itertools | |
import numpy as np | |
from collections import Counter | |
from matplotlib import pyplot as plt | |
def dice_prob(n): | |
sides = [(1,2,3,4,5,6) for x in xrange(n)] | |
combined = itertools.product(*sides) | |
sums = [sum(i) for i in combined] |
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
import sys | |
import math | |
import numpy as np | |
from matplotlib import pyplot as plt | |
# https://en.wikipedia.org/wiki/Binomial_distribution | |
# k successes, n trials, p probability of success | |
def binomial_pmf(k, n, p): | |
nk = math.factorial(n) / (math.factorial(k) * math.factorial(n-k)) | |
return nk * p**k * (1.0-p)**(n-k) |
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 http://funcall.blogspot.sg/2009/03/not-lisp-again.html | |
def deriv(f, dx=.0001): | |
return lambda x: (f(x + dx) - f(x)) / dx | |
cube = lambda x: x**3 | |
d = deriv(cube) | |
for n in xrange(5): | |
print n, d(n) |
OlderNewer