tf_quant_finance.models.longstaff_schwartz.make_polynomial_basis

Last updated: 2023-03-16.

tf_quant_finance.models.longstaff_schwartz.make_polynomial_basis#

View source

Produces a callable from samples to polynomial basis for use in regression.

tf_quant_finance.models.longstaff_schwartz.make_polynomial_basis(
    degree
)

The output callable accepts a scalar Tensor t and a Tensor X of shape [num_samples, dim], computes a centered value Y = X - mean(X, axis=0) and outputs a Tensor of shape [degree * dim, num_samples], where

Z[i*j, k] = X[k, j]**(degree - i) * X[k, j]**i, 0<=i<degree - 1, 0<=j<dim

For example, if degree and dim are both equal to 2, the polynomial basis is 1, X, X**2, Y, Y**2, X * Y, X**2 * Y, X * Y**2, where X and Y are the spatial axes.

Example#

basis = tff.experimental.lsm_algorithm.make_polynomial_basis_v2(2)
x = [[1.0], [2.0], [3.0], [4.0]]
x = tf.expand_dims(x, axis=-1)
basis(x, tf.constant(0, dtype=np.int32))
# Expected result:
[[ 1.  ,  1.  ,  1.  ,  1.  ], [-1.5 , -0.5 ,  0.5 ,  1.5 ],
[ 2.25,  0.25,  0.25,  2.25]]

Args:#

  • degree: An int32 scalar Tensor. The degree of the desired polynomial basis.

Returns:#

A callable from Tensors of shape [batch_size, num_samples, dim] to Tensors of shape [batch_size, (degree + 1)**dim, num_samples].