Ejemplo n.º 1
0
def testMaxn():
    print '===== test maxn ====='
    print '\n>>>normal\n'
    mu = 10
    sigma = 3
    n = 5
    x = []
    y = []
    for i in range(100000):
        m = -sys.maxint
        for j in range(n):
            r = np.random.normal(mu, sigma)
            x.append(r)
            if r > m:
                m = r
        y.append(m)
    ddist0 = DDist.create(x)
    ddist1 = DDist.create(y)
    ddist2 = maxn(n, ddist0)
    print 'base', ddist0.mean, ddist0.std
    print 'sim', ddist1.mean, ddist1.std
    print 'ana', ddist2.mean, ddist2.std
    ddist1.plot('/tmp/test_maxn_normal1.pdf')
    ddist2.plot('/tmp/test_maxn_normal2.pdf')
    print '\n>>>pareto\n'
    a = 1.3
    x0 = 0
    x = []
    y = []
    for i in range(100000):
        m = -sys.maxint
        for j in range(n):
            r = np.random.pareto(a) + x0
            x.append(r)
            if r > m:
                m = r
        y.append(m)
    ddist0 = DDist.create(x, tail=('p', 0.1), tnh=25)
    ddist1 = DDist.create(y, tail=('p', 0.1), tnh=25)
    #ddist0 = DDist.create(x)
    #ddist1 = DDist.create(y)
    ddist2 = maxn(n, ddist0)
    print 'base', ddist0.mean, ddist0.std, 'length', len(ddist0.pmfy), len(
        ddist0.tpmfy)
    print 'sim', ddist1.mean, ddist1.std, 'length', len(ddist1.pmfy), len(
        ddist1.tpmfy)
    print 'ana', ddist2.mean, ddist2.std, 'length', len(ddist2.pmfy), len(
        ddist2.tpmfy)
    ddist1.plot('/tmp/test_maxn_pareto1.pdf')
    ddist2.plot('/tmp/test_maxn_pareto2.pdf')
    print '===== end =====\n'
Ejemplo n.º 2
0
def testMaxn():
    print '===== test maxn ====='
    print '\n>>>normal\n'
    mu = 10
    sigma = 3
    n = 5
    x = []
    y = []
    for i in range(100000):
        m = -sys.maxint
        for j in range(n):
            r = np.random.normal(mu, sigma)
            x.append(r)
            if r > m:
                m = r
        y.append(m)
    ddist0 = DDist.create(x)
    ddist1 = DDist.create(y)
    ddist2 = maxn(n, ddist0)
    print 'base', ddist0.mean, ddist0.std
    print 'sim', ddist1.mean, ddist1.std
    print 'ana', ddist2.mean, ddist2.std
    ddist1.plot('/tmp/test_maxn_normal1.pdf')
    ddist2.plot('/tmp/test_maxn_normal2.pdf')
    print '\n>>>pareto\n'
    a = 1.3
    x0 = 0
    x = []
    y = []
    for i in range(100000):
        m = -sys.maxint
        for j in range(n):
            r = np.random.pareto(a) + x0
            x.append(r)
            if r > m:
                m = r
        y.append(m)
    ddist0 = DDist.create(x, tail=('p', 0.1), tnh=25)
    ddist1 = DDist.create(y, tail=('p', 0.1), tnh=25)
    #ddist0 = DDist.create(x)
    #ddist1 = DDist.create(y)
    ddist2 = maxn(n, ddist0)
    print 'base', ddist0.mean, ddist0.std, 'length', len(ddist0.pmfy), len(ddist0.tpmfy)
    print 'sim', ddist1.mean, ddist1.std, 'length', len(ddist1.pmfy), len(ddist1.tpmfy)
    print 'ana', ddist2.mean, ddist2.std, 'length', len(ddist2.pmfy), len(ddist2.tpmfy)
    ddist1.plot('/tmp/test_maxn_pareto1.pdf')
    ddist2.plot('/tmp/test_maxn_pareto2.pdf')
    print '===== end =====\n'
Ejemplo n.º 3
0
def testCond():
    print '===== test cond ====='
    print '\n>>> normal\n'
    p = 0.8
    mu1 = 10
    sigma1 = 3
    mu2 = 20
    sigma2 = 1
    x1 = []
    x2 = []
    y = []
    for i in range(100000):
        r = np.random.random()
        if r < p:
            s = np.random.normal(mu1, sigma1)
            x1.append(s)
            y.append(s)
        else:
            s = np.random.normal(mu2, sigma2)
            x2.append(s)
            y.append(s)
    ddist0 = DDist.create(x1)
    ddist1 = DDist.create(x2)
    ddist2 = DDist.create(y)
    ddist3 = cond(p, ddist0, ddist1)
    print 'base1', ddist0.mean, ddist0.std
    print 'base2', ddist1.mean, ddist1.std
    print 'sim', ddist2.mean, ddist2.std
    print 'ana', ddist3.mean, ddist3.std
    ddist2.plot('/tmp/test_cond_norm1.pdf')
    ddist3.plot('/tmp/test_cond_norm2.pdf')
    print '\n>>> pareto\n'
    p = 0.5
    a1 = 1.1
    a2 = 1.2
    x1 = []
    x2 = []
    y = []
    for i in range(100000):
        r = np.random.random()
        if r < p:
            s = np.random.pareto(a1)
            x1.append(s)
            y.append(s)
        else:
            s = np.random.pareto(a2) + 10
            x2.append(s)
            y.append(s)
    ddist0 = DDist.create(x1, tail=('p', 0.1), tnh=10)
    ddist1 = DDist.create(x2, tail=('p', 0.1), tnh=10)
    ddist2 = DDist.create(y)
    ddist3 = cond(p, ddist0, ddist1)
    print 'base1', ddist0.mean, ddist0.std
    print 'base2', ddist1.mean, ddist1.std
    print 'sim', ddist2.mean, ddist2.std
    print 'ana', ddist3.mean, ddist3.std
    ddist2.plot('/tmp/test_cond_pareto1.pdf')
    ddist3.plot('/tmp/test_cond_pareto2.pdf')
    print '===== end =====\n'
Ejemplo n.º 4
0
def testCond():
    print '===== test cond ====='
    print '\n>>> normal\n'
    p = 0.8
    mu1 = 10
    sigma1 = 3
    mu2 = 20
    sigma2 = 1
    x1 = []
    x2 = []
    y = []
    for i in range(100000):
        r = np.random.random()
        if r < p:
            s = np.random.normal(mu1, sigma1)
            x1.append(s)
            y.append(s)
        else:
            s = np.random.normal(mu2, sigma2)
            x2.append(s)
            y.append(s)
    ddist0 = DDist.create(x1)
    ddist1 = DDist.create(x2)
    ddist2 = DDist.create(y)
    ddist3 = cond(p, ddist0, ddist1)
    print 'base1', ddist0.mean, ddist0.std
    print 'base2', ddist1.mean, ddist1.std
    print 'sim', ddist2.mean, ddist2.std
    print 'ana', ddist3.mean, ddist3.std
    ddist2.plot('/tmp/test_cond_norm1.pdf')
    ddist3.plot('/tmp/test_cond_norm2.pdf')
    print '\n>>> pareto\n'
    p = 0.5
    a1 = 1.1
    a2 = 1.2
    x1 = []
    x2 = []
    y = []
    for i in range(100000):
        r = np.random.random()
        if r < p:
            s = np.random.pareto(a1)
            x1.append(s)
            y.append(s)
        else:
            s = np.random.pareto(a2) + 10
            x2.append(s)
            y.append(s)
    ddist0 = DDist.create(x1, tail=('p', 0.1), tnh=10)
    ddist1 = DDist.create(x2, tail=('p', 0.1), tnh=10)
    ddist2 = DDist.create(y)
    ddist3 = cond(p, ddist0, ddist1)
    print 'base1', ddist0.mean, ddist0.std
    print 'base2', ddist1.mean, ddist1.std
    print 'sim', ddist2.mean, ddist2.std
    print 'ana', ddist3.mean, ddist3.std
    ddist2.plot('/tmp/test_cond_pareto1.pdf')
    ddist3.plot('/tmp/test_cond_pareto2.pdf')
    print '===== end =====\n'
Ejemplo n.º 5
0
def testoodelay():
    print '===== test oodelay =====\n'
    intvl = 30

    def genexpointvl():
        return np.random.exponential(intvl)

    def genfixedintvl():
        return intvl

    lambd = 100
    a = 1.8
    m = 100
    lb = 50
    tb = 150
    ub = 1000

    def customlatency():
        r = np.random.random()
        if r < 0.2:
            return 30
        elif r < 0.4:
            return 60
        elif r < 0.6:
            return 90
        elif r < 0.8:
            return 120
        else:
            return 180

    def expolatency():
        return np.random.exponential(lambd)

    def paretolatency():
        return np.random.pareto(a) + m

    def uniformlatency():
        r = np.random.random()
        if r < 0.8:
            rr = np.random.random()
            return lb + rr * (tb - lb)
        else:
            rr = np.random.random()
            return tb + rr * (ub - tb)

    intvlkeys = {'fixed': genfixedintvl, 'poisson': genexpointvl}
    intvlopts = {'fixed': 'fixed.interval', 'poisson': 'poisson.lambda'}
    latencykeys = {
        'custom': customlatency,
        'expo': expolatency,
        'pareto': paretolatency,
        'uniform': uniformlatency,
    }
    for ik in intvlkeys:
        for lk in latencykeys:
            print '\n>>> %s, %s\n' % (ik, lk)
            x, y = runoodelay(intvlkeys[ik], latencykeys[lk])
            if lk == 'custom':
                ddist0 = DDist.create(x, h=30, tail=('b', 120), tnh=2)
            elif lk == 'expo':
                ddist0 = DDist.create(x, h=0.5)
            elif lk == 'uniform':
                ddist0 = DDist.create(x, h=0.5, tail=('b', 150), tnh=10)
            else:
                ddist0 = DDist.create(x, h=0.5, tail=('p', 0.1), tnh=10)
            if lk == 'custom':
                ddist1 = DDist.create(y, h=30)
            else:
                ddist1 = DDist.create(y, h=0.5)
            if ik == 'fixed':
                ddist2 = oodelay(ddist0, {
                    'arrival.process': 'fixed',
                    'fixed.interval': intvl
                })
            else:
                ddist2 = oodelay(ddist0, {
                    'arrival.process': 'poisson',
                    'poisson.lambda': 1.0 / intvl
                })
            print 'sim', ddist1.mean, ddist1.std
            print 'ana', ddist2.mean, ddist2.std
            if lk == 'custom':
                print 'sim pmf', ddist1.getPmfxy()
                print 'ana pmf', ddist2.getPmfxy()
    print '===== end =====\n'
Ejemplo n.º 6
0
def testQuorum():
    print '===== test quorum ====='
    lambd = 50
    a = 1.8
    n = 5
    f = 2
    x = {'gen': [], 'exp': [], 'pareto': []}
    yc = {'gen': [], 'exp': [], 'pareto': []}
    yr = {'gen': [], 'exp': [], 'pareto': []}
    y = {'gen': [], 'exp': [], 'pareto': []}
    dists = ['gen', 'exp', 'pareto']
    leader = 0

    def gen():
        r = np.random.random()
        if r < 0.2:
            return 1
        elif r < 0.4:
            return 2
        elif r < 0.6:
            return 3
        elif r < 0.8:
            return 4
        else:
            return 8

    for i in range(100000):
        first = {}
        second = {}
        latencies = {'gen': [], 'exp': [], 'pareto': []}
        learner = np.random.randint(n)
        for acceptor in range(n):
            if leader == acceptor:
                for k in dists:
                    first[k] = 0
            else:
                first['gen'] = gen()
                first['exp'] = np.random.exponential(lambd)
                first['pareto'] = np.random.pareto(a)
                for k in dists:
                    x[k].append(first[k])
            if acceptor == learner:
                for k in dists:
                    second[k] = 0
            else:
                second['gen'] = gen()
                second['exp'] = np.random.exponential(lambd)
                second['pareto'] = np.random.pareto(a)
                for k in dists:
                    x[k].append(second[k])
            for k in dists:
                latencies[k].append(first[k] + second[k])
        for k in dists:
            l = sorted(latencies[k])
            y[k].append(l[n - f - 1])
            if leader == learner:
                assert l[0] == 0
                yc[k].append(l[n - f - 1])
            else:
                yr[k].append(l[n - f - 1])
    for k in dists:
        print '\n>>> %s\n' % k
        if k == 'gen':
            ddist0 = DDist.create(x[k], h=1, tail=('b', 4), tnh=4)
        elif k == 'exp':
            ddist0 = DDist.create(x[k], h=0.5)
        else:
            ddist0 = DDist.create(x[k], h=0.5, tail=('p', 0.1), tnh=20)
            print 'created ddist0'
        if k == 'gen':
            ddist1 = DDist.create(y[k], h=1, tail=('b', 8), tnh=4)
            ddistc1 = DDist.create(yc[k], h=1, tail=('b', 8), tnh=4)
            ddistr1 = DDist.create(yr[k], h=1, tail=('b', 8), tnh=4)
            print 'base pmf', ddist1.getPmfxy()
            print 'base c pmf', ddistc1.getPmfxy()
            print 'base r pmf', ddistr1.getPmfxy()
        else:
            ddist1 = DDist.create(y[k], h=0.05)
            ddistc1 = DDist.create(yc[k], h=0.05)
            ddistr1 = DDist.create(yr[k], h=0.05)
        ddist2 = quorum(n, f, ddist0)
        print 'computed quorum'
        rtrip = ddist0 + ddist0
        ddistc2 = _quorumC(n - 1, n - f - 1, rtrip)
        ddistr2 = _quorumR(n, n - f, ddist0, rtrip)
        if k == 'gen':
            print 'q pmf', ddist2.getPmfxy()
            print 'q c pmf', ddistc2.getPmfxy()
            print 'q r pmf', ddistr2.getPmfxy()
        print 'latency', ddist0.mean, ddist0.std
        print 'quorum sim', ddist1.mean, ddist1.std
        print 'quorum c sim', ddistc1.mean, ddistc1.std
        print 'quorum r sim', ddistr1.mean, ddistr1.std
        print 'quorum ana', ddist2.mean, ddist2.std
        print 'quorum c ana', ddistc2.mean, ddistc2.std
        print 'quorum r ana', ddistr2.mean, ddistr2.std
        ddist1.plot('/tmp/test_quorum_%s1.pdf' % k)
        ddistc1.plot('/tmp/test_quorumc_%s1.pdf' % k)
        ddistr1.plot('/tmp/test_quorumr_%s1.pdf' % k)
        ddist2.plot('/tmp/test_quorum_%s2.pdf' % k)
        ddistc2.plot('/tmp/test_quorumc_%s2.pdf' % k)
        ddistr2.plot('/tmp/test_quorumr_%s2.pdf' % k)
    print '===== end =====\n'
Ejemplo n.º 7
0
def testoodelay():
    print '===== test oodelay =====\n'
    intvl = 30
    def genexpointvl():
        return np.random.exponential(intvl)

    def genfixedintvl():
        return intvl

    lambd = 100
    a = 1.8
    m = 100
    lb = 50
    tb = 150
    ub = 1000
    def customlatency():
        r = np.random.random()
        if r < 0.2:
            return 30
        elif r < 0.4:
            return 60
        elif r < 0.6:
            return 90
        elif r < 0.8:
            return 120
        else:
            return 180

    def expolatency():
        return np.random.exponential(lambd)

    def paretolatency():
        return np.random.pareto(a) + m

    def uniformlatency():
        r = np.random.random()
        if r < 0.8:
            rr = np.random.random()
            return lb + rr * (tb - lb)
        else:
            rr = np.random.random()
            return tb + rr * (ub - tb)

    intvlkeys = {'fixed' : genfixedintvl,
                 'poisson' : genexpointvl}
    intvlopts = {'fixed' : 'fixed.interval',
                 'poisson' : 'poisson.lambda'}
    latencykeys = {'custom' : customlatency,
                   'expo' : expolatency,
                   'pareto' : paretolatency,
                   'uniform' : uniformlatency,
                  }
    for ik in intvlkeys:
        for lk in latencykeys:
            print '\n>>> %s, %s\n'%(ik, lk)
            x, y = runoodelay(intvlkeys[ik], latencykeys[lk])
            if lk == 'custom':
                ddist0 = DDist.create(x, h=30, tail=('b', 120), tnh=2)
            elif lk == 'expo':
                ddist0 = DDist.create(x, h=0.5)
            elif lk == 'uniform':
                ddist0 = DDist.create(x, h=0.5, tail=('b', 150), tnh=10)
            else:
                ddist0 = DDist.create(x, h=0.5, tail=('p', 0.1), tnh=10)
            if lk == 'custom':
                ddist1 = DDist.create(y, h=30)
            else:
                ddist1 = DDist.create(y, h=0.5)
            if ik == 'fixed':
                ddist2 = oodelay(ddist0, {'arrival.process' : 'fixed',
                                          'fixed.interval' : intvl})
            else:
                ddist2 = oodelay(ddist0, {'arrival.process' : 'poisson',
                                          'poisson.lambda' : 1.0 / intvl})
            print 'sim', ddist1.mean, ddist1.std
            print 'ana', ddist2.mean, ddist2.std
            if lk == 'custom':
                print 'sim pmf', ddist1.getPmfxy()
                print 'ana pmf', ddist2.getPmfxy()
    print '===== end =====\n'
Ejemplo n.º 8
0
def testQuorum():
    print '===== test quorum ====='
    lambd = 50
    a = 1.8
    n = 5
    f = 2
    x = {'gen' : [], 'exp':[], 'pareto':[]}
    yc = {'gen' : [], 'exp':[], 'pareto':[]}
    yr = {'gen' : [], 'exp':[], 'pareto':[]}
    y = {'gen' : [], 'exp':[], 'pareto':[]}
    dists = ['gen', 'exp', 'pareto']
    leader = 0
    def gen():
        r = np.random.random()
        if r < 0.2:
            return 1
        elif r < 0.4:
            return 2
        elif r < 0.6:
            return 3
        elif r < 0.8:
            return 4
        else:
            return 8

    for i in range(100000):
        first = {}
        second = {}
        latencies = {'gen':[], 'exp':[], 'pareto':[]}
        learner = np.random.randint(n)
        for acceptor in range(n):
            if leader == acceptor:
                for k in dists:
                    first[k] = 0
            else:
                first['gen'] = gen()
                first['exp'] = np.random.exponential(lambd)
                first['pareto'] = np.random.pareto(a)
                for k in dists:
                    x[k].append(first[k])
            if acceptor == learner:
                for k in dists:
                    second[k] = 0
            else:
                second['gen'] = gen()
                second['exp'] = np.random.exponential(lambd)
                second['pareto'] = np.random.pareto(a)
                for k in dists:
                    x[k].append(second[k])
            for k in dists:
                latencies[k].append(first[k] + second[k])
        for k in dists:
            l = sorted(latencies[k])
            y[k].append(l[n - f - 1])
            if leader == learner:
                assert l[0] == 0
                yc[k].append(l[n - f - 1])
            else:
                yr[k].append(l[n - f - 1])
    for k in dists:
        print '\n>>> %s\n'%k
        if k == 'gen':
            ddist0 = DDist.create(x[k], h=1, tail=('b', 4), tnh=4)
        elif k == 'exp':
            ddist0 = DDist.create(x[k], h=0.5)
        else:
            ddist0 = DDist.create(x[k], h=0.5, tail=('p', 0.1), tnh=20)
            print 'created ddist0'
        if k == 'gen':
            ddist1 = DDist.create(y[k], h=1, tail=('b', 8), tnh=4)
            ddistc1 = DDist.create(yc[k], h=1, tail=('b', 8), tnh=4)
            ddistr1 = DDist.create(yr[k], h=1, tail=('b', 8), tnh=4)
            print 'base pmf', ddist1.getPmfxy()
            print 'base c pmf', ddistc1.getPmfxy()
            print 'base r pmf', ddistr1.getPmfxy()
        else:
            ddist1 = DDist.create(y[k], h=0.05)
            ddistc1 = DDist.create(yc[k], h=0.05)
            ddistr1 = DDist.create(yr[k], h=0.05)
        ddist2 = quorum(n, f, ddist0)
        print 'computed quorum'
        rtrip = ddist0 + ddist0
        ddistc2 = _quorumC(n - 1, n - f - 1, rtrip)
        ddistr2 = _quorumR(n, n - f, ddist0, rtrip)
        if k == 'gen':
            print 'q pmf', ddist2.getPmfxy()
            print 'q c pmf', ddistc2.getPmfxy()
            print 'q r pmf', ddistr2.getPmfxy()
        print 'latency', ddist0.mean, ddist0.std
        print 'quorum sim', ddist1.mean, ddist1.std
        print 'quorum c sim', ddistc1.mean, ddistc1.std
        print 'quorum r sim', ddistr1.mean, ddistr1.std
        print 'quorum ana', ddist2.mean, ddist2.std
        print 'quorum c ana', ddistc2.mean, ddistc2.std
        print 'quorum r ana', ddistr2.mean, ddistr2.std
        ddist1.plot('/tmp/test_quorum_%s1.pdf'%k)
        ddistc1.plot('/tmp/test_quorumc_%s1.pdf'%k)
        ddistr1.plot('/tmp/test_quorumr_%s1.pdf'%k)
        ddist2.plot('/tmp/test_quorum_%s2.pdf'%k)
        ddistc2.plot('/tmp/test_quorumc_%s2.pdf'%k)
        ddistr2.plot('/tmp/test_quorumr_%s2.pdf'%k)
    print '===== end =====\n'