-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmarginal_loopy.py
More file actions
159 lines (111 loc) · 4.79 KB
/
Copy pathmarginal_loopy.py
File metadata and controls
159 lines (111 loc) · 4.79 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import numpy as np
import pickle
import multiprocessing as mp
from multiprocessing import Pool
import matplotlib
# matplotlib.rcParams.update({'font.size': 18})
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import itertools
from collections import defaultdict
from joblib import Parallel, delayed
from scipy.stats import multivariate_normal
# from modules import GaussianDiag, EP, MMSE, PowerEP, StochasticEP, ExpansionEP, ExpansionPowerEP, ExpectationConsistency, LoopyBP, LoopyMP, PPBP, AlphaBP, MMSEalphaBP, ML, VariationalBP, MMSEvarBP, EPalphaBP
import sys
sys.path.append("./src")
from utils import channel_component, sampling_noise, sampling_signal, sampling_H,real2complex
import matplotlib.pyplot as plt
import itertools
from collections import defaultdict
from joblib import Parallel, delayed
from scipy.stats import multivariate_normal
from loopy_modules import LoopyBP, AlphaBP, ML, MMSEalphaBP, Marginal
from utils import channel_component, sampling_noise, sampling_signal, sampling_H,real2complex, ERsampling_S
# configuration
class hparam(object):
num_tx = 9
num_rx = 9
soucrce_prior = [0.5, 0.5]
signal_var = 1
connect_prob = np.linspace(0.0, 0.9, 10)
monte = 30
constellation = [int(-1), int(1)]
alpha = None
stn_var= 1
# algos = {"LoopyBP": {"detector": LoopyBP, "alpha": None},
# }
algos = {"BP": {"detector": LoopyBP, "alpha": None, "legend": "BP", "row": 0},
# "AlphaBP, 0.2": {"detector": AlphaBP, "alpha": 0.2, "legend": r'$\alpha$-BP, 0.2'},
# "MMSEalphaBP, 0.4": {"detector": MMSEalphaBP, "alpha": 0.4, "legend": r'$\alpha$-BP+MMSE, 0.4', "row": 1},
"AlphaBP, 0.4": {"detector": AlphaBP, "alpha": 0.4, "legend": r'$\alpha$-BP, 0.4', "row": 1},
# "AlphaBP, 0.6": {"detector": AlphaBP, "alpha": 0.6, "legend": r'$\alpha$-BP, 0.6'},
"AlphaBP, 0.8": {"detector": AlphaBP, "alpha": 0.8, "legend": r'$\alpha$-BP, 0.8',"row": 2},
"AlphaBP, 1.2": {"detector": AlphaBP, "alpha": 1.2, "legend": r'$\alpha$-BP, 1.2', "row": 3}
}
iter_num = 100
for _, value in algos.items():
value["ser"] = []
def task(erp):
tmp = dict()
for name,_ in hparam.algos.items():
tmp[name] = []
for key, method in hparam.algos.items():
dict_marg = {"true":[], "est": []}
for monte in range(hparam.monte):
# sampling the S and b for exponential function
S, b = ERsampling_S(hparam, erp)
# compute the joint ML detection
detectMg = Marginal(hparam)
true_marginals = detectMg.detect(S, b)
# marginals estimated
hparam.alpha = method['alpha']
detector = method['detector'](None, hparam)
detector.fit(S=S,
b=b,
stop_iter=hparam.iter_num)
estimated_marginals = detector.marginals()
## concatenate the marginals
dict_marg["true"] = np.concatenate((dict_marg["true"], true_marginals[:,0]))
dict_marg["est"] = np.concatenate((dict_marg["est"], estimated_marginals[:,0]))
tmp[key].append(dict_marg)
# performance should be made by comparing with ML
performance = {"erp": erp}
for key, method in hparam.algos.items():
#method["ser"].append( np.mean(tmp[key])/hparam.num_tx )
performance[key] = tmp[key]
return performance
results = []
def collect_result(result):
global results
results.append(result)
# task(hparam.connect_prob[0])
pool = mp.Pool(mp.cpu_count())
results = pool.map(task, list(hparam.connect_prob))
pool.close()
performance = defaultdict(list)
#for the_result in RESULTS:
for connect_prob in list(hparam.connect_prob):
for the_result in results:
if the_result["erp"] == connect_prob:
for key, _ in hparam.algos.items():
performance[key].append( the_result[key] )
# save the experimental results
with open("figures/marginal_prob.pkl", 'wb') as handle:
pickle.dump(performance, handle)
# for snr in hparam.snr:
marker_list = ["o", "<", "+", ">", "v", "1", "2", "3", "8"]
iter_marker_list = iter(marker_list)
figure_format = 2*100 + hparam.connect_prob * 10
figure = plt.figure(figsize=(30,30))
for key, method in hparam.algos.items():
for i, prob in enumerate(hparam.connect_prob):
ax = figure.add_subplot(len(hparam.algos), 10, i+1 + method["row"] * 10, adjustable='box', aspect=1)
ax.scatter(performance[key][i][0]["true"],
performance[key][i][0]["est"])
ax.set_xlim(0,1)
ax.set_ylim(0,1)
# .set(xlabel="Edge Probability", ylabel="MAP Accuracy")
# ax.grid()
figure.tight_layout()
figure.savefig("figures/marginal_acc.pdf")
figure.show()