Created
May 23, 2019 15:42
-
-
Save xiezhenye/4631c6d2e6f344fb63f913a6dc506f10 to your computer and use it in GitHub Desktop.
generate sample data, draw baseline
This file contains hidden or 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
#!/usr/bin/python | |
import json | |
import random | |
import statistics | |
import matplotlib.pyplot as plot | |
import numpy | |
import sys | |
def g(): | |
#return random.expovariate(0.02) | |
return sum([random.expovariate(0.02) for x in xrange(3)])/3 | |
#return numpy.random.poisson(100) | |
data=[[g() for x in xrange(30)] for x in xrange(120)] | |
#j=json.loads(open('sample.txt').read()) | |
#data=[[ d['value'] for d in j['data'][k] ] for k in j['data']] | |
def b1(d,a,b): | |
m1=statistics.mean(d) | |
s1=statistics.stdev(d) | |
u=m1+a*s1 | |
l=m1-a*s1 | |
d2=filter(lambda x:x>l and x < u, d) | |
m2=statistics.mean(d2) | |
s2=statistics.stdev(d2) | |
return m2, m2+b*s2, m2-b*s2 | |
def b2(d,a,b): | |
d2=sorted(d)[a:-a] | |
m=statistics.mean(d2) | |
s=statistics.stdev(d2) | |
return m, m+b*s, m-b*s | |
def b3(d, n): | |
d2=sorted(d) | |
return d2[len(d2)/2], d2[n], d2[-n] | |
def conv(d, n): | |
return [ statistics.mean(d[i:i+n]) for i in xrange(len(d)-n+1) ] | |
c=10 | |
dd2=map(lambda d: b1(d, 1, 3), data) | |
dd3=map(lambda d: b1(d, 3, 3), data) | |
dd1=map(lambda d: b2(d, 3, 3), data) | |
dd4=map(lambda d: b3(d, 2), data) | |
dd5=map(lambda d: b1(d, 3, 3), data) | |
dd = dd3 | |
def col(d, i): | |
return map(lambda t:t[i], d) | |
def histogram(d): | |
m={} | |
for n in d: | |
n = int(n) | |
v=m.get(n, 0) | |
m[n]=v+1 | |
ks = sorted(m.keys()) | |
vs = [ m[k] for k in ks ] | |
return ks, vs | |
#h = histogram(sum(data,[])) | |
#plot.plot(h[0], h[1]) | |
#plot.show() | |
#sys.exit() | |
#plot.ion() | |
x=xrange(len(dd)-c+1) | |
plot.plot(x, conv(col(dd, 0), c), 'k', conv(col(dd, 1), c), 'r', conv(col(dd, 2), c), 'b') | |
x=xrange(len(dd)) | |
#plot.plot(x, col(dd, 0), 'k', col(dd, 1), 'r', col(dd, 2), 'b') | |
plot.plot(x, col(data, 11), 'g') | |
#plot.plot(x, col(data, 0), 'g') | |
plot.plot(x, col(dd, 0), 'k', col(dd, 1), 'r', col(dd, 2), 'b') | |
plot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment