code: def _lsm_loop_body(
      sample_paths,
      exercise_times,
      discount_factors,
      payoff_fn,
      basis_fn,
      num_times,
      exercise_index,
      cashflow,
  ):
      """Finds the optimal exercise point and updates `cashflow`."""
  
      # Index of the sample path that the exercise index maps to.
      tick = exercise_times[exercise_index - 1]
      # Calculate the payoff of each path if exercised now.
      # Shape [num_samples, payoff_dim]
      exercise_value = payoff_fn(sample_paths, tick)
      # Present value of hanging on to the options (using future information).
      # Shape `[num_samples, payoff_dim]`
      continuation_value = continuation_value_fn(
          cashflow, discount_factors, exercise_index
      )
      # Create a design matrix for regression based on the sample paths.
      # Shape `[num_samples, basis_size]`
      design = basis_fn(sample_paths[:, tick, :])
      # Expected present value of hanging on the options.
      # Shape `[num_samples, payoff_dim]`
      expected_continuation = expected_exercise_fn(
          design, continuation_value, exercise_value
      )
      # Update the cashflow matrix to reflect where earlier exercise is optimal.
      # Shape `[num_samples, payoff_dim, num_exercise]`
      rev_cash = _updated_cashflow(
          num_times,
          exercise_index,
          exercise_value,
          expected_continuation,
          cashflow,
      )
      rev_cash.set_shape(cashflow.shape)
      return LsmLoopVars(
          exercise_index=exercise_index - 1, cashflow=rev_cash
      )
  
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 7 for calling a global function with example usage: _lsm_loop_body(sample_paths, exercise_times, discount_factors, payoff_fn, basis_fn, num_times, exercise_index, cashflow) and returns: LsmLoopVars