def profileSimulate(self): startDate, endDate, recordStep, printStep, M, targetGraph = HIVModelUtils.realSimulationParams() meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta() meanTheta = numpy.array([337, 1.4319, 0.211, 0.0048, 0.0032, 0.5229, 0.042, 0.0281, 0.0076, 0.0293]) undirected = True graph = HIVGraph(M, undirected) logging.info("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) model.setT0(startDate) model.setT(startDate+100) model.setRecordStep(recordStep) model.setPrintStep(printStep) model.setParams(meanTheta) logging.debug("MeanTheta=" + str(meanTheta)) ProfileUtils.profile('model.simulate()', globals(), locals())
graphMetrics.breakDist = 0.0 rates = HIVRates(graph, hiddenDegSeq) model = HIVEpidemicModel(graph, rates, T=float(endDate), T0=float(startDate), metrics=graphMetrics) model.setRecordStep(recordStep) 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]))
def testSimulate2(self): startDate = 0.0 endDate = 100.0 M = 1000 meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta() undirected = True graph = HIVGraph(M, undirected) alpha = 2 zeroVal = 0.9 p = Util.powerLawProbs(alpha, zeroVal) hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices()) meanTheta[4] = 0.1 recordStep = 10 printStep = 10 rates = HIVRates(graph, hiddenDegSeq) model = HIVEpidemicModel(graph, rates, endDate, startDate) model.setRecordStep(recordStep) model.setPrintStep(printStep) model.setParams(meanTheta) initialInfected = graph.getInfectedSet() times, infectedIndices, removedIndices, graph = model.simulate(True) #Now test the final graph edges = graph.getAllEdges() for i, j in edges: if graph.vlist.V[i, HIVVertices.genderIndex] == graph.vlist.V[j, HIVVertices.genderIndex] and (graph.vlist.V[i, HIVVertices.orientationIndex] != HIVVertices.bi or graph.vlist.V[j, HIVVertices.orientationIndex] != HIVVertices.bi): self.fail() finalInfected = graph.getInfectedSet() finalRemoved = graph.getRemovedSet() self.assertEquals(numpy.intersect1d(initialInfected, finalRemoved).shape[0], len(initialInfected)) #Test case where there is no contact meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, 0, 0, 0, 0, 0], numpy.float) times, infectedIndices, removedIndices, graph, model = runModel(meanTheta) self.assertEquals(len(graph.getInfectedSet()), 100) self.assertEquals(len(graph.getRemovedSet()), 0) self.assertEquals(graph.getNumEdges(), 0) heteroContactRate = 0.1 meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, 0, 0, 0], numpy.float) times, infectedIndices, removedIndices, graph, model = runModel(meanTheta) self.assertEquals(len(graph.getInfectedSet()), 100) self.assertEquals(len(graph.getRemovedSet()), 0) edges = graph.getAllEdges() for i, j in edges: self.assertNotEqual(graph.vlist.V[i, HIVVertices.genderIndex], graph.vlist.V[j, HIVVertices.genderIndex]) #Number of conacts = rate*people*time infectedSet = graph.getInfectedSet() numHetero = (graph.vlist.V[list(infectedSet), HIVVertices.orientationIndex] == HIVVertices.hetero).sum() self.assertTrue(abs(numHetero*endDate*heteroContactRate- model.getNumContacts()) < 100) heteroContactRate = 0.01 meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, 0, 0, 0], numpy.float) times, infectedIndices, removedIndices, graph, model = runModel(meanTheta) infectedSet = graph.getInfectedSet() numHetero = (graph.vlist.V[list(infectedSet), HIVVertices.orientationIndex] == HIVVertices.hetero).sum() self.assertAlmostEqual(numHetero*endDate*heteroContactRate/100, model.getNumContacts()/100.0, 0)