class_docstr: Tests for methods for the vanilla pricing module.
class_docstr_tok: ["Tests","for","methods","for","the","vanilla","pricing","module","."]
docstr: Tests code consistency through relationship of binary and vanilla prices.
  
  With forward F, strike K, discount rate r, and expiry T, a vanilla call
  option should have price CV:
  
  $$ VC(K) = e^{-rT}( N(d_1)F - N(d_2)K ) $$
  
  A unit of cash paying binary call option should have price BC:
  
  $$ BC(K) = e^{-rT} N(d_2) $$
  
  Where d_1 and d_2 are standard Black-Scholes quanitities and depend on K
  through the ratio F/K. Hence for a small increment e:
  
  $$ (VC(K + e) - Vc(K))/e \approx -N(d_2)e^{-rT} = -BC(K + e) $$
  
  Similarly, for a vanilla put:
  
  $$ (VP(K + e) - VP(K))/e \approx N(-d_2)e^{-rT} = BP(K + e) $$
  
  This enables a test for consistency of pricing between vanilla and binary
  options prices.
docstr_tok: ["Tests","code","consistency","through","relationship","of","binary","and","vanilla","prices",".","With","forward","F",",","strike","K",",","discount","rate","r",",","and","expiry","T",",","a","vanilla","call","option","should","have","price","CV",":","$","$","VC","(","K",")","=","e^","{","-rT","}","(","N","(","d_1",")","F","-","N","(","d_2",")","K",")","$","$","A","unit","of","cash","paying","binary","call","option","should","have","price","BC",":","$","$","BC","(","K",")","=","e^","{","-rT","}","N","(","d_2",")","$","$","Where","d_1","and","d_2","are","standard","Black-Scholes","quanitities","and","depend","on","K","through","the","ratio","F\/K",".","Hence","for","a","small","increment","e",":","$","$","(","VC","(","K","+","e",")","-","Vc","(","K",")",")","\/e","\\approx","-N","(","d_2",")","e^","{","-rT","}","=","-BC","(","K","+","e",")","$","$","Similarly",",","for","a","vanilla","put",":","$","$","(","VP","(","K","+","e",")","-","VP","(","K",")",")","\/e","\\approx","N","(","-d_2",")","e^","{","-rT","}","=","BP","(","K","+","e",")","$","$","This","enables","a","test","for","consistency","of","pricing","between","vanilla","and","binary","options","prices","."]
code: def test_binary_vanilla_call_consistency(self):
      r"""Tests code consistency through relationship of binary and vanilla prices.
  
      With forward F, strike K, discount rate r, and expiry T, a vanilla call
      option should have price CV:
  
      $$ VC(K) = e^{-rT}( N(d_1)F - N(d_2)K ) $$
  
      A unit of cash paying binary call option should have price BC:
  
      $$ BC(K) = e^{-rT} N(d_2) $$
  
      Where d_1 and d_2 are standard Black-Scholes quanitities and depend on K
      through the ratio F/K. Hence for a small increment e:
  
      $$ (VC(K + e) - Vc(K))/e \approx -N(d_2)e^{-rT} = -BC(K + e) $$
  
      Similarly, for a vanilla put:
  
      $$ (VP(K + e) - VP(K))/e \approx N(-d_2)e^{-rT} = BP(K + e) $$
  
      This enables a test for consistency of pricing between vanilla and binary
      options prices.
      """
      np.random.seed(135)
      num_examples = 1000
      forwards = np.exp(np.random.normal(size=num_examples))
      strikes_0 = np.exp(np.random.normal(size=num_examples))
      epsilon = 1e-8
      strikes_1 = strikes_0 + epsilon
      volatilities = np.exp(np.random.normal(size=num_examples))
      expiries = np.random.gamma(
          shape=1.0, scale=1.0, size=num_examples
      )
      call_options = np.random.binomial(n=1, p=0.5, size=num_examples)
      is_call_options = np.array(call_options, dtype=bool)
      discount_factors = np.ones_like(forwards)
  
      option_prices_0 = self.evaluate(
          tff.black_scholes.option_price(
              volatilities=volatilities,
              strikes=strikes_0,
              expiries=expiries,
              forwards=forwards,
              is_call_options=is_call_options,
          )
      )
  
      option_prices_1 = self.evaluate(
          tff.black_scholes.option_price(
              volatilities=volatilities,
              strikes=strikes_1,
              expiries=expiries,
              forwards=forwards,
              is_call_options=is_call_options,
          )
      )
  
      binary_approximation = (
          (-1.0) ** call_options
          * (option_prices_1 - option_prices_0)
          / epsilon
      )
  
      binary_prices = self.evaluate(
          tff.black_scholes.binary_price(
              volatilities=volatilities,
              strikes=strikes_1,
              expiries=expiries,
              forwards=forwards,
              is_call_options=is_call_options,
              discount_factors=discount_factors,
          )
      )
  
      self.assertArrayNear(binary_approximation, binary_prices, 1e-6)
  
imports: import functools
  from absl.testing import parameterized
  import numpy
  import tensorflow.compat.v2
  import tensorflow_probability
  import tf_quant_finance
  from tensorflow.python.framework import test_util
label_desc: Use image node_id 11 for calling the VanillaPrice obj's underlying member method code with example usage: obj.test_binary_vanilla_call_consistency() without return types