Example #1
0
    def __init__(self, t, N, M, xCond=None):
		C1 = 0.5
		C2 = 3.
		
		# Model init
		xDomain = np.arange(2)
				
		# Load parameters
		region = 'dustBowl'
		#region = 'sahel'
		filename = 'parameters/'+region+'Sigma2_N35-55_W90-120_downsampled.csv'
		sigma2 = np.loadtxt(filename, delimiter=',')
		filename = 'parameters/'+region+'MuAb_N35-55_W90-120_downsampled.csv'
		muAb = np.loadtxt(filename, delimiter=',')
		filename = 'parameters/'+region+'MuNorm_N35-55_W90-120_downsampled.csv'
		muNorm = np.loadtxt(filename, delimiter=',')
		filename = 'processedData/'+region+'Yt'+str(t)+'_N35-55_W90-120_downsampled.csv'
		
		Y = np.loadtxt(filename, delimiter=',')
		I = Y.shape[0]
		J = Y.shape[1]
		
		# SMC init
		X = np.zeros( (N, I, J), dtype=bool )
		ancestors = np.zeros( N )
		logZ = 0.
		logW = np.zeros( N )
		w = np.zeros( N )
		ESS = np.zeros( J )
		
		# ---------------
		#      SMC
		# ---------------        
		# SMC first iteration
		params = hlp.params(I = I, muAb = muAb[:,0], muNorm = muNorm[:,0], sigma2 = sigma2[:,0])
		if xCond is not None:
			q = [nested.inner(params, Y[:,0], M, xCond[:,0]) for i in range(N)]
		else:
			q = [nested.inner(params, Y[:,0], M) for i in range(N)]
		logW = np.array([q[i].logZ for i in range(N)])
		maxLogW = np.max(logW)
		w = np.exp(logW - maxLogW)
		logZ = maxLogW + np.log(np.sum(w)) - np.log(N)
		w /= np.sum(w)
		#print w.shape
		ESS[0] = 1/np.sum(w**2)
		#print 'ESS: ',ESS[0]
		#print 'First logZ: ',logZ
		
		ancestors = hlp.resampling(w)
		for i in range(N):
			X[i,:,0] = q[ancestors[i]].simulate()
		
		## SMC MAIN LOOP
		for j in np.arange(1,J):
			#print j
			params = hlp.params(I = I, muAb = muAb[:,j], muNorm = muNorm[:,j], sigma2 = sigma2[:,j])
			if xCond is not None:
				q = [nested.inner(params, Y[:,j], M, xCond[:,j], X[i,:,j-1]) for i in range(N)]
			else:
				q = [nested.inner(params, Y[:,j], M, xSpaceCond = X[i,:,j-1]) for i in range(N)]
			logW = np.array([q[i].logZ for i in range(N)])
			maxLogW = np.max(logW)
			w = np.exp(logW - maxLogW)
			logZ += maxLogW + np.log(np.sum(w)) - np.log(N)
			#print 'j: ',j,' logZ',logZ
			#print 'logW: ',logW
			#print 'Y: ',Y[:,j]
			w /= np.sum(w)
			#print 'Max w: ',np.max(w)
			ESS[j] = 1/np.sum(w**2)
			#print 'ESS: ',ESS[j]
			
			ancestors = hlp.resampling(w)
			for i in range(N):
				X[i,:,j] = q[ancestors[i]].simulate()
		
		#print 'Last logZ: ',logZ
		## Save init to class object
		self.N = N
		self.J = J
		self.I = I
		self.X = X
		self.logZ = logZ
		self.w = w
		self.xCond = xCond
		self.ESS = ESS
Example #2
0
def runNested(d, tauPhi, N, M, nrRuns):
    r"""Run NSMC filtering on high-dimensional LGSS.
    
    Parameters
    ----------
    d : int
        State dimension.
    tauPhi : float
        Measurement precision.
    N : int
        Number of particles, 1st level.
    M : int
        Number of particles, 2nd level.
    nrRuns : int
        Number of independent runs of the algorithm.
    
    Returns
    -------
    Saves E[X] and E[X**2] estimates.
    """
    # Model init
    a = 0.5
    tauPsi = 1.
    tauRho = 1.
    params = hlp.params(a = a,tauPsi = tauPsi,tauRho = tauRho,tauPhi = tauPhi)

    filename = 'simulatedData/d'+str(d)+'tauPhi'+str(tauPhi)+'y.txt'
    y = np.loadtxt(filename)
    T = y.shape[1]

    for j in range(nrRuns):
        # Algorithm init
        logZ = np.zeros(N)
        ancestors = np.zeros(N)
        X = np.zeros((N,d))
        ESS = np.zeros(T)

        # Setup new file
        filename = './results/paper/d'+str(d)+'_N'+str(N)+'_M'+str(M)+'tauPhi'+str(tauPhi)+'_nestedSMCrun'+str(j+1)+'.csv'
        f = open(filename, 'w')
        #f.write('Iter ESS E[x] E[x**2]\n')
        f.close()
        
        for t in range(T):
            q = [ nsmc.nestedFAPF(params, y[:,t], M, X[i,:]) for i in range(N) ]

            for i in range(N):
                logZ[i] = q[i].logZ
                
            maxLz = np.max(logZ)
            w = np.exp(logZ-maxLz)
            w /= np.sum(w)
            ESS[t] = 1/np.sum(w**2)
            ancestors = hlp.resampling(w)
            
            for i in range(N):
                X[i,:] = q[ancestors[i]].simulate(1,BS=True)    
            
            f = open(filename, 'a')
            tmpVec = np.r_[t+1, ESS[t], np.mean(X,axis=0), np.mean(X**2,axis=0)]
            np.savetxt(f, tmpVec.reshape((1,len(tmpVec))),delimiter=',')
            f.close()
Example #3
0
def runNested(d, tauPhi, N, M, nrRuns):
    r"""Run NSMC filtering on high-dimensional LGSS.
    
    Parameters
    ----------
    d : int
        State dimension.
    tauPhi : float
        Measurement precision.
    N : int
        Number of particles, 1st level.
    M : int
        Number of particles, 2nd level.
    nrRuns : int
        Number of independent runs of the algorithm.
    
    Returns
    -------
    Saves E[X] and E[X**2] estimates.
    """
    # Model init
    a = 0.5
    tauPsi = 1.
    tauRho = 1.
    params = hlp.params(a=a, tauPsi=tauPsi, tauRho=tauRho, tauPhi=tauPhi)

    filename = 'simulatedData/d' + str(d) + 'tauPhi' + str(tauPhi) + 'y.txt'
    y = np.loadtxt(filename)
    T = y.shape[1]

    for j in range(nrRuns):
        # Algorithm init
        logZ = np.zeros(N)
        ancestors = np.zeros(N)
        X = np.zeros((N, d))
        ESS = np.zeros(T)

        # Setup new file
        filename = './results/paper/d' + str(d) + '_N' + str(N) + '_M' + str(
            M) + 'tauPhi' + str(tauPhi) + '_nestedSMCrun' + str(j + 1) + '.csv'
        f = open(filename, 'w')
        #f.write('Iter ESS E[x] E[x**2]\n')
        f.close()

        for t in range(T):
            q = [
                nsmc.nestedFAPF(params, y[:, t], M, X[i, :]) for i in range(N)
            ]

            for i in range(N):
                logZ[i] = q[i].logZ

            maxLz = np.max(logZ)
            w = np.exp(logZ - maxLz)
            w /= np.sum(w)
            ESS[t] = 1 / np.sum(w**2)
            ancestors = hlp.resampling(w)

            for i in range(N):
                X[i, :] = q[ancestors[i]].simulate(1, BS=True)

            f = open(filename, 'a')
            tmpVec = np.r_[t + 1, ESS[t],
                           np.mean(X, axis=0),
                           np.mean(X**2, axis=0)]
            np.savetxt(f, tmpVec.reshape((1, len(tmpVec))), delimiter=',')
            f.close()
Example #4
0
    def __init__(self, t, N, M, xCond=None):
        C1 = 0.5
        C2 = 3.

        # Model init
        xDomain = np.arange(2)

        # Load parameters
        region = 'dustBowl'
        #region = 'sahel'
        filename = 'parameters/' + region + 'Sigma2_N35-55_W90-120_downsampled.csv'
        sigma2 = np.loadtxt(filename, delimiter=',')
        filename = 'parameters/' + region + 'MuAb_N35-55_W90-120_downsampled.csv'
        muAb = np.loadtxt(filename, delimiter=',')
        filename = 'parameters/' + region + 'MuNorm_N35-55_W90-120_downsampled.csv'
        muNorm = np.loadtxt(filename, delimiter=',')
        filename = 'processedData/' + region + 'Yt' + str(
            t) + '_N35-55_W90-120_downsampled.csv'

        Y = np.loadtxt(filename, delimiter=',')
        I = Y.shape[0]
        J = Y.shape[1]

        # SMC init
        X = np.zeros((N, I, J), dtype=bool)
        ancestors = np.zeros(N)
        logZ = 0.
        logW = np.zeros(N)
        w = np.zeros(N)
        ESS = np.zeros(J)

        # ---------------
        #      SMC
        # ---------------
        # SMC first iteration
        params = hlp.params(I=I,
                            muAb=muAb[:, 0],
                            muNorm=muNorm[:, 0],
                            sigma2=sigma2[:, 0])
        if xCond is not None:
            q = [
                nested.inner(params, Y[:, 0], M, xCond[:, 0]) for i in range(N)
            ]
        else:
            q = [nested.inner(params, Y[:, 0], M) for i in range(N)]
        logW = np.array([q[i].logZ for i in range(N)])
        maxLogW = np.max(logW)
        w = np.exp(logW - maxLogW)
        logZ = maxLogW + np.log(np.sum(w)) - np.log(N)
        w /= np.sum(w)
        #print w.shape
        ESS[0] = 1 / np.sum(w**2)
        #print 'ESS: ',ESS[0]
        #print 'First logZ: ',logZ

        ancestors = hlp.resampling(w)
        for i in range(N):
            X[i, :, 0] = q[ancestors[i]].simulate()

        ## SMC MAIN LOOP
        for j in np.arange(1, J):
            #print j
            params = hlp.params(I=I,
                                muAb=muAb[:, j],
                                muNorm=muNorm[:, j],
                                sigma2=sigma2[:, j])
            if xCond is not None:
                q = [
                    nested.inner(params, Y[:, j], M, xCond[:, j],
                                 X[i, :, j - 1]) for i in range(N)
                ]
            else:
                q = [
                    nested.inner(params, Y[:, j], M, xSpaceCond=X[i, :, j - 1])
                    for i in range(N)
                ]
            logW = np.array([q[i].logZ for i in range(N)])
            maxLogW = np.max(logW)
            w = np.exp(logW - maxLogW)
            logZ += maxLogW + np.log(np.sum(w)) - np.log(N)
            #print 'j: ',j,' logZ',logZ
            #print 'logW: ',logW
            #print 'Y: ',Y[:,j]
            w /= np.sum(w)
            #print 'Max w: ',np.max(w)
            ESS[j] = 1 / np.sum(w**2)
            #print 'ESS: ',ESS[j]

            ancestors = hlp.resampling(w)
            for i in range(N):
                X[i, :, j] = q[ancestors[i]].simulate()

        #print 'Last logZ: ',logZ
        ## Save init to class object
        self.N = N
        self.J = J
        self.I = I
        self.X = X
        self.logZ = logZ
        self.w = w
        self.xCond = xCond
        self.ESS = ESS