/
gmm_v2.py
314 lines (279 loc) · 11.1 KB
/
gmm_v2.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Gaussian Mixture Models with a two centroid seeking agents
# Kennan Grant, Nitesh Prakash
# Machine Learning, Mid-Term Project
# 3/18/2018
import numpy as np
import pandas as pd
import random as rand
import matplotlib.pyplot as plt
from scipy.stats import stats
from matplotlib.patches import Ellipse
from sklearn.datasets.samples_generator import make_blobs
from sklearn.mixture import GaussianMixture
import random
import math
from scipy.stats import ks_2samp
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
# Counter for no. of clicks on buttons
noMoves = 0
moves = 0
surprises = 0
resetCount = 0
# Iteration Counter
i = 0
# Set initial values
random.seed(1)
max_iter = 100
# Initial location of agents and distribution center
agentR = (-25, -25) # initial location of agentR
agentB = (25, -25) # initial location of agentB
my_centers = ((-5, -5),(5, 5))
# Global Variables to hold models and dataset
gmm = None
ks_df_OLD = None
X_old = None
Y_old = None
# Function to calculate GMM and find the new location of the agents using a predetermined scaling factor
def estimateMeanConvergence(noMoveClick, moveClick, surpriseMe, noOfResets, alpha, noOfSamples):
# declare the below variables as using global scope since they are reassigned in the function
global noMoves
global moves
global surprises
global i
global resetCount
global my_centers
global agentR
global agentB
global max_iter
global gmm
global ks_df_OLD
global X_old
global Y_old
# indicator to check if KS statistic needs to be run
runKS = 0
# indicator to check if same distn. is selected
sameDist = 0
# initialize variables to initial values if reset button is pressed
if noOfResets>resetCount:
resetCount = noOfResets
noMoves = 0
moves = 0
surprises = 0
i = 0
random.seed(1)
max_iter = 100
agentR = (-25, -25)
agentB = (25, -25)
my_centers = ((-5, -5),(5, 5))
gmm = None
ks_df_OLD = None
X_old = None
Y_old = None
# Detect which button was pressed and generate data accordingly
if noMoveClick>noMoves:
move = 0
sameDist = 1
noMoves = noMoveClick
elif moveClick>moves:
move = 1
moves = moveClick
elif surpriseMe>surprises:
move = random.randint(0, 1)
runKS = 1
surprises = surpriseMe
else:
print("Button press not detected. Assuming same distribution")
move = 0
sameDist = 1
print("iteration: %d"%i)
# assign center of distributions
my_centers = ((my_centers[0][0] + 1*move, my_centers[0][1] + 4*move),
(my_centers[1][0] - 3*move, my_centers[1][1] - 1*move))
# draw samples
X, y_true = make_blobs(n_samples=int(noOfSamples), centers=my_centers,
cluster_std=1.5, random_state=i)
# stack observations if new data from same distribution (in order to incorporate more data into estimate of mean).
# otherwise, only use new data.
#
# note: assumes independence of x and y
if i!= 0:
# fit GMM using previous means as initial means
gmm = GaussianMixture(n_components=2, means_init = gmm.means_,
max_iter=max_iter).fit(X)
# extract predicted labels
labels = gmm.predict(X)
ks_df = pd.DataFrame(np.column_stack((X, labels, y_true)))
ks_df.columns=['x1', 'x2', 'labels','true_labels']
# if unknown is selected and if estimated underlying distributions have not changed, stack data.
#
# note: assumes independence of x and y (i.e. uses univariate ks test)
if runKS == 1:
print("Random Distribution:%d"%move)
print(ks_2samp(ks_df[ks_df['labels'] == 1]['x1'], ks_df_OLD[ks_df_OLD['labels'] == 1]['x1'])[1],\
ks_2samp(ks_df[ks_df['labels'] == 1]['x2'], ks_df_OLD[ks_df_OLD['labels'] == 1]['x2'])[1],\
ks_2samp(ks_df[ks_df['labels'] == 0]['x1'], ks_df_OLD[ks_df_OLD['labels'] == 0]['x1'])[1],\
ks_2samp(ks_df[ks_df['labels'] == 0]['x2'], ks_df_OLD[ks_df_OLD['labels'] == 0]['x2'])[1])
if ks_2samp(ks_df[ks_df['labels'] == 1]['x1'], ks_df_OLD[ks_df_OLD['labels'] == 1]['x1'])[1] > .99 and\
ks_2samp(ks_df[ks_df['labels'] == 1]['x2'], ks_df_OLD[ks_df_OLD['labels'] == 1]['x2'])[1] > .99 and\
ks_2samp(ks_df[ks_df['labels'] == 0]['x1'], ks_df_OLD[ks_df_OLD['labels'] == 0]['x1'])[1] > .99 and\
ks_2samp(ks_df[ks_df['labels'] == 0]['x2'], ks_df_OLD[ks_df_OLD['labels'] == 0]['x2'])[1] > .99:
# stack observations
print("Same Distribution: KS")
X = np.vstack((X_old, X))
y_true = np.concatenate((Y_old, y_true), axis = 0)
# y_true = y_true.flatten()
# if same distribution is selected stack data
if sameDist == 1:
# stack observations
print("Same Distribution: Button")
print(ks_2samp(ks_df[ks_df['labels'] == 1]['x1'], ks_df_OLD[ks_df_OLD['labels'] == 1]['x1'])[1],\
ks_2samp(ks_df[ks_df['labels'] == 1]['x2'], ks_df_OLD[ks_df_OLD['labels'] == 1]['x2'])[1],\
ks_2samp(ks_df[ks_df['labels'] == 0]['x1'], ks_df_OLD[ks_df_OLD['labels'] == 0]['x1'])[1],\
ks_2samp(ks_df[ks_df['labels'] == 0]['x2'], ks_df_OLD[ks_df_OLD['labels'] == 0]['x2'])[1])
X = np.vstack((X_old, X))
y_true = np.concatenate((Y_old, y_true), axis = 0)
# y_true = y_true.flatten()
# fit GMM
gmm = GaussianMixture(n_components=2, init_params='kmeans',
max_iter=max_iter).fit(X)
# extract predicted labels
labels = gmm.predict(X)
# create ks df for next iteration
ks_df_OLD = pd.DataFrame(np.column_stack((X, labels, y_true)))
ks_df_OLD.columns=['x1', 'x2', 'labels','true_labels']
# update target means for agents to seek
B_mean_estimate, R_mean_estimate = tuple(gmm.means_[0]), tuple(gmm.means_[1])
## move agents towards respective estimated means
#
# calc distances from agents to respective means
distanceR = (sum((np.array(R_mean_estimate) - np.array(agentR))**2))**(1/2) # unweighted....no log prob...
distanceB = (sum((np.array(B_mean_estimate) - np.array(agentB))**2))**(1/2) # unweighted....no log prob...
# calculate angles
angle_degreeR = math.degrees(math.atan2(R_mean_estimate[1] - agentR[1],
R_mean_estimate[0] - agentR[0]))
angle_degreeB = math.degrees(math.atan2(B_mean_estimate[1] - agentB[1],
B_mean_estimate[0] - agentB[0]))
# scale (if set to 1, agent will move all the way to mean of respective distribution)
# replaced .5 with alpha as the learning rate
scaleR = alpha
scaleB = alpha
# set new agent location (red)
agentR = agentR[0] + scaleR * distanceR * math.cos(angle_degreeR * math.pi / 180),\
agentR[1] + scaleR * distanceR * math.sin(angle_degreeR * math.pi / 180)
# set new agent location (blue)
agentB = agentB[0] + scaleB * distanceB * math.cos(angle_degreeB * math.pi / 180),\
agentB[1] + scaleB * distanceB * math.sin(angle_degreeB * math.pi / 180)
# make data copy for next iteration
X_old = np.copy(X)
Y_old = np.copy(y_true)
#Increment Counter
i+=1
def plotData(ks_df_OLD, agentR, agentB):
# Function that, given the distribution dataframe, and the agents
# location returns plot information.
dataRPlot = go.Scatter(
x=ks_df_OLD[ks_df_OLD['labels'] == 0]['x1'],
y=ks_df_OLD[ks_df_OLD['labels'] == 0]['x2'],
mode='markers',
opacity=0.7,
marker={
'color': 'pink',
'size': 15,
'line': {'width': 0.5, 'color': 'white'}
},
name='Red'
)
dataBPlot = go.Scatter(
x=ks_df_OLD[ks_df_OLD['labels'] == 1]['x1'],
y=ks_df_OLD[ks_df_OLD['labels'] == 1]['x2'],
#text=df[df['continent'] == i]['country'],
mode='markers',
opacity=0.7,
marker={
'color': 'turquoise',
'size': 15,
'line': {'width': 0.5, 'color': 'white'}
},
name='Blue'
)
agentRPlot = go.Scatter(
x=[agentR[0]],
y=[agentR[1]],
#text=df[df['continent'] == i]['country'],
mode='markers',
#opacity=0.7,
marker={
'color': 'red',
'size': 15,
'line': {'width': 0.5, 'color': 'white'}
},
name='Red Agent'
)
agentBPlot = go.Scatter(
x=[agentB[0]],
y=[agentB[1]],
#text=df[df['continent'] == i]['country'],
mode='markers',
#opacity=0.7,
marker={
'color': 'blue',
'size': 15,
'line': {'width': 0.5, 'color': 'white'}
},
name='Blue Agent'
)
data = [dataRPlot, dataBPlot, agentRPlot, agentBPlot]
layout = go.Layout(
xaxis={'title': 'X1'},
yaxis={'title': 'X2'},
height = 500, width =500,
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
fig = dict(data=data, layout=layout)
return fig
# Invoking Dash app
app = dash.Dash()
app.layout = html.Div([
dcc.Graph(
id='GMM with Agent Model'
),
html.Button(id='no-move', n_clicks=0, children='Same Distribution'),
html.Button(id='move', n_clicks=0, children='Different Distribution'),
html.Button(id='surprise-move', n_clicks=0, children='I\'m Feeling Lucky!'),
html.Button(id='reset', n_clicks=0, children='Reset'),
dcc.Input(id='sample-size', type='number', value='15'),
html.Button(id='submit-button', n_clicks=0, children='Submit - Sample Size'),
html.Div(id='slider-descr', children = 'Select Learning Rate'),
dcc.Slider(
id='alpha-slider',
min=0,
max=1,
value=0.5,
step=0.1,
marks = {0:'0', 0.5:'0.5', 1:'1'}
)
])
@app.callback(
dash.dependencies.Output('GMM with Agent Model', 'figure'),
[dash.dependencies.Input('no-move', 'n_clicks'),
dash.dependencies.Input('move', 'n_clicks'),
dash.dependencies.Input('surprise-move', 'n_clicks'),
dash.dependencies.Input('reset', 'n_clicks'),
dash.dependencies.Input('alpha-slider', 'value'),
dash.dependencies.Input('submit-button', 'n_clicks')],
[dash.dependencies.State('sample-size', 'value')]
)
def generateData(noMoveClick, moveClick, surpriseMe, noOfResets, alpha, submitClicks, noOfSamples):
# First Run
print(noMoveClick, moveClick, surpriseMe, noOfResets, alpha, submitClicks, noOfSamples)
estimateMeanConvergence(noMoveClick, moveClick, surpriseMe, noOfResets, alpha, noOfSamples)
# Rename dataframe columns
return plotData(ks_df_OLD, agentR, agentB)
if __name__ == '__main__':
app.run_server(debug = False)