SG++-Doxygen-Documentation

We compute the sparse grid interpolant of the function $$f(x) = \sin(10x_0)+x_1.$$ We perform dimension-adaptive refinement of the sparse grid model, which means we add a complete hierarchical subspace in some dimensions.

For details on dimension-adaptive refinement see

 Hegland, M. Adaptive sparse grids, ANZIAM Journal, 2003, 44, C335-C353

The example can be found in the file subspaceRefinement.py.

1 # import modules
2 import sys
3 import math
4
5 from pysgpp import *
6 import matplotlib.pyplot as plotter
7 from mpl_toolkits.mplot3d import Axes3D

We define the function $$f(x) = \sin(10x_0)+x_1$$ to interpolate.

1 f = lambda x0, x1: math.sin(x0*10)+x1
2

create a two-dimensional piecewise bi-linear grid

1 dim = 2
2 grid = Grid.createLinearGrid(dim)
3 HashGridStorage = grid.getStorage()
4 print("dimensionality: {}".format(dim))
5
6 # create regular grid, level 3
7 level = 3
8 gridGen = grid.getGenerator()
9 gridGen.regular(level)
10 print("number of initial grid points: {}".format(HashGridStorage.getSize()))
11
12
13 # create coefficient vectors
14 alpha = DataVector(HashGridStorage.getSize())
15 print("length of alpha vector: {}".format(alpha.getSize()))
16

To create a dataset we use points on a regular 2d grid with a step size of 1 / rows and 1 / cols.

1 rows = 100
2 cols = 100
3
4 dataSet = DataMatrix(rows*cols,dim)
5 vals = DataVector(rows*cols)
6
7 for i in range(rows):
8  for j in range(cols):
9  #xcoord
10  dataSet.set(i*cols+j,0,i*1.0/rows)
11  #ycoord
12  dataSet.set(i*cols+j,1,j*1.0/cols)
13  vals[i*cols+j] = f(i*1.0/rows,j*1.0/cols)

We refine adaptively 20 times. In every step we recompute the vector of surpluses alpha, the vector with squared errors on the dataset errorVector, and then call the refinement routines.

1 # create coefficient vectors
2 alpha = DataVector(HashGridStorage.getSize())
3 print("length of alpha vector: {}".format(alpha.getSize()))
4
5 # now refine adaptively 2 times
6
7 for refnum in range(2):

Step 1: calculate the surplus vector alpha. In data mining with do it by solving a regression problem as shown in example Classification Example. Here, the function can be evaluated at any point. Hence. we simply evaluate it at the coordinates of the grid points to obtain the nodal values. Then we use hierarchization to obtain the surplus value.

1  for i in range(HashGridStorage.getSize()):
2  gp = HashGridStorage.getPoint(i)
3  alpha[i] = f(gp.getStandardCoordinate(0), gp.getStandardCoordinate(1))
4
5  # hierarchize

Step 2: call refinement routines. PredictiveRefinement implements the decorator pattern and extends the functionality of ANOVAHashRefinement. PredictiveRefinement requires a special kind of refinement functor – PredictiveRefinementIndicator that can access the dataset and the error vector. The refinement itself if performed by calling .free_refine() same for normal refinement in ANOVAHashRefinement. ANOVAHashRefinement creates new grid points only in the dimensions where the parent has level greater 1.

1  #refinement stuff
2  refinement = HashRefinement()
3  decorator = SubspaceRefinement(refinement)
4  functor = SurplusRefinementFunctor(alpha,1)
5  decorator.free_refine(HashGridStorage,functor)
6
7  print("Refinement step %d, new grid size: %d" % (refnum+1, HashGridStorage.getSize()))
8
9
10  # extend alpha vector (new entries uninitialized)
11  alpha.resizeZero(HashGridStorage.getSize())

The output of the program should look like this

 dimensionality:                   2
number of initial grid points:    17
length of alpha vector:           17
length of alpha vector:           17
Refinement step 1, new grid size: 33
Refinement step 2, new grid size: 73