/
dataGen.cc
396 lines (361 loc) · 13.5 KB
/
dataGen.cc
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
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<fstream>
#include<algorithm>
#include<ext/algorithm> // for linux code
//#include<algorithm> // for solaris/unix code
#include<utility>
#include<unistd.h> // for getopt() function
using namespace std;
using namespace __gnu_cxx; // for linux code
enum Digit {MINUS_ONE=-1,ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN};
enum DistMode {FULL_DIM=0,HYPER_RECT_UNIFORM,MVNORMAL,LINEAR_DEPENDENCE};
static const int RANGE = 1000;
static const int SAMPLE_SIZE = 1000000;
static const int NUM_ARGS = TWO;
//input args
int iNoOfClusters=5,iNoOfPts=1000,iNoOfDims=400,iRealDims=50,iMode=2,iNoOfDatasets=1;
double dProbNoise=0.05,dStdDev=0.5,dMaxClusterSzRatio=4.0,dProbOverlap = 0.5,dProbConstrain=0.5,x = 0.5,dBias = 0.05,dWidth=20;//0.25*RANGE;
char caOutFile[300];
bool bCentresOnly = false;
// see Probability Methods for Computer Science - Sheldon Ross (Chpt 9)
double genStdNormal() {
while(1) {
double dY1 = -log(1.0 - (rand()%RANGE)/(double)RANGE);
double dY2 = -log(1.0 - (rand()%RANGE)/(double)RANGE);
double dY = (dY2-(dY1-1)*(dY1-1))/2.0;
if(dY >= 0.0) return ((rand()%RANGE > RANGE/TWO) ? dY1 : -dY1);
}
}
double estimateHashProb(double dStdDev,int iDiv=10) {
int iNoOfHits = 0;
double dDiv = 1.0/iDiv;
for(int i=0;i<SAMPLE_SIZE;++i) {
double dY = double(rand()%(int(dDiv*RANGE)))/RANGE;
double dSample = genStdNormal();
if(dSample <= (-dY+dDiv)/dStdDev && dSample >= -dY/dStdDev)
++iNoOfHits;
}
return double(iNoOfHits)/SAMPLE_SIZE;
}
void createClusterSizes(int* iaCoord,double dBias,int iNoOfClusters,double v,double dProbNoise,ofstream& fOut,int RANGE=1000) {
int iaRange[RANGE];
for(int i = MINUS_ONE;++i < RANGE;) iaRange[i] = i;
// generate P(i \in jth cluster) randomly with uniform bias dBias
int iMinMinAlpha = int(max(dBias,(1.0-dProbNoise)/(ONE+v*(iNoOfClusters-ONE)))*RANGE);
int iMaxMinAlpha = int(RANGE*(1.0-dProbNoise)/(v+iNoOfClusters-ONE));
if(iMaxMinAlpha > iMinMinAlpha) {
iaCoord[ZERO]=rand()%(iMaxMinAlpha-iMinMinAlpha)+iMinMinAlpha;
} else iaCoord[ZERO]=iMinMinAlpha;
int iRangeSize = int(RANGE*(1.0-dProbNoise)-iaCoord[ZERO]*(iNoOfClusters-TWO+v+ONE));
if(iRangeSize<iNoOfClusters-TWO) {
cerr << "ERROR: UNSUITABLE PARAMETERS: Decrease -b " << dBias << " or -a " << v << endl;
exit(0);
}
random_sample_n(iaRange,iaRange+iRangeSize,iaCoord+ONE,iNoOfClusters-TWO);
for(int j=ZERO;++j < iNoOfClusters-TWO;) iaCoord[j]=iaCoord[j+1];
iaCoord[iNoOfClusters-TWO]=iRangeSize;
sort(iaCoord+ONE,iaCoord+iNoOfClusters-ONE);
for(int i=ZERO;++i<iNoOfClusters-ONE;)iaCoord[i] += ((i+1)*iaCoord[ZERO]);
iaCoord[iNoOfClusters-ONE]=int((1.0-dProbNoise)*RANGE);
int iTmp = ZERO;
fOut << iNoOfClusters << ' ' << dProbNoise << ' ';
for(int i=0;i < iNoOfClusters;++i) {
fOut << double(iaCoord[i]-iTmp)/RANGE << ' ';
iTmp=iaCoord[i];
}
fOut << endl;
}
void createClusterParams(int* iaMean,double *daStdDev,int iNoOfDims,int iNoOfClusters,DistMode distMode,int iDims,double dProbConstrain=0.5,double dProbOverlap=0.5) {
int iaRange[RANGE];
double r=2;
int s=2;
for(int iCntr = MINUS_ONE;++iCntr < iNoOfClusters;) {
for(int d = MINUS_ONE;++d < iNoOfDims;) {
/* int iScaleFactor = (rand()%s) + ONE;
if(distMode==MVNORMAL || distMode==HYPER_RECT_UNIFORM)
daStdDev[iNoOfDims*iCntr+d]=iScaleFactor*iScaleFactor*r*r;
daStdDev[iNoOfDims*iCntr+d]=double(10+rand()%20);*/
daStdDev[iNoOfDims*iCntr+d]=dWidth;
}
}
for(int i = MINUS_ONE;++i < RANGE;) iaRange[i] = i;
double x = 0.8;
int iaCoord[iNoOfClusters];
// generate the random full-dimensional centres
switch(distMode) {
case FULL_DIM:
for(int d = MINUS_ONE;++d < iNoOfDims;) {
// choose iNoOfClusters random points in range of dimension as centres
random_sample_n(iaRange,iaRange+RANGE,iaCoord,iNoOfClusters);
random_shuffle(iaCoord,iaCoord+iNoOfClusters);
for(int iCntr = MINUS_ONE;++iCntr < iNoOfClusters;) {
iaMean[iNoOfDims * iCntr + d] = iaCoord[iCntr];
}
}
break;
case HYPER_RECT_UNIFORM:
for(int iCntr = MINUS_ONE;++iCntr < iNoOfClusters;) {
double c = dProbConstrain*((iNoOfDatasets > 1)?(x+ (rand()%int((1.0-x)*1000))/500.0):1);
double o = dProbOverlap*((iNoOfDatasets > 1)?(x+ (rand()%int((1.0-x)*1000))/500.0):1);
double dO = ((iCntr > ZERO) ? (1.0-o)/(1.0-c) : 1.0); //~
for(int d = MINUS_ONE;++d < iNoOfDims;) {
if(iCntr > ZERO && iaMean[iNoOfDims*(iCntr-1)+d]!=MINUS_ONE) {
if(rand()%100 < o*100)
iaMean[iNoOfDims*iCntr+d]=iaMean[iNoOfDims*(iCntr-1)+d]+2*int(daStdDev[iNoOfDims*(iCntr-1)+d]*(((rand()%2)==0)?-1:1));//2 \sigma from prev mean
} else {
if(rand()%100 < c*dO*100)
iaMean[iNoOfDims*iCntr+d]=rand()%RANGE;
}
}
}
break;
case MVNORMAL:
for(int iCntr = MINUS_ONE;++iCntr < iNoOfClusters;) {
double c = dProbConstrain*((iNoOfDatasets > 1)?(x+ (rand()%int((1.0-x)*1000))/500.0):1);
double o = dProbOverlap*((iNoOfDatasets > 1)?(x+ (rand()%int((1.0-x)*1000))/500.0):1);
double dO = ((iCntr > ZERO) ? (1.0-o)/(1.0-c) : 1.0);
for(int d = MINUS_ONE;++d < iNoOfDims;) {
if(iCntr > ZERO && iaMean[iNoOfDims*(iCntr-1)+d]!=MINUS_ONE) {
if(rand()%100 < o*100)
iaMean[iNoOfDims*iCntr+d]=iaMean[iNoOfDims*(iCntr-1)+d]+2*int(daStdDev[iNoOfDims*(iCntr-1)+d]*(((rand()%2)==0)?-1:1));//2 \sigma from prev mean
// iaMean[iNoOfDims*iCntr+d]=iaMean[iNoOfDims*(iCntr-1)+d]+(rand()%(2*int(daStdDev[iNoOfDims*(iCntr-1)+d])))*((rand()%2)?-1:1);
} else {
if(rand()%100 < c*dO*100)
iaMean[iNoOfDims*iCntr+d]=rand()%RANGE;
}
}
}
break;
default:
cerr << "ERROR: UNDEFINED DATA GENERATION MODE " << distMode << endl;
exit(0);
}
#ifdef OLD_DEBUG
for(int i=MINUS_ONE;++i < iNoOfClusters;) {
for(int d=MINUS_ONE;++d < iDims;) cerr << iaMean[iNoOfDims*i+d] << ' ';
cerr << endl;
}
for(int i=MINUS_ONE;++i < iNoOfClusters;) {
for(int d=MINUS_ONE;++d < iDims;) cerr << daStdDev[iNoOfDims*i+d] << ' ';
cerr << endl;
}
#endif
}
void writeNoisyPoint(ofstream& fOut,int iNoOfDims,int iDims) {
// for each dimension
for(int d = MINUS_ONE;++d < iNoOfDims;) {
int iRand = rand();
if(d >= iDims) continue;
fOut << iRand%RANGE;
if(d != iDims - ONE) fOut << ' ';
}
}
void writePoint(ofstream& fOut,int iNoOfDims,int iDims,int* iaMean,double *daStdDev,DistMode distMode) {
double dRand;
int iRand = 100;
// for each dimension
for(int d = MINUS_ONE;++d < iNoOfDims;) {
dRand = genStdNormal();
iRand=rand();
if(d >= iDims) continue;
if(iaMean[d]==MINUS_ONE) {
fOut << iRand%RANGE;
} else {
switch(distMode) {
case FULL_DIM:
fOut << max(0,min(int(iaMean[d]+daStdDev[d]*dRand),RANGE));
break;
case HYPER_RECT_UNIFORM:
fOut << max(0,min(RANGE,iaMean[d]-int(.5*daStdDev[d])+iRand%(int(daStdDev[d]))));
break;
case MVNORMAL:
fOut << max(0,min(int(iaMean[d]+daStdDev[d]*dRand),RANGE));
break;
default:
cerr << "ERROR: UNDEFINED DATA GENERATION MODE " << distMode << endl;
exit(0);
}
}
if(d != iDims - ONE) fOut << ' ';
}
}
void writePt(ofstream& fOut,int iNoOfDims,int iDims,int* iaMean,double *daStdDev,int* iaCoord,DistMode distMode) {
// choose a cluster
int iRand = rand()%RANGE;
int iClusterId = ZERO;
while(iaCoord[iClusterId++] <= iRand);
--iClusterId;
writePoint(fOut,iNoOfDims,iDims,iaMean+iClusterId*iNoOfDims,daStdDev+iClusterId*iNoOfDims,distMode);
}
void parseArguments(int argc,char **argv) {
// read args
if(argc < NUM_ARGS) {
cerr << "USAGE: dataGen\n";
cerr << "\t-a<Max cluster size ratio default="<<dMaxClusterSzRatio<<">\n";
cerr << "\t-b<Min cluster size frac default=" << dBias << ">\n";
cerr << "\t-c<P(dim is bounded) default=" << dProbConstrain << ">\n";
cerr << "\t-d<Real Dims default=" << iRealDims << ">\n";
cerr << "\t-D<#(Dims) default=" << iNoOfDims << ">\n";
cerr << "\t-e<generate subspaces only, no points>\n";
cerr << "\t-m<mode default=" << iMode << ">\n";
cerr << "\t-k<#(Clusters) default=" << iNoOfClusters << ">\n";
cerr << "\t-n<#(pts) default=" << iNoOfPts << ">\n";
cerr << "\t-o<out file>\n";
cerr << "\t-O<P(dim bounding overlaps) default=" << dProbOverlap << ">\n";
cerr << "\t-r<noise default=" << dProbNoise << ">\n";
cerr << "\t-R<range of variation default=" << x << ">\n";
cerr << "\t-s<#(datasets) default=" << iNoOfDatasets << ">\n";
cerr << "\t-w<width of clu in uni dist default=" << dWidth << ">\n";
exit(0);
}
int c;
while((c=getopt(argc,argv,"a:b:c:d:D:e:k:m:n:o:O:r:R:s:w:")) != -1) {
switch(c) {
case 'a':
dMaxClusterSzRatio = atof(optarg);
break;
case 'b':
dBias = atof(optarg);
break;
case 'c':
dProbConstrain = atof(optarg);
break;
case 'd':
iRealDims = atoi(optarg);
break;
case 'D':
iNoOfDims = atoi(optarg);
break;
case 'e':
bCentresOnly = true;
break;
case 'k':
iNoOfClusters = atoi(optarg);
break;
case 'm':
iMode = atoi(optarg);
break;
case 'n':
iNoOfPts = atoi(optarg);
break;
case 'o':
sprintf(caOutFile,"%s",optarg);
break;
case 'O':
dProbOverlap = atof(optarg);
break;
case 'r':
dProbNoise = atof(optarg);
break;
case 'R':
x = atof(optarg);
break;
case 's':
iNoOfDatasets = atoi(optarg);
break;
case 'w':
dWidth = atof(optarg);
break;
default:
cerr << "ERROR: OPTION -" << c << " NOT SUPPORTED\n";
exit(0);
}
}
}
int main(int argc,char **argv) {
parseArguments(argc,argv);
DistMode distMode = DistMode(iMode);
// test validity of parameters
srand(20000);
if(iRealDims > iNoOfDims) {
cerr << " ERROR: INCORRECT DIMENSIONS : NoOfDims=" << iNoOfDims << " RealDims=" << iRealDims << endl;
exit(0);
}
int k = (iNoOfDatasets > 1)? int((1.0+x)*iNoOfClusters):iNoOfClusters;
int iaMean[k*iNoOfDims],iaMeanTmp[iNoOfDims];
double daStdDev[k*iNoOfDims],daStdDevTmp[iNoOfDims];
for(int i=ZERO;i<k*iNoOfDims;++i) {
iaMean[i]=MINUS_ONE;
daStdDev[i]=dStdDev;
}
createClusterParams(iaMean,daStdDev,iNoOfDims,k,distMode,iRealDims,dProbConstrain,dProbOverlap);
for(int iDatasetId = ZERO;iDatasetId < iNoOfDatasets;++iDatasetId) {
// open file to write to
char caFile[1000],caOut[1000],caCentre[1000];
memset(caOut,'\0',1000);
if(iNoOfDatasets>ONE) {
sprintf(caFile,"%.*ss%.2d%s",strlen(caOutFile)-3,caOutFile,iDatasetId,caOutFile+strlen(caOutFile)-3);
} else sprintf(caFile,"%s",caOutFile);
ofstream fHa(caFile,ios::out);
if(fHa.bad()) {
cerr << " ERROR: OPENING FILE " << caFile << endl;
exit(0);
}
strncpy(caOut,caFile,strlen(caFile)-3);
ofstream fOut(caOut,ios::out);
if(fOut.bad()) {
cerr << " ERROR: OPENING FILE " << caOut << endl;
exit(0);
}
sprintf(caCentre,"%s_out",caOut);
ofstream fCentre(caCentre,ios::out);
if(fCentre.bad()) {
cerr << " ERROR: OPENING FILE " << caCentre << endl;
exit(0);
}
// perturb number of points and interesting subspaces in dataset
int n = int(iNoOfPts*((iNoOfDatasets > 1)?x+ (rand()%int(1000*(1.0-x)))/500.0:1));
int k = int(iNoOfClusters*((iNoOfDatasets > 1)?x+ (rand()%int(1000*(1.0-x)))/500.0:1));
int iDims = iRealDims;//int(iRealDims*((iNoOfDatasets > 1)?x+ (rand()%int(1000*(1.0-x)))/500.0:1));
int iaCoord[k];
fCentre << iDims << endl;
if(!bCentresOnly) {
fOut << n << ' ' << iDims << ' ';
fHa << iDims << endl;
createClusterSizes(iaCoord,dBias,k,dMaxClusterSzRatio,dProbNoise,fOut);
}
int iPtsWritten = ZERO,iTmp = ZERO;
for(int iCluId=ZERO;iCluId<k;++iCluId) { // for each cluster
// determine number of points ascribed to that cluster
int iNoOfCluPts = int(n*double(iaCoord[iCluId]-iTmp)/RANGE);
iTmp = iaCoord[iCluId];
for(int d=ZERO;d<iNoOfDims;++d) {
// perturb the centre and deviation of the planted subspace
if(iaMean[iNoOfDims*iCluId+d] < ZERO) {
iaMeanTmp[d]=MINUS_ONE;
daStdDevTmp[d]=dStdDev;
continue;
}
double x = 0.02;
iaMeanTmp[d]=max(0,min(RANGE,int(((iNoOfDatasets > 1)?iaMean[iCluId*iNoOfDims+d]-x*RANGE + 2*(rand()%int(RANGE*x)):iaMean[iCluId*iNoOfDims+d]))));
daStdDevTmp[d]=daStdDev[iCluId*iNoOfDims+d]*((iNoOfDatasets > 1)?(1.0-x) + (rand()%int(1000*x))/500.0:1);
}
for(int d=MINUS_ONE;++d < iDims;) fCentre << iaMeanTmp[d] << ' ';
fCentre << endl;
#ifdef DEBUG
// for(int d=MINUS_ONE;++d < iDims;) fCentre << daStdDevTmp[d] << ' ';
// fCentre << endl;
#endif
if(!bCentresOnly) {
for(int j=ZERO;j<iNoOfCluPts;++j) { // for each point ascribed to it
if((iCluId+j) != ZERO) fHa << endl;
writePoint(fHa,iNoOfDims,iDims,iaMeanTmp,daStdDevTmp,distMode);
}
iPtsWritten += iNoOfCluPts;
}
}
if(!bCentresOnly) {
while(++iPtsWritten <= n) {
fHa << endl;
writeNoisyPoint(fHa,iNoOfDims,iDims);
}
fHa << endl;
fHa.flush();
}
}
return 0;
}