-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB.java
More file actions
529 lines (377 loc) · 10.5 KB
/
Copy pathDB.java
File metadata and controls
529 lines (377 loc) · 10.5 KB
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
import java.util.ArrayList;
import java.util.HashMap;
public class DB {
/**
* creates a matrix based on quartiles
* @param par1 A or Y, unordered/functional input
* @param par2 B or X, unordered/functional input
* @return the matrix
*/
public static double[][] correlationMatrix(double[] par1, double par2[]){
double[] orderX = order(par2);
double[] orderY = order(par1);
double [] dataX = data(orderX);
double[] dataY = data(orderY);
double[][]matrix = new double[4][4];
for(int i =0; i < 4; i++) {
for(int j =0; j < 4; j++) {
matrix[i][j] = IntersectCount(par1, par2, new double[]{dataY[i], dataY[i+1]}, new double[]{dataX[j], dataX[j+1]});
}
}
return matrix;
}
/**
* creates a correlation matrix based on matrices created by using word2vec
* @param par1 the
*
* @return the matrix new matrix
*/
public static double[][][] nthVaribleCorrelationMatrix(double[][] par1,double[][] par2){
double[][][]matrix = new double[par1.length][4][4];
for(int e =0; e < par1.length; e++) {
double[] row = new double[par1[e].length];
double[] row2 = new double[par2[e].length];
for(int x =0; x < par1[e].length; x++) {
row[x] = par1[x][e];
row2[x] = par2[x][e];
}
matrix[e] = correlationMatrix(row, row2);
}
return matrix;
}
/**
* Creates a single matrix for correlation between matrices
* @param par1
* @return
*/
public static double[][] nthCorrelationMatrix(double[][] par1){
double[][]matrix = new double[4][4];
double[][] order = new double[par1.length][par1[0].length];
double[][] data = new double[par1.length][par1[0].length];
for(int i = 0; i < par1.length; i++) {
order[i] = order(par1[i]);
data[i] = data(order[i]);
}
for(int i =0; i < 4; i++) {
for(int j =0; j < 4; j++) {
double[][] bounds = new double[par1.length][data[0].length];
for(int x= 0; x< par1.length; x++) {
//for(int y= 0; y< par1.length; y++) {
bounds[x][i] = data[x][i];
bounds[x][j] =data[x][i+1];
//}
}
matrix[i][j] = IntersectCountMutliD(par1, bounds);
}
}
return matrix;
}
public static int IntersectCount(double[] par1, double[] par2, double[] boundsPar1, double[] boundsPar2) {
HashMap<Integer, Double> par1Map = timesInBoundsWithIndex(par1, boundsPar1);
HashMap<Integer, Double> par2Map = timesInBoundsWithIndex(par2, boundsPar2);
int count = 0;
for(int i = 0; i < par1Map.size(); i++) {
if(par2Map.containsKey(par1Map.keySet().toArray()[i])) {
count++;
}
}
return count;
}
/**
* For two martices
* @param par1
* @param par2
* @param boundsPar1
* @param boundsPar2
* @return
*/
public static int IntersectCountMutliD(double[][] par1, double[][] bounds) {
int count = 0;
ArrayList<HashMap<Integer, Double>> par1Map = timesInBoundsWithIndexMutliD(par1, bounds);
for(int i = 0; i < par1Map.size(); i++) {
for(int j = i+1; j < par1Map.size() - 1; j++) {
//System.out.println(i);
for(int k = 0; k < par1Map.get(j).keySet().toArray().length; k++) {
if(par1Map.get(i).containsKey(par1Map.get(j).keySet().toArray()[k])) {
System.out.println(count);
count++;
}
}
}
}
return count;
}
/**
* For two martices
* @param par1
* @param bounds
* @return
*/
public static ArrayList<HashMap<Integer, Double>> timesInBoundsWithIndexMutliD(double[][] par1,double[][] bounds) {
ArrayList<HashMap<Integer, Double>> out = new ArrayList<HashMap<Integer, Double>>();
HashMap<Integer, Double> indexToVal = new HashMap<Integer, Double>();
for(int i = 0; i < par1.length; i++) {
for(int j = 0; j < par1[i].length; j++) {
boolean p1 = par1[i][j] >= bounds[i][0];
if(p1 && par1[i][j] <= bounds[i][1]) {
indexToVal.put(i, par1[i][j]);
}
}
out.add(indexToVal);// makes sure values are correctly copied
indexToVal.clear();
}
return out;
}
/**
* For two vectors
* @param par1
* @param bounds
* @return
*/
public static HashMap<Integer, Double> timesInBoundsWithIndex(double[] par1,double[] bounds) {
HashMap<Integer, Double> indexToVal = new HashMap<Integer, Double>();
for(int i = 0; i < par1.length; i++) {
if(par1[i] >= bounds[0] && par1[i] <= bounds[1]) {
indexToVal.put(i, par1[i]);
}
}
return indexToVal;
}
/**
*
* @param par1 order par1
* @return 0 = min value, 1 = lower quartile, 2 = median, 3= upper quartile, 4 = max value
*/
public static double[] data(double[] par1) {
double[] out = {lowestVal(par1), lowerQuartile(par1), median(par1),upperQuartile(par1), highestVal(par1)};
return out;
}
/**
* order the input first
* @param par1 the input
* @return the Upper Quartile
*/
public static double upperQuartile(double[] par1) {
ArrayList<Double> par2 = new ArrayList<Double> ();
for(double i : par1) {
par2.add(i);
}
ArrayList<Double> par3 = new ArrayList<Double> ();
if(par1.length % 2 == 0) {
for(int i = (int) (par1.length*.5); i < par1.length; i++) {
par3.add(par1[i]);
}
}
else {
for(int i = (int) ((1 + par1.length) * .5 -1); i < par1.length; i++) {
par3.add(par1[i]);
}
}
double[] quart = new double[par3.size()];
for(int i = 0; i < par3.size(); i++) {
quart[i] = par3.get(i);
}
return median(quart);
}
/**
* order the input first
* @param par1 the input
* @return the Lower Quartile
*/
public static double lowerQuartile(double[] par1) {
ArrayList<Double> par2 = new ArrayList<Double> ();
for(double i : par1) {
par2.add(i);
}
ArrayList<Double> par3 = new ArrayList<Double> ();
if(par1.length % 2 == 0) {
for(int i = 0; i < (int) (par1.length*.5); i++) {
par3.add(par1[i]);
//System.out.println(par1[i]);
}
}
else {
for(int i = 0; i < (int) ((1 + par1.length) * .5 ); i++) {
par3.add(par1[i]);
}
}
double[] quart = new double[par3.size()];
for(int i = 0; i < par3.size(); i++) {
quart[i] = par3.get(i);
}
return median(quart);
}
/**
* order the input first
* @param par1 the input
* @return the median
*/
public static double median(double[] par1) {
if(par1.length % 2 == 0) {
return (par1[par1.length/2] + par1[par1.length/2 - 1]) *.5;
}
else {
return par1[(int) ((1 + par1.length) * .5) - 1];
}
}
/**
* orders the set from smallest to largest
* @param par1
* @return
*/
public static double[] order(double[] par1) {
double[] clone = par1.clone();
double[] ans = new double[par1.length];
for(int i = 0; i < ans.length; i++) {
ans[i] = replacelowestVal(clone);
}
return ans;
}
public static double lowestVal(double[] par1) {
double low = highestVal(par1);
double[] clone = par1.clone();
for(double i : clone) {
if(i<low)
low=i;
}
return low;
}
public static double[] lowestValArrayList(ArrayList<Double>par1) {
double low = Integer.MAX_VALUE;
int index = 0;
for(int i =0; i < par1.size(); i++) {
if(par1.get(i)<low) {
low=par1.get(i);
index = i;
}
}
double[] re ={low, index};
return re;
}
/**
*
* @param par1 the array
* @return
*/
public static double replacelowestVal(double[] par1) {
ArrayList<Double> par2 = new ArrayList<Double> ();
for(double i : par1) {
par2.add(i);
}
double low = highestVal(par1);
//for(int i =0; i < par1.length; i++) {
low=lowestValArrayList(par2)[0];
int index = (int)(lowestValArrayList(par2)[1]);
par2.set(index, highestVal(par1));
for(int i =0; i < par2.size(); i++) {
par1[i] = par2.get(i);
}
//}
return low;
}
public static double highestVal(double[] par1) {
double high = 0;
double[] clone = par1.clone();
for(int i =0; i < par1.length; i++) {
if(par1[i] > high)
high= clone[i];
}
return high;
}
public static double[][] multiplicar(double[][] A, double[][] B) {
int aRows = A.length;
int aColumns = A[0].length;
int bRows = B.length;
int bColumns = B[0].length;
if (aColumns != bRows) {
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
}
double[][] C = new double[aRows][bColumns];
for (int i = 0; i < aRows; i++) {
for (int j = 0; j < bColumns; j++) {
C[i][j] = 0.00000;
}
}
for (int i = 0; i < aRows; i++) { // aRow
for (int j = 0; j < bColumns; j++) { // bColumn
for (int k = 0; k < aColumns; k++) { // aColumn
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
/**
* Does matrix subtaction of probilities in order to find true bayesian correlation
* @param A data set
* @param B control set
* @return A - B
*/
public static double[][] matrixSetSubtraction(double[][] A, double[][] B){
double[][] C = new double[A.length][A[0].length];
for(int i = 0; i < A.length; i++) {
for(int j = 0; j < A[0].length; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
return C;
}
/**
* Gets the trace of a matrix
* @param par1
* @return
*/
public static double trace(double[][] par1) {
double tr = 0;
for(int i = 0; i < par1.length; i++) {
tr += par1[i][i];
}
return tr;
}
/**
* gets correlation based on trace
* @param par1 matrix
* @return correlation
*/
public static double traceCorrelation(double[][] par1) {
double sum =0.0;
for(int i = 0; i < par1.length; i++) {
for(int j = 0; j< par1[j].length; j++) {
sum+= par1[i][j];
}
}
return trace(par1)/sum;
}
/**
* get the bayesian correlation for each quartile
* @param par1 the matrix
* @return
*/
public static double[] BayesianCorrelationQuartiles(double[][] par1) {
double[] ans = new double[par1.length];
for(int i =0; i < par1.length; i++) {
double diag = par1[i][i];
double pb = 0;
for(int j = 0; j < i+1; j++) {
pb += par1[j][i];
}
ans[i] = diag / pb;
}
return ans;
}
/**
* gets the bayesian correlation for the line
* @param par1 the matrix
* @return
*/
public static double BayesianCorrelationLine(double[][] par1) {
double bottom = 0;
for(int i =0; i < par1.length; i++) {
double pb = 0;
for(int j = 0; j < i+1; j++) {
bottom += par1[j][i];
}
}
return trace(par1) / bottom;
}
}