-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverse.py
81 lines (75 loc) · 2.54 KB
/
inverse.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
import numpy as np
import matplotlib.pyplot as plt
import argparse
import yaml
from run import main_run, posterior_probability_computation
from test import generate_random_A, chinese_restaurant_partition
def naive_partitions_from_probs(
list_qt_alpha_proba,
list_qt_beta_proba,
epsilon=0.01,
N_max_found=100,
N=5,
N_max_search=1000,
A_arg=None,
w_arg=None,
):
"""Generates partitions from list posterior probabilities associated to the
two agents."""
list_found = {"A": [], "w": [], "partition_1": [], "partition_2": []}
for seed in range(N_max_search):
print(seed)
if A_arg is None:
A = generate_random_A(N)
else:
A = A_arg
if w_arg is None:
w = A[np.random.randint(0, len(A))]
else:
w = w_arg
partition_1 = chinese_restaurant_partition(N)
partition_2 = chinese_restaurant_partition(N)
results_runs = {}
my_experiment = posterior_probability_computation(
N=N, partition_1=partition_1, partition_2=partition_2, A=A, w=w
)
list_qt_alpha_proba_predicted, list_qt_beta_proba_predicted, lenght, t = (
my_experiment.run()
)
if len(list_qt_alpha_proba_predicted) == len(list_qt_alpha_proba) and len(
list_qt_beta_proba_predicted
) == len(list_qt_beta_proba):
if (
np.linalg.norm(
np.array(list_qt_alpha_proba)
- np.array(list_qt_alpha_proba_predicted)
)
<= epsilon
and np.linalg.norm(
np.array(list_qt_beta_proba)
- np.array(list_qt_beta_proba_predicted)
)
<= epsilon
):
list_found["A"].append(A)
list_found["w"].append(w)
list_found["partition_1"].append(partition_1)
list_found["partition_2"].append(partition_2)
if len(list_found["A"]) > N_max_found:
return list_found
return False
if __name__ == "__main__":
list_qt_alpha_proba = [1 / 4, 1 / 4, 1 / 4, 1 / 4, 1 / 2]
list_qt_beta_proba = [3 / 4, 3 / 4, 3 / 4, 3 / 4, 1 / 2]
epsilon = 0.01
N = 20
N_max_search = 15000
list_found = naive_partitions_from_probs(
list_qt_alpha_proba=list_qt_alpha_proba,
list_qt_beta_proba=list_qt_beta_proba,
epsilon=epsilon,
N=N,
N_max_search=N_max_search,
A_arg=None,
w_arg=None,
)