Пример #1
0
 def test_bet_size_reserve_return_params(self, mock_likely_parameters):
     """
     Tests for successful execution of 'bet_size_reserve' using return_parameters=True.
     Function 'most_likely_parameters' needs to be patched because the 'M2N.mp_fit' method makes use of
     random numbers.
     """
     # Setup the test DataFrame.
     np.random.seed(0)
     sample_size = 500
     start_date = dt.datetime(2000, 1, 1)
     date_step = dt.timedelta(days=1)
     dates = np.array(
         [start_date + i * date_step for i in range(sample_size)])
     shift_dt = np.array([
         dt.timedelta(days=d)
         for d in np.random.uniform(1., 20., sample_size)
     ])
     dates_shifted = dates + shift_dt
     time_1 = pd.Series(data=dates_shifted, index=dates)
     df_events = time_1.to_frame()
     df_events = df_events.rename(columns={0: 't1'})
     df_events['p'] = np.random.uniform(0.0, 1.0, sample_size)
     df_events = df_events[['t1', 'p']]
     df_events['side'] = df_events['p'].apply(lambda x: 1
                                              if x >= 0.5 else -1)
     # Calculate the correct results.
     events_active = get_concurrent_sides(df_events['t1'],
                                          df_events['side'])
     events_active['c_t'] = events_active['active_long'] - events_active[
         'active_short']
     central_moments = [
         moment(events_active['c_t'].to_numpy(), moment=i)
         for i in range(1, 6)
     ]
     raw_moments = raw_moment(central_moments=central_moments,
                              dist_mean=events_active['c_t'].mean())
     m2n_test = M2N(raw_moments,
                    epsilon=1e-5,
                    factor=5,
                    n_runs=25,
                    variant=2,
                    max_iter=10_000,
                    num_workers=1)
     test_results = m2n_test.mp_fit()
     test_params = most_likely_parameters(test_results)
     mock_likely_parameters.return_value = test_params
     test_fit = [
         test_params[key]
         for key in ['mu_1', 'mu_2', 'sigma_1', 'sigma_2', 'p_1']
     ]
     events_active['bet_size'] = events_active['c_t'].apply(
         lambda c: single_bet_size_mixed(c, test_fit))
     # Evaluate.
     eval_events, eval_params = bet_size_reserve(df_events['t1'],
                                                 df_events['side'],
                                                 fit_runs=25,
                                                 return_parameters=True)
     self.assertEqual(test_params, eval_params)
     self.assertTrue(events_active.equals(eval_events))
Пример #2
0
 def test_raw_moment_result(self):
     """
     Tests for the successful execution of the 'raw_moment' helper function.
     """
     centered_test = [
         0.0, 2.11, -4.373999999999999, 30.803699999999996, -153.58572
     ]
     raw_result = raw_moment(centered_test, 0.7)
     raw_correct = [0.7, 2.6, 0.4, 25, -59.8]
     self.assertTrue(np.allclose(raw_result, raw_correct, 1e-7))
Пример #3
0
 :param num_workers: (int) Number of CPU cores to use for multiprocessing execution, set to -1 to use all
  CPU cores. Default is 1.
 :param return_parameters: (bool) If True, function also returns a dictionary of the fited mixture parameters.
 :return: (pandas.DataFrame) The 'events_t1' and 'sides' arguments as columns, with the number of concurrent
  active long, short bets, the difference between long and short, and the bet size in additional columns.
  Also returns the mixture parameters if 'return_parameters' is set to True.
 """
 events_active = get_concurrent_sides(events_t1, sides)
 # Calculate the concurrent difference in active bets: c_t = <current active long> - <current active short>
 events_active[
     'c_t'] = events_active['active_long'] - events_active['active_short']
 # Calculate the first 5 centered and raw moments from the c_t distribution.
 central_mmnts = [
     moment(events_active['c_t'].to_numpy(), moment=i) for i in range(1, 6)
 ]
 raw_mmnts = raw_moment(central_moments=central_mmnts,
                        dist_mean=events_active['c_t'].mean())
 # Fit the mixture of distributions.
 m2n = M2N(raw_mmnts,
           epsilon=epsilon,
           factor=factor,
           n_runs=fit_runs,
           variant=variant,
           max_iter=max_iter,
           num_workers=num_workers)
 df_fit_results = m2n.mp_fit()
 fit_params = most_likely_parameters(df_fit_results)
 params_list = [
     fit_params[key]
     for key in ['mu_1', 'mu_2', 'sigma_1', 'sigma_2', 'p_1']
 ]
 # Calculate the bet size.