def profileSimulateGraphMatch(self): N, matchAlpha, breakDist, purtScale = HIVModelUtils.toyABCParams() startDate, endDate, recordStep, M, targetGraph = HIVModelUtils.toySimulationParams() theta, stdTheta = HIVModelUtils.toyTheta() featureInds= numpy.ones(targetGraph.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] #QCV is fastest and most accurate #PATH is slowests but quite accurate #RANK is very fast by less accurate than PATH #U is fastest but least accurate matcher = GraphMatch("QCV", alpha=matchAlpha, featureInds=featureInds, useWeightM=False) matcher.lambdaM = 50 matcher.init = "rand" graphMetrics = HIVGraphMetrics2(targetGraph, breakDist, matcher, float(endDate)) def run(): times, infectedIndices, removedIndices, graph = HIVModelUtils.simulate(theta, startDate, endDate, recordStep, M, graphMetrics) print("Mean distance " + str(graphMetrics.meanDistance())) ProfileUtils.profile('run()', globals(), locals())
def testSimulate(self): #We want to see if we can get the same simulation twice N, matchAlpha, breakScale, numEpsilons, epsilon, minEpsilon, matchAlg, abcMaxRuns, batchSize, pertScale = HIVModelUtils.toyABCParams() startDate, endDate, recordStep, M, targetGraph = HIVModelUtils.toySimulationParams(test=True) breakSize = (targetGraph.subgraph(targetGraph.removedIndsAt(endDate)).size - targetGraph.subgraph(targetGraph.removedIndsAt(startDate)).size) * breakScale theta, sigmaTheta, pertTheta = HIVModelUtils.toyTheta() model = HIVModelUtils.createModel(theta, targetGraph, startDate, endDate, recordStep, M, matchAlpha, breakSize, matchAlg) model.setParams(theta) times, infectedIndices, removedIndices, graph, compTimes, graphMetrics = HIVModelUtils.simulate(model) numEdges = graph.getNumEdges() lastRemovedIndices = removedIndices[-1] #Simulate again model = HIVModelUtils.createModel(theta, targetGraph, startDate, endDate, recordStep, M, matchAlpha, breakSize, matchAlg) model.setParams(theta) times, infectedIndices, removedIndices, graph, compTimes, graphMetrics = HIVModelUtils.simulate(model) numEdges2 = graph.getNumEdges() lastRemovedIndices2 = removedIndices[-1] self.assertEquals(numEdges, numEdges2) self.assertEquals(lastRemovedIndices, lastRemovedIndices2)
def testSimulate2(self): alpha = 2 zeroVal = 0.9 startDate = 0.0 endDate = 200.0 M = 1000 undirected = True theta, sigmaTheta, pertTheta = HIVModelUtils.toyTheta() numpy.random.seed(21) graph = HIVGraph(M, undirected) p = Util.powerLawProbs(alpha, zeroVal) hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices()) rates = HIVRates(graph, hiddenDegSeq) model = HIVEpidemicModel(graph, rates, endDate, startDate, metrics=None) #model.setRecordStep(recordStep) model.setParams(theta) times, infectedIndices, removedIndices, graph = model.simulate(True) numVertices = graph.size numEdges = graph.getNumEdges() #Try again numpy.random.seed(21) graph = HIVGraph(M, undirected) p = Util.powerLawProbs(alpha, zeroVal) hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices()) rates = HIVRates(graph, hiddenDegSeq) model = HIVEpidemicModel(graph, rates, endDate, startDate, metrics=None) model.setParams(theta) times, infectedIndices, removedIndices, graph = model.simulate(True) numVertices2 = graph.size numEdges2 = graph.getNumEdges() self.assertEquals(numVertices2, numVertices) self.assertEquals(numEdges2, numEdges)
def loadParams(ind): if processReal: resultsDir = PathDefaults.getOutputDir() + "viroscopy/real/theta" + str(ind) + "/" outputDir = resultsDir + "stats/" N, matchAlpha, breakScale, numEpsilons, epsilon, minEpsilon, matchAlg, abcMaxRuns, batchSize, pertScale = HIVModelUtils.realABCParams(True) startDate, endDate, recordStep, M, targetGraph, numInds = HIVModelUtils.realSimulationParams(test=True, ind=ind) realTheta, sigmaTheta, pertTheta = HIVModelUtils.estimatedRealTheta(ind) numInds=2 prefix = "Real" else: resultsDir = PathDefaults.getOutputDir() + "viroscopy/toy/theta/" outputDir = resultsDir + "stats/" N, matchAlpha, breakScale, numEpsilons, epsilon, minEpsilon, matchAlg, abcMaxRuns, batchSize, pertScale = HIVModelUtils.toyABCParams() startDate, endDate, recordStep, M, targetGraph = HIVModelUtils.toySimulationParams(test=True) realTheta, sigmaTheta, pertTheta = HIVModelUtils.toyTheta() prefix = "Toy" numInds = 1 breakSize = (targetGraph.subgraph(targetGraph.removedIndsAt(endDate)).size - targetGraph.subgraph(targetGraph.removedIndsAt(startDate)).size) * breakScale return N, resultsDir, outputDir, recordStep, startDate, endDate, prefix, targetGraph, breakSize, numEpsilons, M, matchAlpha, matchAlg, numInds
for ind in inds: logging.debug("ind=" + str(ind)) N, resultsDir, outputDir, recordStep, startDate, endDate, prefix, targetGraph, breakSize, numEpsilons, M, matchAlpha, matchAlg, numInds = loadParams(ind) #Find the max number t for which we have a complete set of particles t = 0 for i in range(numEpsilons): thetaArray, objArray = loadThetaArray(N, resultsDir, i) if thetaArray.shape[0] == N: t = i logging.debug("Using particle number " + str(t)) times = numpy.arange(startDate, endDate+1, recordStep) realTheta, sigmaTheta, purtTheta = HIVModelUtils.toyTheta() thetaArray, objArray = loadThetaArray(N, resultsDir, t) thetas.append(thetaArray) print(thetaArray) resultsFileName = outputDir + "IdealStats.pkl" stats = Util.loadPickle(resultsFileName) vertexArrayIdeal, idealInfectedIndices, idealRemovedIndices, idealContactGraphStats, idealRemovedGraphStats, idealFinalRemovedDegrees = stats graphStats = GraphStatistics() idealMeasures[ind, numDetectsInd, :] = vertexArrayIdeal[:, numDetectsInd] idealMeasures[ind, maleInd, :] = vertexArrayIdeal[:, maleInd] idealMeasures[ind, femaleInd, :] = vertexArrayIdeal[:, femaleInd] idealMeasures[ind, heteroInd, :] = vertexArrayIdeal[:, heteroInd] idealMeasures[ind, biInd, :] = vertexArrayIdeal[:, biInd] idealMeasures[ind, randDetectInd, :] = vertexArrayIdeal[:, randDetectInd]
import matplotlib.pyplot as plt """ This is the epidemic model for the HIV spread in cuba. We want to see how different graphs can get under the same params but different seeds. """ assert False, "Must run with -O flag" 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, targetGraph = HIVModelUtils.toySimulationParams() meanTheta, sigmaTheta = HIVModelUtils.toyTheta() #In this case, we'll extend the end date endDate *= 2 epsilon = 5.0 reps = 10 graphDists = [] removedArray = [] biArray = [] maleArray = [] femaleArray = [] for i in range(reps): print("i=" + str(i))
N, matchAlpha, breakScale, numEpsilons, epsilon, minEpsilon, matchAlg, abcMaxRuns, batchSize, pertScale = HIVModelUtils.toyABCParams() logging.debug("Total time of simulation is " + str(endDate-startDate)) logging.debug("Posterior sample size " + str(N)) epsilonArray = numpy.ones(numEpsilons)*epsilon breakSize = (targetGraph.subgraph(targetGraph.removedIndsAt(endDate)).size - targetGraph.subgraph(targetGraph.removedIndsAt(startDate)).size) * breakScale logging.debug("Largest acceptable graph is " + str(breakSize)) def createModel(t): """ The parameter t is the particle index. """ return HIVModelUtils.createModel(targetGraph, startDate, endDate, recordStep, M, matchAlpha, breakSize, matchAlg) meanTheta, sigmaTheta, pertTheta = HIVModelUtils.toyTheta() abcParams = HIVABCParameters(meanTheta, sigmaTheta, pertTheta) thetaDir = resultsDir + "theta/" if not os.path.exists(thetaDir): os.mkdir(thetaDir) logging.debug((meanTheta, sigmaTheta)) abcSMC = ABCSMC(epsilonArray, createModel, abcParams, thetaDir, True, minEpsilon=minEpsilon) abcSMC.setPosteriorSampleSize(N) abcSMC.batchSize = batchSize abcSMC.maxRuns = abcMaxRuns abcSMC.setNumProcesses(numProcesses) abcSMC.pertScale = pertScale thetasArray = abcSMC.run()