-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulation_prediction_market.py
101 lines (82 loc) · 2.97 KB
/
simulation_prediction_market.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
"""
Simulation Prediction Market Main Executable
"""
import argparse
import pandas as pd
import numpy as np
from Agents.bayesian_agent import BayesianAgent
from Market_Maker.market_maker import MarketMaker
def parse_command_line():
"""
argument parser
:return: args
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"dataset", type=str, help="specify path to the dataset"
)
parser.add_argument(
"column_name", type=str, help="specify column name in the dataframe to\
make a prediction on"
)
parser.add_argument(
"-d", "--distribution", type=str, help="specify either Bernoulli\
or Gaussian distribution", choices=["Gaussian", "Bernoulli"]
)
args = parser.parse_args()
return args
def import_data(path, column_name):
"""
import real data from csv file
:param: path: str, data path
:return: processed_list: list, list of actual data
"""
res = pd.read_csv(path)
processed_list = res[column_name].tolist()
return processed_list
def main():
args = parse_command_line()
data_path = args.dataset
# the number of datapoints agent observes at a time
# same across all agents
num_observed_sample = 4
# import data
data = import_data(data_path, args.column_name)
real_outcome = sum(data) / len(data)
# initialize market maker
mk = MarketMaker(distribution=args.distribution)
############### Agents enter with Poisson distribution ###############
# enter_times = np.random.poisson(7.5, max_iteration)
# lambda*max_iteration ~= size of dataset
# enter_times = list(np.cumsum(enter_times) + num_observed_sample)
######################################################################
# The agent are now all the same
# They observe different data points though
try:
agent = BayesianAgent(distribution=args.distribution)
except TypeError:
return
enter_time = 0
# The number of agents in this market is not pre-ordained
# The trade will stop once the market reached equilibrium
while True:
last_market_price = mk.current_market_price
observed_data = data[enter_time: enter_time + num_observed_sample]
enter_time += num_observed_sample
# Out of data to observe
if enter_time > len(data):
break
shares_to_buy, _ = agent.calculate_shares_to_buy(observed_data,\
mk.num_trade, mk.current_market_price)
mk.update_param(shares_to_buy)
# TODO: equilibrium is necessary in real life market, not a must here
if mk.market_equilibrium(last_market_price):
break
# Payoff logic
# For Bernoulli distribution the payoff is quite straight forward
# payoff = [agent.security_amount * real_outcome for agent in agents]
print("Current market price is {}, while the real outcome is {}".\
format(mk.current_market_price, real_outcome))
if __name__ == '__main__':
main()