def dont_test_tree(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1') f1 = Fund('f1', {'a1': 10}, 100, 1, 1) f2 = Fund('f2', {'a1': 20}, 100, 1, 1) mi_calc = MarketImpactCalculator() mi_calc.get_updated_price = MagicMock() mi_calc.get_updated_price.side_effect = update_price_side_effects network = AssetFundNetwork.AssetFundsNetwork(funds={ 'f1': f1, 'f2': f2 }, assets={'a1': a1}, mi_calc=mi_calc, limit_trade_step=False) action_manager = ActionsManager(network.assets, 0.5, 1) SysConfig.set("STEP_ORDER_SIZE", 0.5) f1.marginal_call = MagicMock(return_value=False) f2.marginal_call = MagicMock() f2.marginal_call.side_effect = f1_margin_call_side_effect actual_tree = FlashCrashRootChanceGameState(action_manager, af_network=network, defender_budget=50, attacker_budgets=[50, 30]) expected_tree = self.gen_tree() self.assertEqual(actual_tree.chance_prob(), 1. / 2) self.cmp_tree(expected_tree, actual_tree)
def count_game_states(game_network, lower_bound, ratios, portfolios=False): game_network.limit_trade_step = True defender_budget = lower_bound initial_network = copy.deepcopy(game_network) attacker_budgets = [int(defender_budget * r) for r in ratios] if portfolios: actions_mgr = ActionsManager( assets=game_network.assets, step_order_size=SysConfig.get("STEP_ORDER_SIZE"), max_order_num=1, attacker_budgets=attacker_budgets) root = PortfolioFlashCrashRootChanceGameState( action_mgr=actions_mgr, af_network=initial_network, defender_budget=defender_budget) else: actions_mgr = ActionsManager(game_network.assets, SysConfig.get("STEP_ORDER_SIZE"), 1) root = FlashCrashRootChanceGameState(action_mgr=actions_mgr, af_network=initial_network, defender_budget=defender_budget, attacker_budgets=attacker_budgets) print(count(root, 0)) print('num_states =%d, num_assets=%d, num_funds = %d, num_attackers = %d' % (root.tree_size, len(game_network.assets), len( game_network.funds), len(ratios)))
def compute_cfr_equilibrium(action_mgr, network, defender_budget, attacker_budgets, iterations): network.limit_trade_step = True root = FlashCrashRootChanceGameState(action_mgr=action_mgr, af_network=network, defender_budget=defender_budget, attacker_budgets=attacker_budgets) vanilla_cfr = VanillaCFR(root) vanilla_cfr.run(iterations=iterations) vanilla_cfr.compute_nash_equilibrium() defender_eq = vanilla_cfr.value_of_the_game() attackers_eq = {} regrets = {} root = vanilla_cfr.root for attacker in attacker_budgets: attackers_eq[attacker] = root.children[str(attacker)].get_value() regrets[attacker] = vanilla_cfr.cumulative_regrets[root.children[str( attacker)].inf_set()] cumulative_pos_regret = vanilla_cfr.average_total_imm_regret(iterations) return { 'defender': defender_eq, 'attackers': attackers_eq, 'regrets': regrets, 'pos_regret': cumulative_pos_regret, 'exploitability': 2 * cumulative_pos_regret }
def count_vanilla_nods(defender_budget, attacker_budgets, network, step_order_size, max_order_num): vanilla_actions_mgr = ActionsManager(assets=network.assets, step_order_size=step_order_size, max_order_num=max_order_num) vanilla_root = FlashCrashRootChanceGameState( action_mgr=vanilla_actions_mgr, af_network=network, defender_budget=defender_budget, attacker_budgets=attacker_budgets) return vanilla_root.tree_size
def count_game_states_old(game_network, lower_bound, ratios, network): network.limit_trade_step = True defender_budget = lower_bound actions_mgr = ActionsManager(network.assets, SysConfig.get("STEP_ORDER_SIZE"), 1) initial_network = copy.deepcopy(game_network) attacker_budgets = [int(defender_budget * r) for r in ratios] root = FlashCrashRootChanceGameState(action_mgr=actions_mgr, af_network=initial_network, defender_budget=defender_budget, attacker_budgets=attacker_budgets) print('num_states =%d, num_assets=%d, num_funds = %d, num_attackers = %d' % (root.tree_size, len(game_network.assets), len( game_network.funds), len(ratios)))
def play_flash_crash(): config = GameConfig() config.num_assets = 10 config.num_funds = 10 network = generate_rand_network(config) root = FlashCrashRootChanceGameState(network, BUDGETS) chance_sampling_cfr = ChanceSamplingCFR(root) chance_sampling_cfr.run(iterations=1000) chance_sampling_cfr.compute_nash_equilibrium() # read Nash-Equilibrum via chance_sampling_cfr.nash_equilibrium member # try chance_sampling_cfr.value_of_the_game() function to get value of the game (-1/18) # vanilla cfr vanilla_cfr = VanillaCFR(root) vanilla_cfr.run(iterations=1000) vanilla_cfr.compute_nash_equilibrium()