Last updated: 2023-03-16.
tf_quant_finance.math.pde.steppers.douglas_adi.multidim_parabolic_equation_step#
Performs one step in time to solve a multidimensional PDE.
tf_quant_finance.math.pde.steppers.douglas_adi.multidim_parabolic_equation_step(
time, next_time, coord_grid, value_grid, boundary_conditions,
time_marching_scheme, second_order_coeff_fn=None, first_order_coeff_fn=None,
zeroth_order_coeff_fn=None, inner_second_order_coeff_fn=None,
inner_first_order_coeff_fn=None, dtype=None, name=None
)
Typically one doesn’t need to use this function directly, unless they have
a custom time marching scheme. A simple stepper function for multidimensional
PDEs can be found in douglas_adi.py.
The PDE is of the form
dV/dt + Sum[a_ij d2(A_ij V)/dx_i dx_j, 1 <= i, j <=n] +
Sum[b_i d(B_i V)/dx_i, 1 <= i <= n] + c V = 0.
from time t0 to time t1. The solver can go both forward and backward in
time. Here a_ij, A_ij, b_i, B_i and c are coefficients that may
depend on spatial variables x and time t.
Here V is the unknown function, V_{...} denotes partial derivatives
w.r.t. dimensions specified in curly brackets, i and j denote spatial
dimensions, r is the spatial radius-vector.
Args:#
time: Real scalarTensor. The time before the step.next_time: Real scalarTensor. The time after the step.coord_grid: List ofnrank 1 realTensors.nis the dimension of the domain. The i-thTensorhas shape either[d_i]orB + [d_i]whered_iis the size of the grid along axisiandBis a batch shape. The coordinates of the grid points. Corresponds to the spatial gridGabove.value_grid: RealTensorcontaining the function values at timetimewhich have to be evolved to timenext_time. The shape of theTensormust broadcast withB + [d_1, d_2, ..., d_n].Bis the batch dimensions (one or more), which allow multiple functions (with potentially different boundary/final conditions and PDE coefficients) to be evolved simultaneously.boundary_conditions: The boundary conditions. Only rectangular boundary conditions are supported. A list of tuples of sizen(space dimension of the PDE). The elements of the Tuple can be either a Python Callable orNonerepresenting the boundary conditions at the minimum and maximum values of the spatial variable indexed by the position in the list. E.g., forn=2, the length ofboundary_conditionsshould be 2,boundary_conditions[0][0]describes the boundary(y_min, x), andboundary_conditions[1][0]- the boundary(y, x_min).Nonevalues mean that the second order terms for that dimension on the boundary are assumed to be zero, i.e., ifboundary_conditions[k][0]is None, ‘dV/dt + Sum[a_ij d2(A_ij V)/dx_i dx_j, 1 <= i, j <=n, i!=k+1, j!=k+1] + Sum[b_i d(B_i V)/dx_i, 1 <= i <= n] + c V = 0.’ For notNonevalues, the boundary conditions are accepted in the formalpha(t, x) V + beta(t, x) V_n = gamma(t, x), whereV_nis the derivative with respect to the exterior normal to the boundary. Each callable receives the current timetand thecoord_gridat the current time, and should return a tuple ofalpha,beta, andgamma. Each can be a number, a zero-rankTensoror aTensorwhose shape is the grid shape with the corresponding dimension removed. For example, for a two-dimensional grid of shape(b, ny, nx), wherebis the batch size,boundary_conditions[0][i]withi = 0, 1should return a tuple of either numbers, zero-rank tensors or tensors of shape(b, nx). Similarly forboundary_conditions[1][i], except the tensor shape should be(b, ny).alphaandbetacan also beNonein case of Neumann and Dirichlet conditions, respectively. Default value:None. Unlike settingNoneto individual elements ofboundary_conditions, setting the entireboundary_conditionsobject toNonemeans Dirichlet conditions with zero value on all boundaries are applied.time_marching_scheme: A callable which represents the time marching scheme for solving the PDE equation. Ifu(t)is space-discretized vector of the solution of a PDE, a time marching scheme approximately solves the equationdu_inner/dt = A(t) u_inner(t) + A_mixed(t) u(t) + b(t)foru(t2)givenu(t1), or vice versa if going backwards in time. HereAis a banded matrix containing contributions from the current and neighboring points in space,A_mixedare contributions of mixed terms,bis an arbitrary vector (inhomogeneous term), andu_innerisuwith boundaries with Robin conditions trimmed. Multidimensional time marching schemes are usually based on the idea of ADI (alternating direction implicit) method: the time step is split into substeps, and in each substep only one dimension is treated “implicitly”, while all the others are treated “explicitly”. This way one has to solve only tridiagonal systems of equations, but not more complicated banded ones. A few examples of time marching schemes (Douglas, Craig-Sneyd, etc.) can be found in [1]. The callable consumes the following arguments by keyword:inner_value_grid: Grid of solution values at the current time of the same
dtypeasvalue_gridand shape ofvalue_grid[..., 1:-1].t1: Lesser of the two times defining the step.
t2: Greater of the two times defining the step.
equation_params_fn: A callable that takes a scalar
Tensorargument representing time and returns a tuple of two elements. The first one representsA. The length must be the number of dimensions (n_dims), and A[i] must have lengthn_dims - i.A[i][0]is a tridiagonal matrix representing influence of the neighboring points along the dimensioni. It is a tuple of superdiagonal, diagonal, and subdiagonal parts of the tridiagonal matrix. The shape of these tensors must be same as ofvalue_grid. superdiagonal[…, -1] and subdiagonal[…, 0] are ignored.A[i][j]withi < j < n_dimsare tuples of four Tensors with same shape asvalue_gridrepresenting the influence of four points placed diagonally from the given point in the plane of dimensionsiandj. Denotingk,lthe indices of a given grid point in the plane, the four Tensors represent contributions of points(k+1, l+1),(k+1, l-1),(k-1, l+1), and(k-1, l-1), in this order. The second element in the tuple is a list of contributions tob(t)associated with each dimension. E.g. ifb(t)comes from boundary conditions, then it is split correspondingly. Each element in the list is a Tensor with the shape ofvalue_grid. For example a 2D problem withvalue_grid.shape = (b, ny, nx), wherebis the batch size. The elementsAijare non-zero ifi = joriis a neighbor ofjin the x-y plane. Depict these non-zero elements on the grid as follows:
``` a_mm a_y- a_mp a_x- a_0 a_x+ a_pm a_y+ a_pp ``` The callable should return ``` ([[(a_y-, a_0y, a_y+), (a_pp, a_pm, a_mp, a_pp)], [None, (a_x-, a_0x, a_x+)]], [b_y, b_x]) ``` where `a_0x + a_0y = a_0` (the splitting is arbitrary). Note that there is no need to repeat the non-diagonal term `(a_pp, a_pm, a_mp, a_pp)` for the second time: it's replaced with `None`. All the elements `a_...` may be different for each point in the grid, so they are `Tensors` of shape `(B, ny, nx)`. `b_y` and `b_x` are also `Tensors` of that shape.
A callable that accepts a
Tensorof shapeinner_value_gridand appends boundaries according to the boundary conditions, i.e. transformsu_innertou.n_dims: A Python integer, the spatial dimension of the PDE.
has_default_lower_boundary: A Python list of booleans of length
n_dims. List indices enumerate the dimensions withTruevalues marking default lower boundary condition along corresponding dimensions, andFalsevalues indicating Robin boundary conditions.has_default_upper_boundary: Similar to has_default_lower_boundary, but for upper boundaries.
The callable should return a
Tensorof the same shape anddtypeasvalues_gridthat represents an approximate solution of the space-discretized PDE.second_order_coeff_fn: Callable returning the second order coefficienta_{ij}(t, r)evaluated at given timet. The callable accepts the following arguments:t: The time at which the coefficient should be evaluated.locations_grid: aTensorrepresenting a grid of locationsrat which the coefficient should be evaluated. Returns an objectAsuch thatA[i][j]is defined andA[i][j]=a_{ij}(r, t), where0 <= i < n_dimsandi <= j < n_dims. For example, the object may be a list of lists or a rank 2 Tensor. Only the elements withj >= iwill be used, and it is assumed thata_{ji} = a_{ij}, soA[i][j] withj < imay returnNone. EachA[i][j]should be a Number, aTensorbroadcastable to the shape of the grid represented bylocations_grid, orNoneif corresponding term is absent in the equation. Also, the callable itself may be None, meaning there are no second-order derivatives in the equation. For example, forn_dims=2, the callable may return either[[a_yy, a_xy], [a_xy, a_xx]]or[[a_yy, a_xy], [None, a_xx]]`.first_order_coeff_fn: Callable returning the first order coefficientsb_{i}(t, r)evaluated at given timet. The callable accepts the following arguments:t: The time at which the coefficient should be evaluated.locations_grid: aTensorrepresenting a grid of locationsrat which the coefficient should be evaluated. Returns a list or an 1DTensor,i-th element of which representsb_{i}(t, r). Each element should be a Number, aTensorbroadcastable to the shape of of the grid represented bylocations_grid, or None if corresponding term is absent in the equation. The callable itself may be None, meaning there are no first-order derivatives in the equation.zeroth_order_coeff_fn: Callable returning the zeroth order coefficientc(t, r)evaluated at given timet. The callable accepts the following arguments:t: The time at which the coefficient should be evaluated.locations_grid: aTensorrepresenting a grid of locationsrat which the coefficient should be evaluated. Should return a Number or aTensorbroadcastable to the shape of the grid represented bylocations_grid. May also return None or be None if the shift term is absent in the equation.inner_second_order_coeff_fn: Callable returning the coefficients under the second derivatives (i.e.A_ij(t, x)above) at given timet. The requirements are the same as forsecond_order_coeff_fn.inner_first_order_coeff_fn: Callable returning the coefficients under the first derivatives (i.e.B_i(t, x)above) at given timet. The requirements are the same as forfirst_order_coeff_fn.dtype: The dtype to use.name: The name to give to the ops. Default value: None which meansparabolic_equation_stepis used.
Returns:#
A sequence of two Tensors. The first one is a Tensor of the same
dtype and shape as coord_grid and represents a new coordinate grid
after one iteration. The second Tensor is of the same shape and dtype
asvalues_grid and represents an approximate solution of the equation after
one iteration.
References:#
[1] Tinne Haentjens, Karek J. in’t Hout. ADI finite difference schemes for the Heston-Hull-White PDE. https://arxiv.org/abs/1111.4087