-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_distribution_at_time.py
122 lines (99 loc) · 4.77 KB
/
plot_distribution_at_time.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from argparse import ArgumentParser
from math import floor, ceil
from matplotlib import pyplot as plt, rc
from matplotlib.ticker import FormatStrFormatter
from numpy import arange
from numpy import median
from numpy.random import seed
from mechanism_names import parse_mechanisms, describe_mechanisms
from simulations import Graph
def plot_distribution_at_time(mechanism_classes, gamma, outdegree, d, time, num_runs, random_seed, plot_path=None):
"""Plot a histogram of maximum weight frequencies at time T over many runs.
Args:
mechanism_classes (list of classes inheriting from Mechanism)
gamma (float): gamma in the graph model
outdegree (int): k in the graph model
d (float): probability of delegating in the graph model
time (int): total time steps to run
num_runs (int): number of runs at which to compute max degree at time T
random_seed (int): seed for randomness
plot_path (string / None): Output path for graphic. File extensions as allowed by matplotlib, e.g. pdf.
"""
mechanism_abbrevs = ""
for mechanism_class in mechanism_classes:
mechanism_abbrevs += mechanism_class.PLOT_ABBREVIATION
title = (f"hist_{mechanism_abbrevs}_g{round(gamma * 100)}_k{outdegree}_d{round(d * 100)}_T{time}_num{num_runs}"
f"_sd{random_seed}")
if plot_path is None:
plot_path = f"data/histplots/{title}.pdf"
rc('font', **{'family': 'serif', 'serif': ['Libertine']})
rc('text', usetex=True)
seed(random_seed)
mechanisms_history = {observer_class.PLOT_ABBREVIATION: [] for observer_class in mechanism_classes}
assert num_runs > 0
for i in range(num_runs):
graph = Graph(gamma, d, outdegree)
mechanisms = [observer_class(graph) for observer_class in mechanism_classes]
print(i)
for t in range(2, time + 1):
graph.add_node()
for mechanism in mechanisms:
delegations = mechanism.get_delegations()
max_weight = mechanism.max_weight_from_delegations(delegations)
mechanisms_history[mechanism.PLOT_ABBREVIATION].append(max_weight)
all_max_weight_histories = []
mechanism_names = []
for mechanism in mechanisms_history:
all_max_weight_histories.append(mechanisms_history[mechanism])
for mechanism in mechanisms:
mechanism_names.append(mechanism.PLOT_LABEL)
min_weight = floor(min(min(all_max_weight_histories)))
max_weight = ceil(max(max(all_max_weight_histories)))
# plot
num_plots = len(all_max_weight_histories)
fig, ax = plt.subplots(num_plots, sharex=True, sharey=True, figsize=(6.4, 3.2))
for i in range(num_plots):
ax[i].hist(all_max_weight_histories[i], normed=1, bins=arange(min_weight - 0.5, max_weight + 1.5, 1),
color=mechanism_classes[i].PLOT_COLOR, label=mechanism_classes[i].PLOT_LABEL)
ax[i].axvline(median(all_max_weight_histories[i]), color='black', linestyle='solid', linewidth=1)
ax[i].legend(loc=1)
ax[i].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
ax = fig.add_subplot(111, frameon=False)
plt.tick_params(labelcolor='none', top='off', bottom='off', left='off', right='off')
plt.grid(False)
plt.xlabel('maximum weight')
plt.ylabel('frequency')
ax.yaxis.labelpad = 10
plt.savefig(plot_path, bbox_inches='tight')
if __name__ == "__main__":
parser = ArgumentParser(description='Inputs.')
parser.add_argument('-g', type=float, nargs='+', default=[1],
help='values of gamma (list of float)')
parser.add_argument('-k', type=int, nargs='+', default=[2],
help='values of k > 0 (list of int)')
parser.add_argument('-d', type=float, nargs='+', default=[0.5],
help='values of d \in (0,1) (list of float)')
parser.add_argument('-t', type=int, default=100,
help='value of T > 0 (int)')
parser.add_argument('-num', type=int, default=10,
help='value of num iters > 0 (int)')
parser.add_argument('-sd', type=int, default=0,
help='value of seed (int)')
parser.add_argument('-m', type=str, default='prcsAa',
help='mechanisms to use:\n' + describe_mechanisms(False))
parser.add_argument('-o', type=str, default=None,
help='write path for plot')
args = parser.parse_args()
gammas = args.g
ks = args.k
ds = args.d
time = args.t
num_runs = args.num
random_seed = args.sd
plot_path = args.o
mechanism_classes = parse_mechanisms(args.m, False)
print(args)
for gamma in gammas:
for d in ds:
for k in ks:
plot_distribution_at_time(mechanism_classes, gamma, k, d, time, num_runs, random_seed, plot_path)