This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
simulations.py
565 lines (447 loc) · 20.2 KB
/
simulations.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import numpy as np
def gen_coeffs(num_dim):
"""
Helper function for generating a linear simulation.
:param num_dim: number of dimensions for the simulation
:return: a vector of coefficients
"""
coeff_vec = np.array([1 / (x+1) for x in range(num_dim)])
return coeff_vec.reshape(-1, 1)
def gen_x_unif(num_samp, num_dim, low=-1, high=1):
"""
Helper function for generating n samples from d-dimensional vector
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:return: uniformly distributed simulated data matrix
"""
uniform_vec = np.array(np.random.uniform(low=low, high=high,
size=num_samp * num_dim))
data_mat = uniform_vec.reshape(num_samp, num_dim)
return data_mat
def linear_sim(num_samp, num_dim, noise=1, indep=False, low=-1, high=1):
"""
Function for generating a linear simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 1
:param indep: whether to sample x and y independently, defaults to false
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
coeffs = gen_coeffs(num_dim)
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim == 1):
kappa = 1
else:
kappa = 0
y = (np.dot(a=x, b=coeffs) + kappa*noise*gauss_noise)
if indep:
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
return x, y
def exp_sim(num_samp, num_dim, noise=10, indep=False, low=0, high=3):
"""
Function for generating an exponential simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 10
:param indep: whether to sample x and y independently, defaults to false
:param low: the lower limit of the data matrix, defaults to 0
:param high: the upper limit of the data matrix, defaults to 3
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
coeffs = gen_coeffs(num_dim)
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim == 1):
kappa = 1
else:
kappa = 0
y = (np.exp(np.dot(a=x, b=coeffs)) + kappa*noise*gauss_noise)
if indep:
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
return x, y
def cub_sim(num_samp, num_dim, noise=80, indep=False, low=-1, high=1,
cub_coeff=np.array([-12, 48, 128]), scale=1/3):
"""
Function for generating a cubic simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 80
:param indep: whether to sample x and y independently, defaults to False
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:param cub_coeff: coefficients of the cubic function where each value
corresponds to the respective order coefficientj,
defaults to [-12, 48, 128]
:param scale: scaling center of the cubic, defaults to 1/3
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
coeffs = gen_coeffs(num_dim)
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim == 1):
kappa = 1
else:
kappa = 0
x_coeffs = np.dot(a=x, b=coeffs)
y = ((cub_coeff[2] * (x_coeffs-scale)**3)
+ (cub_coeff[1] * (x_coeffs-scale)**2)
+ (cub_coeff[0] * (x_coeffs-scale))
+ kappa * noise * gauss_noise)
if indep:
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
return x, y
def joint_sim(num_samp, num_dim, noise=0.5):
"""
Function for generating a joint-normal simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 80
:return: the data matrix and a response array
"""
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim > 1):
kappa = 1
else:
kappa = 0
rho = 1 / (2*num_dim)
sig = np.diag(np.ones(shape=(2*num_dim)))
sig[num_dim: (2*num_dim), 0: num_dim] = rho
sig[0: num_dim, num_dim: (2*num_dim)] = rho
samp = (np.random.multivariate_normal(cov=sig, mean=np.zeros(2*num_dim),
size=num_samp))
if num_dim == 1:
y = samp[:, (num_dim):(2*num_dim)] + kappa*noise*gauss_noise
x = samp[:, 0:num_dim]
else:
y = samp[:, (num_dim+1):(2*num_dim)] + kappa*noise*gauss_noise
x = samp[:, 0:num_dim]
return x, y
def step_sim(num_samp, num_dim, noise=1, indep=False, low=-1, high=1):
"""
Function for generating a joint-normal simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 1
:param indep: whether to sample x and y independently, defaults to false
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
coeffs = gen_coeffs(num_dim)
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim > 1):
kappa = 1
else:
kappa = 0
x_coeff = np.dot(a=x, b=coeffs)
x_coeff_temp = x_coeff.copy()
x_coeff_temp[x_coeff <= 0] = 0
x_coeff_temp[x_coeff > 0] = 1
y = (x_coeff_temp + kappa*noise*gauss_noise)
if indep:
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
return x, y
def quad_sim(num_samp, num_dim, noise=1, indep=False, low=-1, high=1):
"""
Function for generating a quadratic simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 1
:param indep: whether to sample x and y independently, defaults to false
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
coeffs = gen_coeffs(num_dim)
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim == 1):
kappa = 1
else:
kappa = 0
y = ((np.dot(a=x, b=coeffs)**2) + kappa*noise*gauss_noise)
if indep:
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
return x, y
def w_sim(num_samp, num_dim, noise=1, indep=False, low=-1, high=1):
"""
Function for generating a w-shaped simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 1
:param indep: whether to sample x and y independently, defaults to false
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
u = gen_x_unif(num_samp, num_dim, low=low, high=high)
coeffs = gen_coeffs(num_dim)
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim == 1):
kappa = 1
else:
kappa = 0
gauss_noise = np.random.normal(loc=0, scale=1, size=(x.shape[0], 1))
y = (4 * ((np.dot(a=x, b=coeffs)**2 - 0.5)**2 + np.dot(a=u, b=coeffs)/500)
+ kappa*noise*gauss_noise)
if indep:
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
return x, y
def spiral_sim(num_samp, num_dim, noise=0.4, low=0, high=5):
"""
Function for generating a spiral simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 0.4
:param low: the lower limit of the data matrix, defaults to 0
:param high: the upper limit of the data matrix, defaults to 5
:return: the data matrix and a response array
"""
uniform_dist = gen_x_unif(num_samp, num_dim=1, low=low, high=high)
the_x = np.array(np.cos(np.pi * uniform_dist)).reshape(num_samp, 1)
y = uniform_dist * np.sin(np.pi * uniform_dist)
x = np.zeros(shape=(num_samp, num_dim))
if num_dim > 1:
for i in range(num_dim - 1):
x[:, i] = np.squeeze((y * np.power(the_x, i)))
x[:, num_dim-1] = np.squeeze(uniform_dist * the_x)
gauss_noise = np.random.normal(loc=0, scale=1, size=(x.shape[0], 1))
y = y + noise*num_dim*gauss_noise
return x, y
def ubern_sim(num_samp, num_dim, noise=0.5, bern_prob=0.5):
"""
Function for generating an uncorrelated bernoulli simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 0.5
:param bern_prob: the bernoulli probability, defaults to 0.5
:return: the data matrix and a response array
"""
if num_dim > 1:
kappa = 1
else:
kappa = 0
binom_dist = np.random.binomial(1, p=bern_prob, size=(num_samp, 1))
sig = np.diag(np.ones(shape=num_dim) * num_dim)
gauss_noise1 = (np.random.multivariate_normal(
cov=sig,
mean=np.zeros(num_dim),
size=num_samp
))
x = (np.array(np.random.binomial(1, size=num_samp * num_dim,
p=bern_prob)).reshape(num_samp, num_dim)
+ noise*gauss_noise1)
coeffs = gen_coeffs(num_dim)
y = np.empty(shape=(num_samp, 1))
y[:] = np.nan
gauss_noise2 = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
for i in range(num_samp):
y[i] = (np.dot((2*binom_dist[i]-1) * coeffs.T, x[i, :])
+ kappa*noise*gauss_noise2[i])
return x, y
def log_sim(num_samp, num_dim, noise=3, indep=False, base=2):
"""
Function for generating a logarithmic simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 1
:param indep: whether to sample x and y independently, defaults to false
:param base: the base of the log, defaults to 2
:return: the data matrix and a response array
"""
sig = np.diag(np.ones(shape=(num_dim)))
x = (np.random.multivariate_normal(cov=sig, mean=np.zeros(num_dim),
size=num_samp))
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim == 1):
kappa = 1
else:
kappa = 0
y = (base * np.divide(np.log(np.abs(x)), np.log(base)
+ kappa*noise*gauss_noise))
if indep:
x = (np.random.multivariate_normal(cov=sig, mean=np.zeros(num_dim),
size=num_samp))
return x, y
def root_sim(num_samp, num_dim, noise=0.25, indep=False, low=-1, high=1, n_root=4):
"""
Function for generating an nth root simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 1
:param indep: whether to sample x and y independently, defaults to false
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:param n_root: the root of the simulation, defaults to 4
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
coeffs = gen_coeffs(num_dim)
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim == 1):
kappa = 1
else:
kappa = 0
y = (np.power(np.abs(np.dot(a=x, b=coeffs.reshape(num_dim, 1))), 1/n_root)
+ kappa*noise*gauss_noise)
if indep:
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
return x, y
def sin_sim(num_samp, num_dim, noise=1, indep=False, low=-1, high=1, period=4*np.pi):
"""
Function for generating a sinusoid simulation.
Note: For producing 4*pi and 16*pi simulations, change the ``period`` to the respective value.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 1
:param indep: whether to sample x and y independently, defaults to false
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:param period: the period of the sine wave, defaults to 4*pi
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
if num_dim > 1 or noise > 0:
sig = np.diag(np.ones(shape=(num_dim)))
v = (np.random.multivariate_normal(cov=sig, mean=np.zeros(num_dim),
size=num_samp))
x = x + 0.02*num_dim*v
gauss_noise = np.random.normal(loc=0, scale=1, size=(num_samp, 1))
if (num_dim == 1):
kappa = 1
else:
kappa = 0
y = np.sin(x*period) + kappa*noise*gauss_noise
if indep:
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
if num_dim > 1:
sig = np.diag(np.ones(shape=(num_dim)))
v = (np.random.multivariate_normal(cov=sig, mean=np.zeros(num_dim),
size=num_samp))
x = x + 0.02*num_dim*v
return x, y
def square_sim(num_samp, num_dim, noise=1, indep=False, low=-1, high=1, period=-np.pi/8):
"""
Function for generating a square or diamond simulation.
Note: For producing square or diamond simulations, change the ``period`` to -pi/8 or -pi/4.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 0.05
:param indep: whether to sample x and y independently, defaults to false
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:param period: the period of the sine and cosine square equation, defaults to 4*pi
:return: the data matrix and a response array
"""
u = gen_x_unif(num_samp, num_dim, low=low, high=high)
v = gen_x_unif(num_samp, num_dim, low=low, high=high)
sig = np.diag(np.ones(shape=(num_dim)))
gauss_noise = (np.random.multivariate_normal(cov=sig,
mean=np.zeros(num_dim),
size=num_samp))
x = u*np.cos(period) + v*np.sin(period) + 0.05*num_dim*gauss_noise
y = -u*np.sin(period) + v*np.cos(period)
if indep:
u = gen_x_unif(num_samp, num_dim, low=low, high=high)
v = gen_x_unif(num_samp, num_dim, low=low, high=high)
sig = np.diag(np.ones(shape=(num_dim)))
gauss_noise = (np.random.multivariate_normal(cov=sig,
mean=np.zeros(
num_dim),
size=num_samp))
x = u*np.cos(period) + v*np.sin(period) + 0.05*num_dim*gauss_noise
return x, y
def two_parab_sim(num_samp, num_dim, noise=2, low=-1, high=1, prob=0.5):
"""
Function for generating a two parabolas simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 2
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:param prob: the binomial probability, defaults to 0.5
:return: the data matrix and a response array
"""
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
coeffs = gen_coeffs(num_dim)
u = np.random.binomial(1, p=prob, size=(num_samp, 1))
gauss_noise = gen_x_unif(num_samp, num_dim, low=0, high=1)
if (num_dim == 1):
kappa = 1
else:
kappa = 0
y = (np.power(np.dot(x, coeffs.reshape(num_dim, 1)), 2) +
noise*kappa*gauss_noise) * (u - 0.5)
return x, y
def circle_sim(num_samp, num_dim, noise=0.4, low=-1, high=1, radius=1):
"""
Function for generating a circle or ellipse simulation.
Note: For producing circle or ellipse simulations, change the ``radius`` to 1 or 5.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param noise: noise level of the simulation, defaults to 0.4
:param low: the lower limit of the data matrix, defaults to -1
:param high: the upper limit of the data matrix, defaults to 1
:param radius: the radius of the circle or ellipse, defaults to 1
:return: the data matrix and a response array
"""
if num_dim > 1:
kappa = 1
else:
kappa = 0
x = gen_x_unif(num_samp, num_dim, low=low, high=high)
rx = radius * np.ones((num_samp, num_dim))
z = gen_x_unif(num_samp, num_dim, low=low, high=high)
sig = np.diag(np.ones(shape=(num_dim)))
gauss_noise = (np.random.multivariate_normal(cov=sig,
mean=np.zeros(num_dim),
size=num_samp))
ry = np.ones((num_samp, num_dim))
x[:, 0] = np.cos(z[:, 0].reshape((num_samp)) * np.pi)
for i in range(num_dim - 1):
x[:, i+1] = (x[:, i].reshape((num_samp)) * np.cos(z[:, i+1].reshape((num_samp)) * np.pi))
x[:, i] = (x[:, i].reshape((num_samp)) * np.sin(z[:, i+1].reshape((num_samp)) * np.pi))
x = rx * x + noise*rx*gauss_noise
y = ry * np.sin(z[:, 0].reshape((num_samp, 1)) * np.pi)
return x, y
def multi_noise_sim(num_samp, num_dim):
"""
Function for generating a multiplicative noise simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:return: the data matrix and a response array
"""
sig = np.diag(np.ones(shape=(num_dim)))
u = np.random.multivariate_normal(
cov=sig, mean=np.zeros(num_dim), size=num_samp)
x = np.random.multivariate_normal(
cov=sig, mean=np.zeros(num_dim), size=num_samp)
y = u * x
return x, y
def multi_indep_sim(num_samp, num_dim, prob=0.5, sep1=3, sep2=2):
"""
Function for generating a multimodal independence simulation.
:param num_samp: number of samples for the simulation
:param num_dim: number of dimensions for the simulation
:param prob: the binomial probability, defaults to 0.5
:param sep1: determines the size and separation of clusters, defaults to 3
:param sep2: determines the size and separation of clusters, defaults to 2
:return: the data matrix and a response array
"""
sig = np.diag(np.ones(shape=(num_dim)))
u = np.random.multivariate_normal(
cov=sig, mean=np.zeros(num_dim), size=num_samp)
v = np.random.multivariate_normal(
cov=sig, mean=np.zeros(num_dim), size=num_samp)
u_2 = np.random.binomial(1, p=prob, size=(num_samp, 1))
v_2 = np.random.binomial(1, p=prob, size=(num_samp, 1))
x = u/sep1 + sep2*u_2 - 1
y = v/sep1 + sep2*v_2 - 1
return x, y