code: def implied_vol(
      *,
      prices,
      strikes,
      expiries,
      spots=None,
      forwards=None,
      discount_factors=None,
      is_call_options=None,
      validate_args=False,
      polya_factor=(2 / np.pi),
      dtype=None,
      name=None,
  ):
      """Approximates the implied vol using the Stefanica-Radiocic algorithm.
  
      Finds an approximation to the implied vol using the Polya approximation for
      the Normal CDF. This algorithm was described by Stefanica and Radiocic in
      ref [1]. They show that if the Normal CDFs appearing in the Black Scholes
      formula for the option price are replaced with Polya's approximation, the
      implied vol can be solved for analytically. The Polya approximation produces
      absolute errors of less than 0.003 and the resulting implied vol is fairly
      close to the true value. For practical purposes, this may not be accurate
      enough so this result should be used as a starting point for some method with
      controllable tolerance (e.g. a root finder).
  
      #### References:
      [1]: Dan Stefanica and Rados Radoicic. An explicit implied volatility formula.
        International Journal of Theoretical and Applied Finance,
        Vol. 20, no. 7, 2017.
        https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2908494
      [2]: Omar Eidous, Samar Al-Salman. One-term approximation for Normal
        distribution function. Mathematics and Statistics 4(1), 2016.
        http://www.hrpub.org/download/20160229/MS2-13405192.pdf
  
      Args:
        prices: A real `Tensor` of any shape. The prices of the options whose
          implied vol is to be calculated.
        strikes: A real `Tensor` of the same dtype as `prices` and a shape that
          broadcasts with `prices`. The strikes of the options.
        expiries: A real `Tensor` of the same dtype as `prices` and a shape that
          broadcasts with `prices`. The expiry for each option. The units should
          be such that `expiry * volatility**2` is dimensionless.
        spots: A real `Tensor` of any shape that broadcasts to the shape
          of the `prices`. The current spot price of the underlying. Either this
          argument or the `forwards` (but not both) must be supplied.
        forwards: A real `Tensor` of any shape that broadcasts to the shape of
          `prices`. The forwards to maturity. Either this argument or the `spots`
          must be supplied but both must not be supplied.
        discount_factors: An optional real `Tensor` of same dtype as the `prices`.
          If not None, these are the discount factors to expiry (i.e. e^(-rT)).
          If None, no discounting is applied (i.e. it is assumed that the
          undiscounted option prices are provided ). If `spots` is supplied and
          `discount_factors` is not None then this is also used to compute the
          forwards to expiry.
          Default value: None, equivalent to discount factors = 1.
        is_call_options: A boolean `Tensor` of a shape compatible with `prices`.
          Indicates whether the option is a call (if True) or a put (if False).
          If not supplied, call options are assumed.
        validate_args: A Python bool. If True, indicates that arguments should be
          checked for correctness before performing the computation. The checks
          performed are: (1) Forwards/spots and strikes are positive. (2) The prices
            satisfy the arbitrage bounds (i.e. for call options, checks the
            inequality `max(F-K, 0) <= Price <= F` and for put options, checks that
            `max(K-F, 0) <= Price <= K`.). (3) Checks that the prices are not too
            close to the bounds. It is numerically unstable to compute the implied
            vols from options too far in the money or out of the money.
          Default value: False
        polya_factor: A real scalar. The coefficient to use in the
          approximation for the Normal CDF. The approximation is: `N(x) ~ 0.5 + 0.5
            * sign(x) * sqrt[ 1 - exp(-k * x**2) ]` where `k` is the coefficient
            supplied with `polya_factor`. The original Polya approximation has the
            value `2 / pi` and this is approximation used in Ref [1]. However, as
            described in Ref [2], a slightly more accurate approximation is achieved
            if we use the value of `k=5/8`).
        dtype: `tf.Dtype` to use when converting arguments to `Tensor`s. If not
          supplied, the default TensorFlow conversion will take place. Note that
          this argument does not do any casting for `Tensor`s or numpy arrays.
          Default value: None.
        name: (Optional) Python str. The name prefixed to the ops created by this
          function. If not supplied, the default name 'implied_vol' is
          used.
          Default value: None
  
      Returns:
        implied_vols: A `Tensor` of the same dtype as `prices` and shape as the
          common broadcasted shape of `(prices, spots/forwards, strikes, expiries)`.
          The approximate implied total volatilities computed using the Polya
          approximation method.
  
      Raises:
        ValueError: If both `forwards` and `spots` are supplied or if neither is
          supplied.
      """
      if (spots is None) == (forwards is None):
          raise ValueError(
              "Either spots or forwards must be supplied but not both."
          )
  
      name = name or "implied_vol"
      with tf.name_scope(name):
          prices = tf.convert_to_tensor(
              prices, dtype=dtype, name="prices"
          )
          dtype = prices.dtype
          strikes = tf.convert_to_tensor(
              strikes, dtype=dtype, name="strikes"
          )
          expiries = tf.convert_to_tensor(
              expiries, dtype=dtype, name="expiries"
          )
          if discount_factors is None:
              discount_factors = tf.convert_to_tensor(
                  1.0, dtype=dtype, name="discount_factors"
              )
          else:
              discount_factors = tf.convert_to_tensor(
                  discount_factors, dtype=dtype, name="discount_factors"
              )
  
          if forwards is not None:
              forwards = tf.convert_to_tensor(
                  forwards, dtype=dtype, name="forwards"
              )
          else:
              spots = tf.convert_to_tensor(
                  spots, dtype=dtype, name="spots"
              )
              forwards = spots / discount_factors
  
          control_inputs = None
          if validate_args:
              control_inputs = _validate_args_control_deps(
                  prices,
                  forwards,
                  strikes,
                  expiries,
                  discount_factors,
                  is_call_options,
              )
          with tf.compat.v1.control_dependencies(control_inputs):
              adjusted_strikes = strikes * discount_factors
              normalized_prices = prices / adjusted_strikes
              normalized_forwards = forwards / strikes
              return _approx_implied_vol_polya(
                  normalized_prices,
                  normalized_forwards,
                  expiries,
                  is_call_options,
                  polya_factor,
              )
  
imports: import numpy
  import tensorflow.compat.v2
label_desc: Use image node_id 1 for calling a global function with example usage: implied_vol() and returns: _approx_implied_vol_polya