There are many available methods to interpolate points when solving global models. dolo provides several of them. They can been used with exactly the same interface. By default, dolo uses smolyak interpolation.
We demonstrate the use of the `SmolyakGrid` object using the function \(x,y \rightarrow \exp \left( -x^2 - y^2 \right)\). We define it on a subset of \([-1,1]^2\) and use a smolyak product of chebychev polynomials to approximate it at other points.
Let define the state space \([-1,1]^2\) and the interpolation object.
from dolo.numeric.smolyak import SmolyakGrid
import numpy
bounds = numpy.array([
[ -1, -1 ], # lower bounds
[ 1, 1 ] # upper bounds
])
sg = SmolyakGrid(bounds, 3) # 3 is the smolyak parameter l
print(sg.grid.shape)
The points selected by the smolyak algorithm are accessible in `sg.grid` which is a `2x13` matrix. They can by plotted with:
sg.plot_grid()
Let define a two-variables function and evaluate it on the grid and we initialize the interpolator with these values.
fun = lambda x,y: numpy.exp( -numpy.power(x,2) - numpy.power(y,2) )
values_on_the_grid = fun( sg.grid[0,:], sg.grid[1,:] )
sg.set_values(values_on_the_grid)
Now we construct a random matrix `s` of size```2xN``` where each column is a point of the state space. On these states, we compute interpolated values and compare it with the true ones.
from numpy.random import multivariate_normal as mvtnorm
mean = numpy.zeros(2)
cov = numpy.eye(2)
s = mvtnorm(mean,cov,50).T
interpolated_values = sg.interpolate( s )
true_values = fun(s[0,:], s[1,:])
max_error = abs( true_values - interpolated_values ).max()
print( 'Maximum error : {}'.format(max_error) )