예제 #1
0
  def test_weighted_avg_price_is_correct_with_same_weights(self):
    np.random.seed(0)
    num_samples = 16
    num_underlyings = 7
    prices = np.random.uniform(size=[num_samples, num_underlyings]).astype(
        np.float32)

    weighted_avg_prices = payoffs.weighted_avg_price(prices)

    with self.session() as session:
      weighted_avg_prices_eval = session.run(weighted_avg_prices)

    self.assertAllClose(weighted_avg_prices_eval, np.mean(prices, axis=-1))
예제 #2
0
  def test_weighted_avg_price_is_correct_with_specified_weights(self):
    np.random.seed(0)
    num_samples = 16
    num_underlyings = 7
    prices = np.random.uniform(size=[num_samples, num_underlyings]).astype(
        np.float32)
    weights = np.random.uniform(size=[num_underlyings]).astype(np.float32)

    weighted_avg_prices = payoffs.weighted_avg_price(prices, weights)

    with self.session() as session:
      weighted_avg_prices_eval = session.run(weighted_avg_prices)

    normalized_weights = np.expand_dims(weights / np.sum(weights), 0)
    self.assertAllClose(weighted_avg_prices_eval,
                        np.sum(prices * normalized_weights, axis=-1))