def test2DNormalDist(self): # prepare data U = dists.J( [dists.Normal(2.0, .5, -1, 4), dists.Normal(1.0, .5, -1, 3)]) U = dists.J( [dists.Normal(0.5, .5, -1, 2), dists.Normal(0.5, .4, -1, 2)]) np.random.seed(1234567) trainSamples = U.rvs(300) testSamples = U.rvs(1000) # build parameter set dist = SGDEdist.byLearnerSGDEConfig( trainSamples, config={ "grid_level": 5, "grid_type": "modlinear", "refinement_numSteps": 0, "refinement_numPoints": 10, "regularization_type": "Laplace", "crossValidation_lambda": 0.000562341, "crossValidation_enable": False, "crossValidation_kfold": 5, "crossValidation_silent": False, "sgde_makePositive": False, "sgde_makePositive_candidateSearchAlgorithm": "joined", "sgde_makePositive_interpolationAlgorithm": "setToZero", "sgde_makePositive_generateConsistentGrid": False, "sgde_makePositive_verbose": True, "sgde_unitIntegrand": True }, bounds=U.getBounds()) fig = plt.figure() plotDensity2d(U) fig.show() fig = plt.figure() plotSG2d(dist.grid, dist.alpha, addContour=True, show_negative=True, show_grid_points=True) fig.show() print("2d: mean = %g ~ %g" % (U.mean(), dist.mean())) print("2d: var = %g ~ %g" % (U.var(), dist.var())) plt.show() print("KL = %g" % U.klDivergence(dist, testSamples, testSamples)) print("CE = %g" % dist.crossEntropy(testSamples)) print("MSE = %g" % dist.l2error(U, testSamples, testSamples))
def test_2DNormalDist_variance(self): # prepare data U = dists.J( [dists.Normal(2.0, .5, -1, 4), dists.Normal(1.0, .5, -1, 3)]) # U = dists.J([dists.Normal(0.5, .5, -1, 2), # dists.Normal(0.5, .4, -1, 2)]) # define linear transformation trans = JointTransformation() for a, b in U.getBounds(): trans.add(LinearTransformation(a, b)) # get a sparse grid approximation grid = Grid.createPolyGrid(U.getDim(), 10) grid.getGenerator().regular(5) gs = grid.getStorage() # now refine adaptively 5 times p = DataVector(gs.getDimension()) nodalValues = np.ndarray(gs.getSize()) # set function values in alpha for i in range(gs.getSize()): gs.getPoint(i).getStandardCoordinates(p) nodalValues[i] = U.pdf(trans.unitToProbabilistic(p.array())) # hierarchize alpha = hierarchize(grid, nodalValues) # # make positive # alpha_vec = DataVector(alpha) # createOperationMakePositive().makePositive(grid, alpha_vec) # alpha = alpha_vec.array() dist = SGDEdist(grid, alpha, bounds=U.getBounds()) fig = plt.figure() plotDensity2d(U) fig.show() fig = plt.figure() plotSG2d(dist.grid, dist.alpha, addContour=True, show_negative=True, show_grid_points=True) fig.show() print("2d: mean = %g ~ %g" % (U.mean(), dist.mean())) print("2d: var = %g ~ %g" % (U.var(), dist.var())) plt.show()
def plotDebug(self, grid, alpha, addedGridPoints, candidates): # ----------------------------------------------------------------- # plot result fig = plt.figure() plotSG2d(grid, alpha, show_negative=True, show_grid_points=True) plt.title("iteration = %i" % self.candidateSearchAlgorithm.iteration) p = DataVector(grid.getStorage().getDimension()) for gp in candidates: gp.getStandardCoordinates(p) plt.plot(p[0], p[1], "o ", color="green") for gp in addedGridPoints: gp.getStandardCoordinates(p) plt.plot(p[0], p[1], "o ", color="yellow") fig.show()
def testSettings(self): # set distributions of the input parameters builder = ParameterBuilder() up = builder.defineUncertainParameters() up.new().isCalled('x').withUniformDistribution(0, 1) up.new().isCalled('y').withUniformDistribution(0, 1) params = builder.andGetResult() def postprocessor(x, *args, **kws): return {'x': np.array([x[0], -x[0]]), 'time': list(range(len(x)))} f = lambda x, **kws: [x[0], 2 * x[0], 4 * x[0], 8 * x[0]] toi = [0, 1] # set up uq setting builder = ASGCUQManagerBuilder().withParameters(params)\ .withTypesOfKnowledge([KnowledgeTypes.SIMPLE, KnowledgeTypes.SQUARED])\ .withQoI("x")\ .withTimeStepsOfInterest(toi)\ .useInterpolation() builder.defineUQSetting().withSimulation(f)\ .withPostprocessor(postprocessor) builder.defineSampler().withGrid().withLevel(3)\ .hasType(GridType_Poly)\ .withDegree(2)\ .withBoundaryLevel(1) uqManager = builder.andGetResult() uqManager.runNextSamples() # define analysis uqAnalysis = ASGCAnalysisBuilder().withUQManager(uqManager)\ .withAnalyticEstimationStrategy()\ .andGetResult() # plot result ans = {} for t in uqManager.getTimeStepsOfInterest(): grid, alpha = uqManager.getKnowledge().getSparseGridFunction( t=t, qoi="x") fig = plt.figure() ans[t] = plotSG2d(grid, alpha, show_grid_points=True, show_numbers=True) fig.show() assert all(ans[0] == -ans[1]) plt.show()
level = 6 grid = Grid.createLinearGrid(2) grid.getGenerator().regular(level) gs = grid.getStorage() nodalValues = DataVector(grid.getSize()) p = DataVector(gs.getDimension()) for i in range(gs.getSize()): gs.getCoordinates(gs.getPoint(i), p) nodalValues[i] = dist.pdf(p.array()) alpha = hierarchize(grid, nodalValues) # plot the result fig = plt.figure() plotSG2d(grid, alpha) plt.title("plotSG: vol = %g" % (doQuadrature(grid, alpha))) fig.show() sgdeDist = SGDEdist(grid, alpha) fig = plt.figure() plotSGDE2d(sgdeDist) plt.title("plotSGDE: vol = %g" % (doQuadrature(grid, alpha))) fig.show() fig = plt.figure() plotDensity2d(sgdeDist) plt.title("plotDensity: vol = %g" % (doQuadrature(grid, alpha))) fig.show()
builder = builder.withStopPolicy().withAdaptiveIterationLimit(3) builder = builder.withCGSolver() builder.withAccuracy(1e-7) builder.withImax(100) # Create the final learner object learner = builder.andGetResult() gs = learner.grid.getStorage() print ("Dimensions: %i" % gs.getDimension()) print ("Grid points: %i" % gs.getSize()) print("================== Starting learning ==================") learner.setVerbosity(False) learner.learnData() print(learner.alpha) print("=======================================================") if numDims == 1: plt.scatter(samples[:, 0], values) plotSG1d(learner.grid, learner.alpha, color="red") else: plotSG2d(learner.grid, learner.alpha) plt.scatter(samples[:, 0], samples[:, 1]) plt.show()
def makePositive(self, alpha): """ insert recursively all grid points such that the function is positive defined. Interpolate the function values for the new grid points using the registered algorithm. @param alpha: numpy array hierarchical coefficients """ # make sure that the function is positive at every existing grid point grid = self.grid alpha = self.makeCurrentNodalValuesPositive(grid, alpha) # start adding points newGridPoints = [] iteration = 1 while True: # copy the old grid newGrid = copyGrid(grid) # add all those grid points which have an ancestor with negative # coefficient if self.verbose: print( "-" * 60 ) print( "%i:" % iteration ) print( "adding full grid points" ) addedGridPoints = self.addFullGridPoints(newGrid, alpha) if len(addedGridPoints) == 0: newAlpha = alpha break if self.verbose: print( "learning the new density" ) newGridPoints += addedGridPoints # set the function value at the new grid points to zero newNodalValues = dehierarchize(grid, alpha) newNodalValues = np.append(newNodalValues, newGrid.getSize() - len(newNodalValues)) newAlpha = np.append(alpha, np.zeros(newGrid.getSize() - len(alpha))) # compute now the hierarchical coefficients for the newly # added points newAlpha = self.algorithm.computeHierarchicalCoefficients(newGrid, newAlpha, addedGridPoints) # the function does not have to be positive now -> check it again if self.verbose: print( "force function to be positive" ) # check manually if all nodal points are positive newNodalValues = dehierarchize(newGrid, newAlpha) # collect all negative function values forceToBePositive = [] newGs = newGrid.getStorage() for i in range(newGs.getSize()): gp = newGs.getPoint(i) if newNodalValues[newGs.getSequenceNumber(gp)] < 0.: forceToBePositive.append(gp) if len(forceToBePositive) > 0: # if not, interpolate the function values for the negative # grid points if self.verbose: warnings.warn("manually forcing the the function to be positive") newAlpha = self.makeCurrentGridPositive(newGrid, newAlpha) # newAlpha = SetGridPointsToZero().computeHierarchicalCoefficients(grid, newAlpha, forceToBePositive) # newAlpha = InterpolateParents().computeHierarchicalCoefficients(grid, newAlpha, forceToBePositive) if newGrid.getSize() == grid.getSize(): break if self.verbose: fig = plt.figure() plotSG2d(newGrid, newAlpha, show_grid_points=True, show_negative=True) plt.title("iteration = %i" % iteration) fig.show() print( "%i + %i = %i for discretization" % (grid.getSize(), newGrid.getSize() - grid.getSize(), newGrid.getSize(),)) iteration += 1 grid, alpha = newGrid, newAlpha # coarsening: remove all new grid points with negative surplus coarsedGrid, coarsedAlpha, _ = self.coarsening(newGrid, newAlpha, newGridPoints) if self.verbose: print( "%i - %i = %i grid size after coarsening" % (newGrid.getSize(), newGrid.getSize() - coarsedGrid.getSize(), coarsedGrid.getSize(),)) # scale the result such that the integral over it is one # c = createOperationQuadrature(coarsedGrid).doQuadrature(coarsedAlpha) # coarsedAlpha.mult(1. / c) # security check for positiveness checkPositivity(coarsedGrid, coarsedAlpha) return coarsedGrid, coarsedAlpha
def makePositive(self, alpha): """ insert recursively all grid points such that the function is positive defined. Interpolate the function values for the new grid points using the registered algorithm. @param alpha: DataVector hierarchical coefficients """ # make sure that the function is positive at every existing grid point grid = self.grid alpha = self.makeCurrentGridPositive(grid, alpha) # start adding points newGridPoints = [] iteration = 1 while True: # copy the old grid newGrid = copyGrid(grid) # add all those grid points which have an ancestor with negative # coefficient if self.verbose: print "-" * 60 print "%i:" % iteration print "adding full grid points" addedGridPoints = self.addFullGridPoints(newGrid, alpha) if len(addedGridPoints) == 0: newAlpha = alpha break if self.verbose: print "learning the new density" newGridPoints += addedGridPoints # set the function value at the new grid points to zero newNodalValues = dehierarchize(grid, alpha) newNodalValues.resizeZero(newGrid.getSize()) newAlpha = DataVector(alpha) newAlpha.resizeZero(newGrid.getSize()) # compute now the hierarchical coefficients for the newly # added points newAlpha = self.algorithm.computeHierarchicalCoefficients(newGrid, newAlpha, addedGridPoints) # the function does not have to be positive now -> check it again if self.verbose: print "force function to be positive" # check manually if all nodal points are positive newNodalValues = dehierarchize(newGrid, newAlpha) # collect all negative function values forceToBePositive = [] newGs = newGrid.getStorage() for i in xrange(newGs.size()): gp = newGs.get(i) if newNodalValues[newGs.seq(gp)] < 0.: forceToBePositive.append(gp) if len(forceToBePositive) > 0: # if not, interpolate the function values for the negative # grid points if self.verbose: warnings.warn("manually forcing the the function to be positive") newAlpha = self.makeCurrentGridPositive(newGrid, newAlpha) # newAlpha = SetGridPointsToZero().computeHierarchicalCoefficients(grid, newAlpha, forceToBePositive) # newAlpha = InterpolateParents().computeHierarchicalCoefficients(grid, newAlpha, forceToBePositive) if newGrid.getSize() == grid.getSize(): break if self.verbose: fig = plt.figure() plotSG2d(newGrid, newAlpha) plt.title("iteration = %i" % iteration) fig.show() print "%i + %i = %i for discretization" % (grid.getSize(), newGrid.getSize() - grid.getSize(), newGrid.getSize(),) iteration += 1 grid, alpha = newGrid, newAlpha # coarsening: remove all new grid points with negative surplus coarsedGrid, coarsedAlpha, _ = self.coarsening(newGrid, newAlpha, newGridPoints) if self.verbose: print "%i - %i = %i grid size after coarsening" % (newGrid.getSize(), newGrid.getSize() - coarsedGrid.getSize(), coarsedGrid.getSize(),) # scale the result such that the integral over it is one # c = createOperationQuadrature(coarsedGrid).doQuadrature(coarsedAlpha) # coarsedAlpha.mult(1. / c) # security check for positiveness checkPositivity(coarsedGrid, coarsedAlpha) return coarsedGrid, coarsedAlpha
# np.sum([gp.getLevel(d) for d in xrange(numDims)]), # [gp.getIndex(d) for d in xrange(numDims)], # yi) sgdeDist = SGDEdist(grid, alpha, trainData=trainSamples, bounds=dist.getBounds()) print("-" * 80) print("(gs=%i) -> %g (%g, %g)" % (sgdeDist.grid.getSize(), dist.klDivergence(sgdeDist, testSamples), sgdeDist.crossEntropy(testSamples), sgdeDist.vol)) if numDims == 2 and plot: # plot the result # fig = plt.figure() # plotGrid2d(grid, alpha, show_numbers=False) # plt.title("pos: #gp = %i, kldivergence = %g, log = %g" % (grid.getStorage().getSize(), # dist.klDivergence(sgdeDist, testSamples), # dist.crossEntropy(testSamples))) fig.show() fig = plt.figure() plotSG2d(grid, sgdeDist.alpha, show_negative=True, show_grid_points=True) fig.show() fig, ax, _ = plotSG3d(grid, sgdeDist.alpha) # ax.set_title("positive") fig.show() plt.show()