-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdatasets.py
474 lines (396 loc) · 16.6 KB
/
datasets.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
import numpy as np
def sample(p, k):
full = np.arange(p)
select = sorted(np.random.choice(full, k, replace=False))
return select
def sparse_beta_generator(p, Nonzero, k, M):
Tbeta = np.zeros([p, M])
beta_value = beta_generator(k, M)
Tbeta[Nonzero, :] = beta_value
return Tbeta
def beta_generator(k, M):
# # strong_num <- 3
# # moderate_num <- 7
# # weak_num <- 5
# # strong_num <- 10
# # moderate_num <- 10
# # weak_num <- 10
strong_num = int(k * 0.3)
moderate_num = int(k * 0.4)
weak_num = k - strong_num - moderate_num
# signal_num = strong_num + moderate_num + weak_num
strong_signal = np.random.normal(
0, 10, strong_num * M).reshape(strong_num, M)
moderate_signal = np.random.normal(
0, 5, moderate_num * M).reshape(moderate_num, M)
weak_signal = np.random.normal(0, 2, weak_num * M).reshape(weak_num, M)
beta_value = np.concatenate((strong_signal, moderate_signal, weak_signal))
beta_value = beta_value[sample(k, k), :]
# beta_value = np.random.normal(size=(k, M))
return beta_value
class make_glm_data:
r"""
Generate a dataset with single response.
Parameters
----------
n: int
The number of observations.
p: int
The number of predictors of interest.
k: int
The number of nonzero coefficients in the
underlying regression model.
family: {gaussian, binomial, poisson, gamma, cox}
The distribution of the simulated response.
"gaussian" for univariate quantitative response,
"binomial" for binary classification response,
"poisson" for counting response,
"gamma" for positive continuous response,
"cox" for left-censored response.
rho: float, optional, default=0
A parameter used to characterize the pairwise
correlation in predictors.
corr_type: string, optional, default="const"
The structure of correlation matrix.
"const" for constant pairwise correlation,
"exp" for pairwise correlation with exponential decay.
sigma: float, optional, default=1
The variance of the gaussian noise.
It would be unused if snr is not None.
coef_: array_like, optional, default=None
The coefficient values in the underlying regression model.
censoring: bool, optional, default=True
For Cox data, it indicates whether censoring is existed.
c: int, optional, default=1
For Cox data and censoring=True, it indicates the maximum
censoring time.
So that all observations have chances to be censored at (0, c).
scal: float, optional, default=10
The scale of survival time in Cox data.
snr: float, optional, default=None
A numerical value controlling the signal-to-noise ratio (SNR)
in gaussian data.
class_num: int, optional, default=3
The number of possible classes in oridinal dataset, i.e.
:math:`y \in \{0, 1, 2, ..., \text{class_num}-1\}`
Attributes
----------
x: array-like, shape(n, p)
Design matrix of predictors.
y: array-like, shape(n,)
Response variable.
coef_: array-like, shape(p,)
The coefficients used in the underlying regression model.
It has k nonzero values.
Notes
-----
The output, whose type is named ``data``, contains three elements:
``x``, ``y`` and ``coef_``, which correspond the variables, responses
and coefficients, respectively.
Each row of ``x`` or ``y`` indicates a sample and is independent to the
other.
We denote :math:`x, y, \beta` for one sample in the math formulas below.
* Linear Regression
* Usage: ``family='gaussian'[, sigma=...]``
* Model: :math:`y \sim N(\mu, \sigma^2),\ \mu = x^T\beta`.
* the coefficient :math:`\beta\sim U[m, 100m]`,
where :math:`m = 5\sqrt{2\log p/n}`;
* the variance :math:`\sigma = 1`.
* Logistic Regression
* Usage: ``family='binomial'``
* Model: :math:`y \sim \text{Binom}(\pi),\
\text{logit}(\pi) = x^T \beta`.
* the coefficient :math:`\beta\sim U[2m, 10m]`,
where :math:`m = 5\sqrt{2\log p/n}`.
* Poisson Regression
* Usage: ``family='poisson'``
* Model: :math:`y \sim \text{Poisson}(\lambda),\
\lambda = \exp(x^T \beta)`.
* the coefficient :math:`\beta\sim U[2m, 10m]`,
where :math:`m = 5\sqrt{2\log p/n}`.
* Gamma Regression
* Usage: ``family='gamma'``
* Model: :math:`y \sim \text{Gamma}(k, \theta),\
k\theta = -1/(x^T \beta + \epsilon), k\sim U[0.1, 100.1]`
in shape-scale definition.
* the coefficient :math:`\beta\sim U[m, 100m]`,
where :math:`m = 5\sqrt{2\log p/n}`.
* Cox PH Survival Analysis
* Usage: ``family='cox'[, scal=..., censoring=..., c=...]``
* Model: :math:`y=\min(t,C)`,
where :math:`t = \left[-\dfrac{\log U}{\exp(X \beta)}\right]^s,\
U\sim N(0,1),\ s=\dfrac{1}{\text{scal}}` and
censoring time :math:`C\sim U(0, c)`.
* the coefficient :math:`\beta\sim U[2m, 10m]`,
where :math:`m = 5\sqrt{2\log p/n}`;
* the scale of survival time :math:`\text{scal} = 10`;
* censoring is enabled, and max censoring time :math:`c=1`.
* Ordinal Regression
* Usage: ``family='ordinal'[, class_num=...]``
* Model: :math:`y\in \{0, 1, \dots, n_{class}\}`,
:math:`\mathbb{P}(y\leq i) = \dfrac{1}
{1+\exp(-x^T\beta - \varepsilon_i)}`,
where :math:`i\in \{0, 1, \dots, n_{class}\}` and
:math:`\forall i<j, \varepsilon_i < \varepsilon_j`.
* the coefficient :math:`\beta\sim U[-M, M]`,
where :math:`M = 125\sqrt{2\log p/n}`;
* the intercept: :math:`\forall i,\varepsilon_i\sim U[-M, M]`;
* the number of classes :math:`n_{class}=3`.
"""
def __init__(self, n, p, k, family, rho=0, corr_type="const", sigma=1,
coef_=None,
censoring=True, c=1, scal=10, snr=None, class_num=3):
self.n = n
self.p = p
self.k = k
self.family = family
if corr_type == "exp":
# generate correlation matrix with exponential decay
R = np.zeros((p, p))
for i in range(p):
for j in range(i, p):
R[i, j] = rho ** abs(i - j)
R = R + R.T - np.identity(p)
elif corr_type == "const":
# generate correlation matrix with constant correlation
R = np.ones((p, p)) * rho
for i in range(p):
R[i, i] = 1
else:
raise ValueError(
"corr_type should be \'const\' or \'exp\'")
x = np.random.multivariate_normal(mean=np.zeros(p), cov=R, size=(n,))
nonzero = sample(p, k)
Tbeta = np.zeros(p)
sign = np.random.choice([1, -1], k)
if family == "gaussian":
m = 5 * np.sqrt(2 * np.log(p) / n)
M = 100 * m
if coef_ is None:
Tbeta[nonzero] = np.random.uniform(m, M, k) * sign
else:
Tbeta = coef_
if snr is None:
y = np.matmul(x, Tbeta) + sigma * np.random.normal(0, 1, n)
else:
y = np.matmul(x, Tbeta)
power = np.mean(np.square(y))
npower = power / 10 ** (snr / 10)
noise = np.random.randn(len(y)) * np.sqrt(npower)
y += noise
elif family == "binomial":
m = 5 * sigma * np.sqrt(2 * np.log(p) / n)
if coef_ is None:
Tbeta[nonzero] = np.random.uniform(2 * m, 10 * m, k) * sign
else:
Tbeta = coef_
xbeta = np.matmul(x, Tbeta)
xbeta[xbeta > 30] = 30
xbeta[xbeta < -30] = -30
p = np.exp(xbeta) / (1 + np.exp(xbeta))
y = np.random.binomial(1, p)
elif family == "poisson":
x = x / 16
m = 5 * sigma * np.sqrt(2 * np.log(p) / n)
if coef_ is None:
Tbeta[nonzero] = np.random.uniform(2 * m, 10 * m, k) * sign
# Tbeta[nonzero] = np.random.normal(0, 4*m, k)
else:
Tbeta = coef_
xbeta = np.matmul(x, Tbeta)
xbeta[xbeta > 30] = 30
xbeta[xbeta < -30] = -30
lam = np.exp(xbeta)
y = np.random.poisson(lam=lam)
elif family == "cox":
m = 5 * sigma * np.sqrt(2 * np.log(p) / n)
if coef_ is None:
Tbeta[nonzero] = np.random.uniform(2 * m, 10 * m, k) * sign
else:
Tbeta = coef_
time = np.power(-np.log(np.random.uniform(0, 1, n)) /
np.exp(np.matmul(x, Tbeta)), 1 / scal)
if censoring:
ctime = c * np.random.uniform(0, 1, n)
status = (time < ctime) * 1
censoringrate = 1 - sum(status) / n
print("censoring rate:" + str(censoringrate))
for i in range(n):
time[i] = min(time[i], ctime[i])
else:
status = np.ones(n)
print("no censoring")
y = np.hstack((time.reshape((-1, 1)), status.reshape((-1, 1))))
elif family == "gamma":
x = x / 16
m = 5 * np.sqrt(2 * np.log(p) / n)
if coef_ is None:
Tbeta[nonzero] = np.random.uniform(m, 100 * m, k) * sign
else:
Tbeta = coef_
# add noise
eta = x @ Tbeta + np.random.normal(0, sigma, n)
# set coef_0 to make eta<0
eta = eta - np.abs(np.max(eta)) - 10
eta = -1 / eta
# set the shape para of gamma uniformly in [0.1,100.1]
shape_para = 100 * np.random.uniform(0, 1) + 0.1
y = np.random.gamma(
shape=shape_para,
scale=eta / shape_para,
size=n)
elif family == "ordinal":
M = 125 * np.sqrt(2 * np.log(p) / n)
if coef_ is None:
Tbeta[nonzero] = np.random.uniform(-M, M, k)
else:
Tbeta = coef_
intercept = np.sort(np.random.uniform(-M, M, class_num - 1))
eta = x @ Tbeta[:, np.newaxis] + intercept
logit = 1 / (1 + np.exp(-eta))
# prob
prob = np.zeros((n, class_num))
prob[:, 0] = logit[:, 0]
prob[:, 1:class_num - 1] = (logit[:, 1:class_num - 1] -
logit[:, 0:class_num - 2])
prob[:, class_num - 1] = 1 - logit[:, class_num - 2]
# y
y = np.zeros(n)
for i in range(n):
y[i] = np.random.choice(np.arange(class_num), 1, p=prob[i, :])
else:
raise ValueError(
"Family should be \'gaussian\', \'binomial\', "
"\'poisson\', \'gamma\', \'cox\', or \'ordinal\'.")
self.x = x
self.y = y
self.coef_ = Tbeta
class make_multivariate_glm_data:
r"""
Generate a dataset with multi-responses.
Parameters
----------
n: int, optional, default=100
The number of observations.
p: int, optional, default=100
The number of predictors of interest.
family: {multigaussian, multinomial, poisson}, optional
default="multigaussian".
The distribution of the simulated multi-response.
"multigaussian" for multivariate quantitative responses,
"multinomial" for multiple classification responses,
"poisson" for counting responses.
k: int, optional, default=10
The number of nonzero coefficients in the underlying regression model.
M: int, optional, default=1
The number of responses.
rho: float, optional, default=0.5
A parameter used to characterize the pairwise correlation
in predictors.
corr_type: string, optional, default="const"
The structure of correlation matrix.
"const" for constant pairwise correlation,
"exp" for pairwise correlation with exponential decay.
coef_: array_like, optional, default=None
The coefficient values in the underlying regression model.
sparse_ratio: float, optional, default=None
The sparse ratio of predictor matrix (x).
Attributes
----------
x: array-like, shape(n, p)
Design matrix of predictors.
y: array-like, shape(n, M)
Response variable.
coef_: array-like, shape(p, M)
The coefficients used in the underlying regression model.
It is rowwise sparse, with k nonzero rows.
Notes
-----
The output, whose type is named ``data``, contains three elements:
``x``, ``y`` and ``coef_``, which correspond the variables, responses
and coefficients, respectively.
Note that the ``y`` and ``coef_`` here are both matrix:
1. each row of ``x`` and ``y`` indicates a sample;
2. each column of ``coef_`` corresponds to the effect on one response.
It is rowwise sparsity. Under this setting, a "useful" variable is
relevant to all responses.
We :math:`x, y, \beta` for one sample in the math formulas below.
* Multitask Regression
* Usage: ``family='multigaussian'``
* Model: :math:`y \sim MVN(\mu, \Sigma),\ \mu^T=x^T \beta`.
* the variance :math:`\Sigma = \text{diag}(1, 1, \cdots, 1)`;
* the coefficient :math:`\beta` contains 30% "strong" values, 40%
"moderate" values and the rest are "weak". They come from
:math:`N(0, 10)`, :math:`N(0, 5)` and :math:`N(0, 2)`,
respectively.
* Multinomial Regression
* Usage: ``family='multinomial'``
* Model: :math:`y` is a "0-1" array with only one "1". Its index is
chosed under probabilities :math:`\pi = \exp(x^T \beta)`.
* the coefficient :math:`\beta` contains 30% "strong" values, 40%
"moderate" values and the rest are "weak". They come from
:math:`N(0, 10)`, :math:`N(0, 5)` and :math:`N(0, 2)`,
respectively.
"""
def __init__(self,
n=100, p=100, k=10, family="multigaussian", rho=0.5,
corr_type="const", coef_=None, M=1, sparse_ratio=None):
if corr_type == "exp":
# generate correlation matrix with exponential decay
R = np.zeros((p, p))
for i in range(p):
for j in range(i, p):
R[i, j] = rho ** abs(i - j)
R = R + R.T - np.identity(p)
elif corr_type == "const":
# generate correlation matrix with constant correlation
R = np.ones((p, p)) * rho
for i in range(p):
R[i, i] = 1
else:
raise ValueError(
"corr_type should be \'const\' or \'exp\'")
X = np.random.multivariate_normal(mean=np.zeros(p), cov=R, size=(n,))
if sparse_ratio is not None:
sparse_size = int((1 - sparse_ratio) * n * p)
position = sample(n * p, sparse_size)
print(position)
for i in range(sparse_size):
X[int(position[i] / p), position[i] % p] = 0
Nonzero = sample(p, k)
# Nonzero = np.array([0, 1, 2])
# Nonzero[:k] = 1
if coef_ is None:
Tbeta = sparse_beta_generator(p, Nonzero, k, M)
else:
Tbeta = coef_
if family in ("multigaussian", "gaussian"):
eta = np.matmul(X, Tbeta)
y = eta + np.random.normal(0, 1, n * M).reshape(n, M)
elif family in ("multinomial", "binomial"):
for i in range(M):
Tbeta[:, i] = Tbeta[:, i] - Tbeta[:, M - 1]
eta = np.exp(np.matmul(X, Tbeta))
# y2 = np.zeros(n)
y = np.zeros([n, M])
index = np.linspace(0, M - 1, M)
for i in range(n):
p = eta[i, :] / np.sum(eta[i, :])
j = np.random.choice(index, size=1, replace=True, p=p)
# print(j)
y[i, int(j[0])] = 1
# y2[i] = j
elif family == "poisson":
X = X / 16
eta = np.matmul(X, Tbeta)
eta[eta > 30] = 30
eta[eta < -30] = -30
lam = np.exp(eta)
y = np.random.poisson(lam=lam)
else:
raise ValueError(
"Family should be \'gaussian\', \'multigaussian\', "
"or \'multinomial\'.")
self.x = X
self.y = y
self.coef_ = Tbeta