Пример #1
0
 def test_bet_size_probability_stepsize(self):
     """
     Tests for successful execution of 'bet_size_probability' with 'step_size' greater than 0.
     """
     # Setup the test DataFrame.
     dates_test = np.array([
         dt.datetime(2000, 1, 1) + i * dt.timedelta(days=1)
         for i in range(5)
     ])
     shift_dt = np.array([dt.timedelta(days=0.5 * i + 1) for i in range(5)])
     dates_shifted_test = dates_test + shift_dt
     events_test = pd.DataFrame(data=[[0.55, 1], [0.7, -1], [0.95, 1],
                                      [0.65, -1], [0.85, 1]],
                                columns=['prob', 'side'],
                                index=dates_test)
     events_test['t1'] = dates_shifted_test
     # Calculate correct output.
     signal_0 = get_signal(events_test['prob'], 2, events_test['side'])
     df_signal_0 = signal_0.to_frame('signal').join(events_test['t1'],
                                                    how='left')
     signal_1 = df_signal_0.signal
     signal_1 = discrete_signal(signal0=signal_1, step_size=0.1)
     # Evaluate test.
     self.assertTrue(
         signal_1.equals(
             bet_size_probability(events=events_test,
                                  prob=events_test['prob'],
                                  num_classes=2,
                                  pred=events_test['side'],
                                  step_size=0.1)))
Пример #2
0
def bet_size_probability(events,
                         prob,
                         num_classes,
                         pred=None,
                         step_size=0.0,
                         average_active=False,
                         num_threads=1):
    """
    Calculates the bet size using the predicted probability. Note that if 'average_active' is True, the returned
    pandas.Series will be twice the length of the original since the average is calculated at each bet's open and close.

    :param events: (pandas.DataFrame) Contains at least the column 't1', the expiry datetime of the product, with
     a datetime index, the datetime the position was taken.
    :param prob: (pandas.Series) The predicted probability.
    :param num_classes: (int) The number of predicted bet sides.
    :param pred: (pd.Series) The predicted bet side. Default value is None which will return a relative bet size
     (i.e. without multiplying by the side).
    :param step_size: (float) The step size at which the bet size is discretized, default is 0.0 which imposes no
     discretization.
    :param average_active: (bool) Option to average the size of active bets, default value is False.
    :param num_threads: (int) The number of processing threads to utilize for multiprocessing, default value is 1.
    :return: (pandas.Series) The bet size, with the time index.
    """
    signal_0 = get_signal(prob, num_classes, pred)
    events_0 = signal_0.to_frame('signal').join(events['t1'], how='left')
    if average_active:
        signal_1 = avg_active_signals(events_0, num_threads)
    else:
        signal_1 = events_0.signal

    if abs(step_size) > 0:
        signal_1 = discrete_signal(signal0=signal_1, step_size=abs(step_size))

    return signal_1
Пример #3
0
 def test_bet_size_probability_default(self):
     """
     Tests for successful execution using the default arguments of 'bet_size_probability', which are:
      average_active = False
      step_size = 0.0
     """
     # Setup the test DataFrame.
     dates_test = np.array([
         dt.datetime(2000, 1, 1) + i * dt.timedelta(days=1)
         for i in range(5)
     ])
     shift_dt = np.array([dt.timedelta(days=0.5 * i + 1) for i in range(5)])
     dates_shifted_test = dates_test + shift_dt
     events_test = pd.DataFrame(data=[[0.55, 1], [0.7, -1], [0.95, 1],
                                      [0.65, -1], [0.85, 1]],
                                columns=['prob', 'side'],
                                index=dates_test)
     events_test['t1'] = dates_shifted_test
     # Calculate correct output.
     signal_0 = get_signal(events_test['prob'], 2, events_test['side'])
     df_signal_0 = signal_0.to_frame('signal').join(events_test['t1'],
                                                    how='left')
     signal_1 = df_signal_0.signal
     # Evaluate test.
     self.assertTrue(
         signal_1.equals(
             bet_size_probability(events_test, events_test['prob'], 2,
                                  events_test['side'])))
Пример #4
0
    def test_get_signal(self):
        """
        Tests calculating the bet size from probability.
        """
        # Test get_signal when supplying a value to argument 'pred'.
        test_bet_size_1 = get_signal(prob=self.prob,
                                     num_classes=2,
                                     pred=self.side)
        self.assertEqual(self.bet_size.equals(test_bet_size_1), True)

        # Test get_signal when no value provided for 'pred'.
        test_bet_size_2 = get_signal(prob=self.prob, num_classes=2)
        self.assertEqual(self.bet_size.abs().equals(test_bet_size_2), True)

        # Test for prob.shape[0] == 0.
        df_empty = pd.DataFrame({'a': []})
        return_val = get_signal(df_empty, 2)
        self.assertIsInstance(return_val, pd.Series)
        self.assertEqual(0, len(return_val))