|
1
|
|
package baseCode.math;
|
|
2
|
|
|
|
3
|
|
import baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix;
|
|
4
|
|
import baseCode.dataStructure.matrix.DenseDoubleMatrix2DNamed;
|
|
5
|
|
import baseCode.dataStructure.matrix.SparseDoubleMatrix2DNamed;
|
|
6
|
|
import cern.colt.function.DoubleFunction;
|
|
7
|
|
import cern.colt.list.DoubleArrayList;
|
|
8
|
|
import cern.colt.matrix.DoubleMatrix1D;
|
|
9
|
|
import cern.jet.math.Functions;
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
public class MatrixStats {
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
|
26
|
1
|
public static DenseDoubleMatrix2DNamed correlationMatrix(
|
|
27
|
|
AbstractNamedDoubleMatrix data ) {
|
|
28
|
1
|
DenseDoubleMatrix2DNamed result = new DenseDoubleMatrix2DNamed( data
|
|
29
|
|
.rows(), data.rows() );
|
|
30
|
|
|
|
31
|
1
|
for ( int i = 0; i < data.rows(); i++ ) {
|
|
32
|
30
|
DoubleArrayList irow = new DoubleArrayList( data.getRow( i ) );
|
|
33
|
30
|
for ( int j = i + 1; j < data.rows(); j++ ) {
|
|
34
|
435
|
DoubleArrayList jrow = new DoubleArrayList( data.getRow( j ) );
|
|
35
|
435
|
double c = DescriptiveWithMissing.correlation( irow, jrow );
|
|
36
|
435
|
result.setQuick( i, j, c );
|
|
37
|
435
|
result.setQuick( j, i, c );
|
|
38
|
|
}
|
|
39
|
|
}
|
|
40
|
1
|
result.setRowNames( data.getRowNames() );
|
|
41
|
1
|
result.setColumnNames( data.getRowNames() );
|
|
42
|
|
|
|
43
|
1
|
return result;
|
|
44
|
|
}
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
0
|
public static SparseDoubleMatrix2DNamed correlationMatrix(
|
|
52
|
|
AbstractNamedDoubleMatrix data, double threshold ) {
|
|
53
|
0
|
SparseDoubleMatrix2DNamed result = new SparseDoubleMatrix2DNamed( data
|
|
54
|
|
.rows(), data.rows() );
|
|
55
|
|
|
|
56
|
0
|
for ( int i = 0; i < data.rows(); i++ ) {
|
|
57
|
0
|
DoubleArrayList irow = new DoubleArrayList( data.getRow( i ) );
|
|
58
|
0
|
for ( int j = i + 1; j < data.rows(); j++ ) {
|
|
59
|
0
|
DoubleArrayList jrow = new DoubleArrayList( data.getRow( j ) );
|
|
60
|
0
|
double c = DescriptiveWithMissing.correlation( irow, jrow );
|
|
61
|
0
|
if ( Math.abs( c ) > threshold ) {
|
|
62
|
0
|
result.setQuick( i, j, c );
|
|
63
|
0
|
result.setQuick( j, i, c );
|
|
64
|
|
}
|
|
65
|
|
}
|
|
66
|
|
}
|
|
67
|
0
|
result.setRowNames( data.getRowNames() );
|
|
68
|
0
|
result.setColumnNames( data.getRowNames() );
|
|
69
|
|
|
|
70
|
0
|
return result;
|
|
71
|
|
}
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
1
|
public static double min( AbstractNamedDoubleMatrix matrix ) {
|
|
80
|
|
|
|
81
|
1
|
int totalRows = matrix.rows();
|
|
82
|
1
|
int totalColumns = matrix.columns();
|
|
83
|
|
|
|
84
|
1
|
double min = Double.MAX_VALUE;
|
|
85
|
|
|
|
86
|
1
|
for ( int i = 0; i < totalRows; i++ ) {
|
|
87
|
30
|
for ( int j = 0; j < totalColumns; j++ ) {
|
|
88
|
360
|
double val = matrix.getQuick( i, j );
|
|
89
|
360
|
if ( Double.isNaN( val ) ) {
|
|
90
|
0
|
continue;
|
|
91
|
|
}
|
|
92
|
|
|
|
93
|
360
|
if ( val < min ) {
|
|
94
|
12
|
min = val;
|
|
95
|
|
}
|
|
96
|
|
|
|
97
|
|
}
|
|
98
|
|
}
|
|
99
|
1
|
if ( min == Double.MAX_VALUE ) {
|
|
100
|
0
|
return Double.NaN;
|
|
101
|
|
}
|
|
102
|
1
|
return min;
|
|
103
|
|
|
|
104
|
|
}
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
|
112
|
1
|
public static double max( AbstractNamedDoubleMatrix matrix ) {
|
|
113
|
|
|
|
114
|
1
|
int totalRows = matrix.rows();
|
|
115
|
1
|
int totalColumns = matrix.columns();
|
|
116
|
|
|
|
117
|
1
|
double max = -Double.MAX_VALUE;
|
|
118
|
|
|
|
119
|
1
|
for ( int i = 0; i < totalRows; i++ ) {
|
|
120
|
30
|
for ( int j = 0; j < totalColumns; j++ ) {
|
|
121
|
360
|
double val = matrix.getQuick( i, j );
|
|
122
|
360
|
if ( Double.isNaN( val ) ) {
|
|
123
|
0
|
continue;
|
|
124
|
|
}
|
|
125
|
|
|
|
126
|
360
|
if ( val > max ) {
|
|
127
|
14
|
max = val;
|
|
128
|
|
}
|
|
129
|
|
}
|
|
130
|
|
}
|
|
131
|
|
|
|
132
|
1
|
if ( max == -Double.MAX_VALUE ) {
|
|
133
|
0
|
return Double.NaN;
|
|
134
|
|
}
|
|
135
|
|
|
|
136
|
1
|
return max;
|
|
137
|
|
|
|
138
|
|
}
|
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
|
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
|
150
|
1
|
public static void rbfNormalize(
|
|
151
|
|
AbstractNamedDoubleMatrix matrixToNormalize, final double sigma ) {
|
|
152
|
|
|
|
153
|
|
|
|
154
|
1
|
DoubleFunction f = new DoubleFunction() {
|
|
155
|
360
|
public double apply( double value ) {
|
|
156
|
360
|
return Math.exp( -value / sigma );
|
|
157
|
|
}
|
|
158
|
|
};
|
|
159
|
|
|
|
160
|
1
|
for ( int j = 0; j < matrixToNormalize.rows(); j++ ) {
|
|
161
|
|
|
|
162
|
30
|
DoubleMatrix1D row = matrixToNormalize.viewRow( j );
|
|
163
|
30
|
row.assign( f );
|
|
164
|
30
|
double sum = row.zSum();
|
|
165
|
30
|
row.assign( Functions.div( sum ) );
|
|
166
|
|
|
|
167
|
|
}
|
|
168
|
|
}
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
|
|
|
176
|
0
|
public static void countsNormalize (
|
|
177
|
|
AbstractNamedDoubleMatrix matrixToNormalize, final double sigma ) {
|
|
178
|
|
|
|
179
|
0
|
final double min = MatrixStats.min( matrixToNormalize );
|
|
180
|
0
|
DoubleFunction f = new DoubleFunction() {
|
|
181
|
0
|
public double apply( double value ) {
|
|
182
|
0
|
return value - min + 1;
|
|
183
|
|
}
|
|
184
|
|
};
|
|
185
|
|
|
|
186
|
0
|
for ( int j = 0; j < matrixToNormalize.rows(); j++ ) {
|
|
187
|
0
|
DoubleMatrix1D row = matrixToNormalize.viewRow( j );
|
|
188
|
0
|
row.assign( f );
|
|
189
|
0
|
double sum = row.zSum();
|
|
190
|
0
|
row.assign( Functions.div( sum ) );
|
|
191
|
|
}
|
|
192
|
|
}
|
|
193
|
|
|
|
194
|
|
}
|