code: def _continuation_value_fn(
      cashflow, discount_factors, exercise_index
  ):
      """Returns the discounted value of the right hand part of the cashflow tensor.
  
      Args:
        cashflow: A real `Tensor` of shape
          `[num_samples, batch_size, num_exercise]`. Tracks the optimal cashflow of
           each sample path for each payoff dimension at each exercise time.
        discount_factors: A `Tensor` of shape `[num_exercise_times]` or of rank 3
          and compatible with `[num_samples, batch_size, num_exercise_times]`.
          The `dtype` should be the same as of `samples`
        exercise_index: An integer scalar `Tensor` representing the index of the
          exercise time of interest. Should be less than `num_exercise_times`.
  
      Returns:
        A `[num_samples, batch_size]` `Tensor` whose entries represent the sum of
        those elements to the right of `exercise_index` in `cashflow`, discounted to
        the time indexed by `exercise_index`. When `exercise_index` is zero, the
        return represents the sum of the cashflow discounted to present value for
        each sample path.
      """
      _, _, num_cashflow = cashflow.shape.as_list()
      disc_factors_are_used = (
          tf.range(num_cashflow + 1) >= exercise_index + 1
      )
      discount_factors_slice = tf.transpose(
          tf.transpose(discount_factors)[exercise_index]
      )
      total_discount_factors = tf.where(
          disc_factors_are_used, discount_factors, 0
      ) / tf.expand_dims(discount_factors_slice, axis=-1)
      cashflows_are_used = tf.range(num_cashflow) >= exercise_index
      cashflow_masked = tf.where(cashflows_are_used, cashflow, 0)
      total_discount_factors_slice = tf.transpose(
          tf.transpose(total_discount_factors)[1:]
      )
      return tf.math.reduce_sum(
          cashflow_masked * total_discount_factors_slice, axis=2
      )
  
file_path: tf-quant-finance/tf_quant_finance/experimental/lsm_algorithm/lsm_v2.py
imports: import collections
  import tensorflow.compat.v2
label_desc: Use image node_id 4 for calling a global function with example usage: _continuation_value_fn(cashflow, discount_factors, exercise_index) and returns: tf