parameters[5, :]) * (parameters[2, :]**2) / parameters[3, :] poissonparameters1 = repeat(poissonparameters1[:,newaxis], Nx, axis = 1) poissonparameters2 = repeat(poissonparameters2[:,newaxis], Nx, axis = 1) for indextheta in range(Ntheta): allK1 = array(random.poisson(lam = array(poissonparameters1[indextheta,:]))).reshape(Nx) allK1[allK1 > 10**4] = 10**4 allK1 = array(allK1).reshape(Nx) sumK1 = numpysum(allK1) allK2 = array(random.poisson(lam = poissonparameters2[indextheta,:])).reshape(Nx) allK2[allK2 > 10**4] = 10**4 allK2 = array(allK2).reshape(Nx) sumK2 = numpysum(allK2) alluniforms1 = random.uniform(size = 2 * sumK1) alluniforms2 = random.uniform(size = 2 * sumK2) subresults = subtransitionAndWeight(states[..., indextheta], y, parameters[:, indextheta], \ alluniforms1, allK1, alluniforms2, allK2) newstates[..., indextheta] = subresults["states"] weights[..., indextheta] = subresults["weights"] return {"states": newstates , "weights": weights} modelx = SSM("SV multi-factor", xdimension = 5, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setTransitionAndWeight(transitionAndWeight) modelx.addStateFiltering() modelx.addStatePrediction()
newstates = zeros_like(states) # -------------- poissonparameters = parameters[2, :] * (parameters[0, :]**2) / parameters[1, :] poissonparameters = repeat(poissonparameters[:,newaxis], Nx, axis = 1) for indextheta in range(Ntheta): allK = random.poisson(lam = poissonparameters[indextheta,:]) allK = array(allK).reshape(Nx) sumK = sum(allK) alluniforms = random.uniform(size = 2 * sumK) subresults = subtransitionAndWeight(states[..., indextheta], y, parameters[:, indextheta], \ alluniforms, allK) newstates[..., indextheta] = subresults["states"] weights[..., indextheta] = subresults["weights"] # -------------- return {"states": newstates , "weights": weights} modelx = SSM("SV one-factor", xdimension = 2, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.parameters = array([0.5, 0.0625, 0.01])
# print '\n', score_y0_p, score_y0 if y0 == 0: score0 = score_y0_p/score_y0 - 1 + 0.5 * pow(score_y0_p/score_y0 - 1, 2) else: score_y0_m = average(a = nbinom.pmf(y0-1, n, p) , weights = W) #minus 1 score0 = score_y0_p/score_y0 - score_y0/score_y0_m + 0.5 * pow(score_y0_p/score_y0 - 1, 2) if y1 == 0: score1 = score_y1_p/score_y1 - 1 + 0.5 * pow(score_y1_p/score_y1 - 1, 2) else: score_y1_m = average(a = nbinom.pmf(y1-1, n, p) , weights = W) #minus 1 score1 = score_y1_p/score_y1 - score_y1/score_y1_m + 0.5 * pow(score_y1_p/score_y1 - 1, 2) compositeHScore = score0 + score1 simpleHScore = zeros(1) return {"simpleHScore" : simpleHScore , "compositeHScore" : compositeHScore} modelx = SSM(name = "logistic diffusion model x", xdimension = 1, ydimension = 2, HScoreInference = HScoreInference, continuousObs = continuousObs) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) modelx.setComputeHscore(computeHscore) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([0.05, 0.1, 1])) #put true params here to generate synthetic data, ow. COMMENT and choose a datafile in userfile.py modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction()
innerterm0 = 1. + parameters(1, j) / parameters(2, j) * (y(0) - states(k, 0, j)); innerterm1 = 1. + parameters(1, j) / parameters(2, j) * (y(1) - states(k, 0, j)); logdensity0 = - logsigma - (oneoverxi + 1) * log(innerterm0) - exp(- oneoverxi * log(innerterm0)); logdensity1 = - logsigma - (oneoverxi + 1) * log(innerterm1) - exp(- oneoverxi * log(innerterm1)); logsurv0 = - exp(- oneoverxi * log(innerterm0)); weights(k, j) = logdensity0 + logdensity1 - logsurv0; } } """ #old surv0, it was wrong: //logsurv0 = log(1 - exp(-exp((-1 / parameters(1, j)) * log(innerterm0)))); weave.inline(code,['Nx', 'Ntheta', 'states', 'y', 'parameters', 'weights', 'noise'], \ type_converters=weave.converters.blitz, libraries = ["m"]) return {"states": states , "weights": weights} modelx = SSM("Athletics records", xdimension = 2, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setTransitionAndWeight(transitionAndWeight) modelx.excludedobservations = [17] ###### old functions passed to the old filtering system #def firststate(xparticles, thetaparticles, t): # return xparticles[:, 0, :] #def CIlow(xparticles, thetaparticles, t): # Nx = xparticles.shape[0] # Ntheta = xparticles.shape[2] # result = zeros((Nx, Ntheta)) # tempvalue = log(1 - 0.025) # for j in range(Ntheta): # result[:,j] = xparticles[:, 0, j] - thetaparticles[2, j] / thetaparticles[1, j] * (1 - (- tempvalue)**(+thetaparticles[1, j])) # return result
{ states(k, 0, j) = states(k, 0, j) + temptransition * noise(k, j); weights(k, j) = tempmeasure1 + tempmeasure2 * ((double) y(0) - states(k, 0, j)) * ((double) y(0) - states(k, 0, j)); } } """ y = array([y]) Nx = states.shape[0] Ntheta = states.shape[2] weights = zeros((Nx, Ntheta)) noise = random.normal(size=(Nx, Ntheta), loc=0, scale=1) weave.inline( code, ['Nx', 'Ntheta', 'states', 'y', 'parameters', 'noise', 'weights'], type_converters=weave.converters.blitz, libraries=["m"]) return {"states": states, "weights": weights} modelx = SSM(name="Linear Gaussian model x", xdimension=1, ydimension=1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([0.5, 0.1])) modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction()
return {"states": states, "weights": weights} def transitionAndWeight(states, y, parameters, t): Nx = states.shape[0] Ntheta = states.shape[2] weights = zeros((Nx, Ntheta)) newstates = zeros_like(states) # -------------- poissonparameters = parameters[2, :] * (parameters[0, :] ** 2) / parameters[1, :] poissonparameters = repeat(poissonparameters[:, newaxis], Nx, axis=1) for indextheta in range(Ntheta): allK = random.poisson(lam=poissonparameters[indextheta, :]) allK = array(allK).reshape(Nx) sumK = sum(allK) alluniforms = random.uniform(size=2 * sumK) subresults = subtransitionAndWeight(states[..., indextheta], y, parameters[:, indextheta], alluniforms, allK) newstates[..., indextheta] = subresults["states"] weights[..., indextheta] = subresults["weights"] # -------------- return {"states": newstates, "weights": weights} modelx = SSM("SV one-factor", xdimension=2, ydimension=1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.parameters = array([0.5, 0.0625, 0.01])
transitionGF = SourceModule(transitionKernel).get_function("transition") def transitionCUDA(states, y, parameters, t): THREADS_PER_BLOCK_X = 16 THREADS_PER_BLOCK_Y = 16 y = array(y, dtype = float32) states = states.astype(float32) parameters = parameters.astype(float32) Nx, statedim, Ntheta = states.shape noise = random.normal(size = (Nx, Ntheta), loc = 0, scale = 1).astype(float32) num_blocks_x = int(math.ceil(Nx / THREADS_PER_BLOCK_X)) num_blocks_y = int(math.ceil(Ntheta / THREADS_PER_BLOCK_Y)) transitionGF(drv.In(y), drv.InOut(states), \ drv.In(parameters), \ drv.InOut(noise), \ drv.In(array(Nx, dtype = int32)), \ drv.In(array(Ntheta, dtype = int32)), \ drv.In(array(statedim, dtype = int32)), \ block = (THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y, 1), grid = (num_blocks_x, num_blocks_y)) return {"states": states, "weights": noise} modelx = SSM("Population Dynamic model x (M2), reparameterized (using CUDA)", xdimension = 1, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionCUDA) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.parameters = array([0.05**2, 0.05**2, log(1), 0.18, 1, 0.2])
THREADS_PER_BLOCK_Y = 16 y = array(y, dtype = float32) states = states.astype(float32) parameters = parameters.astype(float32) Nx, statedim, Ntheta = states.shape noise = random.normal(size = (Nx, Ntheta), loc = 0, scale = 1).astype(float32) num_blocks_x = int(math.ceil(Nx / THREADS_PER_BLOCK_X)) num_blocks_y = int(math.ceil(Ntheta / THREADS_PER_BLOCK_Y)) transitionGF(drv.In(y), drv.InOut(states), \ drv.In(parameters), \ drv.InOut(noise), \ drv.In(array(t, dtype = float32)), \ drv.In(array(Nx, dtype = int32)), \ drv.In(array(Ntheta, dtype = int32)), \ drv.In(array(statedim, dtype = int32)), \ block = (THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y, 1), grid = (num_blocks_x, num_blocks_y)) return {"states": states, "weights": noise} modelx = SSM(name = "Periodic Gaussian model x (using CUDA)", xdimension = 1, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionCUDA) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.parameters = array([10, 1])
poissonparameters2 = (1 - parameters[6, :]) * (parameters[4, :] + \ parameters[5, :]) * (parameters[2, :]**2) / parameters[3, :] poissonparameters1 = repeat(poissonparameters1[:, newaxis], Nx, axis=1) poissonparameters2 = repeat(poissonparameters2[:, newaxis], Nx, axis=1) for indextheta in range(Ntheta): allK1 = array( random.poisson( lam=array(poissonparameters1[indextheta, :]))).reshape(Nx) allK1[allK1 > 10**4] = 10**4 allK1 = array(allK1).reshape(Nx) sumK1 = numpysum(allK1) allK2 = array( random.poisson(lam=poissonparameters2[indextheta, :])).reshape(Nx) allK2[allK2 > 10**4] = 10**4 allK2 = array(allK2).reshape(Nx) sumK2 = numpysum(allK2) alluniforms1 = random.uniform(size=2 * sumK1) alluniforms2 = random.uniform(size=2 * sumK2) subresults = subtransitionAndWeight(states[..., indextheta], y, parameters[:, indextheta], \ alluniforms1, allK1, alluniforms2, allK2) newstates[..., indextheta] = subresults["states"] weights[..., indextheta] = subresults["weights"] return {"states": newstates, "weights": weights} modelx = SSM("SV multi-factor", xdimension=5, ydimension=1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setTransitionAndWeight(transitionAndWeight) modelx.addStateFiltering() modelx.addStatePrediction()
temptransition = %(SIGMA)s; for(int k = 0; k < Nx; k++) { states(k, 0, j) = parameters(0, j) * states(k, 0, j) + temptransition * noise(k, j); weights(k, j) = tempmeasure1 + tempmeasure2 * ((double) y(0) - states(k, 0, j)) * ((double) y(0) - states(k, 0, j)); } } """ % {"TAU2": TAU2, "SIGMA": SIGMA} y = array([y]) Nx = states.shape[0] Ntheta = states.shape[2] weights = zeros((Nx, Ntheta)) noise = random.normal(size = (Nx, Ntheta), loc = 0, scale = 1) weave.inline(code,['Nx', 'Ntheta', 'states', 'y', 'parameters', 'noise', 'weights'], \ type_converters=weave.converters.blitz, libraries = ["m"]) return {"states": states , "weights": weights} modelx = SSM(name = "Simplest model x", xdimension = 1, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([rho])) modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction()
# the following prevents computational issues while not changing the results, # since parameters such that allK > 10**4 are very very likely to have small weights # alternatively this fix can be seen as a modification of the model where # the poisson law is replaced by a truncated poisson law allK[allK > 10**4] = 10**4 allK = array(allK).reshape(Nx) sumK = sum(allK) alluniforms = random.uniform(size=2 * sumK) subresults = subtransitionAndWeight(states[..., indextheta], y, parameters[:, indextheta], \ alluniforms, allK) newstates[..., indextheta] = subresults["states"] weights[..., indextheta] = subresults["weights"] return {"states": newstates, "weights": weights} modelx = SSM("SV one-factor", xdimension=2, ydimension=1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([0, 0, 0.5, 0.0625, 0.01])) modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction() def predictionSquaredObservations(xparticles, thetaweights, thetaparticles, t): Nx = xparticles.shape[0] Ntheta = xparticles.shape[2] result = zeros(3)
def transitionCUDA(states, y, parameters, t): THREADS_PER_BLOCK_X = 16 THREADS_PER_BLOCK_Y = 16 y = array(y, dtype=float32) states = states.astype(float32) parameters = parameters.astype(float32) Nx, statedim, Ntheta = states.shape noise = random.normal(size=(Nx, Ntheta), loc=0, scale=1).astype(float32) num_blocks_x = int(math.ceil(Nx / THREADS_PER_BLOCK_X)) num_blocks_y = int(math.ceil(Ntheta / THREADS_PER_BLOCK_Y)) transitionGF(drv.In(y), drv.InOut(states), \ drv.In(parameters), \ drv.InOut(noise), \ drv.In(array(Nx, dtype = int32)), \ drv.In(array(Ntheta, dtype = int32)), \ drv.In(array(statedim, dtype = int32)), \ block = (THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y, 1), grid = (num_blocks_x, num_blocks_y)) return {"states": states, "weights": noise} modelx = SSM("Population Dynamic model x (M2), reparameterized (using CUDA)", xdimension=1, ydimension=1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionCUDA) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.parameters = array([0.05**2, 0.05**2, log(1), 0.18, 1, 0.2])
for(int k = 0; k < Nx; k++) { states(k, 0, j) = 0.5 * states(k, 0, j) + 25 * states(k, 0, j) / (1 + states(k, 0, j) * states(k, 0, j)) + 8 * cos(1.2 * (t(0) - 1)) + temptransition * noise(k, j); term = (double) y(0) - (states(k, 0, j) * states(k, 0, j) / 20); weights(k, j) = tempmeasure1 + tempmeasure2 * term * term; } } """ y = array([y]) t = array([t]) Nx = states.shape[0] Ntheta = states.shape[2] weights = zeros((Nx, Ntheta)) noise = random.normal(size = (Nx, Ntheta), loc = 0, scale = 1) weave.inline(code,['Nx', 'Ntheta', 'states', 'y', 't', 'parameters', 'noise', 'weights'], type_converters=weave.converters.blitz, libraries = ["m"]) return {"states": states , "weights": weights} modelx = SSM(name = "Periodic Gaussian model x", xdimension = 1, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([10, 1]))
for(int k = 0; k < Nx; k++){ states(k, 0, j) = states(k, 0, j) + parameters(3, j) * (1 - temptrans * exp(parameters(5, j) * states(k, 0, j))) + sqrt(parameters(0, j)) * noise(k, j); weights(k, j) = tempmeasure1 - 0.5 / parameters(1, j) * ((double) y(0) - exp(states(k, 0, j))) * ((double) y(0) - exp(states(k, 0, j))); } } """ y = array([y]) Nx = states.shape[0] Ntheta = states.shape[2] weights = zeros((Nx, Ntheta)) noise = random.normal(size = (Nx, Ntheta), loc = 0, scale = 1) weave.inline(code,['Nx', 'Ntheta', 'states', 'y', 'parameters', 'noise', 'weights'], type_converters=weave.converters.blitz, libraries = ["m"]) return {"states": states , "weights": weights} modelx = SSM("Population Dynamic model x (M2), reparameterized", xdimension = 1, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([0.05**2, 0.05**2, log(1), 0.18, 1, 2])) modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction()
weights(k, j) = tempmeasure1 - 0.5 / parameters(1, j) * ((double) y(0) - exp(states(k, 0, j))) * ((double) y(0) - exp(states(k, 0, j))); } } """ y = array([y]) Nx = states.shape[0] Ntheta = states.shape[2] weights = zeros((Nx, Ntheta)) noise = random.normal(size=(Nx, Ntheta), loc=0, scale=1) weave.inline( code, ['Nx', 'Ntheta', 'states', 'y', 'parameters', 'noise', 'weights'], type_converters=weave.converters.blitz, libraries=["m"]) return {"states": states, "weights": weights} modelx = SSM("Population Dynamic model x (M2), reparameterized", xdimension=1, ydimension=1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([0.05**2, 0.05**2, log(1), 0.18, 1, 2])) modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction()
innerterm0 = 1. + parameters(1, j) / parameters(2, j) * (y(0) - states(k, 0, j)); innerterm1 = 1. + parameters(1, j) / parameters(2, j) * (y(1) - states(k, 0, j)); logdensity0 = - logsigma - (oneoverxi + 1) * log(innerterm0) - exp(- oneoverxi * log(innerterm0)); logdensity1 = - logsigma - (oneoverxi + 1) * log(innerterm1) - exp(- oneoverxi * log(innerterm1)); logsurv0 = - exp(- oneoverxi * log(innerterm0)); weights(k, j) = logdensity0 + logdensity1 - logsurv0; } } """ #old surv0, it was wrong: //logsurv0 = log(1 - exp(-exp((-1 / parameters(1, j)) * log(innerterm0)))); weave.inline(code,['Nx', 'Ntheta', 'states', 'y', 'parameters', 'weights', 'noise'], \ type_converters=weave.converters.blitz, libraries = ["m"]) return {"states": states, "weights": weights} modelx = SSM("Athletics records", xdimension=2, ydimension=1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setTransitionAndWeight(transitionAndWeight) modelx.excludedobservations = [17] ###### old functions passed to the old filtering system #def firststate(xparticles, thetaparticles, t): # return xparticles[:, 0, :] #def CIlow(xparticles, thetaparticles, t): # Nx = xparticles.shape[0] # Ntheta = xparticles.shape[2] # result = zeros((Nx, Ntheta)) # tempvalue = log(1 - 0.025) # for j in range(Ntheta): # result[:,j] = xparticles[:, 0, j] - thetaparticles[2, j] / thetaparticles[1, j] * (1 - (- tempvalue)**(+thetaparticles[1, j]))
""" #comboKernel = comboKernelTemplate % {"Ntheta": Ntheta} transitionKernel = transitionKernelTemplate transitionGF = SourceModule(transitionKernel).get_function("transition") def transitionCUDA(states, y, parameters, t): THREADS_PER_BLOCK_X = 16 THREADS_PER_BLOCK_Y = 16 y = array(y, dtype = float32) states = states.astype(float32) parameters = parameters.astype(float32) Nx, statedim, Ntheta = states.shape noise = random.normal(size = (Nx, Ntheta), loc = 0, scale = 1).astype(float32) num_blocks_x = int(math.ceil(Nx / THREADS_PER_BLOCK_X)) num_blocks_y = int(math.ceil(Ntheta / THREADS_PER_BLOCK_Y)) transitionGF(drv.In(y), drv.InOut(states), \ drv.In(parameters), \ drv.InOut(noise), \ drv.In(array(Nx, dtype = int32)), \ drv.In(array(Ntheta, dtype = int32)), \ drv.In(array(statedim, dtype = int32)), \ block = (THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y, 1), grid = (num_blocks_x, num_blocks_y)) return {"states": states, "weights": noise} modelx = SSM(name = "Linear Gaussian model x (using CUDA)", xdimension = 1, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionCUDA) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.parameters = array([0.8, 0.4])
Dparam = parameters.shape[0] #dimension of the postulated model/parameter thetaweights = thetaweights / sum( thetaweights) #first transform to normalized ones xNormConst = sum(xweights, axis=0) grad = zeros(Ntheta) lap = zeros(Ntheta) hScore_theta = zeros(Ntheta) #store simple H score for each theta particle compositeHScore = zeros(1) simpleHScore = zeros(1) weave.inline(code,['states', 'y', 'parameters', 'thetaweights', 'xweights', 'xNormConst', 'hScore_theta', \ 'Nx', 'Ntheta', 'Dparam', 'grad', 'lap', 'compositeHScore', 'simpleHScore'], \ type_converters=weave.converters.blitz, libraries = ["m"]) return {"simpleHScore": simpleHScore, "compositeHScore": compositeHScore} modelx = SSM(name="Simplest model x", xdimension=1, ydimension=1, HScoreInference=HScoreInference, continuousObs=continuousObs) modelx.setComputeHscore(computeHscore) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([0.9])) modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction()
# print "\n ! number of variables to generate for some theta-particle:", t, indextheta, numpymax(allK) # the following prevents computational issues while not changing the results, # since parameters such that allK > 10**4 are very very likely to have small weights # alternatively this fix can be seen as a modification of the model where # the poisson law is replaced by a truncated poisson law allK[allK > 10**4] = 10**4 allK = array(allK).reshape(Nx) sumK = sum(allK) alluniforms = random.uniform(size = 2 * sumK) subresults = subtransitionAndWeight(states[..., indextheta], y, parameters[:, indextheta], \ alluniforms, allK) newstates[..., indextheta] = subresults["states"] weights[..., indextheta] = subresults["weights"] return {"states": newstates , "weights": weights} modelx = SSM("SV one-factor", xdimension = 2, ydimension = 1) modelx.setFirstStateGenerator(firstStateGenerator) modelx.setObservationGenerator(observationGenerator) modelx.setTransitionAndWeight(transitionAndWeight) # Values used to generate the synthetic dataset when needed: # (untransformed parameters) modelx.setParameters(array([0, 0, 0.5, 0.0625, 0.01])) modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction() def predictionSquaredObservations(xparticles, thetaweights, thetaparticles, t): Nx = xparticles.shape[0] Ntheta = xparticles.shape[2] result = zeros(3) observations = zeros(Nx * Ntheta) weightobs = zeros(Nx * Ntheta)