def findDerivative(args): pertScale, startDate, endDate, recordStep, M, targetGraph, seed = args numpy.random.seed(seed) meanTheta, sigmaTheta = HIVModelUtils.toyTheta() epsilon = 5.0 undirected = True alpha = 2 zeroVal = 0.9 p = Util.powerLawProbs(alpha, zeroVal) graph = HIVGraph(M, undirected) featureInds= numpy.ones(graph.vlist.getNumFeatures(), numpy.bool) featureInds[HIVVertices.dobIndex] = False featureInds[HIVVertices.infectionTimeIndex] = False featureInds[HIVVertices.hiddenDegreeIndex] = False featureInds[HIVVertices.stateIndex] = False featureInds = numpy.arange(featureInds.shape[0])[featureInds] matcher = GraphMatch("PATH", alpha=0.5, featureInds=featureInds, useWeightM=False) abcParams = HIVABCParameters(meanTheta, sigmaTheta, pertScale) newTheta = abcParams.perturbationKernel(meanTheta) undirected = True graph = HIVGraph(M, undirected) graphMetrics = HIVGraphMetrics2(targetGraph, epsilon, matcher, float(endDate)) graphMetrics.breakDist = 1.0 hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices()) rates = HIVRates(graph, hiddenDegSeq) model = HIVEpidemicModel(graph, rates, T=float(endDate), T0=float(startDate), metrics=graphMetrics) model.setRecordStep(recordStep) model.setParams(meanTheta) times, infectedIndices, removedIndices, graph = model.simulate(True) return abs(0.7 - graphMetrics.distance())/numpy.linalg.norm(newTheta-meanTheta)
return model if len(sys.argv) > 1: numProcesses = int(sys.argv[1]) else: numProcesses = multiprocessing.cpu_count() purtScale = 0.02 meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta() abcParams = HIVABCParameters(meanTheta, sigmaTheta, purtScale) thetaDir = resultsDir + "theta/" abcSMC = ABCSMC(epsilonArray, createModel, abcParams, thetaDir, True) abcSMC.setPosteriorSampleSize(posteriorSampleSize) abcSMC.batchSize = 50 abcSMC.maxRuns = 2000 thetasArray = abcSMC.run() meanTheta = numpy.mean(thetasArray, 0) stdTheta = numpy.std(thetasArray, 0) logging.debug(thetasArray) logging.debug("meanTheta=" + str(meanTheta)) logging.debug("stdTheta=" + str(stdTheta)) logging.debug("realTheta=" + str(HIVModelUtils.toyTheta()[0])) logging.debug("New epsilon array: " + str(abcSMC.epsilonArray)) logging.debug("Number of ABC runs: " + str(abcSMC.numRuns)) logging.debug("All done!")
of times and average the results. The purpose is to test the ABC model selection by using a known value of theta. """ logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) numpy.seterr(all='raise') numpy.random.seed(24) numpy.set_printoptions(suppress=True, precision=4, linewidth=100) startDate, endDate, recordStep, M = HIVModelUtils.toySimulationParams(False) endDate += HIVModelUtils.toyTestPeriod numRepetitions = 1 undirected = True outputDir = PathDefaults.getOutputDir() + "viroscopy/toy/" theta, sigmaTheta = HIVModelUtils.toyTheta() graphList = [] for j in range(numRepetitions): graph = HIVGraph(M, undirected) logging.debug("Created graph: " + str(graph)) alpha = 2 zeroVal = 0.9 p = Util.powerLawProbs(alpha, zeroVal) hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices()) rates = HIVRates(graph, hiddenDegSeq) model = HIVEpidemicModel(graph, rates)