class_docstr: Tests for methods in implied_vol module.
class_docstr_tok: ["Tests","for","methods","in","implied_vol","module","."]
docstr: Basic test of the implied vol calculation.
docstr_tok: ["Basic","test","of","the","implied","vol","calculation","."]
code: def test_implied_vol(self):
      """Basic test of the implied vol calculation."""
      np.random.seed(6589)
      n = 100
      dtypes = [np.float32, np.float64]
      for dtype in dtypes:
          volatilities = np.exp(np.random.randn(n) / 2)
          forwards = np.exp(np.random.randn(n))
          strikes = forwards * (1 + (np.random.rand(n) - 0.5) * 0.2)
          expiries = np.exp(np.random.randn(n))
          prices = self.evaluate(
              bs.option_price(
                  volatilities=volatilities,
                  strikes=strikes,
                  expiries=expiries,
                  forwards=forwards,
                  dtype=dtype,
              )
          )
          # Using the default method
          implied_vols_default = self.evaluate(
              bs.implied_vol(
                  prices=prices,
                  strikes=strikes,
                  expiries=expiries,
                  forwards=forwards,
                  dtype=dtype,
              )
          )
          self.assertArrayNear(volatilities, implied_vols_default, 0.2)
  
          # Using Newton explicitly
          implied_vols_newton = self.evaluate(
              bs.implied_vol(
                  prices=prices,
                  strikes=strikes,
                  expiries=expiries,
                  forwards=forwards,
                  dtype=dtype,
                  method=bs.ImpliedVolMethod.NEWTON,
              )
          )
          self.assertArrayNear(
              implied_vols_default, implied_vols_newton, 1e-5
          )
  
          # Using approximation.
          implied_vols_approx = self.evaluate(
              bs.implied_vol(
                  prices=prices,
                  strikes=strikes,
                  expiries=expiries,
                  forwards=forwards,
                  dtype=dtype,
                  method=bs.ImpliedVolMethod.FAST_APPROX,
              )
          )
          self.assertArrayNear(volatilities, implied_vols_approx, 0.6)
  
file_path: tf-quant-finance/tf_quant_finance/black_scholes/implied_vol_lib_test.py
imports: from absl.testing import parameterized
  import numpy
  import tensorflow.compat.v2
  import tf_quant_finance
  from tensorflow.python.framework import test_util
label_desc: Use image node_id 1 for calling the ImpliedVolTest obj's underlying member method code with example usage: obj.test_implied_vol() without return types