code: def make_polynomial_basis(degree):
      """Produces a callable from samples to polynomial basis for use in regression.
  
      The output callable accepts 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
      ```python
      basis = make_polynomial_basis(2)
      x = [1.0, 2.0, 3.0, 4.0]
      x = tf.expand_dims(x, -1)
      basis(x)
      # Expected result:
      [[ 1.0, 1.0, 1.0, 1.0], [-1.5, -0.5, 0.5, 1.5]]
      ```
  
      Args:
        degree: An `int32` scalar `Tensor`. The degree of the desired polynomial
          basis.
  
      Returns:
        A callable from `Tensor`s of shape `[num_samples, dim]` to `Tensor`s of
        shape `[degree * dim, num_samples]`.
  
      Raises:
        ValueError: If `degree` is less than `1`.
      """
      tf.debugging.assert_greater_equal(
          degree,
          0,
          message="Degree of polynomial basis can not be negative.",
      )
  
      def basis(sample_paths):
          """Computes polynomial basis expansion at the given sample points.
  
          Args:
            sample_paths: A `Tensor`s of either `flot32` or `float64` dtype and of
              shape `[num_samples, dim]` where `dim` has to be statically known.
  
          Returns:
            A `Tensor`s of shape `[degree * dim, num_samples]`.
          """
          samples = tf.convert_to_tensor(sample_paths)
          dim = samples.shape.as_list()[-1]
          grid = tf.range(0, degree + 1, dtype=samples.dtype)
  
          samples_centered = samples - tf.math.reduce_mean(
              samples, axis=0
          )
          samples_centered = tf.expand_dims(samples_centered, -2)
          grid = tf.meshgrid(*(dim * [grid]))
          grid = tf.reshape(tf.stack(grid, -1), [-1, dim])
          # Shape [num_samples, degree * dim]
          basis_expansion = tf.reduce_prod(samples_centered**grid, -1)
          return tf.transpose(basis_expansion)
  
      return basis
  
file_path: tf-quant-finance/tf_quant_finance/experimental/lsm_algorithm/lsm.py
imports: import collections
  import tensorflow.compat.v2
label_desc: Use image node_id 1 for calling a global function with example usage: make_polynomial_basis(degree) and returns: basis, tf