def checkPositivity(grid, alpha): # define a full grid of maxlevel of the grid gs = grid.getStorage() fullGrid = Grid.createLinearGrid(gs.dim()) fullGrid.createGridGenerator().full(gs.getMaxLevel()) fullHashGridStorage = fullGrid.getStorage() A = DataMatrix(fullHashGridStorage.size(), fullHashGridStorage.dim()) p = DataVector(gs.dim()) for i in xrange(fullHashGridStorage.size()): fullHashGridStorage.get(i).getCoords(p) A.setRow(i, p) res = evalSGFunctionMulti(grid, alpha, A) ymin, ymax, cnt = 0, -1e10, 0 for i, yi in enumerate(res.array()): if yi < 0. and abs(yi) > 1e-13: cnt += 1 ymin = min(ymin, yi) ymax = max(ymax, yi) A.getRow(i, p) print " %s = %g" % (p, yi) if cnt > 0: print "warning: function is not positive" print "%i/%i: [%g, %g]" % (cnt, fullHashGridStorage.size(), ymin, ymax) return cnt == 0
def testSave(self): filename = pathlocal + '/datasets/saving.arff.gz' testPoints = [[0.307143, 0.130137, 0.050000], [0.365584, 0.105479, 0.050000], [0.178571, 0.201027, 0.050000], [0.272078, 0.145548, 0.050000], [0.318831, 0.065411, 0.050000], [0.190260, 0.086986, 0.050000], [0.190260, 0.062329, 0.072500], [0.120130, 0.068493, 0.072500], [0.225325, 0.056164, 0.072500], [0.213636, 0.050000, 0.072500]] testValues = [ -1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, -1.000000, -1.000000, -1.000000, -1.000000 ] attributes = { "x0": "NUMERIC", "x1": "NUMERIC", "x2": "NUMERIC", "class": "NUMERIC", } size = len(testPoints) dim = len(testPoints[0]) point = DataVector(dim) points = DataMatrix(size, dim) for row in xrange(size): for col in xrange(dim): point[col] = testPoints[row][col] points.setRow(row, point) adapter = ARFFAdapter(filename) adapter.save(points, testValues, attributes) (points, values) = adapter.loadData().getPointsValues() size = len(testPoints) dim = len(testPoints[0]) testVector = DataVector(dim) for rowIdx in xrange(size): points.getRow(rowIdx, testVector) for colIdx in xrange(dim): if cvar.USING_DOUBLE_PRECISION: self.assertEqual(testVector[colIdx], testPoints[rowIdx][colIdx]) else: self.assertAlmostEqual(testVector[colIdx], testPoints[rowIdx][colIdx]) self.assertEqual(values[rowIdx], testValues[rowIdx]) os.remove(filename)
def testSave(self): filename = pathlocal + '/datasets/saving.arff.gz' testPoints = [[0.307143,0.130137,0.050000], [0.365584,0.105479,0.050000], [0.178571,0.201027,0.050000], [0.272078,0.145548,0.050000], [0.318831,0.065411,0.050000], [0.190260,0.086986,0.050000], [0.190260,0.062329,0.072500], [0.120130,0.068493,0.072500], [0.225325,0.056164,0.072500], [0.213636,0.050000,0.072500] ] testValues = [-1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, -1.000000, -1.000000, -1.000000, -1.000000] attributes = { "x0":"NUMERIC", "x1":"NUMERIC", "x2":"NUMERIC", "class":"NUMERIC", } size = len(testPoints) dim = len(testPoints[0]) point = DataVector(dim) points = DataMatrix(size, dim) for row in xrange(size): for col in xrange(dim): point[col] = testPoints[row][col] points.setRow(row, point) adapter = ARFFAdapter(filename) adapter.save(points, testValues, attributes) (points, values) = adapter.loadData().getPointsValues() size = len(testPoints) dim = len(testPoints[0]) testVector = DataVector(dim) for rowIdx in xrange(size): points.getRow(rowIdx, testVector) for colIdx in xrange(dim): if cvar.USING_DOUBLE_PRECISION: self.assertEqual(testVector[colIdx], testPoints[rowIdx][colIdx]) else: self.assertAlmostEqual(testVector[colIdx], testPoints[rowIdx][colIdx]) self.assertEqual(values[rowIdx], testValues[rowIdx]) os.remove(filename)
def computeErrors(jgrid, jalpha, grid1, alpha1, grid2, alpha2, n=200): """ Compute some errors to estimate the quality of the interpolation. @param jgrid: Grid, new discretization @param jalpha: DataVector, new surpluses @param grid1: Grid, old discretization @param alpha1: DataVector, old surpluses @param grid2: Grid, old discretization @param alpha2: DataVector, old surpluses @return: tuple(<float>, <float>), maxdrift, l2norm """ jgs = jgrid.getStorage() # create control samples samples = DataMatrix(np.random.rand(n, jgs.getDimension())) # evaluate the sparse grid functions jnodalValues = evalSGFunctionMulti(jgrid, jalpha, samples) # eval grids nodalValues1 = evalSGFunctionMulti(grid1, alpha1, samples) nodalValues2 = evalSGFunctionMulti(grid2, alpha2, samples) # compute errors p = DataVector(jgs.getDimension()) err = DataVector(n) for i in range(n): samples.getRow(i, p) y = nodalValues1[i] * nodalValues2[i] if abs(jnodalValues[i]) > 1e100: err[i] = 0.0 else: err[i] = abs(y - jnodalValues[i]) # get error statistics # l2 l2norm = err.l2Norm() # maxdrift err.abs() maxdrift = err.max() return maxdrift, l2norm
def computeErrors(jgrid, jalpha, grid1, alpha1, grid2, alpha2, n=200): """ Compute some errors to estimate the quality of the interpolation. @param jgrid: Grid, new discretization @param jalpha: DataVector, new surpluses @param grid1: Grid, old discretization @param alpha1: DataVector, old surpluses @param grid2: Grid, old discretization @param alpha2: DataVector, old surpluses @return: tuple(<float>, <float>), maxdrift, l2norm """ jgs = jgrid.getStorage() # create control samples samples = DataMatrix(np.random.rand(n, jgs.dim())) # evaluate the sparse grid functions jnodalValues = evalSGFunctionMulti(jgrid, jalpha, samples) # eval grids nodalValues1 = evalSGFunctionMulti(grid1, alpha1, samples) nodalValues2 = evalSGFunctionMulti(grid2, alpha2, samples) # compute errors p = DataVector(jgs.dim()) err = DataVector(n) for i in xrange(n): samples.getRow(i, p) y = nodalValues1[i] * nodalValues2[i] if abs(jnodalValues[i]) > 1e100: err[i] = 0.0 else: err[i] = abs(y - jnodalValues[i]) # get error statistics # l2 l2norm = err.l2Norm() # maxdrift err.abs() maxdrift = err.max() return maxdrift, l2norm
def computeErrors(jgrid, jalpha, grid, alpha, f, n=200): """ Compute some errors to estimate the quality of the interpolation. @param jgrid: Grid, new discretization @param jalpha: DataVector, new surpluses @param grid: Grid, old discretization @param alpha: DataVector, old surpluses @param f: function, to be interpolated @param n: int, number of Monte Carlo estimates for error estimation @return: tuple(<float>, <float>), maxdrift, l2norm """ jgs = jgrid.getStorage() # create control samples samples = DataMatrix(np.random.rand(n, jgs.dim())) # evaluate the sparse grid functions jnodalValues = evalSGFunctionMulti(jgrid, jalpha, samples) nodalValues = evalSGFunctionMulti(grid, alpha, samples) # compute errors p = DataVector(jgs.dim()) err = DataVector(n) for i in xrange(n): samples.getRow(i, p) y = f(p.array(), nodalValues[i]) err[i] = abs(y - jnodalValues[i]) # get error statistics # l2 l2norm = err.l2Norm() # maxdrift err.abs() maxdrift = err.max() return maxdrift, l2norm
def computeCoefficients(jgrid, grid, alpha, f): """ Interpolate function f, which depends on some sparse grid function (grid, alpha) on jgrid @param jgrid: Grid, new discretization @param grid: Grid, old discretization @param alpha: DataVector, surpluses for grid @param f: function, to be interpolated @return: DataVector, surpluses for jgrid """ jgs = jgrid.getStorage() # dehierarchization p = DataVector(jgs.dim()) A = DataMatrix(jgs.size(), jgs.dim()) for i in xrange(jgs.size()): jgs.get(i).getCoords(p) A.setRow(i, p) nodalValues = evalSGFunctionMulti(grid, alpha, A) # apply f to all grid points jnodalValues = DataVector(jgs.size()) for i in xrange(len(nodalValues)): A.getRow(i, p) # print i, p.array(), nodalValues[i], alpha.min(), alpha.max() # if nodalValues[i] < -1e20 or nodalValues[i] > 1e20: # from pysgpp.extensions.datadriven.uq.operations import evalSGFunction, evalSGFunctionMultiVectorized # print alpha.min(), alpha.max() # print evalSGFunction(grid, alpha, p) # print evalSGFunctionMulti(grid, alpha, DataMatrix([p.array()])) # print evalSGFunctionMultiVectorized(grid, alpha, DataMatrix([p.array()])) # import ipdb; ipdb.set_trace() jnodalValues[i] = f(p.array(), nodalValues[i]) jalpha = hierarchize(jgrid, jnodalValues) return jalpha
class TestWeightedRefinementOperator(unittest.TestCase): def setUp(self): # # Grid # DIM = 2 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.getGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1 / DELTA) for i in range(DELTA_RECI): for j in range(DELTA_RECI): xs.append([DELTA * i, DELTA * j]) random.seed(1208813) ys = [random.randint(-10, 10) for i in range(DELTA_RECI**2)] # print xs # print ys self.trainData = DataMatrix(xs) self.classes = DataVector(ys) self.alpha = DataVector([3, 6, 7, 9, -1]) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) opEval = createOperationEval(self.grid) for i in range(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__( i, self.classes[i] - opEval.eval(self.alpha, coord)) #print "Errors:" #print self.errors # # Functor # self.functor = WeightedErrorRefinementFunctor(self.alpha, self.grid) self.functor.setTrainDataset(self.trainData) self.functor.setClasses(self.classes) self.functor.setErrors(self.errors) def test_1(self): storage = self.grid.getStorage() coord = DataVector(storage.getDimension()) num_coeff = self.alpha.__len__() values = [ self.functor.__call__(storage, i) for i in range(storage.getSize()) ] expect = [] opEval = createOperationEval(self.grid) for i in range(num_coeff): # print i val = 0 single = DataVector(num_coeff) single.__setitem__(i, self.alpha.__getitem__(i)) for j in range(self.trainData.getNrows()): self.trainData.getRow(j, coord) val += abs( opEval.eval(single, coord) * (self.errors.__getitem__(j)**2)) expect.append(val) # print values # print expect # print [ values[i]/expect[i] for i in xrange(values.__len__())] self.assertEqual(values, expect)
class TestPersistentRefinementOperator(unittest.TestCase): def setUp(self): # # Grid # self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1 / DELTA) for i in xrange(DELTA_RECI): for j in xrange(DELTA_RECI): xs.append([DELTA * i, DELTA * j]) random.seed(1208813) ys = [random.randint(-10, 10) for i in xrange(DELTA_RECI**2)] # print xs # print ys self.trainData = DataMatrix(xs) self.classes = DataVector(ys) self.alpha = DataVector([3, 6, 7, 9, -1]) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) for i in xrange(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__( i, self.classes[i] - self.grid.eval(self.alpha, coord)) # # Functor # self.functor = PersistentErrorRefinementFunctor(self.alpha, self.grid) self.functor.setTrainDataset(self.trainData) self.functor.setClasses(self.classes) self.functor.setErrors(self.errors) self.accum = DataVector(self.alpha.__len__()) self.accum.setAll(0.0) def test_1(self): storage = self.grid.getStorage() coord = DataVector(storage.dim()) num_coeff = self.alpha.__len__() # # First part # values = [ self.functor.__call__(storage, i) for i in xrange(storage.size()) ] expect = [] for j in xrange(num_coeff): row = DataVector(DIM) tmp_alpha = DataVector(self.alpha.__len__()) tmp_alpha.setAll(0.0) tmp_alpha.__setitem__(j, 1.0) current = 0 for i in xrange(self.trainData.getNrows()): self.trainData.getRow(i, row) current += (self.errors.__getitem__(i) * self.grid.eval(tmp_alpha, row))**2 self.accum.__setitem__( j, self.accum.__getitem__(j) * (1 - BETA) + BETA * current * abs(self.alpha.__getitem__(j))) expect.append(self.accum.__getitem__(j)) self.assertEqual(values, expect) # # Second part # values = [ self.functor.__call__(storage, i) for i in xrange(storage.size()) ] expect = [] for j in xrange(num_coeff): row = DataVector(DIM) tmp_alpha = DataVector(self.alpha.__len__()) tmp_alpha.setAll(0.0) tmp_alpha.__setitem__(j, 1.0) current = 0 for i in xrange(self.trainData.getNrows()): self.trainData.getRow(i, row) current += (self.errors.__getitem__(i) * self.grid.eval(tmp_alpha, row))**2 self.accum.__setitem__( j, self.accum.__getitem__(j) * (1 - BETA) + BETA * current * abs(self.alpha.__getitem__(j))) expect.append(self.accum.__getitem__(j)) self.assertEqual(values, expect)
class TestOnlinePredictiveRefinementDimension(unittest.TestCase): def setUp(self): # # Grid # DIM = 2 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.getGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1 / DELTA) for i in range(DELTA_RECI): for j in range(DELTA_RECI): xs.append([DELTA * i, DELTA * j]) random.seed(1208813) ys = [random.randint(-10, 10) for i in range(DELTA_RECI**2)] self.trainData = DataMatrix(xs) self.classes = DataVector(ys) self.alpha = DataVector([3, 6, 7, 9, -1]) self.multEval = createOperationMultipleEval(self.grid, self.trainData) opEval = createOperationEval(self.grid) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) for i in range(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__( i, abs(self.classes[i] - opEval.eval(self.alpha, coord))) # # OnlinePredictiveRefinementDimension # hash_refinement = HashRefinement() self.strategy = OnlinePredictiveRefinementDimension(hash_refinement) self.strategy.setTrainDataset(self.trainData) self.strategy.setClasses(self.classes) self.strategy.setErrors(self.errors) def test_1(self): storage = self.grid.getStorage() gridSize = self.grid.getSize() numDim = storage.getDimension() print("######") print("Expected result:") print("######") expected = {} for j in range(gridSize): HashGridPoint = storage.getPoint(j) HashGridPoint.setLeaf(False) print("Point: ", j, " (", HashGridPoint.toString(), ")") for d in range(numDim): # # Get left and right child # leftChild = HashGridPoint(HashGridPoint) rightChild = HashGridPoint(HashGridPoint) storage.left_child(leftChild, d) storage.right_child(rightChild, d) # # Check if point is refinable # if storage.isContaining(leftChild) or storage.isContaining( rightChild): continue # # Insert children temporarily # storage.insert(leftChild) storage.insert(rightChild) val1 = self.calc_indicator_value(leftChild) val2 = self.calc_indicator_value(rightChild) storage.deleteLast() storage.deleteLast() print("Dimension: ", d) print("Left Child: ", val1) print("Right Child: ", val2) print("") expected[(j, d)] = val1 + val2 print("") for k, v in list(expected.items()): print((k, v)) print("######") print("Actual result:") print("######") actual = refinement_map({}) self.strategy.collectRefinablePoints(storage, 10, actual) for k, v in list(actual.items()): print((k, v)) # # Assertions # for k, v in list(expected.items()): self.assertEqual(actual[k], v) def calc_indicator_value(self, index): numData = self.trainData.getNrows() numCoeff = self.grid.getSize() seq = self.grid.getStorage().seq(index) num = 0 denom = 0 tmp = DataVector(numCoeff) self.multEval.multTranspose(self.errors, tmp) num = tmp.__getitem__(seq) num **= 2 alpha = DataVector(numCoeff) col = DataVector(numData) alpha.__setitem__(seq, 1.0) self.multEval.mult(alpha, col) col.sqr() denom = col.sum() if denom == 0: print("Denominator is zero") value = 0 else: value = num / denom return value
class TestWeightedRefinementOperator(unittest.TestCase): def setUp(self): # # Grid # DIM = 2 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1/DELTA) for i in xrange(DELTA_RECI): for j in xrange(DELTA_RECI): xs.append([DELTA*i, DELTA*j]) random.seed(1208813) ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)] # print xs # print ys self.trainData = DataMatrix(xs) self.classes = DataVector(ys) self.alpha = DataVector([3, 6, 7, 9, -1]) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) for i in xrange(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__ (i, self.classes[i] - self.grid.eval(self.alpha, coord)) #print "Errors:" #print self.errors # # Functor # self.functor = WeightedErrorRefinementFunctor(self.alpha, self.grid) self.functor.setTrainDataset(self.trainData) self.functor.setClasses(self.classes) self.functor.setErrors(self.errors) def test_1(self): storage = self.grid.getStorage() coord = DataVector(storage.dim()) num_coeff = self.alpha.__len__() values = [self.functor.__call__(storage,i) for i in xrange(storage.size())] expect = [] for i in xrange(num_coeff): # print i val = 0 single = DataVector(num_coeff) single.__setitem__(i, self.alpha.__getitem__(i)) for j in xrange(self.trainData.getNrows()): self.trainData.getRow(j, coord) val += abs( self.grid.eval(single, coord) * (self.errors.__getitem__(j)**2) ) expect.append(val) # print values # print expect # print [ values[i]/expect[i] for i in xrange(values.__len__())] self.assertEqual(values, expect)
class TestOnlinePredictiveRefinementDimension(unittest.TestCase): def setUp(self): # # Grid # DIM = 2 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1/DELTA) for i in xrange(DELTA_RECI): for j in xrange(DELTA_RECI): xs.append([DELTA*i, DELTA*j]) random.seed(1208813) ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)] self.trainData = DataMatrix(xs) self.classes = DataVector(ys) self.alpha = DataVector([3, 6, 7, 9, -1]) self.multEval = createOperationMultipleEval(self.grid, self.trainData) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) for i in xrange(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__ (i, abs(self.classes[i] - self.grid.eval(self.alpha, coord))) # # OnlinePredictiveRefinementDimension # hash_refinement = HashRefinement(); self.strategy = OnlinePredictiveRefinementDimension(hash_refinement) self.strategy.setTrainDataset(self.trainData) self.strategy.setClasses(self.classes) self.strategy.setErrors(self.errors) def test_1(self): storage = self.grid.getStorage() gridSize = self.grid.getSize() numDim = storage.dim() print "######" print "Expected result:" print "######" expected = {} for j in xrange(gridSize): HashGridIndex = storage.get(j) HashGridIndex.setLeaf(False) print "Point: ", j, " (", HashGridIndex.toString(), ")" for d in xrange(numDim): # # Get left and right child # leftChild = HashGridIndex(HashGridIndex) rightChild = HashGridIndex(HashGridIndex) storage.left_child(leftChild, d) storage.right_child(rightChild, d) # # Check if point is refinable # if storage.has_key(leftChild) or storage.has_key(rightChild): continue # # Insert children temporarily # storage.insert(leftChild) storage.insert(rightChild) val1 = self.calc_indicator_value(leftChild) val2 = self.calc_indicator_value(rightChild) storage.deleteLast() storage.deleteLast() print "Dimension: ", d print "Left Child: ", val1 print "Right Child: ", val2 print "" expected[(j, d)] = val1 + val2 print "" for k, v in expected.iteritems(): print(k, v) print "######" print "Actual result:" print "######" actual = refinement_map({}) self.strategy.collectRefinablePoints(storage, 10, actual) for k, v in actual.iteritems(): print(k, v) # # Assertions # for k, v in expected.iteritems(): self.assertEqual(actual[k], v) def calc_indicator_value(self, index): numData = self.trainData.getNrows() numCoeff = self.grid.getSize() seq = self.grid.getStorage().seq(index) num = 0 denom = 0 tmp = DataVector(numCoeff) self.multEval.multTranspose(self.errors, tmp) num = tmp.__getitem__(seq) num **= 2 alpha = DataVector(numCoeff) col = DataVector(numData) alpha.__setitem__(seq, 1.0) self.multEval.mult(alpha, col) col.sqr() denom = col.sum() if denom == 0: print "Denominator is zero" value = 0 else: value = num/denom return value