code: def expected_exercise_fn(design, continuation_value, exercise_value):
      """Returns the expected continuation value for each path.
  
      Args:
        design: A real `Tensor` of shape `[basis_size, num_samples]`.
        continuation_value: A `Tensor` of shape `[num_samples, payoff_dim]` and of
          the same dtype as `design`. The optimal value of the option conditional on
          not exercising now or earlier, taking future information into account.
        exercise_value: A `Tensor` of the same shape and dtype as
          `continuation_value`. Value of the option if exercised immideately at
          the current time
  
      Returns:
        A `Tensor` of the same shape and dtype as `continuation_value` whose
        `(n, v)`-th entry represents the expected continuation value of sample path
        `n` under the `v`-th payoff scheme.
      """
      # We wish to value each option under different payoffs, expressed through a
      # multidimensional payoff function. While the basis calculated from the sample
      # paths is the same for each payoff, the LSM algorithm requires us to fit a
      # regression model only on the in-the-money paths, which are payoff dependent,
      # hence we create multiple copies of the regression design (basis) matrix and
      # zero out rows for out of the money paths under each payoff.
      batch_design = tf.broadcast_to(
          tf.expand_dims(design, -1),
          design.shape + [continuation_value.shape[-1]],
      )
      mask = tf.cast(exercise_value > 0, design.dtype)
      # Zero out contributions from samples we'd never exercise at this point (i.e.,
      # these extra observations do not change the regression coefficients).
      masked = tf.transpose(batch_design * mask, perm=(2, 1, 0))
      # For design matrix X and response y, the coefficients beta of the best linear
      # unbiased estimate are contained in the equation X'X beta = X'y. Here `lhs`
      # is X'X and `rhs` is X'y, or rather a tensor of such left hand and right hand
      # sides, one for each payoff dimension.
      lhs = tf.matmul(masked, masked, transpose_a=True)
      # Use pseudo inverse for the regression matrix to ensure stability of the
      # algorithm.
      lhs_pinv = tf.linalg.pinv(lhs)
      rhs = tf.matmul(
          masked,
          tf.expand_dims(tf.transpose(continuation_value), -1),
          transpose_a=True,
      )
      beta = tf.linalg.matmul(lhs_pinv, rhs)
      continuation = tf.matmul(
          tf.transpose(batch_design, perm=(2, 1, 0)), beta
      )
      return tf.maximum(tf.transpose(tf.squeeze(continuation, -1)), 0.0)
  
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 5 for calling a global function with example usage: expected_exercise_fn(design, continuation_value, exercise_value) and returns: tf