Created
February 3, 2014 15:09
-
-
Save dhotson/8785532 to your computer and use it in GitHub Desktop.
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 __future__ import division | |
from math import * | |
from random import random, gauss | |
def p_smooth(c, n): | |
return (c + 1) / (n + 2) | |
def binomial(c, n): | |
p = p_smooth(c, n) | |
return 2*sqrt((1/n if n > 0 else 1) * p * (1-p)) | |
def wilson(c, n): | |
z = 1.96 | |
z2 = 1.96 ** 2 | |
phat = c / n | |
plus_minus = z * sqrt((1/n) * phat * (1-phat) + z2/(4 * (n**2))) | |
return [ | |
1 / (1 + (z2/n)) * (phat + 1/(2*n) * z2 - plus_minus), | |
1 / (1 + (z2/n)) * (phat + 1/(2*n) * z2 + plus_minus)] | |
c = 0 | |
n = 0 | |
alpha = 0.75 # Exp smoothing factor | |
last_score = 0 | |
v_smooth = 0 | |
for i in range(0,100): | |
r = gauss(0.5 + sin(i/10)/2, 0.01) | |
if (random() < r): | |
v = 1 | |
print "+ ", | |
else: | |
v = 0 | |
print "- ", | |
# v_smooth = v_smooth + alpha * (v - v_smooth) | |
c += v | |
n += 1 | |
# score = sum([p_smooth(c, n), -binomial(c, n)]) | |
score = wilson(c, n)[0] | |
print "%2.1f %4f %4f -- %3d %3d" % ( | |
r, | |
c/n, | |
wilson(c, n)[0], | |
int(100*round(score,2)), | |
int(100*round(score-last_score,2)) | |
) | |
last_score = score |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment