-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathms_artificial.py
More file actions
175 lines (149 loc) · 6.44 KB
/
ms_artificial.py
File metadata and controls
175 lines (149 loc) · 6.44 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
implements a multi-scale Gaussian mixture model for
generating artificial 2D data.
"""
import numpy as np
import tree
import random
#ok what are we going to do.
#suppose we have N people and K basic profiles.
#what's a profile? it's a set of basic answers to a question class.
#so for each person we will do the following:
#pick a profile 1->K
#then pick noise levels up the chain.
#so say we have 64 people, and we want 4 basic people profiles.
#about 16 end up in each category.
#so they each have a pure profile.
#now we want to further subdivide them into some # of groups
#and put some noise on those groups.
#so we assign them each a subgroup, and pick gaussian noise for each subgroup
#then we keep going until there are no more divisions possible.
def make_profile(profile_index,n_questions,means):
radix = len(means)
digits = [means[(profile_index / (radix**i)) % radix] for i in xrange(n_questions)]
return tuple(digits)
class ProbabilityField(object):
def __init__(self,means_matrix):
assert np.sum(means_matrix > 1.0) == 0
assert np.sum(means_matrix < 0.0) == 0
self.means = means_matrix
def permute(self):
if self.means.ndim == 1:
np.random.shuffle(self.means)
else:
assert self.means.ndim == 2
np.random.shuffle(self.means)
np.random.shuffle(self.means.T)
def realize(self):
r = np.random.rand(*self.means.shape)
data = -1*np.ones(self.means.shape)
data += 2*(r < self.means)
return data
class ArtificialQuestionnaire(object):
mean_choices = None
question_count = None
question_levels = []
question_noise_base = 0.01
question_noise_alpha = 0.25
people_count = None
people_levels = []
people_noise_base = 0.01
people_noise_alpha = 0.25
def __init__(self,**kwargs):
sqrt_flag= False
for key,val in kwargs.items():
if hasattr(self,key):
setattr(self,key,val)
if key == "sqrt":
sqrt_flag = val
if self.make_trees(sqrt_flag):
self.gen_means()
def _verify(self):
if self.mean_choices is None:
return False
if self.question_count is None:
return False
if self.question_levels == []:
return False
if self.people_count is None:
return False
if self.people_levels == []:
return False
return True
def make_trees(self,sqrt_flag=False):
if self._verify():
people_tree = tree.ClusterTreeNode(range(self.people_count))
for idx,level in enumerate(self.people_levels):
for y in people_tree.dfs_level(idx+1):
if level == 0:
y.create_subclusters(range(y.size))
else:
if sqrt_flag:
y.create_subclusters(np.sqrt(np.random.randint(0,level**2,size=y.size)).astype(np.int))
else:
y.create_subclusters(np.random.randint(0,level,size=y.size))
people_tree.make_index()
question_tree = tree.ClusterTreeNode(range(self.question_count))
for idx,level in enumerate(self.question_levels):
for y in question_tree.dfs_level(idx+1):
if level == 0:
y.create_subclusters(range(y.size))
else:
if sqrt_flag:
y.create_subclusters(np.sqrt(np.random.randint(0,level**2,size=y.size)).astype(np.int))
else:
y.create_subclusters(np.random.randint(0,level,size=y.size))
question_tree.make_index()
self.question_tree = question_tree
self.people_tree = people_tree
return True
else:
print "couldn't make trees, some data missing..."
return False
def gen_means(self):
people_profiles = len(self.people_tree.dfs_level(2))
question_profiles = len(self.question_tree.dfs_level(2))
for node in self.people_tree:
people_noise_var = self.people_noise_base*(self.people_noise_alpha**(node.level-2))
if node.idx == 0:
node.noise = np.zeros(question_profiles)
else:
node.noise = np.random.normal(0.0,np.sqrt(people_noise_var),question_profiles)
if node.level == 1:
node.mean = np.zeros(question_profiles)
elif node.level == 2:
max_profile = len(self.mean_choices)**question_profiles
bits = len(bin(max_profile))
node.profile = random.getrandbits(bits)
while node.profile > max_profile:
node.profile = random.getrandbits(bits)
node.mean = np.array(make_profile(node.profile,question_profiles,self.mean_choices))
else:
node.mean = node.parent.mean + node.noise
people_means = np.vstack([node.mean for node in self.people_tree.leaves()]).T
for node in self.question_tree:
question_noise_var = self.question_noise_base*(self.question_noise_alpha**(node.level-2))
if node.idx == 0:
node.noise = np.zeros(people_profiles)
else:
node.noise = np.random.normal(0.0,question_noise_var,people_profiles)
if node.level == 1:
node.mean = np.zeros(people_profiles)
else:
node.mean = node.parent.mean + node.noise
question_noise = np.vstack([node.mean for node in self.question_tree.leaves()])
self.means = np.zeros([self.question_count,self.people_count])
for question in self.question_tree.dfs_level(2):
self.means[question.elements,:] = people_means[question.idx-1,:]
for person in self.people_tree.dfs_level(2):
self.means[:,person.elements] += np.tile(question_noise[:,person.idx-1],(len(person.elements),1)).T
self.data = self.means.copy()
self.data = self.data*2 - 1.0
self.data[self.data > 1.0] = 1.0
self.data[self.data < -1.0] = -1.0
def realize(self):
data = np.zeros(self.means.shape)
r = np.random.rand(*self.means.shape)
data[r<self.means] = 1.0
data[data==0] = -1.0
return data