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()
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()
} """ y = array([y]) Nx = states.shape[0] Ntheta = states.shape[2] 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 = "Linear Gaussian 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.5, 1])) modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction()
# 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()
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()
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()
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()
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) for j in range(Ntheta): observations[(Nx * j):(Nx * (j+1))] = \ observationGenerator(xparticles[..., j], thetaparticles[:, j]).reshape(Nx) weightobs[(Nx * j):(Nx * (j + 1))] = repeat(thetaweights[j],
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]))
y = array([y]) Nx = states.shape[0] Ntheta = states.shape[2] 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} #call the above: computeHscore(self.xparticles, self.observations[t], self.thetaparticles, xweights_safe, thetaweights) modelx = SSM(name = "Nested LGSSM model x", xdimension = M, ydimension = 1, 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([3, 5])) #(array([3, 5])) #true params modelx.addStateFiltering() modelx.addStatePrediction() modelx.addObsPrediction()
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) for j in range(Ntheta): observations[(Nx * j):(Nx * (j+1))] = \ observationGenerator(xparticles[..., j], thetaparticles[:, j]).reshape(Nx) weightobs[(Nx * j):(Nx * (j+1))] = repeat(thetaweights[j], repeats = Nx) observations = power(observations, 2) weightobs = weightobs / sum(weightobs)