def test_option_prices_pos_carries(self, discount_rates, volatilities,
                                    expiries, expected_prices):
     """Tests the prices for positive cost_of_carries."""
     spots = np.array([80.0, 90.0, 100.0, 110.0, 120.0] * 2)
     strikes = np.array([100.0] * 10)
     is_call_options = [True] * 5 + [False] * 5
     cost_of_carries = 0.04
     computed_prices, converged, failed = adesi_whaley(
         volatilities=volatilities,
         strikes=strikes,
         expiries=expiries,
         discount_rates=discount_rates,
         cost_of_carries=cost_of_carries,
         spots=spots,
         is_call_options=is_call_options,
         dtype=tf.float64)
     with self.subTest(name='ExpectedPrices'):
         self.assertAllClose(expected_prices,
                             computed_prices,
                             rtol=5e-3,
                             atol=5e-3)
     with self.subTest(name='AllConverged'):
         self.assertAllEqual(converged, tf.ones_like(computed_prices))
     with self.subTest(name='NonFailed'):
         self.assertAllEqual(failed, tf.zeros_like(computed_prices))
 def test_option_prices_no_cost_of_carries(self, dtype, discount_rates,
                                           volatilities, expiries,
                                           expected_prices):
     """Tests the prices when no cost_of_carries is supplied."""
     spots = np.array([80.0, 90.0, 100.0, 110.0, 120.0])
     strikes = np.array([100.0, 100.0, 100.0, 100.0, 100.0])
     is_call_options = False
     computed_prices, converged, failed = adesi_whaley(
         volatilities=volatilities,
         strikes=strikes,
         expiries=expiries,
         discount_rates=discount_rates,
         spots=spots,
         is_call_options=is_call_options,
         tolerance=1e-5,  # float32 does not converge to tolerance 1e-8
         dtype=dtype)
     with self.subTest(name='ExpectedPrices'):
         self.assertAllClose(expected_prices,
                             computed_prices,
                             rtol=5e-3,
                             atol=5e-3)
     with self.subTest(name='AllConverged'):
         self.assertAllEqual(converged, tf.ones_like(computed_prices))
     with self.subTest(name='NonFailed'):
         self.assertAllEqual(failed, tf.zeros_like(computed_prices))
 def test_option_prices(self, discount_rates, volatilities, expiries,
                        expected_prices):
     """Tests that the prices are correct."""
     spots = np.array([80.0, 90.0, 100.0, 110.0, 120.0] * 2)
     strikes = np.array([100.0] * 10)
     is_call_options = np.array([True] * 5 + [False] * 5)
     cost_of_carries = -0.04
     computed_prices, converged, failed = self.evaluate(
         adesi_whaley(volatilities=volatilities,
                      strikes=strikes,
                      expiries=expiries,
                      discount_rates=discount_rates,
                      cost_of_carries=cost_of_carries,
                      is_call_options=is_call_options,
                      spots=spots,
                      dtype=tf.float32))
     expected_prices = np.array(expected_prices)
     self.assertArrayNear(expected_prices, computed_prices, 5e-3)
    def test_option_prices_b_equal_r(self, dtype, discount_rates, volatilities,
                                     expiries, expected_prices):
        """Tests that the prices are correct.
    Also tests that single and double precision both work"""
        spots = np.array([80.0, 90.0, 100.0, 110.0, 120.0])
        strikes = np.array([100.0, 100.0, 100.0, 100.0, 100.0])
        cost_of_carries = discount_rates
        is_call_options = False
        computed_prices, converged, failed = self.evaluate(
            adesi_whaley(volatilities=volatilities,
                         strikes=strikes,
                         expiries=expiries,
                         discount_rates=discount_rates,
                         cost_of_carries=cost_of_carries,
                         spots=spots,
                         is_call_options=is_call_options,
                         dtype=dtype))

        expected_prices = np.array(expected_prices)

        self.assertArrayNear(expected_prices, computed_prices, 5e-3)
 def test_option_prices_all_call_options(self):
     """Tests call prices with zero discount."""
     dtype = tf.float64
     computed_prices, converged, failed = adesi_whaley(
         volatilities=[0.2],
         strikes=[104, 90],
         expiries=[0.1, 1.0],
         spots=[100],
         tolerance=1e-08,
         discount_rates=[0.0, 0.0],
         dtype=dtype)
     # Computed using tff.black_scholes.option_price_binomial
     expected_prices = [1.05226366, 13.58957787]
     with self.subTest(name='ExpectedPrices'):
         self.assertAllClose(expected_prices,
                             computed_prices,
                             rtol=1e-4,
                             atol=5e-4)
     with self.subTest(name='AllConverged'):
         self.assertAllEqual(converged, tf.ones_like(computed_prices))
     with self.subTest(name='NonFailed'):
         self.assertAllEqual(failed, tf.zeros_like(computed_prices))