コード例 #1
0
ファイル: samplex_orig.py プロジェクト: phdenzel/glass
def rwalk(id, nmodels, samplex, q, cmdq, vec, twiddle, eval, evec, seed):

    S = np.zeros(samplex.eqs.shape[0])
    S0 = np.zeros(samplex.eqs.shape[0])

    vec = vec.copy('A')
    eqs = samplex.eqs.copy('A')

    accepted = 0
    rejected = 0

    offs = ' ' * 39
    Log(offs + 'STARTING rwalk THREAD %i [this thread makes %i models]' %
        (id, nmodels),
        overwritable=True)

    eqs[:, 1:] = np.dot(samplex.eqs[:, 1:], evec)

    #csamplex.set_rwalk_seed(1 + id + samplex.random_seed)
    if seed is not None: csamplex.set_rwalk_seed(1 + seed)

    log_time = time.clock()

    offs = ' ' * 36
    state = ''
    for i in xrange(nmodels):

        accepted = 0
        rejected = 0

        done = False

        vec[:] = np.dot(evec.T, vec)
        accepted, rejected, t = csamplex.rwalk(samplex, eqs, vec, eval, S, S0,
                                               twiddle, accepted, rejected)
        vec[:] = np.dot(evec, vec)

        r = accepted / (accepted + rejected)

        if time.clock() - log_time > 3:
            Log(offs +
                '% 2s THREAD %3i  %i  %4.1f%% accepted  (%6i/%6i Acc/Rej)  twiddle %5.2f  time %5.3fs  %i left.'
                % (state, id, i, 100 * r, accepted, rejected, twiddle, t,
                   nmodels - i),
                overwritable=True)
            log_time = time.clock()

        #print ' '*36, '% 2s THREAD %3i  %i  %4.1f%% accepted  (%6i/%6i Acc/Rej)  twiddle %5.2f  time %5.3fs  %i left.' % (state, id, i, 100*r, accepted, rejected, twiddle, t, nmodels-i)
        assert np.all(vec >= 0), vec[vec < 0]
        #if numpy.any(vec < 0): sys.exit(0)

        samplex.project(vec)

        q.put([id, vec.copy('A')])
コード例 #2
0
ファイル: samplex.py プロジェクト: jpcoles/glass
def rwalk(id, nmodels, samplex, q, cmdq, vec,twiddle, eval,evec,seed):

    S   = np.zeros(samplex.eqs.shape[0])
    S0  = np.zeros(samplex.eqs.shape[0])

    vec  = vec.copy('A')
    eqs  = samplex.eqs.copy('A')

    accepted = 0
    rejected = 0

    offs = ' '*39
    Log( offs + 'STARTING rwalk THREAD %i [this thread makes %i models]' % (id,nmodels), overwritable=True)

    eqs[:,1:] = np.dot(samplex.eqs[:,1:], evec)

    #csamplex.set_rwalk_seed(1 + id + samplex.random_seed)
    if seed is not None: csamplex.set_rwalk_seed(1 + seed)

    log_time = time.clock()

    offs = ' '*36
    state = ''
    for i in xrange(nmodels):

        accepted = 0
        rejected = 0

        done = False

        vec[:] = np.dot(evec.T, vec)
        accepted,rejected,t = csamplex.rwalk(samplex, eqs, vec,eval,S,S0, twiddle, accepted,rejected)
        vec[:] = np.dot(evec, vec)

        r = accepted / (accepted + rejected)

        if time.clock() - log_time > 3:
            Log( offs + '% 2s THREAD %3i  %i  %4.1f%% accepted  (%6i/%6i Acc/Rej)  twiddle %5.2f  time %5.3fs  %i left.' % (state, id, i, 100*r, accepted, rejected, twiddle, t, nmodels-i), overwritable=True )
            log_time = time.clock()

        #print ' '*36, '% 2s THREAD %3i  %i  %4.1f%% accepted  (%6i/%6i Acc/Rej)  twiddle %5.2f  time %5.3fs  %i left.' % (state, id, i, 100*r, accepted, rejected, twiddle, t, nmodels-i)
        assert np.all(vec >= 0), vec[vec < 0]
        #if numpy.any(vec < 0): sys.exit(0)

        samplex.project(vec)

        q.put([id,vec.copy('A')])
コード例 #3
0
ファイル: samplex.py プロジェクト: RafiKueng/glass
def rwalk_burnin(id, nmodels, burnin_len, samplex, q, cmdq, ackq, vec, twiddle, eval,evec):

    S   = np.zeros(samplex.eqs.shape[0])
    S0  = np.zeros(samplex.eqs.shape[0])

    vec  = vec.copy('A')
    eval = eval.copy('A')
    evec = evec.copy('A')
    eqs  = samplex.eqs.copy('A')

    accepted = 0
    rejected = 0

    offs = ' '*39
    Log( offs + 'STARTING rwalk_burnin THREAD %i' % id, overwritable=True)

    eqs[:,1:] = np.dot(samplex.eqs[:,1:], evec)
    #vec[:] = np.dot(evec.T, vec)
    I = np.eye(evec.shape[0]).copy('F')

    csamplex.set_rwalk_seed(1 + id + samplex.random_seed)

    offs = ' '*36
    i = 0
    j=0
    while True:
        j+= 1


        done = False
        try:
            while not done:
                cmd = cmdq.get()
                if cmd[0] == 'CONT':
                    break
                elif cmd[0] == 'NEW DATA':
                    eval[:],evec[:],twiddle = cmd[1]
                    eqs[:,1:] = np.dot(samplex.eqs[:,1:], evec)
                elif cmd[0] == 'REQ TWIDDLE':
                    ackq.put(twiddle)
                elif cmd[0] == 'WAIT':
                    ackq.put('OK')
                elif cmd[0] == 'STOP':
                    done = True
                elif cmd[0] == 'RWALK':
                    done = True
                else:
                    print 'Unknown cmd:', cmd
        except QueueEmpty:
            pass

        if done:
            break

        vec[:] = np.dot(evec.T, vec)

        accepted = False
        while not accepted:
            Naccepted = 0
            Nrejected = 0

            Naccepted,Nrejected,t = csamplex.rwalk(samplex, eqs, vec,eval,S,S0, twiddle, Naccepted,Nrejected)

            r = Naccepted / (Naccepted + Nrejected)
            msg = 'THREAD %3i]  %i/%i  %4.1f%% accepted  (%6i/%6i Acc/Rej)  twiddle %5.2f  time %5.3fs' % (id, i, burnin_len, 100*r, Naccepted, Nrejected, twiddle, t)

            #-------------------------------------------------------------------
            # If the actual acceptance rate was OK then leave this loop,
            # otherwise change our step size twiddle factor to improve the rate.
            # Even if the accepance rate was OK, we adjust the twiddle but only
            # with a certain probability. This drives the acceptance rate to 
            # the specified one even if we are within the tolerance but doesn't
            # throw away the results if we are not so close. This allows for
            # a larger tolerance.
            #-------------------------------------------------------------------
            accepted =  np.abs(r - samplex.accept_rate) < samplex.accept_rate_tol

            state = 'B'
            if not accepted:
                twiddle *= 1 + ((r-samplex.accept_rate) / samplex.accept_rate / 2)
                twiddle = max(1e-14,twiddle)
                state = 'R' + state

            Log( offs + '% 2s %s' % (state, msg), overwritable=True )
            #print ' '*36, '% 2s %s' % (state, msg)

        vec[:] = np.dot(evec, vec)

        if random() < np.abs(r - samplex.accept_rate)/samplex.accept_rate_tol:
            twiddle *= 1 + ((r-samplex.accept_rate) / samplex.accept_rate / 2)
            twiddle = max(1e-14,twiddle)

        assert np.all(vec >= 0), vec[vec < 0]
        #if np.any(vec < 0): sys.exit(0)

        samplex.project(vec)

        i += 1
        q.put([id,vec.copy('A')])

    time_begin = time.clock()
    if cmd[0] == 'RWALK':
        rwalk(id, nmodels, samplex, q, cmdq, vec, twiddle, eval, evec)
    time_end = time.clock()

    cmd = cmdq.get()
    assert cmd[0] == 'STOP', cmd[0]
    ackq.put(['TIME', time_end-time_begin])
コード例 #4
0
ファイル: samplex.py プロジェクト: phdenzel/glass
def rwalk_burnin(id, nmodels, burnin_len, samplex, q, cmdq, ackq, vec, twiddle, eval,evec, seed):

    lclq = []

    S   = np.zeros(samplex.eqs.shape[0])
    S0  = np.zeros(samplex.eqs.shape[0])

    vec  = vec.copy('A')
    eval = eval.copy('A')
    evec = evec.copy('A')
    eqs  = samplex.eqs.copy('A')

    accepted = 0
    rejected = 0

    offs = ' '*39
    Log( offs + 'STARTING rwalk_burnin THREAD %i' % id, overwritable=True)

    eqs[:,1:] = np.dot(samplex.eqs[:,1:], evec)
    #vec[:] = np.dot(evec.T, vec)
    I = np.eye(evec.shape[0]).copy('F')

    #csamplex.set_rwalk_seed(1 + id + samplex.random_seed)
    csamplex.set_rwalk_seed(1 + seed)

    log_time = time.clock()

    #t0=0
    #t1=time.clock()

    offs = ' '*36
    i = 0
    j=0
    while True:
        j+= 1

        #t0=time.clock()

        put_immediate = False
        done = False
        try:
            while not done:
                #if 1:
                cmd = cmdq.get()
                if cmd[0] == 'CONT':
                    break
#                   if lclq:
#                       q.put([id,lclq])
#                       lclq = []
#                   else:
#                       put_immediate = True
                elif cmd[0] == 'NEW DATA':
                    eval[:],evec[:],twiddle = cmd[1]
                    eqs[:,1:] = np.dot(samplex.eqs[:,1:], evec)
                    lclq = []
                    i=0
                elif cmd[0] == 'REQ TWIDDLE':
                    ackq.put(twiddle)
                elif cmd[0] == 'WAIT':
                    ackq.put('OK')
                elif cmd[0] == 'STOP':
                    done = True
                elif cmd[0] == 'RWALK':
                    done = True
                else:
                    print 'Unknown cmd:', cmd
        except QueueEmpty:
            pass

        if done:
            break

        while len(lclq) < 10:
            vec[:] = np.dot(evec.T, vec)

            #t1=time.clock()


            accepted = False
            while not accepted:
                Naccepted = 0
                Nrejected = 0

                Naccepted,Nrejected,t = csamplex.rwalk(samplex, eqs, vec,eval,S,S0, twiddle, Naccepted,Nrejected)

                r = Naccepted / (Naccepted + Nrejected)

                #-------------------------------------------------------------------
                # If the actual acceptance rate was OK then leave this loop,
                # otherwise change our step size twiddle factor to improve the rate.
                # Even if the accepance rate was OK, we adjust the twiddle but only
                # with a certain probability. This drives the acceptance rate to 
                # the specified one even if we are within the tolerance but doesn't
                # throw away the results if we are not so close. This allows for
                # a larger tolerance.
                #-------------------------------------------------------------------
                accepted =  np.abs(r - samplex.accept_rate) < samplex.accept_rate_tol

                state = 'B'
                if not accepted:
                    twiddle *= 1 + ((r-samplex.accept_rate) / samplex.accept_rate / 2)
                    #twiddle *= (r/samplex.accept_rate)
                    twiddle = max(1e-14,twiddle)
                    state = 'R' + state

                if time.clock() - log_time > 3:
                    msg = 'THREAD %3i]  %i/%i  %4.1f%% accepted  (%6i/%6i Acc/Rej)  twiddle %5.2f  time %5.3fs backlog %i' % (id, i, burnin_len, 100*r, Naccepted, Nrejected, twiddle, t, len(lclq))
                    Log( offs + '% 2s %s' % (state, msg), overwritable=True )
                    log_time = time.clock()

                #print ' '*36, '% 2s %s' % (state, msg)

            #print 'thread %i, %f' % (id,t1-t0)

            vec[:] = np.dot(evec, vec)

            if random() < np.abs(r - samplex.accept_rate)/samplex.accept_rate_tol:
                twiddle *= 1 + ((r-samplex.accept_rate) / samplex.accept_rate / 2)
                #twiddle *= (r/samplex.accept_rate)
                twiddle = max(1e-14,twiddle)

            assert np.all(vec >= 0), vec[vec < 0]
            #if np.any(vec < 0): sys.exit(0)

            samplex.project(vec)

            i += 1
#           if put_immediate:
#               q.put([id,[vec.copy('A')]])
#           else:
            lclq.append(vec.copy('A'))

        q.put([id,lclq,'BURNIN'])
        lclq = []



    time_begin = time.clock()
    if cmd[0] == 'RWALK':
        rwalk(id, nmodels, samplex, q, cmdq, vec, twiddle, eval, evec, seed=None)
    time_end = time.clock()

    cmd = cmdq.get()
    assert cmd[0] == 'STOP', cmd[0]
    ackq.put(['TIME', time_end-time_begin])