-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNoNbTrolsModel.py
122 lines (98 loc) · 2.61 KB
/
NoNbTrolsModel.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
import numpy as np
import numpy.random as npr
import scipy.stats as ss
import utilities as ut
import math
from Model import *
from NbTrolsModel import *
class NoNbTrolsModel(Model):
"""
See docstring describing class NbTrolsModel
"""
def __init__(self, nb, na):
"""
Constructor
Parameters
----------
nb : int
na : int
Returns
-------
None
"""
Model.__init__(self, nb, na)
def get_shapes1(self):
"""
Returns a list of the shapes of the elements of a list1.
Returns
-------
list[tuple]
"""
na = self.na
nb = self.nb
powna = 1 << na
return [(1 << k,) for k in range(na)] + [(powna,)]*nb
def prob_x(self, x,
list1_angs,
verbose=False):
"""
Returns probability of input x, P(x).
Parameters
----------
x : int
x is an int in range(powna).
list1_angs : list[np.array]
verbose : bool
Returns
-------
float
"""
return NbTrolsModel.static_prob_x(x, self.na, list1_angs, verbose)
def prob_y_given_x_and_angs_prior(self, y, x,
list1_angs,
verbose=False):
"""
Returns the probability of y given x and list1_angs, P(y | x,
list1_angs).
Parameters
----------
y : int
int in range(pownb)
x : int
int in range(powna)
list1_angs : list[np.array]
verbose : bool
Returns
-------
float
"""
na = self.na
prob = 1.
# y = output in decimal
ybin = ut.dec_to_bin_vec(y, self.nb)
num_trols = na
for angs in list1_angs[na:]:
ylast_bit = ybin[num_trols-na]
factor = ut.ang_to_cs2_prob(angs[x], ylast_bit)
if verbose:
print('num_trols=', num_trols,
"ybin[:num_trols-na], y_last_bit",
ybin[:num_trols-na],
ylast_bit)
prob *= factor
num_trols += 1
if verbose:
print('\tybin, prob:', ybin, prob)
return prob
if __name__ == "__main__":
def main():
nsam = 10
na = 2
nb = 3
mod = NoNbTrolsModel(nb, na)
y_nsam_nb, x_nsam_na = mod.gen_toy_data(nsam, verbose=True)
print("x_nsam_na:")
print(x_nsam_na)
print("\ny_nsam_nb:")
print(y_nsam_nb)
main()