def dreamzPTSwap(func, priorFunc, X, XP, XT, args=None): TU = unique(XT) nChains, ndim = shape(X) for i in range(len(TU) - 1, 0, -1): Tlow = TU[i - 1] Thigh = TU[i] betaLow = 1.0 / Tlow betaHigh = 1.0 / Thigh # pick chains with matching temperatures to swap idx = where(XT == Tlow)[0] j = sampleWithoutReplacement(idx, 1)[0] Plow = XP[j] idx = where(XT == Thigh)[0] i = sampleWithoutReplacement(idx, 1)[0] Phigh = XP[i] alpha = (Phigh - Plow) * (betaLow - betaHigh) if log(uniform()) < alpha: xx = copy(X[j]) xxp = copy(XP[j]) X[j] = X[i] XP[j] = XP[i] X[i] = xx XP[i] = xxp return X, XP
def DEStep(func, priorFunc, \ X, XP, \ ncr=0, \ gamma=1.0, eps=1.e-6, e=0.05, \ fitVector=None, args=None): nChains, ndim = shape(X) nHist = len(XP) XPNEW = copy(XP) XNEW = copy(X) if all(fitVector == None): parIndex = arange(ndim) else: parIndex = copy(fitVector) success = [] idx = arange(nHist) mask = ones(nHist, dtype=bool) for i in range(nChains): ''' Sample three indices at random. ''' mask[i] = False # ensure current chain is not included in selection r = sampleWithoutReplacement(idx[mask], 3) # select random mask[i] = True # restore current chain for future iterations Z0 = X[r[0]] Z1 = X[r[1]] Z2 = X[r[2]] ''' Create an evolution vector from those samples. ''' delta = Z2 - Z1 ''' Differential evolution step: make a trial, new solution. ''' x = copy(X[i]) pp = copy(parIndex) if ncr > 0 and ncr < ndim: # deal with limited number of crossovers pp = sampleWithoutReplacement(parIndex, ncr) x[pp] = Z0[pp] + (1.0+normal(size=len(pp))*e)*gamma*delta[pp] + \ normal(size=len(pp))*eps pf = priorFunc(x) if ~isneginf(pf): if args == None: xp = func(x) else: xp = func(x, *args) else: xp = 0. # doesn't matter ''' Acceptance ratio setp. ''' alpha = xp + pf - XP[i] dice = log(uniform()) if dice < alpha: XNEW[i] = x XPNEW[i] = xp + pf success += [1] else: success += [0] return XNEW, XPNEW, success
def dreamzPTStep(func, priorFunc, \ X, XP, XT, Z, ZP, ZT, gamma=1.0, eps=1.e-6, e=0.05): nChains, ndim = shape(X) nHist = len(ZP) #idx = arange(nHist) for i in range(nChains): ''' Make a list of Z index values at the same temperature as X[i]. ''' idx = arange(nHist)[ZT==XT[i]] ''' Sample two indices at random. ''' r1, r2 = sampleWithoutReplacement(idx, 2) # select random ''' Create an evolution vector from those samples. ''' delta = Z[r2] - Z[r1] ''' Differential evolution step: make a trial, new solution. ''' x = X[i] + (1.0+normal()*e)*gamma*delta + normal()*eps xp = func(x) ''' Acceptance ratio setp. Notice the inclusion of temperature. ''' pfi = priorFunc(X[i]) pf = priorFunc(x) alpha = exp((xp - XP[i])/XT[i] \ + pf - pfi) if uniform() < alpha: X[i] = x XP[i] = xp return X, XP
def takeAZStep(func, priorFunc, \ X, XP, Z, ZP, \ ncr, navg, \ gamma, eps, e, \ fitVector, args, plow, phigh, nHist, \ parIndex, i): ''' Sample two indices at random. ''' #r = sampleWithoutReplacement(idx, 2*navg) # select random r = sampleIndexWithoutReplacement(nHist, 2 * navg) Z1 = sum(Z[r[0:navg], :], axis=0) Z2 = sum(Z[r[navg:], :], axis=0) ''' Create an evolution vector from those samples. ''' delta = Z2 - Z1 ''' Differential evolution step: make a trial, new solution. ''' x = copy(X[i]) pp = copy(parIndex) if ncr > 0 and ncr < ndim: # deal with limited number of crossovers pp = sampleWithoutReplacement(parIndex, ncr) x[pp] += (1.0+normal(size=len(pp))*e)*gamma*delta[pp] + \ normal(size=len(pp))*eps ''' Try reflecting ''' if plow != None and phigh != None: idx = (x > phigh) x[idx] = 2 * phigh[idx] - x[idx] idx = (x < plow) x[idx] = 2 * plow[idx] - x[idx] pf = priorFunc(x) if ~isneginf(pf): if args == None: xp = func(x) else: xp = func(x, *args) else: xp = 0. # doesn't matter ''' Acceptance ratio setp. ''' alpha = xp + pf - XP[i] dice = log(uniform()) if dice < alpha: X[i] = x XP[i] = xp + pf psuccess = 1 else: psuccess = 0 pass return psuccess
def dreamzPTSwap(func, priorFunc, X, XP, XT, args=None): TU = unique(XT) ntemps = len(TU) nChains, ndim = shape(X) #XNEW = copy(X) #XPNEW = copy(XP) for i in range(len(TU)-1,0,-1): Tlow = TU[i-1] Thigh = TU[i] betaLow = 1.0 / Tlow betaHigh = 1.0 / Thigh #tfactor = (Tlow - Thigh)/(Tlow*Thigh) # pick chains with matching temperatures to swap idx = where(XT == Tlow)[0] j = sampleWithoutReplacement(idx, 1)[0] Plow = XP[j] idx = where(XT == Thigh)[0] i = sampleWithoutReplacement(idx, 1)[0] Phigh = XP[i] pfi = priorFunc(X[i]) pfj = priorFunc(X[j]) alpha = (Phigh - Plow)*(betaLow - betaHigh) # how to use priors? if log(uniform()) < alpha: xx = copy(X[j]) xxp = copy(XP[j]) X[j] = X[i] XP[j] = XP[i] X[i] = xx XP[i] = xxp ''' XNEW[i] = X[j] XNEW[j] = X[i] XPNEW[i] = XP[j] XPNEW[j] = XP[i] ''' return X, XP
def evolve(i): ''' Make a list of Z index values at the same temperature as X[i]. ''' thisTemp = XT[i] ''' Sample indices at random. Take into account averaging. ''' r = sampleIndexWithoutReplacement(nn, 2 * navg) * nChains + i Z1 = sum(Z[r[0:navg], :], axis=0) Z2 = sum(Z[r[navg:], :], axis=0) ''' Create an evolution vector from those samples. ''' delta = Z2 - Z1 ''' Differential evolution step: make a trial, new solution. ''' x = copy(X[i]) pp = copy(parIndex) if ncr > 0 and ncr < ndim: # deal with limited number of crossovers pp = sampleWithoutReplacement(parIndex, ncr) x[pp] += (1.0 + normal(size=len(pp)) * e) * gamma * delta[pp] + normal( size=len(pp)) * eps ''' Try reflecting ''' if plow != None and phigh != None: idx = (x > phigh) x[idx] = 2 * phigh[idx] - x[idx] idx = (x < plow) x[idx] = 2 * plow[idx] - x[idx] pf = priorFunc(x) if ~isneginf(pf): if args == None: xp = func(x) else: xp = func(x, *args) else: xp = 0. # doesn't matter xp = xp + pf alpha = (xp - XP[i]) / XT[i] dice = log(uniform()) successi = dice < alpha if successi: X[i] = x XP[i] = xp success[i] = int(successi) return
def dreamzStep(func, X, XP, Z, ZP, gamma=0.1, eps=1.e-6, e=0.05): nChains, ndim = shape(X) nHist = len(ZP) idx = arange(nHist) for i in range(nChains): r1, r2 = sampleWithoutReplacement(idx, 2) delta = Z[r2] - Z[r1] x = X[i] + (1.0 + normal() * e) * gamma * delta + normal() * eps xp = func(x) alpha = exp(xp - XP[i]) if uniform() < alpha: X[i] = x XP[i] = xp return X, XP
def dreamPTSwap(func, X, XP, XT): nChains, ndim = shape(X) XNEW = copy(X) XPNEW = copy(XP) i = sampleWithoutReplacement(range(1, nChains), 1)[0] j = i - 1 betaLow = 1.0 / XT[j] betaHigh = 1.0 / XT[i] alpha = exp((XP[i] - XP[j]) * (betaHigh - betaLow)) if uniform() < alpha: XNEW[i] = X[j] XNEW[j] = X[i] XPNEW[i] = XP[j] XPNEW[j] = XP[i] return XNEW, XPNEW
def dreamSwap(func, priorFunc, \ X, XP, \ eps=1.e-6, fitVector=None, args=None): nChains, ndim = shape(X) nHist = len(XP) XPNEW = copy(XP) XNEW = copy(X) if fitVector == None: parIndex = arange(ndim) else: parIndex = copy(fitVector) success = [] idx = arange(nHist) mask = ones(nHist, dtype=bool) for i in range(nChains): ''' Sample another index at random. ''' mask[i] = False # ensure current chain is not included in selection r = sampleWithoutReplacement(idx[mask], 1) # select random mask[i] = True # restore current chain for future iterations Z1 = X[r[0],:] x = copy(X[i]) pp = copy(parIndex) x[pp] = Z1[pp] + normal(size=len(pp))*eps pf = priorFunc(x) if ~isneginf(pf): if args == None: xp = func(x) else: xp = func(x, *args) else: xp = 0. # doesn't matter ''' Acceptance ratio setp. ''' alpha = xp + pf - XP[i] dice = log(uniform()) if dice < alpha: XNEW[i] = x XPNEW[i] = xp + pf return XNEW, XPNEW
def dreamzPTStep(func, priorFunc, \ X, XP, XT, Z, ZP, ZT, \ ncr=0, navg=1, \ eps=1.e-6, e=0.05, \ fitVector=None, args=None, plow=None, phigh=None, thin=10): nChains, ndim = shape(X) nHist = len(ZP) if all(fitVector == None): parIndex = arange(ndim) else: parIndex = copy(fitVector) #idx = arange(nHist) success = [] idx = arange(nHist) #mask = zeros(nHist, dtype=bool) nn = nHist / nChains if any(fitVector != None): nfit = len(fitVector) else: nfit = ndim for j in range(thin): for i in range(nChains): nav = randint(1, navg + 1) if ncr > 0: #nc = np.random.randint(1,ncr+1) nc = ncrDraw(ncr, nfit) else: nc = nfit gamma = 2.38 / sqrt(2.0 * nc * nav) if uniform() < 0.1: # 10% of the dreamz steps, hop between modes gamma = 1.0 ''' Make a list of Z index values at the same temperature as X[i]. ''' thisTemp = XT[i] ''' Sample indices at random. Take into account averaging. ''' r = sampleIndexWithoutReplacement(nn, 2 * nav) * nChains + i Z1 = sum(Z[r[0:nav], :], axis=0) Z2 = sum(Z[r[nav:], :], axis=0) ''' Create an evolution vector from those samples. ''' delta = Z2 - Z1 ''' Differential evolution step: make a trial, new solution. ''' x = copy(X[i]) pp = copy(parIndex) if nc > 0 and nc < ndim: # deal with limited number of crossovers pp = sampleWithoutReplacement(parIndex, nc) x[pp] += (1.0 + normal(size=len(pp)) * e) * gamma * delta[pp] + normal(size=len(pp)) * eps ''' Try reflecting ''' if plow != None and phigh != None: idx = (x > phigh) x[idx] = 2 * phigh[idx] - x[idx] idx = (x < plow) x[idx] = 2 * plow[idx] - x[idx] pf = priorFunc(x) if ~isneginf(pf): if args == None: xp = func(x) + pf else: xp = func(x, *args) + pf alpha = (xp - XP[i]) / thisTemp dice = log(uniform()) if dice < alpha: X[i] = x XP[i] = xp if thisTemp == 1: success += [1] else: if thisTemp == 1: success += [0] else: if thisTemp == 1: success += [0] return X, XP, success
def dreamStep(func, priorFunc, \ X, XP, \ ncr=0, navg=1, \ gamma=1.0, eps=1.e-6, e=0.05, \ fitVector=None, args=None, violateDB=False): nChains, ndim = shape(X) nHist = len(XP) XPNEW = copy(XP) XNEW = copy(X) if all(fitVector == None): parIndex = arange(ndim) else: parIndex = copy(fitVector) success = [] #idx = arange(nHist) #mask = ones(nHist, dtype=bool) for i in range(nChains): ''' Sample two indices at random. ''' #if not violateDB: # violateDB may be desirable during burn-in #mask[i] = False # ensure current chain is not included in selection if violateDB: r = sampleIndexWithoutReplacement(nHist, 2 * navg) else: r = sampleIndexWithoutReplacement(nHist, 2 * navg, mask=[i]) #r = sampleWithoutReplacement(idx[mask], 2*navg) # select random #mask[i] = True # restore current chain for future iterations Z1 = sum(X[r[0:navg], :], axis=0) Z2 = sum(X[r[navg:], :], axis=0) ''' Create an evolution vector from those samples. ''' delta = Z2 - Z1 ''' Differential evolution step: make a trial, new solution. ''' x = copy(X[i]) pp = copy(parIndex) if ncr > 0 and ncr < ndim: # deal with limited number of crossovers pp = sampleWithoutReplacement(parIndex, ncr) x[pp] += (1.0+normal(size=len(pp))*e)*gamma*delta[pp] + \ normal(size=len(pp))*eps pf = priorFunc(x) if ~isneginf(pf): if args == None: xp = func(x) else: xp = func(x, *args) else: xp = 0. # doesn't matter ''' Acceptance ratio setp. ''' alpha = xp + pf - XP[i] dice = log(uniform()) if dice < alpha: XNEW[i] = x XPNEW[i] = xp + pf success += [1] else: success += [0] return XNEW, XPNEW, success
def dreamzStep(func, priorFunc, \ X, XP, Z, ZP, \ ncr=0, navg=1, \ gamma=1.0, eps=1.e-6, e=0.05, \ fitVector=None, args=None, plow=None, phigh=None): nChains, ndim = shape(X) nHist = len(ZP) if all(fitVector == None): parIndex = arange(ndim) else: parIndex = copy(fitVector) success = [] #idx = arange(nHist) for i in range(nChains): ''' Sample two indices at random. ''' r = sampleIndexWithoutReplacement(nHist, 2 * navg) Z1 = sum(Z[r[0:navg], :], axis=0) Z2 = sum(Z[r[navg:], :], axis=0) ''' Create an evolution vector from those samples. ''' delta = Z2 - Z1 ''' Differential evolution step: make a trial, new solution. ''' x = copy(X[i]) pp = copy(parIndex) if ncr > 0 and ncr < ndim: # deal with limited number of crossovers pp = sampleWithoutReplacement(parIndex, ncr) x[pp] += (1.0+normal(size=len(pp))*e)*gamma*delta[pp] + \ normal(size=len(pp))*eps ''' Try reflecting ''' if plow != None and phigh != None: idx = (x > phigh) x[idx] = 2 * phigh[idx] - x[idx] idx = (x < plow) x[idx] = 2 * plow[idx] - x[idx] pf = priorFunc(x) if ~isneginf(pf): if args == None: xp = func(x) else: xp = func(x, *args) else: xp = 0. # doesn't matter ''' Acceptance ratio setp. ''' alpha = xp + pf - XP[i] dice = log(uniform()) if dice < alpha: X[i] = x XP[i] = xp + pf success += [1] else: success += [0] return X, XP, success
def dreamzPTStep(func, priorFunc, \ X, XP, XT, Z, ZP, ZT, \ ncr=0, navg=1, \ gamma=1.0, eps=1.e-6, e=0.05, \ fitVector=None, args=None, plow=None, phigh=None): nChains, ndim = shape(X) nHist = len(ZP) if fitVector == None: parIndex = arange(ndim) else: parIndex = copy(fitVector) #idx = arange(nHist) success = [] idx = arange(nHist) #mask = zeros(nHist, dtype=bool) nn = nHist / nChains for i in range(nChains): ''' Make a list of Z index values at the same temperature as X[i]. ''' thisTemp = XT[i] ''' Sample indices at random. Take into account averaging. ''' #mask[ZT == thisTemp] = True # The following approach is too slow! #r = sampleWithoutReplacement(idx[mask], 2*navg) # select random # restore mask #sdx = where(ZT==thisTemp)[0] #sdx = arange(i,nHist,nChains) # assumes 1 temperature per chain #nn = len(sdx) #r = sdx[sampleIndexWithoutReplacement(nn, 2*navg)] r = sampleIndexWithoutReplacement(nn,2*navg)*nChains + i #mask[ZT == thisTemp] = False # reset for future chain Z1 = sum(Z[r[0:navg],:], axis=0) Z2 = sum(Z[r[navg:],:], axis=0) ''' Create an evolution vector from those samples. ''' delta = Z2 - Z1 ''' Differential evolution step: make a trial, new solution. ''' x = copy(X[i]) pp = copy(parIndex) if ncr > 0 and ncr < ndim: # deal with limited number of crossovers pp = sampleWithoutReplacement(parIndex, ncr) x[pp] += (1.0+normal(size=len(pp))*e)*gamma*delta[pp] + normal(size=len(pp))*eps ''' Try reflecting ''' if plow != None and phigh != None: idx = (x > phigh) x[idx] = 2*phigh[idx] - x[idx] idx = (x < plow) x[idx] = 2*plow[idx] - x[idx] pf = priorFunc(x) if ~isneginf(pf): if args == None: xp = func(x) else: xp = func(x, *args) else: xp = 0. # doesn't matter ''' Acceptance ratio setp. Notice the inclusion of temperature. ''' if ~isneginf(pf): #pfi = priorFunc(X[i]) alpha = (xp - XP[i])/XT[i] \ + pf# - pfi dice = log(uniform()) if dice < alpha: X[i] = x XP[i] = xp + pf if XT[i] == 1: success += [1] else: if XT[i] == 1: success += [0] else: if XT[i] == 1: success += [0] return X, XP, success
def dreamzStep(func, priorFunc, \ X, XP, Z, ZP, \ ncr=0, navg=1, \ gamma=1.0, eps=1.e-6, e=0.05, \ fitVector=None, args=None, plow=None, phigh=None): nChains, ndim = shape(X) nHist = len(ZP) if fitVector == None: parIndex = arange(ndim) else: parIndex = copy(fitVector) success = [] #idx = arange(nHist) XNEW = X * 0.0 sfunc = partial(getLogProb, func, priorFunc, \ args) for i in range(nChains): ''' Sample two indices at random. ''' #r = sampleWithoutReplacement(idx, 2*navg) # select random r = sampleIndexWithoutReplacement(nHist, 2*navg) Z1 = sum(Z[r[0:navg],:], axis=0) Z2 = sum(Z[r[navg:],:], axis=0) ''' Create an evolution vector from those samples. ''' delta = Z2 - Z1 ''' Differential evolution step: make a trial, new solution. ''' x = copy(X[i]) pp = copy(parIndex) if ncr > 0 and ncr < ndim: # deal with limited number of crossovers pp = sampleWithoutReplacement(parIndex, ncr) x[pp] += (1.0+normal(size=len(pp))*e)*gamma*delta[pp] + \ normal(size=len(pp))*eps ''' Try reflecting ''' if plow != None and phigh != None: idx = (x > phigh) x[idx] = 2*phigh[idx] - x[idx] idx = (x < plow) x[idx] = 2*plow[idx] - x[idx] XNEW[i] = x pool = Pool(processes=2) XPNEW = array(pool.map(sfunc, XNEW)) pool.close() pool.join() #print XPNEW alpha = XPNEW - XP dice = log(uniform(size=nChains)) success = dice < alpha XP[success] = XPNEW[success] X[success,:] = XNEW[success,:] success = list(success.astype(int)) ''' for i in range(nChains): alpha = XPNEW[i] - XP[i] dice = log(uniform()) if dice < alpha: X[i] = x XP[i] = XPNEW[i] success += [1] else: success += [0] ''' return X, XP, success