-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampling.py
312 lines (278 loc) · 11.5 KB
/
sampling.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 8 19:16:18 2018
@author: jb
"""
import numpy as np
from hamiltonMove import hamilton, langevin, langevin2
#from densityEstimawKNN import GradNormalKNN
from densityEstimawKNN import Gradient as gp
from pr import Pr_DensityGaussian, Pr_KDE
from rsample import randomSub
import matplotlib.pyplot as plt
def generating(iterNumMax, alpha, h, pr_thres, num_subset, num_feature, allsample):
#select specific feature to update
# =============================================================================
# print('featureIndex:', len(featureIndex))
# print('x:', len(x))
# xs = [x[j] for j in featureIndex]
# #print('xs: ', len(xs))
# s = np.shape(xs)
# vInit = np.random.uniform(0,10,s)
# =============================================================================
#print('vint: ',len(vInit))
g = gp('GradNormal')
#init pool of samples
generatedSample = []
x_initial = []
#n_all, p_all = np.shape(allsample)
vNext = np.zeros(num_feature)
v = np.zeros(num_feature)
for i in range(iterNumMax):
sample, featureIndex, sample_x = randomSub(allsample, num_subset, num_feature)
#xs = sample[np.random.choice(len(sample))]
#init start V
kk = 100
v[featureIndex] = vNext[featureIndex]
if i == 0:
# =============================================================================
# x1 = sample_x[np.random.choice(len(sample_x))]
# x2 = sample_x[np.random.choice(len(sample_x))]
# vInit = (x1[featureIndex] - x2[featureIndex]) / kk
# #vInit = np.random.uniform(0,11,num_feature) - 5
# #v = vInit.copy() * kk/ np.linalg.norm(vInit)
# v = vInit.copy()
# =============================================================================
x = sample_x[np.random.choice(len(sample_x))]
x1 = sample_x[np.random.choice(len(sample_x))]
vInit = np.zeros(num_feature)
vInit[featureIndex] = (x1[featureIndex] - x[featureIndex]) / kk
#print('vInit:',vInit)
v = vInit.copy()
x_initial = x.copy()
#xs = [x[j] for j in featureIndex]
grad = g.GradNormal(sample_x, x, num_feature, h)
x, vNext = hamilton(x, v, alpha, grad, i)
v = np.zeros(num_feature).copy()
# =============================================================================
# for index in featureIndex:
# x[featureIndex[index]] = xs[index]
# =============================================================================
probxnew = Pr_DensityGaussian(x, allsample, h)
xnow = x.copy()
print('pr:', probxnew)
# =============================================================================
# u = np.random.uniform(0,1)
# if (probxnew/pr_thres) >= u:
# generatedSample.append(x)
# =============================================================================
#CI method
if probxnew >= pr_thres:
generatedSample.append(x)
# =============================================================================
# plt.figure(1)
# plt.title('first step')
# plt.imshow(x1.reshape((8, 8)), cmap=plt.cm.binary, interpolation='nearest')
# =============================================================================
return generatedSample, x_initial, xnow, featureIndex, x1
def generating2(iterNumMax, yita, h, pr_thres, num_subset, num_feature, allsample, mu, damp):
g = gp('GradNormal')
#init pool of samples
generatedSample = []
x_initial = []
n_all, p_all = np.shape(allsample)
#vNext = np.zeros(p_all)
v = np.zeros(p_all)
for i in range(iterNumMax):
sample, featureIndex, sample_x = randomSub(allsample, num_subset, num_feature)
#kk = 0.1
kk = 1
#v[featureIndex] = vNext[featureIndex]
if i == 0:
flag0 = np.random.choice(len(sample_x))
x = sample_x[flag0]
# =============================================================================
# x1 = sample_x[np.random.choice(len(sample_x))]
# vInit = np.zeros(p_all)
# vInit[featureIndex] = (x1[featureIndex] - x[featureIndex]) / kk
# #print('vInit:',vInit)
# v = vInit.copy()
# x_initial = x.copy()
# =============================================================================
x_initial = x.copy()
flag = np.random.choice(len(sample_x))
x1 = sample_x[flag]
vInit = np.zeros(p_all)
vInit[featureIndex] = (x1[featureIndex] - x[featureIndex]) / kk
#print('vInit:',vInit)
v = vInit.copy()
#xs = [x[j] for j in featureIndex]
#grad = g.GradNormal(sample_x, x, num_feature)
# =============================================================================
# x, vNext = hamilton(x, v, alpha, grad, i)
# =============================================================================
x_input = x.copy()
for j in range(300):
# =============================================================================
# if j == 99:
# x, vNext = langevin2(x, v, yita, mu, damp, grad, j)
# else:
# x, v = langevin2(x, v, yita, mu, damp, grad, j)
# =============================================================================
#x_previous = x.copy()
#print('xprevious:',x_previous)
grad = g.GradNormal(sample_x, x, num_feature)
x, v = langevin2(x, v, yita, mu, damp, grad, j)
if j >= 100:
probxnew = Pr_DensityGaussian(x, allsample, h)
u = np.random.uniform(0,1)
if (probxnew/pr_thres) >= u:
generatedSample.append(x)
break
flag_now = j
#print('x',x)
#print('j:',j)
probxnew = Pr_DensityGaussian(x, allsample, h)
#x, vNext = langevin2(x, v, yita, mu, damp, grad, i)
v = np.zeros(p_all).copy()
#probxnew = Pr_DensityGaussian(x, allsample, h)
xnow = x.copy()
print('pr:', probxnew)
if j == flag_now:
x = x_input.copy()
# =============================================================================
# u = np.random.uniform(0,1)
# if (probxnew/pr_thres) >= u:
# generatedSample.append(x)
# print('flag:',flag)
# else:
# x = x_input.copy()
# =============================================================================
# =============================================================================
# plt.figure(1)
# plt.title('first step')
# plt.imshow(x1.reshape((8, 8)), cmap=plt.cm.binary, interpolation='nearest')
# =============================================================================
print('flag0:',flag0)
return generatedSample, x_initial, xnow, featureIndex, x1
def generating3(iterNumMax, yita, h, pr_thres, num_subset, num_feature, allsample, mu, damp, u):
g = gp('GradNormal')
#init pool of samples
generatedSample = []
rejectSample = []
status = []
v0 = []
x_initial = []
n_all, p_all = np.shape(allsample)
#vNext = np.zeros(p_all)
v = np.zeros(p_all)
for i in range(iterNumMax):
sample, featureIndex, sample_x = randomSub(allsample, num_subset, num_feature)
kk = 1
#v[featureIndex] = vNext[featureIndex]
if i == 0:
flag0 = np.random.choice(len(sample_x))
x = sample_x[flag0]
x_initial = x.copy()
flag = np.random.choice(len(sample_x))
x1 = sample_x[flag]
vInit = np.zeros(p_all)
vInit[featureIndex] = (x1[featureIndex] - x[featureIndex]) / kk
v = vInit.copy()
v0.append(vInit)
x_input = x.copy()
for j in range(1000):
grad = g.GradNormal(sample_x, x, num_feature)
x, v = langevin2(x, v, yita, mu, damp, grad, j)
probxnew = Pr_DensityGaussian(x, allsample, h)
v = np.zeros(p_all).copy()
xnow = x.copy()
print('pr:', probxnew)
#importance sampling
if (probxnew/pr_thres) >= u:
generatedSample.append(x)
status.append(1)
else:
rejectSample.append(x)
status.append(0)
x = x_input.copy()
return generatedSample, x_initial, xnow, rejectSample, status
def generating_logp(iterNumMax, yita, h, pr_thres, num_subset, num_feature, allsample, mu, damp, u):
g = gp('GradNormal')
#init pool of samples
generatedSample = []
rejectSample = []
status = []
v0 = []
x_initial = []
n_all, p_all = np.shape(allsample)
#vNext = np.zeros(p_all)
v = np.zeros(p_all)
for i in range(iterNumMax):
sample, featureIndex, sample_x = randomSub(allsample, num_subset, num_feature)
kk = 1
#v[featureIndex] = vNext[featureIndex]
if i == 0:
flag0 = np.random.choice(len(sample_x))
x = sample_x[flag0]
x_initial = x.copy()
flag = np.random.choice(len(sample_x))
x1 = sample_x[flag]
vInit = np.zeros(p_all)
vInit[featureIndex] = (x1[featureIndex] - x[featureIndex]) / kk
v = vInit.copy()
v0.append(vInit)
x_input = x.copy()
for j in range(1000):
grad = g.GradNormal(sample_x, x, num_feature, h)
x, v = langevin2(x, v, yita, mu, damp, grad, j)
probxnew = Pr_KDE(x, allsample, h)
v = np.zeros(p_all).copy()
xnow = x.copy()
print('pr:', probxnew)
#importance sampling
if (probxnew/pr_thres) >= u:
generatedSample.append(x)
status.append(1)
else:
rejectSample.append(x)
status.append(0)
x = x_input.copy()
return generatedSample, x_initial, xnow, rejectSample, status
def generating_logp_NB(iterNumMax, yita, h, num_subset, num_feature, allsample, mu, damp):
g = gp('GradNormal')
#init pool of samples
generatedSample = []
v0 = []
n_all, p_all = np.shape(allsample)
#vNext = np.zeros(p_all)
v = np.zeros(p_all)
for i in range(iterNumMax):
sample, featureIndex, sample_x = randomSub(allsample, num_subset, num_feature)
kk = 1
#v[featureIndex] = vNext[featureIndex]
# =============================================================================
# if i == 0:
# flag0 = np.random.choice(len(sample_x))
# x = sample_x[flag0]
# =============================================================================
flag0 = np.random.choice(len(sample_x))
x = sample_x[flag0]
flag = np.random.choice(len(sample_x))
x1 = sample_x[flag]
vInit = np.zeros(p_all)
vInit[featureIndex] = (x1[featureIndex] - x[featureIndex]) / kk
v = vInit.copy()
v0.append(vInit)
for j in range(100):
grad = g.GradNormal(sample_x, x, num_feature, h)
x, v = langevin2(x, v, yita, mu, damp, grad, j)
#print('j')
probxnew = Pr_KDE(x, allsample, h)
print('pr:', probxnew)
#print('sample'+str(i))
v = np.zeros(p_all).copy()
generatedSample.append(x)
x = []
return generatedSample