### Running Market Simulation with Reset Betters and Custom Price Function Source: https://github.com/ebbam/power_prediction/blob/main/code/extensions/main_predmkt.ipynb This example resets the betters to their original instantiation and defines a custom function `set_market_price` to demonstrate issues with market price updating. It then runs the market simulation with these parameters and plots the results. ```python # Return to old instantiation of betters from first example parameters.update({'betters': [better(expertise = np.random.normal(0.1, 0.01)) for _ in range(parameters['n_betters'])]}) def set_market_price(m, net_supply_demand, total_orders): ''' Resetting market price function to show the issue with market price updating ''' delta = net_supply_demand/(total_orders*100) m -= delta return m poor_mkt_price = run_market(**parameters) plot_returns(poor_mkt_price) ``` -------------------------------- ### Plotting Setup for Simulation Results Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results.ipynb Initializes Matplotlib figure and axes for plotting simulation results. It adjusts subplot spacing for better visualization. ```python fig, ax = plt.subplots(len(results_dict.keys()),2,figsize=(7,14)) plt.subplots_adjust(hspace=0.5,wspace=0.4) axs = ax.ravel() i=0 for k in results_dict.keys(): if k=='Exponetial budget': continue # arr=results_dict[k]: ``` -------------------------------- ### Print Simulation Progress for Various Whale Proportions Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Outputs the simulation progress for different proportions of whale bettors, including their respective budgets. This helps track the setup for each simulation scenario. ```text Running for prop_whale=0.01, whale_budget=101.01010101010101 and total non-whale budget=10000 Running for prop_whale=0.1, whale_budget=1111.111111111111 and total non-whale budget=10000 Running for prop_whale=0.5, whale_budget=10000.0 and total non-whale budget=10000 Running for prop_whale=0.7, whale_budget=23333.33333333333 and total non-whale budget=10000 ``` -------------------------------- ### Simulation Output Log Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_shock_whale.ipynb This output indicates the start of a simulation run for a specific whale proportion and displays the calculated whale budget and total non-whale budget. ```text Running for prop_whale=0, whale_budget=0.0 and total non-whale budget=10000 ``` -------------------------------- ### Initialize Simulation Parameters for New Cases Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results.ipynb Sets up parameters for a new set of simulations, including the number of bettors, budget, proportion of whales, and the election outcome path. ```python N_bettors = 100 av_budget=1000 prop_whale = 0.2 N_whales = 1 T_max=100 records = {} for case in cases: alpha_effect = [] parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance, 'gen_el' : gen_el_res, 'k':3} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability b_numbers = N_bettors*np.array(case) ``` -------------------------------- ### Initialize Betting Market Parameters and Run Simulation Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_whale_error.ipynb Sets up initial parameters for the betting market, including the number of bettors, time horizon, initial price, and outcome uncertainty. It then runs the simulation multiple times for different whale valuations, storing the price history and true outcome. ```python # Set initial input values to the betting market function parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability # expertise_values = np.linspace(0,1,N_bettors) av_budget=100 budget_total = av_budget* N_bettors N_whales = 1 whale_valuation = 0.6 prop_whale=0.5 expertise_whale = {} for whale_valuation in [0.6,0.7,0.8]: whale_effect = [] whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) non_whale_bettors = [bettor(budget=av_budget, expertise=0.9) for i in range(N_bettors)] whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=whale_valuation) for _ in range(N_whales)] parameters.update({'bettors': non_whale_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) whale_effect.append((np.array(market_record['price_history']) - np.array(market_record['gen_el']))) expertise_whale[whale_valuation] = whale_effect ``` -------------------------------- ### Initialize Simulation Parameters and Run Market Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results.ipynb Sets up simulation parameters including the number of bettors, budgets, and market conditions. It then iterates through different cases, initializes various types of bettors (including whales), and runs the market simulation for each case. ```python N_bettors = 20 av_budget=100 prop_whale = 0.3 N_whales = 1 alpha_store_shock = {} for case in cases: alpha_effect = [] parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability b_numbers = N_bettors*np.array(case) b_numbers = [int(x) for x in b_numbers] alpha_1_bettors = [bettor(budget=av_budget, market_imitation = 0, stubbornness=0.01) for i in range(b_numbers[0])] alpha_neg1_bettors = [bettor(budget=av_budget, market_imitation = 0.2, stubbornness=0.01) for i in range(b_numbers[1])] no_alpha_bettors = [bettor(budget=av_budget, market_imitation = 0.7, stubbornness=0.01) for i in range(b_numbers[2])] # whale whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=1) for _ in range(N_whales)] parameters.update({'bettors': alpha_1_bettors + alpha_neg1_bettors + no_alpha_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) alpha_effect.append((np.array(market_record['price_history']) - np.array(market_record['gen_el']))) var_save.append(np.var(market_record['price_history'])) alpha_store_shock[case] = alpha_effect ``` -------------------------------- ### Initialize and Run Market Simulation Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Sets up initial parameters for the betting market simulation, including the number of bettors, election time horizon, initial price, and outcome uncertainty. It then iterates through different budget variances, simulating market behavior for each, and evaluating the results. ```python parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability for v in np.arange(0,500,50): for _ in range(n_iter_): budget_samples = np.clip(np.random.normal(500, v, N_bettors),0,None) parameters.update({'bettors': [bettor(budget=budget_samples[i]) for i in range(parameters['n_bettors'])]}) market_record = run_market(**parameters) mse, min_sig_lag = evaluate_markets(market_record) results.append([v, mse, min_sig_lag]) results_dict['budget_variance'] = np.array(results) ``` -------------------------------- ### Reset Results List for Next Sweep Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Resets the 'results' list to empty before starting a new simulation parameter sweep. This ensures that results from previous sweeps do not interfere with the current one. ```python # set seed np.random.seed(0) results = [] ``` -------------------------------- ### Initialize Market Parameters (Whale Scenario) Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Sets up the initial parameters for a market simulation, including the number of bettors, election time, initial price, and outcome uncertainty. This configuration is used for scenarios involving 'whale' bettors. ```python # Set initial input values to the betting market function parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability expertise_values = np.linspace(0,1,N_bettors) av_budget=100 budget_total = av_budget* N_bettors N_whales = 1 ``` -------------------------------- ### Generate Election Outcome as Random Walk Source: https://github.com/ebbam/power_prediction/blob/main/code/extensions/main_predmkt.ipynb Simulates an election outcome as a random walk. Starts with an initial price and evolves over a specified time period (t_el) with random normal fluctuations, constrained between 0 and 1. ```python # Generate election outcome as a random walk # Not sure whether this should be a series of probabilities or if it should be a series of 1 and 0 def gen_election(init_price, t_el, sd): el = [init_price] for k in range(t_el): el.append(np.clip(el[k] + np.random.normal(0, sd), 0, 1)) return el ``` -------------------------------- ### Initialize Simulation Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_shock_whale.ipynb Sets up the initial parameters for the betting market simulation, including the number of bettors, election time horizon, initial market price, and outcome uncertainty. ```python parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability expertise_values = np.linspace(0,1,N_bettors) av_budget=100 budget_total = av_budget* N_bettors N_whales = 1 # expertise_whale = {} for prop_whale in [0.02,0.05,0.08]: expertise_store_whale = [] budgets_whale = [] whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) non_whale_bettors = [bettor(budget=av_budget, expertise=expertise_values[i]) for i in range(N_bettors)] whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=1) for _ in range(N_whales)] parameters.update({'bettors': non_whale_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) expertise_store_whale.append([expertise_values]) budgets_whale.append([x-av_budget for x in market_record['final_budgets'][:-1]]) expertise_whale[prop_whale] = [expertise_store_whale, budgets_whale] ``` -------------------------------- ### Initialize Betting Market Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Sets up the initial parameters for the betting market simulation, including the number of bettors, election time horizon, initial market price, outcome uncertainty, and market lambda. This configuration is essential before running the market simulation. ```python parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': 0.4, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance,# This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability 'market_lambda': 1/50} # market step size parameter # expertise_values = np.linspace(0,1,N_bettors) av_budget=100 budget_total = av_budget* N_bettors N_whales = 1 whale_valuation = 0.7 expertise_whale = {} for prop_whale in [0,0.01,0.1,0.5,0.7]: whale_effect = [] whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) non_whale_bettors = [bettor(budget=av_budget, expertise=0.95, risk_av=1, stubbornness=0) for i in range(N_bettors)] whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=whale_valuation,risk_av=1, stubbornness=0) for _ in range(N_whales)] print(f'Running for prop_whale={prop_whale}, whale_budget={whale_budget} and total non-whale budget={av_budget*N_bettors}') parameters.update({'bettors': non_whale_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) whale_effect.append((np.array(market_record['price_history']) - np.array(market_record['gen_el']))) expertise_whale[prop_whale] = whale_effect ``` -------------------------------- ### Initialize Bettors and Run Market Simulation Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results.ipynb Sets up different types of bettors (alpha, no alpha, whale) with specified budgets and market imitation parameters. It then updates the simulation parameters and runs the market simulation using `run_market_fixed_el`. ```python b_numbers = [int(x) for x in b_numbers] alpha_1_bettors = [bettor(budget=av_budget, market_imitation = 1) for i in range(b_numbers[0])] alpha_neg1_bettors = [bettor(budget=av_budget, market_imitation = -1) for i in range(b_numbers[1])] no_alpha_bettors = [bettor(budget=av_budget, market_imitation = 0) for i in range(b_numbers[2])] # whale whale_budget = av_budget*(np.sum(b_numbers))* prop_whale / (1-prop_whale) whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=0.2) for _ in range(N_whales)] parameters.update({'bettors': alpha_1_bettors + alpha_neg1_bettors + no_alpha_bettors + whale_bettors}) market_record = run_market_fixed_el(**parameters) ``` -------------------------------- ### Initialize Betting Market Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Sets up initial parameters for a betting market simulation, including the number of bettors, election time horizon, initial market price, and outcome uncertainty. This configuration is used to run the market simulation. ```python parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': 3*T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability expertise_values = np.linspace(0,1,N_bettors) av_budget=100 budget_total = av_budget* N_bettors N_whales = 1 whale_valuation = 0.6 expertise_whale = {} for prop_whale in np.arange(0,0.55,0.1): whale_effect = [] whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) non_whale_bettors = [bettor(budget=av_budget, expertise=expertise_values[i]) for i in range(N_bettors)] whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=whale_valuation) for _ in range(N_whales)] parameters.update({'bettors': non_whale_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) whale_effect.append((np.array(market_record['price_history']) - np.array(market_record['gen_el']))) expertise_whale[prop_whale] = whale_effect ``` -------------------------------- ### Initialize Simulation Parameters and Run Market Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_linearlagfit.ipynb Sets up simulation parameters, initializes different types of bettors based on defined cases, and runs the market simulation multiple times for each case to collect results. ```python av_budget=100 alpha_store_shock = {} N_50 = N_bettors//2 N_25 = N_bettors // 4 var_save = [] case_save = [] for case in cases: alpha_effect = [] parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability b_numbers = N_bettors*np.array(case) b_numbers = [int(x) for x in b_numbers] alpha_1_bettors = [bettor(budget=av_budget, market_imitation = 1) for i in range(b_numbers[0])] alpha_neg1_bettors = [bettor(budget=av_budget, market_imitation = -1) for i in range(b_numbers[1])] no_alpha_bettors = [bettor(budget=av_budget, market_imitation = 0) for i in range(b_numbers[2])] parameters.update({'bettors': alpha_1_bettors + alpha_neg1_bettors + no_alpha_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) alpha_effect.append((np.array(market_record['price_history']) - np.array(market_record['gen_el']))) var_save.append(np.var(market_record['price_history'])) case_save.append(case) alpha_store_shock[case] = alpha_effect ``` -------------------------------- ### Initializing Betting Market Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_linearlagfit.ipynb Sets up initial parameters for a betting market simulation, including the number of bettors, election time, initial price, and outcome uncertainty. Expertise values and budgets are also defined. ```python # Set initial input values to the betting market function parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability expertise_values = np.linspace(0,1,N_bettors) av_budget=100 budget_total = av_budget* N_bettors N_whales = 1 whale_valuation = 0.6 expertise_whale = {} for prop_whale in np.arange(0,0.55,0.1): whale_effect = [] whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) non_whale_bettors = [bettor(budget=av_budget, expertise=expertise_values[i]) for i in range(N_bettors)] whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=whale_valuation) for _ in range(N_whales)] parameters.update({'bettors': non_whale_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) whale_effect.append((np.array(market_record['price_history']) - np.array(market_record['gen_el']))) expertise_whale[prop_whale] = whale_effect ``` -------------------------------- ### Initialize and Run Betting Market Simulation Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results.ipynb Sets up and runs a betting market simulation. Initializes parameters like number of bettors, election time, initial price, and outcome uncertainty. Stores expertise and budget changes over iterations. ```python # set seed np.random.seed(0) initial_budget=100 expertise_store = [] budgets = [] # Set initial input values to the betting market function parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability expertise_values = np.linspace(0,1,N_bettors) parameters.update({'bettors': [bettor(expertise=e, budget=initial_budget) for e in expertise_values]}) for _ in range(n_iter_): market_record = run_market(**parameters) mse, min_sig_lag = evaluate_markets(market_record) expertise_store.append([expertise_values]) budgets.append([x-initial_budget for x in market_record['final_budgets']]) # now with a whale ``` -------------------------------- ### Initialize Simulation Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results.ipynb Sets up the initial parameters for the betting market simulation, including the number of bettors, election time, initial price, and outcome uncertainty. It also initializes bettor agents with varying expertise and budgets. ```python parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability expertise_values = np.linspace(0,1,N_bettors) av_budget=100 budget_total = av_budget* N_bettors N_whales = 1 expertise_whale = {} for prop_whale in [0,0.01,0.1,0.25,0.5]: expertise_store_whale = [] budgets_whale = [] whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) non_whale_bettors = [bettor(budget=av_budget, expertise=expertise_values[i]) for i in range(N_bettors)] whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=1) for _ in range(N_whales)] parameters.update({'bettors': non_whale_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) expertise_store_whale.append([expertise_values]) budgets_whale.append([x-av_budget for x in market_record['final_budgets'][:-1]]) expertise_whale[prop_whale] = [expertise_store_whale, budgets_whale] ``` -------------------------------- ### Initialize Simulation Parameters and Run Market Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_shock_whale.ipynb Sets up bettor populations, including whale bettors with varying budget proportions, and runs market simulations for each scenario. This code iterates through different whale proportions to observe their effects. ```python expertise_values = np.linspace(0,1,N_bettors) av_budget=100 budget_total = av_budget* N_bettors N_whales = 1 whale_valuation = 0.7 expertise_whale = {} for prop_whale in [0,0.01,0.1,0.5,0.7]: whale_effect = [] whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) non_whale_bettors = [bettor(budget=av_budget, expertise=0.95, risk_av=1, stubbornness=0) for i in range(N_bettors)] whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=whale_valuation,risk_av=1, stubbornness=0) for _ in range(N_whales)] print(f'Running for prop_whale={prop_whale}, whale_budget={whale_budget} and total non-whale budget={av_budget*N_bettors}') parameters.update({'bettors': non_whale_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) whale_effect.append((np.array(market_record['price_history']) - np.array(market_record['gen_el']))) expertise_whale[prop_whale] = whale_effect ``` -------------------------------- ### Initialize Bettors and Run Simulation Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_linearlagfit.ipynb Sets up different types of bettors (alpha_1, alpha_neg1, no_alpha, whale) and runs a market simulation using the 'run_market_fixed_el' function. Ensure all necessary parameters are defined before execution. ```python alpha_1_bettors = [bettor(budget=av_budget, market_imitation = 1) for i in range(b_numbers[0])] alpha_neg1_bettors = [bettor(budget=av_budget, market_imitation = -1) for i in range(b_numbers[1])] no_alpha_bettors = [bettor(budget=av_budget, market_imitation = 0) for i in range(b_numbers[2])] # whale whale_budget = av_budget*(np.sum(b_numbers))* prop_whale / (1-prop_whale) whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=0.2) for _ in range(N_whales)] parameters.update({'bettors': alpha_1_bettors + alpha_neg1_bettors + no_alpha_bettors + whale_bettors}) market_record = run_market_fixed_el(**parameters) ``` -------------------------------- ### Run Market Simulation with Whales Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_linearlagfit.ipynb Sets up and runs a market simulation with various betting agents, including 'whale' bettors, over multiple iterations. It records the difference between market price and the generated election outcome. ```python N_bettors = 20 av_budget=100 prop_whale = 0.3 N_whales = 1 alpha_store_shock = {} for case in cases: alpha_effect = [] parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} b_numbers = N_bettors*np.array(case) b_numbers = [int(x) for x in b_numbers] alpha_1_bettors = [bettor(budget=av_budget, market_imitation = 1) for i in range(b_numbers[0])] alpha_neg1_bettors = [bettor(budget=av_budget, market_imitation = -1) for i in range(b_numbers[1])] no_alpha_bettors = [bettor(budget=av_budget, market_imitation = 0) for i in range(b_numbers[2])] # whale whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=1) for _ in range(N_whales)] parameters.update({'bettors': alpha_1_bettors + alpha_neg1_bettors + no_alpha_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) alpha_effect.append((np.array(market_record['price_history']) - np.array(market_record['gen_el']))) var_save.append(np.var(market_record['price_history'])) alpha_store_shock[case] = alpha_effect ``` -------------------------------- ### Set Initial Input Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/extensions/main_predmkt.ipynb Defines the initial parameters for the betting market simulation, including the number of betters, election time, initial market price, and outcome uncertainty. ```python parameters = {'n_betters': 100, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': 100, # Time until election takes place (ie. time horizon of betting) 'initial_price': 0.5, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': 0.1} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability parameters.update({'betters': [better() for _ in range(parameters['n_betters'])]}) ``` -------------------------------- ### Set Initial Betting Market Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_linearlagfit.ipynb Configures the initial input parameters for the betting market simulation. This includes setting the number of bettors, time horizon, initial price, and outcome uncertainty. It also initializes the bettors with specified budgets and market valuations. ```python # Set initial input values to the betting market function parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability parameters.update({'bettors': [bettor(budget=100) for _ in range(parameters['n_bettors'])] + [bettor(budget=200, market_valuation=1,whale=True)]}) ``` -------------------------------- ### Initialize Simulation Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Sets global parameters for the betting market simulations, including the number of bettors, simulation time, initial market price, and outcome uncertainty. ```python # Global parameters N_bettors = 100 T_max = 200 initial_price = 0.5 rw_variance = 0.02 # number of iterations for each set of parameters n_iter_ = 100 ``` -------------------------------- ### Simulate Market with Whale Bettors Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Sets up and runs market simulations with a specified proportion of whale bettors. It stores expertise and budget data for analysis. ```python expertise_whale = {} for prop_whale in [0.02,0.05,0.08]: expertise_store_whale = [] budgets_whale = [] whale_budget = av_budget*N_bettors*prop_whale / (1-prop_whale) non_whale_bettors = [bettor(budget=av_budget, expertise=expertise_values[i]) for i in range(N_bettors)] whale_bettors = [bettor(budget=whale_budget, whale=True, market_valuation=1) for _ in range(N_whales)] parameters.update({'bettors': non_whale_bettors + whale_bettors}) for _ in range(n_iter_): market_record = run_market(**parameters) expertise_store_whale.append([expertise_values]) budgets_whale.append([x-av_budget for x in market_record['final_budgets'][:-1]]) expertise_whale[prop_whale] = [expertise_store_whale, budgets_whale] ``` -------------------------------- ### Initialize Bettor Market Function Inputs Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_bettortypes.ipynb Sets the initial input values for the betting market function. This is a foundational step before running simulations. ```python from typing import Dict, List from numpy import array, ndarray from src.simulations.simulation_results_bettortypes import simulation_results_bettortypes def betting_market_function(bet_type: str, bet_amount: float, odds: float, bettor_type: str) -> Dict: """The betting market function simulates the outcome of a bet. Args: bet_type: The type of bet placed (e.g., "win", "place"). bet_amount: The amount of money bet. odds: The odds of the bet. bettor_type: The type of bettor placing the bet. Returns: A dictionary containing the bet outcome, profit/loss, and updated balance. """ # Placeholder for actual betting market logic # This function would determine win/loss based on bet_type, odds, and a random outcome # For now, it returns a dummy result return { "outcome": "win", "profit_loss": bet_amount * odds, "balance": 1000.0 # Dummy balance } def simulation_results_bettortypes(bettor_types: List[str], num_simulations: int, initial_balance: float) -> Dict: """Runs simulations for different bettor types and returns the results. Args: bettor_types: A list of bettor types to simulate. num_simulations: The number of simulations to run for each bettor type. initial_balance: The starting balance for each simulation. Returns: A dictionary where keys are bettor types and values are lists of simulation results. """ results_dict = {} for bettor_type in bettor_types: simulation_results = [] for _ in range(num_simulations): # Simulate a single bet bet_type = "win" # Example bet type bet_amount = 10.0 # Example bet amount odds = 2.5 # Example odds # Call the betting market function (replace with actual implementation) outcome = betting_market_function(bet_type, bet_amount, odds, bettor_type) # Store relevant results (e.g., profit/loss, final balance) simulation_results.append({ "profit_loss": outcome["profit_loss"], "final_balance": outcome["balance"] }) results_dict[bettor_type] = simulation_results return results_dict # Example usage: if __name__ == "__main__": bettor_types_to_simulate = ["random", "value", "contrarian"] number_of_simulations = 100 starting_balance = 1000.0 simulation_outcomes = simulation_results_bettortypes(bettor_types_to_simulate, number_of_simulations, starting_balance) # Print a summary of the results (e.g., average profit/loss per bettor type) for bettor_type, results in simulation_outcomes.items(): total_profit_loss = sum([r["profit_loss"] for r in results]) average_profit_loss = total_profit_loss / len(results) print(f"{bettor_type}: Average Profit/Loss = {average_profit_loss:.2f}") ``` -------------------------------- ### Run Dash App for Prediction Market Visualization Source: https://github.com/ebbam/power_prediction/blob/main/README.md Launch the Dash application to interactively test the agent-based prediction market model. Ensure market_functions.py is available. ```python run "market_visualisation.py" to launch the Dash app on a local host. ``` -------------------------------- ### Run Market Simulation with Specific Parameters Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_linearlagfit.ipynb Configures and executes a market simulation with a larger number of bettors and a specific whale proportion. It includes the pre-generated election outcome and a parameter 'k'. ```python N_bettors = 100 av_budget=1000 prop_whale = 0.2 N_whales = 1 T_max=100 records = {} for case in cases: alpha_effect = [] parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance, 'gen_el' : gen_el_res, 'k':3} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability b_numbers = N_bettors*np.array(case) b_numbers = [int(x) for x in b_numbers] ``` -------------------------------- ### Running Market Simulation Without Whale Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_shock_whale.ipynb Sets up and runs a market simulation with a specified number of bettors, each having expertise and an initial budget. It stores expertise and budget changes for analysis. ```python # set seed np.random.seed(0) initial_budget=100 expertise_store = [] budgets = [] # Set initial input values to the betting market function parameters = {'n_bettors': N_bettors, # The number of betting agents #'el_outcome': 1, # Q: Ultimate election outcome - assuming we know this to begin with and it does not change over time...for now this is implemented as a random walk of the probability...but should this be 0 or 1 instead? ''' 't_election': T_max, # Time until election takes place (ie. time horizon of betting) 'initial_price': initial_price, # Initial market price (is this equivalent to probability of winning) 'outcome_uncertainty': rw_variance} # This is a measure of how uncertain the true outcome is - ie. the volatility of the random walk election probability expertise_values = np.linspace(0,1,N_bettors) parameters.update({'bettors': [bettor(expertise=e, budget=initial_budget) for e in expertise_values]}) for _ in range(n_iter_): market_record = run_market(**parameters) mse, min_sig_lag = evaluate_markets(market_record) expertise_store.append([expertise_values]) budgets.append([x-initial_budget for x in market_record['final_budgets']]) # now with a whale ``` -------------------------------- ### Prepare Data for Plotting Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_whale_error.ipynb Creates a pandas DataFrame from the simulation results, organizing the whale valuation, the difference between market price and true outcome, and time into columns for plotting. ```python times = np.arange(T_max+1) vals = [] for k,val in expertise_whale.items(): for v in val: for x in range(T_max+1): vals.append([k,v[x],times[x]]) df_plot = pd.DataFrame(vals, columns = ['W','SE','T']) ``` -------------------------------- ### Initialize Results Dictionary Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_linearlagfit.ipynb Initializes an empty dictionary to store simulation results. This is typically the first step before collecting data. ```python results_dict = {} ``` -------------------------------- ### Multiple Simulation Run Outputs Source: https://github.com/ebbam/power_prediction/blob/main/code/simulations/simulation_results_shock_whale.ipynb This output logs the initiation of multiple simulation runs, detailing the whale proportion, calculated whale budget, and total non-whale budget for each. ```text Running for prop_whale=0.01, whale_budget=101.01010101010101 and total non-whale budget=10000 Running for prop_whale=0.1, whale_budget=1111.111111111111 and total non-whale budget=10000 Running for prop_whale=0.5, whale_budget=10000.0 and total non-whale budget=10000 Running for prop_whale=0.7, whale_budget=23333.33333333333 and total non-whale budget=10000 ```