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))
def test_most_likely_parameters_list_arg(self): """ Tests the helper function 'most_likely_parameters' when passing a list to 'ignore_columns'. """ mu_1_list = [ -2.074149682208028, -2.1464760973734522, -1.7318027625411423, -1.7799163398785354, -1.9766582333677596 ] mu_2_list = [ 0.9958122958772418, 0.9927128514876395, 1.013574632526087, 1.0065707257309104, 1.009533655971151 ] sigma_1_list = [ 1.9764097851543956, 1.9516780127056625, 2.080573657129795, 2.071328499049906, 1.9988591140726848 ] sigma_2_list = [ 1.002964090440232, 1.0054392587806025, 0.9872577865302316, 0.9909001363163131, 0.9971327048101786 ] p_1_list = [ 0.09668610445835334, 0.09379917992315062, 0.11351960785118335, 0.10993400151299484, 0.10264463363929438 ] df_test = pd.DataFrame.from_dict({ 'mu_1': mu_1_list, 'mu_2': mu_2_list, 'sigma_1': sigma_1_list, 'sigma_2': sigma_2_list, 'p_1': p_1_list }) most_likely_correct = { 'mu_1': -2.03765, 'mu_2': 1.00863, 'sigma_1': 1.9832, 'sigma_2': 1.00012, 'p_1': 0.09942 } d_results = most_likely_parameters(data=df_test, ignore_columns=['error']) self.assertTrue( np.allclose(list(d_results.values()), list(most_likely_correct.values()), 1e-7))
# 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. events_active['bet_size'] = events_active['c_t'].apply( lambda c: single_bet_size_mixed(c, params_list)) if return_parameters: return events_active, fit_params return events_active def confirm_and_cast_to_df(d_vars): """