def quad(f, g, dim): intf = integrate(g, (symx, 0, 1)) if dim == 2: intf = integrate(intf, (symy, 0, 1)) grid, alpha = interpolate(f, 4, dim, GridType_Linear) grid2, alpha2 = interpolate(f, 4, dim, GridType_PolyBoundary, 2) f1 = createOperationQuadrature(grid).doQuadrature(alpha) f2 = createOperationQuadrature(grid2).doQuadrature(alpha2) f3 = intf.evalf() return f1, f2, f3
def quad(f, g): intf = integrate(g, symx) grid, alpha = interpolate(f, 4, 1) f1 = createOperationQuadrature(grid).doQuadrature(alpha) f2 = intf.subs(symx, 1) - intf.subs(symx, 0) return f1, f2.evalf()
def __init__(self, grid, alpha, trainData=None, bounds=None, config=None, learner=None, unitIntegrand=True, isPositive=True): super(SGDEdist, self).__init__(grid.getStorage().getDimension(), trainData, bounds) self.grid = grid.clone() self.alpha = alpha.copy() self.alpha_vec = DataVector(alpha) if trainData is not None: self.trainData = trainData.copy() else: self.trainData = None self.config = config self.unitIntegrand = unitIntegrand if learner is None and trainData is not None: trainData_vec = DataMatrix(trainData) self.learner = SparseGridDensityEstimator(self.grid, self.alpha_vec, trainData_vec) else: self.learner = learner if trainData is None: self.dim = grid.getStorage().getDimension() elif self.dim != grid.getStorage().getDimension(): raise AttributeError("the dimensionality of the data differs from the one of the grid") assert self.grid.getSize() == len(self.alpha) if isPositive: self.vol = createOperationQuadrature(self.grid).doQuadrature(self.alpha_vec) else: # do monte carlo quadrature to estimate the volume n = 20000 numDims = grid.getStorage().getDimension() generator = LatinHypercubeSampleGenerator(numDims, n) samples = np.ndarray((n, numDims)) sample = DataVector(numDims) for i in range(samples.shape[0]): generator.getSample(sample) samples[i, :] = sample.array() values = evalSGFunction(grid, alpha, samples) self.vol = np.mean([max(0.0, value) for value in values]) # scale the coefficients such that it has unit integrand self.unnormalized_alpha = np.array(self.alpha / self.vol) self.unnormalized_alpha_vec = DataVector(self.unnormalized_alpha) self.vol *= self.trans.vol() if unitIntegrand and self.vol > 1e-13: self.alpha /= self.vol self.alpha_vec.mult(1. / self.vol)
def testQuadratureTruncated(self): def f(x): return 1. grid, alpha = interpolate(f, 2, 3) alpha = DataVector(grid.getStorage().getSize()) for ix in range(0, grid.getStorage().getSize()): alpha.setAll(0.0) alpha[ix] = 1. gp = grid.getStorage().getPoint(ix) accLevel = sum( [max(1, gp.getLevel(d)) for d in range(gp.getDimension())]) self.assertTrue( createOperationQuadrature(grid).doQuadrature( alpha) == 2**-accLevel, "%g != %g" % (createOperationQuadrature(grid).doQuadrature(alpha), 2** -accLevel))
def doQuadrature(grid, alpha): try: return createOperationQuadrature(grid).doQuadrature(alpha) except Exception: # import ipdb; ipdb.set_trace() s = 0.0 gs = grid.getStorage() # set function values for n_alpha for i in xrange(gs.size()): gp = gs.get(i) q = 1.0 for d in xrange(gs.dim()): q *= getIntegral(grid, gp.getLevel(d), gp.getIndex(d)) s += alpha[i] * q return s
def doQuadrature(grid, alpha): try: return createOperationQuadrature(grid).doQuadrature(alpha) except Exception: # import ipdb; ipdb.set_trace() s = 0.0 gs = grid.getStorage() # set function values for n_alpha for i in xrange(gs.size()): gp = gs.get(i) q = 1.0 for d in xrange(gs.dim()): q *= getIntegral(grid, gp.getLevel(d), gp.getIndex(d)) s += alpha[i] * q return s
def doQuadrature(grid, alpha): try: alphaVec = DataVector(alpha) return createOperationQuadrature(grid).doQuadrature(alphaVec) except Exception: # import ipdb; ipdb.set_trace() s = 0.0 gs = grid.getStorage() # set function values for n_alpha for i in range(gs.getSize()): gp = gs.getPoint(i) q = 1.0 for d in range(gs.getDimension()): q *= getIntegral(grid, gp.getLevel(d), gp.getIndex(d)) s += alpha[i] * q return s
def estimateDensitySGDE(trainSamplesUnit, testSamplesUnit=None, testSamplesProb=None, pathResults="/tmp", dist=None, optimization='l2', iteration=0, levels=[1, 2, 3, 4, 5], refNr=0, refPoints=0, nSamples=1000): """ Estimates a sparse grid density for different levels and refinements by optimizing over a given quantity. @param trainSamplesUnit: @param testSamplesUnit: @param testSamplesProb: @param pathResults: @param dist: @param optimization: @param iteration: @param levels: @param refNr: @param refPoints: """ config = """ [general] method = dmest [files] inFileTrain = %s usingTrain = %s inFileTest = %s outFileTest = %s usingTest = %s [dmest] gridFile = %s lambda = -1 # 0.01 regType=Laplace refNr = %i refPoints = %i writeGridFile = %s writeAlphaFile = %s samp_rejectionTrialMax = 5000 samp_numSamples = %i samp_outFile = %s printSurfaceFile = %s """ # write the samples to file if len(trainSamplesUnit.shape) == 1: n, dim = trainSamplesUnit.shape[0], 1 usingTrainTag = "%i" % dim else: n, dim = trainSamplesUnit.shape usingTrainTag = "1:%i" % dim trainSamplesUnitFile = os.path.join(pathResults, "samples_%i_%i_train.csv" % (iteration, n)) np.savetxt(trainSamplesUnitFile, trainSamplesUnit) testSamplesUnitFile = "" usingTestTag = "" if testSamplesUnit is not None: testSamplesUnitFile = os.path.join(pathResults, "samples_%i_%i_test.csv" % (iteration, n)) if dim == 1: usingTestTag = "%i" % dim else: usingTestTag = "1:%i" % dim np.savetxt(testSamplesUnitFile, testSamplesUnit) # collector arrays accGridSizes = np.array([]) accLevels = np.array([]) accL2error = np.array([]) accCrossEntropy = np.array([]) accKLDivergence = np.array([]) # best estimation ans = None bestMeasure = 1e20 bestSetting = None for level in levels: # define output files gridFile = os.path.join(pathResults, "samples_%i_%i_l%i.grid" % (iteration, n, level)) alphaFile = os.path.join(pathResults, "samples_%i_%i_l%i.alpha.arff" % (iteration, n, level)) sampleFile = os.path.join(pathResults, "samples_%i_%i_l%i.csv" % (iteration, n, level)) likelihoodFile = "" if testSamplesUnit is not None: likelihoodFile = os.path.join(pathResults, "samples_%i_%i_l%i_likelihood.csv" % (iteration, n, level)) surfaceFile = "" if dim == 2: surfaceFile = os.path.join(pathResults, "samples_%i_%i_l%i.xyz" % (iteration, n, level)) gnuplotJpegFile = os.path.join(pathResults, "samples_%i_%i_l%i_gnuplot.jpg" % (iteration, n, level)) sgdeJpegFile = os.path.join(pathResults, "samples_%i_%i_l%i_sgde.jpg" % (iteration, n, level)) sgdePositiveJpegFile = os.path.join(pathResults, "samples_%i_%i_l%i_sgdePositive.jpg" % (iteration, n, level)) configFile = os.path.join(pathResults, "sgde_%i_%i_l%i.cfg" % (iteration, n, level)) gnuplotConfig = os.path.join(pathResults, "sgde_%i_%i_l%i.gnuplot" % (iteration, n, level)) # generate the grid grid = Grid.createLinearBoundaryGrid(dim) grid.createGridGenerator().regular(level) if grid.getSize() <= n: print " l=%i" % level, fd = open(gridFile, "w") fd.write(grid.serialize()) fd.close() # write config to file fd = open(configFile, "w") fd.write(config % (trainSamplesUnitFile, usingTrainTag, testSamplesUnitFile, likelihoodFile, usingTestTag, gridFile, refNr, refPoints, gridFile, alphaFile, nSamples, sampleFile, surfaceFile)) fd.close() sgdeDist = SGDEdist.byConfig(configFile) grid, alpha = sgdeDist.grid, sgdeDist.alpha # ----------------------------------------------------------- # do some plotting if dim == 2: # gnuplot sgdeDist.gnuplot(gnuplotJpegFile, gnuplotConfig=gnuplotConfig) # ----------------------------------------------------------- # matplotlib l2error = np.NAN kldivergence = np.NAN crossEntropy = sgdeDist.crossEntropy(testSamplesUnit) if dist is not None: l2error = dist.l2error(sgdeDist, testSamplesUnit, testSamplesProb) kldivergence = dist.klDivergence(sgdeDist, testSamplesUnit, testSamplesProb) fig = plt.figure() plotSG2d(grid, alpha) plt.title("N=%i: vol=%g, kl=%g, log=%g, l2error=%g" % (grid.getSize(), doQuadrature(grid, alpha), kldivergence, crossEntropy, l2error)) fig.savefig(sgdeJpegFile) plt.close(fig) # ----------------------------------------------------------- # copy grid and coefficients gridFileNew = os.path.join(pathResults, "samples_%i_%i_sgde.grid" % (iteration, n)) alphaFileNew = os.path.join(pathResults, "samples_%i_%i_sgde.alpha.arff" % (iteration, n)) sampleFileNew = os.path.join(pathResults, "samples_%i_%i_sgde.csv" % (iteration, n)) copy2(gridFile, gridFileNew) copy2(alphaFile, alphaFileNew) copy2(sampleFile, sampleFileNew) # ----------------------------------------------------------- # # make it positive and do all over again # opPositive = OperationMakePositive(sgdeDist.grid) # alg = EstimateDensityAlgorithm(configFile) # opPositive.setInterpolationAlgorithm(alg) # grid, alpha = opPositive.makePositive(sgdeDist.alpha) # scale to unit integrand alpha.mult(1. / createOperationQuadrature(grid).doQuadrature(alpha)) sgdeDist.grid = grid sgdeDist.alpha = alpha gridFileNew = os.path.join(pathResults, "samples_%i_%i_l%i_positive.grid" % (iteration, n, level)) alphaFileNew = os.path.join(pathResults, "samples_%i_%i_l%i_positive.alpha.arff" % (iteration, n, level)) fd = open(gridFileNew, "w") fd.write(Grid.serialize(grid)) fd.close() writeAlphaARFF(alphaFileNew, alpha) # ----------------------------------------------------------- # collect statistics accGridSizes = np.append(accGridSizes, grid.getSize()) accLevels = np.append(accLevels, level) l2error = np.NAN kldivergence = np.NAN crossEntropy = sgdeDist.crossEntropy(testSamplesUnit) if dist is not None: l2error = dist.l2error(sgdeDist, testSamplesUnit, testSamplesProb) kldivergence = dist.klDivergence(sgdeDist, testSamplesUnit, testSamplesProb) accL2error = np.append(accL2error, l2error) accCrossEntropy = np.append(accCrossEntropy, crossEntropy) accKLDivergence = np.append(accKLDivergence, kldivergence) if dim == 2: # ----------------------------------------------------------- # do some plotting fig = plt.figure() plotSG2d(grid, alpha) plt.title("N=%i: vol=%g, kl=%g, log=%g, l2error=%g" % (grid.getSize(), doQuadrature(grid, alpha), kldivergence, crossEntropy, l2error)) fig.savefig(sgdePositiveJpegFile) plt.close(fig) # ----------------------------------------------------------- # select the best density available based on the given criterion if optimization == 'crossEntropy': measure = crossEntropy elif optimization == 'kldivergence': measure = kldivergence elif optimization == 'l2': measure = l2error else: raise AttributeError('optimization "%s" is not known for density estimation' % optimization) isBest = measure < bestMeasure if isBest: bestMeasure = measure if ans is None or isBest: ans = sgdeDist bestSetting = {'level': level, 'gridSize': grid.getSize(), 'l2error': l2error, 'KLDivergence': kldivergence, 'crossEntropy': crossEntropy} # ----------------------------------------------------------- # copy grid and coefficients gridFileNew = os.path.join(pathResults, "samples_%i_%i.grid" % (iteration, n)) alphaFileNew = os.path.join(pathResults, "samples_%i_%i.alpha.arff" % (iteration, n)) sampleFileNew = os.path.join(pathResults, "samples_%i_%i.csv" % (iteration, n)) copy2(gridFile, gridFileNew) copy2(alphaFile, alphaFileNew) copy2(sampleFile, sampleFileNew) gridFileNew = os.path.join(pathResults, "samples_%i_%i_positive.grid" % (iteration, n)) alphaFileNew = os.path.join(pathResults, "samples_%i_%i_positive.alpha.arff" % (iteration, n)) fd = open(gridFileNew, "w") fd.write(Grid.serialize(ans.grid)) fd.close() writeAlphaARFF(alphaFileNew, ans.alpha) # ----------------------------------------------------------- print ": %s = %g <= %g" % (optimization, measure, bestMeasure) print # ----------------------------------------------------------- # write results to file statsfilename = os.path.join(pathResults, "sg_sgde_%i_%i_all.stats.arff" % (iteration, n)) writeDataARFF({'filename': statsfilename, 'data': DataMatrix(np.vstack(([n] * len(accGridSizes), accGridSizes, accLevels, accL2error, accKLDivergence, accCrossEntropy)).transpose()), 'names': ['sampleSize', 'gridSize', 'level', 'l2error', 'KLDivergence', 'crossEntropy']}) # ----------------------------------------------------------- statsfilename = os.path.join(pathResults, "sg_sgde_%i_%i.stats.arff" % (iteration, n)) writeDataARFF({'filename': statsfilename, 'data': DataMatrix(np.vstack(([n], bestSetting['gridSize'], bestSetting['level'], bestSetting['l2error'], bestSetting['KLDivergence'], bestSetting['crossEntropy'])).transpose()), 'names': ['sampleSize', 'gridSize', 'level', 'l2error', 'KLDivergence', 'crossEntropy']}) # ----------------------------------------------------------- return ans
# create regular grid, level 3 level = 3 gridGen = grid.createGridGenerator() gridGen.regular(level) print "number of grid points: {}".format(gridStorage.size()) # create coefficient vector alpha = DataVector(gridStorage.size()) for i in xrange(gridStorage.size()): gp = gridStorage.get(i) alpha[i] = f((gp.getCoord(0), gp.getCoord(1))) createOperationHierarchisation(grid).doHierarchisation(alpha) # direct quadrature opQ = createOperationQuadrature(grid) res = opQ.doQuadrature(alpha) print "exact integral value: {}".format(res) # Monte Carlo quadrature using 100000 paths opMC = OperationQuadratureMC(grid, 100000) res = opMC.doQuadrature(alpha) print "Monte Carlo value: {:.6f}".format(res) res = opMC.doQuadrature(alpha) print "Monte Carlo value: {:.6f}".format(res) # Monte Carlo quadrature of a function res = opMC.doQuadratureFunc(f) print "MC value (f): {:.6f}".format(res) # Monte Carlo quadrature of error
def computeScale(self): # normalize density self.scale = createOperationQuadrature(self.grid).doQuadrature(self.alpha)
## grid points to obtain the nodal values. Then we use ## hierarchization to obtain the surplus value. # create coefficient vector alpha = pysgpp.DataVector(gridStorage.getSize()) for i in range(gridStorage.getSize()): gp = gridStorage.getPoint(i) p = tuple([gp.getStandardCoordinate(j) for j in range(dim)]) alpha[i] = f(p) pysgpp.createOperationHierarchisation(grid).doHierarchisation(alpha) ## Now we compute and compare the quadrature using four different methods available in SG++. # direct quadrature opQ = pysgpp.createOperationQuadrature(grid) res = opQ.doQuadrature(alpha) print("exact integral value: {}".format(res)) # Monte Carlo quadrature using 100000 paths opMC = pysgpp.OperationQuadratureMC(grid, 100000) res = opMC.doQuadrature(alpha) print("Monte Carlo value: {:.6f}".format(res)) res = opMC.doQuadrature(alpha) print("Monte Carlo value: {:.6f}".format(res)) # Monte Carlo quadrature of a standard parabola res = opMC.doQuadratureFunc(f) print("MC value (f): {:.6f}".format(res)) # Monte Carlo quadrature of error
def estimateDensitySGDE(trainSamplesUnit, testSamplesUnit=None, testSamplesProb=None, pathResults="/tmp", dist=None, optimization='l2', iteration=0, levels=[1, 2, 3, 4, 5], refNr=0, refPoints=0, nSamples=1000): """ Estimates a sparse grid density for different levels and refinements by optimizing over a given quantity. @param trainSamplesUnit: @param testSamplesUnit: @param testSamplesProb: @param pathResults: @param dist: @param optimization: @param iteration: @param levels: @param refNr: @param refPoints: """ config = """ [general] method = dmest [files] inFileTrain = %s usingTrain = %s inFileTest = %s outFileTest = %s usingTest = %s [dmest] gridFile = %s lambda = -1 # 0.01 regType=Laplace refNr = %i refPoints = %i writeGridFile = %s writeAlphaFile = %s samp_rejectionTrialMax = 5000 samp_numSamples = %i samp_outFile = %s printSurfaceFile = %s """ # write the samples to file if len(trainSamplesUnit.shape) == 1: n, dim = trainSamplesUnit.shape[0], 1 usingTrainTag = "%i" % dim else: n, dim = trainSamplesUnit.shape usingTrainTag = "1:%i" % dim trainSamplesUnitFile = os.path.join( pathResults, "samples_%i_%i_train.csv" % (iteration, n)) np.savetxt(trainSamplesUnitFile, trainSamplesUnit) testSamplesUnitFile = "" usingTestTag = "" if testSamplesUnit is not None: testSamplesUnitFile = os.path.join( pathResults, "samples_%i_%i_test.csv" % (iteration, n)) if dim == 1: usingTestTag = "%i" % dim else: usingTestTag = "1:%i" % dim np.savetxt(testSamplesUnitFile, testSamplesUnit) # collector arrays accGridSizes = np.array([]) accLevels = np.array([]) accL2error = np.array([]) accCrossEntropy = np.array([]) accKLDivergence = np.array([]) # best estimation ans = None bestMeasure = 1e20 bestSetting = None for level in levels: # define output files gridFile = os.path.join( pathResults, "samples_%i_%i_l%i.grid" % (iteration, n, level)) alphaFile = os.path.join( pathResults, "samples_%i_%i_l%i.alpha.arff" % (iteration, n, level)) sampleFile = os.path.join( pathResults, "samples_%i_%i_l%i.csv" % (iteration, n, level)) likelihoodFile = "" if testSamplesUnit is not None: likelihoodFile = os.path.join( pathResults, "samples_%i_%i_l%i_likelihood.csv" % (iteration, n, level)) surfaceFile = "" if dim == 2: surfaceFile = os.path.join( pathResults, "samples_%i_%i_l%i.xyz" % (iteration, n, level)) gnuplotJpegFile = os.path.join( pathResults, "samples_%i_%i_l%i_gnuplot.jpg" % (iteration, n, level)) sgdeJpegFile = os.path.join( pathResults, "samples_%i_%i_l%i_sgde.jpg" % (iteration, n, level)) sgdePositiveJpegFile = os.path.join( pathResults, "samples_%i_%i_l%i_sgdePositive.jpg" % (iteration, n, level)) configFile = os.path.join(pathResults, "sgde_%i_%i_l%i.cfg" % (iteration, n, level)) gnuplotConfig = os.path.join( pathResults, "sgde_%i_%i_l%i.gnuplot" % (iteration, n, level)) # generate the grid grid = Grid.createLinearBoundaryGrid(dim) grid.createGridGenerator().regular(level) if grid.getSize() <= n: print " l=%i" % level, fd = open(gridFile, "w") fd.write(grid.serialize()) fd.close() # write config to file fd = open(configFile, "w") fd.write(config % (trainSamplesUnitFile, usingTrainTag, testSamplesUnitFile, likelihoodFile, usingTestTag, gridFile, refNr, refPoints, gridFile, alphaFile, nSamples, sampleFile, surfaceFile)) fd.close() sgdeDist = SGDEdist.byConfig(configFile) grid, alpha = sgdeDist.grid, sgdeDist.alpha # ----------------------------------------------------------- # do some plotting if dim == 2: # gnuplot sgdeDist.gnuplot(gnuplotJpegFile, gnuplotConfig=gnuplotConfig) # ----------------------------------------------------------- # matplotlib l2error = np.NAN kldivergence = np.NAN crossEntropy = sgdeDist.crossEntropy(testSamplesUnit) if dist is not None: l2error = dist.l2error(sgdeDist, testSamplesUnit, testSamplesProb) kldivergence = dist.klDivergence(sgdeDist, testSamplesUnit, testSamplesProb) fig = plt.figure() plotSG2d(grid, alpha) plt.title("N=%i: vol=%g, kl=%g, log=%g, l2error=%g" % (grid.getSize(), doQuadrature(grid, alpha), kldivergence, crossEntropy, l2error)) fig.savefig(sgdeJpegFile) plt.close(fig) # ----------------------------------------------------------- # copy grid and coefficients gridFileNew = os.path.join( pathResults, "samples_%i_%i_sgde.grid" % (iteration, n)) alphaFileNew = os.path.join( pathResults, "samples_%i_%i_sgde.alpha.arff" % (iteration, n)) sampleFileNew = os.path.join( pathResults, "samples_%i_%i_sgde.csv" % (iteration, n)) copy2(gridFile, gridFileNew) copy2(alphaFile, alphaFileNew) copy2(sampleFile, sampleFileNew) # ----------------------------------------------------------- # # make it positive and do all over again # opPositive = OperationMakePositive(sgdeDist.grid) # alg = EstimateDensityAlgorithm(configFile) # opPositive.setInterpolationAlgorithm(alg) # grid, alpha = opPositive.makePositive(sgdeDist.alpha) # scale to unit integrand alpha.mult(1. / createOperationQuadrature(grid).doQuadrature(alpha)) sgdeDist.grid = grid sgdeDist.alpha = alpha gridFileNew = os.path.join( pathResults, "samples_%i_%i_l%i_positive.grid" % (iteration, n, level)) alphaFileNew = os.path.join( pathResults, "samples_%i_%i_l%i_positive.alpha.arff" % (iteration, n, level)) fd = open(gridFileNew, "w") fd.write(Grid.serialize(grid)) fd.close() writeAlphaARFF(alphaFileNew, alpha) # ----------------------------------------------------------- # collect statistics accGridSizes = np.append(accGridSizes, grid.getSize()) accLevels = np.append(accLevels, level) l2error = np.NAN kldivergence = np.NAN crossEntropy = sgdeDist.crossEntropy(testSamplesUnit) if dist is not None: l2error = dist.l2error(sgdeDist, testSamplesUnit, testSamplesProb) kldivergence = dist.klDivergence(sgdeDist, testSamplesUnit, testSamplesProb) accL2error = np.append(accL2error, l2error) accCrossEntropy = np.append(accCrossEntropy, crossEntropy) accKLDivergence = np.append(accKLDivergence, kldivergence) if dim == 2: # ----------------------------------------------------------- # do some plotting fig = plt.figure() plotSG2d(grid, alpha) plt.title("N=%i: vol=%g, kl=%g, log=%g, l2error=%g" % (grid.getSize(), doQuadrature(grid, alpha), kldivergence, crossEntropy, l2error)) fig.savefig(sgdePositiveJpegFile) plt.close(fig) # ----------------------------------------------------------- # select the best density available based on the given criterion if optimization == 'crossEntropy': measure = crossEntropy elif optimization == 'kldivergence': measure = kldivergence elif optimization == 'l2': measure = l2error else: raise AttributeError( 'optimization "%s" is not known for density estimation' % optimization) isBest = measure < bestMeasure if isBest: bestMeasure = measure if ans is None or isBest: ans = sgdeDist bestSetting = { 'level': level, 'gridSize': grid.getSize(), 'l2error': l2error, 'KLDivergence': kldivergence, 'crossEntropy': crossEntropy } # ----------------------------------------------------------- # copy grid and coefficients gridFileNew = os.path.join( pathResults, "samples_%i_%i.grid" % (iteration, n)) alphaFileNew = os.path.join( pathResults, "samples_%i_%i.alpha.arff" % (iteration, n)) sampleFileNew = os.path.join( pathResults, "samples_%i_%i.csv" % (iteration, n)) copy2(gridFile, gridFileNew) copy2(alphaFile, alphaFileNew) copy2(sampleFile, sampleFileNew) gridFileNew = os.path.join( pathResults, "samples_%i_%i_positive.grid" % (iteration, n)) alphaFileNew = os.path.join( pathResults, "samples_%i_%i_positive.alpha.arff" % (iteration, n)) fd = open(gridFileNew, "w") fd.write(Grid.serialize(ans.grid)) fd.close() writeAlphaARFF(alphaFileNew, ans.alpha) # ----------------------------------------------------------- print ": %s = %g <= %g" % (optimization, measure, bestMeasure) print # ----------------------------------------------------------- # write results to file statsfilename = os.path.join( pathResults, "sg_sgde_%i_%i_all.stats.arff" % (iteration, n)) writeDataARFF({ 'filename': statsfilename, 'data': DataMatrix( np.vstack( ([n] * len(accGridSizes), accGridSizes, accLevels, accL2error, accKLDivergence, accCrossEntropy)).transpose()), 'names': [ 'sampleSize', 'gridSize', 'level', 'l2error', 'KLDivergence', 'crossEntropy' ] }) # ----------------------------------------------------------- statsfilename = os.path.join(pathResults, "sg_sgde_%i_%i.stats.arff" % (iteration, n)) writeDataARFF({ 'filename': statsfilename, 'data': DataMatrix( np.vstack(([n], bestSetting['gridSize'], bestSetting['level'], bestSetting['l2error'], bestSetting['KLDivergence'], bestSetting['crossEntropy'])).transpose()), 'names': [ 'sampleSize', 'gridSize', 'level', 'l2error', 'KLDivergence', 'crossEntropy' ] }) # ----------------------------------------------------------- return ans
# create regular grid, level 3 level = 3 gridGen = grid.createGridGenerator() gridGen.regular(level) print "number of grid points: {}".format(gridStorage.size()) # create coefficient vector alpha = DataVector(gridStorage.size()) for i in xrange(gridStorage.size()): gp = gridStorage.get(i) alpha[i] = f((gp.getCoord(0), gp.getCoord(1))) createOperationHierarchisation(grid).doHierarchisation(alpha) # direct quadrature opQ = createOperationQuadrature(grid) res = opQ.doQuadrature(alpha) print "exact integral value: {}".format(res) # Monte Carlo quadrature using 100000 paths opMC = OperationQuadratureMC(grid, 100000) res = opMC.doQuadrature(alpha) print "Monte Carlo value: {:.6f}".format(res) res = opMC.doQuadrature(alpha) print "Monte Carlo value: {:.6f}".format(res) # Monte Carlo quadrature of a function res = opMC.doQuadratureFunc(f) print "MC value (f): {:.6f}".format(res) # Monte Carlo quadrature of error
# create regular grid gridGen = grid.createGridGenerator() gridGen.regular(level) # create coefficient vector alpha = DataVector(gridStorage.size()) alpha.setAll(0.0) # set function values in alpha for i in range(gridStorage.size()): gp = gridStorage.get(i) p = [gp.getCoord(j) for j in range(gridStorage.dim())] if gridStorage.dim() == 1: p = p[0] alpha[i] = f(p) # hierarchize createOperationHierarchisation(grid).doHierarchisation(alpha) fRef = integrate(h, (symx, 0, 1)).evalf() cnt_fail = 0 for _ in range(20): f1 = createOperationQuadrature(grid).doQuadrature(alpha) if isnan(f1): cnt_fail += 1 assert cnt_fail == 0 assert (abs(fRef - f1) / fRef) < 1e-7
def computeScale(self): # normalize density self.scale = createOperationQuadrature(self.grid).doQuadrature( self.alpha)