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
def test_avg_active_signals(self): """ Tests the avg_active_signals function. Also implicitly tests the molecular multiprocessing function mp_avg_active_signals. """ test_avg_active = avg_active_signals(self.events_2) self.assertEqual(self.avg_active.equals(test_avg_active), True)
def test_bet_size_probability_avg_active(self): """ Tests for successful execution of 'bet_size_probability' with 'average_active' set to True. """ # 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 = avg_active_signals(df_signal_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'], average_active=True)))