code: def swaption_price(
      *,
      volatilities: types.RealTensor,
      expiries: types.RealTensor,
      floating_leg_start_times: types.RealTensor,
      floating_leg_end_times: types.RealTensor,
      fixed_leg_payment_times: types.RealTensor,
      floating_leg_daycount_fractions: types.RealTensor,
      fixed_leg_daycount_fractions: types.RealTensor,
      fixed_leg_coupon: types.RealTensor,
      floating_leg_start_times_discount_factors: types.RealTensor,
      floating_leg_end_times_discount_factors: types.RealTensor,
      fixed_leg_payment_times_discount_factors: types.RealTensor,
      notional: types.RealTensor = None,
      is_payer_swaption: types.BoolTensor = None,
      is_normal_volatility: bool = True,
      dtype: tf.DType = None,
      name: str = None,
  ) -> types.RealTensor:
      """Calculates the price of European Swaptions using the Black model.
  
      A European Swaption is a contract that gives the holder an option to enter a
      swap contract at a future date at a prespecified fixed rate. A swaption that
      grants the holder to pay fixed rate and receive floating rate is called a
      payer swaption while the swaption that grants the holder to receive fixed and
      pay floating payments is called the receiver swaption. Typically the start
      date (or the inception date) of the swap coincides with the expiry of the
      swaption.
  
      #### Example
      The example shows how value a batch of 1y x 1y and 1y x 2y swaptions using the
      Black (normal) model for the swap rate.
  
      ````python
      import numpy as np
      import tensorflow.compat.v2 as tf
      import tf_quant_finance as tff
  
      dtype = tf.float64
  
      volatilities = [0.01, 0.005]
      expiries = [1.0, 1.0]
      float_leg_start_times = [[1.0, 1.25, 1.5, 1.75, 2.0, 2.0, 2.0, 2.0],
                                [1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75]]
      float_leg_end_times = [[1.25, 1.5, 1.75, 2.0, 2.0, 2.0, 2.0, 2.0],
                              [1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0]]
      fixed_leg_payment_times = [[1.25, 1.5, 1.75, 2.0, 2.0, 2.0, 2.0, 2.0],
                                  [1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0]]
      float_leg_daycount_fractions = [[0.25, 0.25, 0.25, 0.25, 0.0, 0.0, 0.0, 0.0],
                                       [0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
                                       0.25]]
      fixed_leg_daycount_fractions = [[0.25, 0.25, 0.25, 0.25, 0.0, 0.0, 0.0, 0.0],
                                       [0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
                                       0.25]]
      fixed_leg_coupon = [0.011, 0.011]
      discount_fn = lambda x: np.exp(-0.01 * np.array(x))
      price = self.evaluate(
      tff.black_scholes.swaption_price(
          volatilities=volatilities,
          expiries=expiries,
          floating_leg_start_times=float_leg_start_times,
          floating_leg_end_times=float_leg_end_times,
          fixed_leg_payment_times=fixed_leg_payment_times,
          floating_leg_daycount_fractions=float_leg_daycount_fractions,
          fixed_leg_daycount_fractions=fixed_leg_daycount_fractions,
          fixed_leg_coupon=fixed_leg_coupon,
          floating_leg_start_times_discount_factors=discount_fn(
              float_leg_start_times),
          floating_leg_end_times_discount_factors=discount_fn(
              float_leg_end_times),
          fixed_leg_payment_times_discount_factors=discount_fn(
              fixed_leg_payment_times),
          is_normal_volatility=is_normal_model,
          notional=100.,
          dtype=dtype))
      # Expected value: [0.3458467885511461, 0.3014786656395892] # shape = (2,)
      ````
  
      Args:
        volatilities: Real `Tensor` of any shape and dtype. The Black volatilities
          of the swaptions to price. The shape of this input determines the number
          (and shape) of swaptions to be priced and the shape of the output.
        expiries: A real `Tensor` of same shape and dtype as `volatilities`. The
          time to expiration of the swaptions.
        floating_leg_start_times: A real `Tensor` of the same dtype as
          `volatilities`. The times when accrual begins for each payment in the
          floating leg. The shape of this input should be `expiries.shape + [m]` or
          `batch_shape + [m]` where `m` denotes the number of floating payments in
          each leg.
        floating_leg_end_times: A real `Tensor` of the same dtype as `volatilities`.
          The times when accrual ends for each payment in the floating leg. The
          shape of this input should be `batch_shape + [m]` where `m` denotes
          the number of floating payments in each leg.
        fixed_leg_payment_times: A real `Tensor` of the same dtype as
          `volatilities`.  The payment times for each payment in the fixed leg.
          The shape of this input should be `batch_shape + [n]` where `n` denotes
          the number of fixed payments in each leg.
        floating_leg_daycount_fractions: A real `Tensor` of the same dtype and
          compatible shape as `floating_leg_start_times`. The daycount fractions
          for each payment in the floating leg.
        fixed_leg_daycount_fractions: A real `Tensor` of the same dtype and
          compatible shape as `fixed_leg_payment_times`. The daycount fractions
          for each payment in the fixed leg.
        fixed_leg_coupon: A real `Tensor` of the same dtype and shape compatible
          to `batch_shape`. The fixed coupon rate for each payment in the fixed leg.
        floating_leg_start_times_discount_factors: A real `Tensor` of the same
          shape and dtype as `floating_leg_start_times`. The discount factors
          corresponding to `floating_leg_start_times`.
        floating_leg_end_times_discount_factors: A real `Tensor` of the same
          shape and dtype as `floating_leg_end_times`. The discount factors
          corresponding to `floating_leg_end_times`.
        fixed_leg_payment_times_discount_factors: A real `Tensor` of the same
          shape and dtype as `fixed_leg_payment_times`. The discount factors
          corresponding to `fixed_leg_payment_times`.
        notional: An optional `Tensor` of same dtype and compatible shape as
          `volatilities` specifying the notional amount for the underlying swap.
           Default value: None in which case the notional is set to 1.
        is_payer_swaption: A boolean `Tensor` of a shape compatible with `expiries`.
          Indicates whether the swaption is a payer (if True) or a receiver
          (if False) swaption. If not supplied, payer swaptions are assumed.
        is_normal_volatility: An optional Python boolean specifying whether the
          `volatilities` correspond to normal Black volatility (if True) or
          lognormal Black volatility (if False).
          Default value: True, which corresponds to normal volatility.
        dtype: The default dtype to use when converting values to `Tensor`s.
          Default value: `None` which means that default dtypes inferred by
          TensorFlow are used.
        name: Python string. The name to give to the ops created by this function.
          Default value: `None` which maps to the default name
          `hw_swaption_price`.
  
      Returns:
        A `Tensor` of real dtype and shape `batch_shape` containing the
        computed swaption prices.
      """
      name = name or "black_swaption_price"
      del floating_leg_daycount_fractions
      with tf.name_scope(name):
          volatilities = tf.convert_to_tensor(
              volatilities, dtype=dtype, name="volatilities"
          )
          dtype = dtype or volatilities.dtype
          expiries = tf.convert_to_tensor(
              expiries, dtype=dtype, name="expiries"
          )
          floating_leg_start_times = tf.convert_to_tensor(
              floating_leg_start_times,
              dtype=dtype,
              name="float_leg_start_times",
          )
          floating_leg_end_times = tf.convert_to_tensor(
              floating_leg_end_times,
              dtype=dtype,
              name="float_leg_end_times",
          )
          fixed_leg_payment_times = tf.convert_to_tensor(
              fixed_leg_payment_times,
              dtype=dtype,
              name="fixed_leg_payment_times",
          )
          fixed_leg_daycount_fractions = tf.convert_to_tensor(
              fixed_leg_daycount_fractions,
              dtype=dtype,
              name="fixed_leg_daycount_fractions",
          )
          fixed_leg_coupon = tf.convert_to_tensor(
              fixed_leg_coupon, dtype=dtype, name="fixed_leg_coupon"
          )
          float_leg_start_times_discount_factors = tf.convert_to_tensor(
              floating_leg_start_times_discount_factors,
              dtype=dtype,
              name="float_leg_start_times_discount_factors",
          )
          float_leg_end_times_discount_factors = tf.convert_to_tensor(
              floating_leg_end_times_discount_factors,
              dtype=dtype,
              name="float_leg_end_times_discount_factors",
          )
          fixed_leg_payment_times_discount_factors = (
              tf.convert_to_tensor(
                  fixed_leg_payment_times_discount_factors,
                  dtype=dtype,
                  name="fixed_leg_payment_times_discount_factors",
              )
          )
  
          notional = tf.convert_to_tensor(
              notional, dtype=dtype, name="notional"
          )
          if is_payer_swaption is None:
              is_payer_swaption = True
          is_payer_swaption = tf.convert_to_tensor(
              is_payer_swaption, dtype=tf.bool, name="is_payer_swaption"
          )
  
          swap_annuity = tf.math.reduce_sum(
              fixed_leg_daycount_fractions
              * fixed_leg_payment_times_discount_factors,
              axis=-1,
          )
          forward_swap_rate = (
              tf.math.reduce_sum(
                  float_leg_start_times_discount_factors
                  - float_leg_end_times_discount_factors,
                  axis=-1,
              )
              / swap_annuity
          )
          swaption_value = option_price(
              volatilities=volatilities,
              strikes=fixed_leg_coupon,
              expiries=expiries,
              forwards=forward_swap_rate,
              is_call_options=is_payer_swaption,
              is_normal_volatility=is_normal_volatility,
              dtype=dtype,
              name=name + "_option_price",
          )
          return notional * swap_annuity * swaption_value
  
file_path: tf-quant-finance/tf_quant_finance/black_scholes/vanilla_prices.py
imports: import numpy
  import tensorflow.compat.v2
  from tf_quant_finance import types
label_desc: Use image node_id 5 for calling a global function with example usage: swaption_price() and returns: unknown