Skip to content

Instantly share code, notes, and snippets.

View drvinceknight's full-sized avatar
😀
👍

Vince Knight drvinceknight

😀
👍
View GitHub Profile
\documentclass[11pt]{article}
\usepackage{url}
\usepackage{tikz}
\usepackage{calc}
\reversemarginpar
\usepackage[paper=letterpaper,
marginparwidth=1in,
from __future__ import division
import matplotlib.pyplot as plt
def dictplot(D, f, title):
plt.figure()
values = [v / sum(list(D.values())) for v in list(D.values())]
plt.bar(range(len(D)), values, align='center')
plt.xticks(range(len(D)), D.keys())
plt.ylabel('Probability')
plt.title(title)
# LaTeX Files
*.aux
*.glo
*.idx
*.log
*.toc
*.ist
*.acn
*.acr
*.alg
"""
Sage script for example in blog post.
"""
class Tandem_Queue():
"""
A class for an instance of the tandem_queue
"""
def __init__(self, c_1, N, c_2, Lambda, mu_1, mu_2, p):
self.c_1 = c_1
self.c_2 = c_2
@drvinceknight
drvinceknight / 2by2games
Created September 26, 2014 11:34
A Sage interact to play with 2 by 2 games
def Pure_Nash_Check(A,B,i,j):
if i==0:
if j==0:
if A[0,0]<A[1,0] or B[0,0]<B[0,1]:
return False
if j==1:
if A[0,1]<A[1,1] or B[0,1]<B[0,0]:
return False
if i==1:
if j==0:
@interact
def _(A=matrix(RDF,3,3,[8,2,0,1,7,2,1,3,6])/10,x_init=("$\pi^{(0)}$",vector([.2,.5,.3]))):
x_init = vector(x_init)
data = [x_init * (A^i) for i in range(20)]
pi = zip(*data)
p = line(enumerate(pi[0]),color='red',legend_label='$\pi_1$',thickness=3)
p += line(enumerate(pi[1]),color='blue',legend_label='$\pi_2$',thickness=3)
p += line(enumerate(pi[2]),color='green',legend_label='$\pi_3$',thickness=3)
p.ymax(1)
show(p)
@drvinceknight
drvinceknight / Checking the exponential approximation
Last active August 29, 2015 14:07
There is a nice paper by Cleve Moler and Charles Van Loan called: "Nineteen Dubious Ways to Compute the Exponential of a Matrix" which is a return to a paper (by the same authors) called: "Nineteen Dubious Ways to Compute the Exponential of a Matrix". This interact simply looks at the Series method. The inputs allow control of the number of term…
@interact
def _(A=matrix(RDF,[[1,3,1],[5,-2,2],[3,3,4]]),n=("$n$",slider(0,100,1)),Precision=slider(0,10,1,4)):
exp_A=exp(A)
approx_exp=sum([A^i/factorial(i) for i in range(n+1)]).n(digits=Precision)
p=list_plot([(sum([A^i/factorial(i) for i in range(n+1)])-exp(A)).norm(2) for n in range(0,21)])
p.axes_labels(['$n$','$|| e^{A}-\sum_{i=0}^{n}A^{i}/{i!}||_2$'])
print "\n"
print "Value of approximation for n=%s: \n%s" % (n, approx_exp)
print "\n"
print "Exact value: \n%s" % exp(A)
for i in range(10):
print i
# Shortest possible simulation of an MM1 Q (with warmup). Contributors: Geraint, Jason, Vince, Harald and Chris.
from random import expovariate
lmbda, mu, T, warmup, waits, sample, arrival_time, start_time, end_time = 1, 2, 5000, 1000, [], lambda x: expovariate(x), 0, 0, 0
while end_time < T:
arrival_time += sample(lmbda)
start_time = max(end_time, arrival_time)
end_time = start_time + sample(mu)
if arrival_time > warmup: waits.append(start_time - arrival_time)
print 'Mean wait: ' + str(sum(waits) / len(waits))
"""
Going to simulate 100 '1st two days of xmas'
"""
from __future__ import division
import random
partidges = []
turtle_doves= []
for xmas in range(100):
partidges.append(random.choice([0,1,1,1,2]))