1 | /*
|
---|
2 | * Class RankAnalysis
|
---|
3 | *
|
---|
4 | * USAGE: Matrix Rank Analysis
|
---|
5 | *
|
---|
6 | * WRITTEN BY: Dr Michael Thomas Flanagan
|
---|
7 | *
|
---|
8 | * DATE: August - September 2008
|
---|
9 | * UPDATE: 12 October 2008
|
---|
10 | *
|
---|
11 | * DOCUMENTATION:
|
---|
12 | * See Michael Thomas Flanagan's Java library web page:
|
---|
13 | * http://www.ee.ucl.ac.uk/~mflanaga/java/RankAnalysis.html
|
---|
14 | * http://www.ee.ucl.ac.uk/~mflanaga/java/
|
---|
15 | *
|
---|
16 | * Copyright (c) 2008 Michael Thomas Flanagan
|
---|
17 | *
|
---|
18 | * PERMISSION TO COPY:
|
---|
19 | *
|
---|
20 | * Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
|
---|
21 | * provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
|
---|
22 | * and associated documentation or publications.
|
---|
23 | *
|
---|
24 | * Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
|
---|
25 | * and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
|
---|
26 | *
|
---|
27 | * Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
|
---|
28 | * the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
|
---|
29 | *
|
---|
30 | * Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
|
---|
31 | * Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
|
---|
32 | * or its derivatives.
|
---|
33 | *
|
---|
34 | ***************************************************************************************/
|
---|
35 |
|
---|
36 | package agents.anac.y2015.agentBuyogV2.flanagan.analysis;
|
---|
37 |
|
---|
38 | import java.util.ArrayList;
|
---|
39 | import java.util.Vector;
|
---|
40 |
|
---|
41 | import agents.anac.y2015.agentBuyogV2.flanagan.analysis.Cronbach;
|
---|
42 | import agents.anac.y2015.agentBuyogV2.flanagan.analysis.Stat;
|
---|
43 | import agents.anac.y2015.agentBuyogV2.flanagan.io.FileOutput;
|
---|
44 | import agents.anac.y2015.agentBuyogV2.flanagan.math.ArrayMaths;
|
---|
45 | import agents.anac.y2015.agentBuyogV2.flanagan.math.Conv;
|
---|
46 | import agents.anac.y2015.agentBuyogV2.flanagan.math.Fmath;
|
---|
47 | import agents.anac.y2015.agentBuyogV2.flanagan.math.Matrix;
|
---|
48 |
|
---|
49 | import java.math.BigDecimal;
|
---|
50 | import java.math.BigInteger;
|
---|
51 | import java.util.*;
|
---|
52 | import java.text.*;
|
---|
53 |
|
---|
54 | public class RankAnalysis{
|
---|
55 |
|
---|
56 | private double[][] values = null; // matrix of values whose rank is required
|
---|
57 | private double[][] errors = null; // matrix of errors of the values
|
---|
58 |
|
---|
59 | private double[] valuesDiagonal = null; // diagonal of values whose rank is required
|
---|
60 | private double[] errorsDiagonal = null; // diagonal of errors of the values
|
---|
61 |
|
---|
62 | private double[][] reducedValues = null; // reduced matrix of values whose rank is required
|
---|
63 | private double[][] reducedErrors = null; // reduced matrix of standard deviations of the values
|
---|
64 |
|
---|
65 | private double[] reducedValuesDiagonal = null; // diagonal of reduced values
|
---|
66 | private double[] reducedErrorsDiagonal = null; // diagonal of reduved errors
|
---|
67 | private double[] reducedValueOverError = null; // ratio of reduced value diagonal over reduced error diagonal
|
---|
68 | private double[] probabilityValues = null; // P-values for above ratios
|
---|
69 | private double[] mcMullen = null; // Criteria of McMullen, Jaskunas and Tinoco
|
---|
70 |
|
---|
71 | private int numberOfRows = 0; // number of rows
|
---|
72 | private int numberOfColumns = 0; // number of columns
|
---|
73 | private int diagonalLength = 0; // length of diagonal
|
---|
74 |
|
---|
75 | private int errorType = 3; // = 0 matrix of individual errors supplied
|
---|
76 | // = 1 common error for all elements in each each row supplied
|
---|
77 | // = 2 single common error for all elements in the matrix supplied
|
---|
78 | // = 3 no error/s supplied
|
---|
79 |
|
---|
80 | private double[] errorRowMeans = null; // means of the rows of errors
|
---|
81 | private double[] errorColumnMeans = null; // means of the columns of errors
|
---|
82 |
|
---|
83 | private int numberOfMissingErrors = 0; // number of missing errors (entered as NaN)
|
---|
84 | private boolean rowOption = true; // = true - missing errors replaced by the appropriate row mean
|
---|
85 | // = false - missing errors replaced by the appropriate column mean
|
---|
86 |
|
---|
87 | private boolean rankAnalysisDone = false; // = true when rank analysis performed
|
---|
88 |
|
---|
89 |
|
---|
90 | // CONSTRUCTORS
|
---|
91 | // Individual error for each value
|
---|
92 | public RankAnalysis(double[][] values, double[][] errors){
|
---|
93 | this.values = Conv.copy(values);
|
---|
94 | this.errors = Conv.copy(errors);
|
---|
95 | this.errorType = 0;
|
---|
96 | this.preprocessDataOne();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public RankAnalysis(float[][] values, float[][] errors){
|
---|
100 | Matrix matv = new Matrix(values);
|
---|
101 | this.values = matv.getArrayCopy();
|
---|
102 | Matrix mate = new Matrix(errors);
|
---|
103 | this.errors = mate.getArrayCopy();
|
---|
104 | this.errorType = 0;
|
---|
105 | this.preprocessDataOne();
|
---|
106 | }
|
---|
107 |
|
---|
108 | public RankAnalysis(long[][] values, long[][] errors){
|
---|
109 | Matrix matv = new Matrix(values);
|
---|
110 | this.values = matv.getArrayCopy();
|
---|
111 | Matrix mate = new Matrix(errors);
|
---|
112 | this.errors = mate.getArrayCopy();
|
---|
113 | this.errorType = 0;
|
---|
114 | this.preprocessDataOne();
|
---|
115 | }
|
---|
116 |
|
---|
117 | public RankAnalysis(int[][] values, int[][] errors){
|
---|
118 | Matrix matv = new Matrix(values);
|
---|
119 | this.values = matv.getArrayCopy();
|
---|
120 | Matrix mate = new Matrix(errors);
|
---|
121 | this.errors = mate.getArrayCopy();
|
---|
122 | this.errorType = 0;
|
---|
123 | this.preprocessDataOne();
|
---|
124 | }
|
---|
125 |
|
---|
126 | public RankAnalysis(BigDecimal[][] values, BigDecimal[][] errors){
|
---|
127 | Matrix matv = new Matrix(values);
|
---|
128 | this.values = matv.getArrayCopy();
|
---|
129 | Matrix mate = new Matrix(errors);
|
---|
130 | this.errors = mate.getArrayCopy();
|
---|
131 | this.errorType = 0;
|
---|
132 | this.preprocessDataOne();
|
---|
133 | }
|
---|
134 |
|
---|
135 | public RankAnalysis(BigInteger[][] values, BigInteger[][] errors){
|
---|
136 | Matrix matv = new Matrix(values);
|
---|
137 | this.values = matv.getArrayCopy();
|
---|
138 | Matrix mate = new Matrix(errors);
|
---|
139 | this.errors = mate.getArrayCopy();
|
---|
140 | this.errorType = 0;
|
---|
141 | this.preprocessDataOne();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public RankAnalysis(ArrayMaths[] values, ArrayMaths[] errors){
|
---|
145 | Matrix matv = new Matrix(values);
|
---|
146 | this.values = matv.getArrayCopy();
|
---|
147 | Matrix mate = new Matrix(errors);
|
---|
148 | this.errors = mate.getArrayCopy();
|
---|
149 | this.errorType = 0;
|
---|
150 | this.preprocessDataOne();
|
---|
151 | }
|
---|
152 |
|
---|
153 | public RankAnalysis(ArrayList<Object>[] values, ArrayList<Object>[] errors){
|
---|
154 | Matrix matv = new Matrix(values);
|
---|
155 | this.values = matv.getArrayCopy();
|
---|
156 | Matrix mate = new Matrix(errors);
|
---|
157 | this.errors = mate.getArrayCopy();
|
---|
158 | this.errorType = 0;
|
---|
159 | this.preprocessDataOne();
|
---|
160 | }
|
---|
161 |
|
---|
162 | public RankAnalysis(Vector<Object>[] values, Vector<Object>[] errors){
|
---|
163 | Matrix matv = new Matrix(values);
|
---|
164 | this.values = matv.getArrayCopy();
|
---|
165 | Matrix mate = new Matrix(errors);
|
---|
166 | this.errors = mate.getArrayCopy();
|
---|
167 | this.errorType = 0;
|
---|
168 | this.preprocessDataOne();
|
---|
169 | }
|
---|
170 |
|
---|
171 | public RankAnalysis(Matrix values, Matrix errors){
|
---|
172 | this.values = values.getArrayCopy();
|
---|
173 | this.errors = errors.getArrayCopy();
|
---|
174 | this.errorType = 0;
|
---|
175 | this.preprocessDataOne();
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 |
|
---|
180 | // Common error for each row
|
---|
181 | public RankAnalysis(double[][] values, double[] errors){
|
---|
182 | this.values = Conv.copy(values);
|
---|
183 | this.errors = this.oneToTwo(Conv.copy(errors), this.values[0].length);
|
---|
184 | this.errorType = 1;
|
---|
185 | this.preprocessDataOne();
|
---|
186 | }
|
---|
187 |
|
---|
188 | public RankAnalysis(float[][] values, float[] errors){
|
---|
189 | Matrix matv = new Matrix(values);
|
---|
190 | this.values = matv.getArrayCopy();
|
---|
191 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
192 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
193 | this.errorType = 1;
|
---|
194 | this.preprocessDataOne();
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | public RankAnalysis(long[][] values, long[] errors){
|
---|
199 | Matrix matv = new Matrix(values);
|
---|
200 | this.values = matv.getArrayCopy();
|
---|
201 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
202 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
203 | this.errorType = 1;
|
---|
204 | this.preprocessDataOne();
|
---|
205 | }
|
---|
206 |
|
---|
207 | public RankAnalysis(int[][] values, int[] errors){
|
---|
208 | Matrix matv = new Matrix(values);
|
---|
209 | this.values = matv.getArrayCopy();
|
---|
210 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
211 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
212 | this.errorType = 1;
|
---|
213 | this.preprocessDataOne();
|
---|
214 | }
|
---|
215 |
|
---|
216 | public RankAnalysis(BigDecimal[][] values, BigDecimal[] errors){
|
---|
217 | Matrix matv = new Matrix(values);
|
---|
218 | this.values = matv.getArrayCopy();
|
---|
219 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
220 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
221 | this.errorType = 1;
|
---|
222 | this.preprocessDataOne();
|
---|
223 | }
|
---|
224 |
|
---|
225 | public RankAnalysis(BigInteger[][] values, BigInteger[] errors){
|
---|
226 | Matrix matv = new Matrix(values);
|
---|
227 | this.values = matv.getArrayCopy();
|
---|
228 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
229 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
230 | this.errorType = 1;
|
---|
231 | this.preprocessDataOne();
|
---|
232 | }
|
---|
233 |
|
---|
234 | public RankAnalysis(ArrayMaths[] values, ArrayMaths errors){
|
---|
235 | Matrix matv = new Matrix(values);
|
---|
236 | this.values = matv.getArrayCopy();
|
---|
237 | this.errors = this.oneToTwo(errors.array(), this.values[0].length);
|
---|
238 | this.errorType = 1;
|
---|
239 | this.preprocessDataOne();
|
---|
240 | }
|
---|
241 |
|
---|
242 | public RankAnalysis(ArrayList<Object>[] values, ArrayList<Object> errors){
|
---|
243 | Matrix matv = new Matrix(values);
|
---|
244 | this.values = matv.getArrayCopy();
|
---|
245 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
246 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
247 | this.errorType = 1;
|
---|
248 | this.preprocessDataOne();
|
---|
249 | }
|
---|
250 |
|
---|
251 | public RankAnalysis(Vector<Object>[] values, Vector<Object> errors){
|
---|
252 | Matrix matv = new Matrix(values);
|
---|
253 | this.values = matv.getArrayCopy();
|
---|
254 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
255 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
256 | this.errorType = 1;
|
---|
257 | this.preprocessDataOne();
|
---|
258 | }
|
---|
259 |
|
---|
260 | public RankAnalysis(Scores values){
|
---|
261 | this.values = values.usedScoresAsRowPerItem();
|
---|
262 | Matrix mat = new Matrix(this.values);
|
---|
263 | double[] errors = mat.rowStandardDeviations();
|
---|
264 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
265 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
266 | this.errorType = 1;
|
---|
267 | this.preprocessDataOne();
|
---|
268 | }
|
---|
269 |
|
---|
270 | public RankAnalysis(Cronbach values){
|
---|
271 | this.values = values.usedScoresAsRowPerItem();
|
---|
272 | Matrix mat = new Matrix(this.values);
|
---|
273 | double[] errors = mat.rowStandardDeviations();
|
---|
274 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
275 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
276 | this.errorType = 1;
|
---|
277 | this.preprocessDataOne();
|
---|
278 | }
|
---|
279 |
|
---|
280 | public RankAnalysis(PCA values){
|
---|
281 | this.values = values.usedScoresAsRowPerItem();
|
---|
282 | Matrix mat = new Matrix(this.values);
|
---|
283 | double[] errors = mat.rowStandardDeviations();
|
---|
284 | ArrayMaths ame = new ArrayMaths(errors);
|
---|
285 | this.errors = this.oneToTwo(ame.array(), this.values[0].length);
|
---|
286 | this.errorType = 1;
|
---|
287 | this.preprocessDataOne();
|
---|
288 | }
|
---|
289 |
|
---|
290 | // Common error for all values
|
---|
291 | public RankAnalysis(double[][] values, double commonError){
|
---|
292 | this.values = Conv.copy(values);
|
---|
293 | this.errorType = 2;
|
---|
294 | this.preprocessDataTwo(commonError);
|
---|
295 | }
|
---|
296 |
|
---|
297 | public RankAnalysis(float[][] values, float commonError){
|
---|
298 | Matrix matv = new Matrix(values);
|
---|
299 | this.values = matv.getArrayCopy();
|
---|
300 | this.errorType = 2;
|
---|
301 | this.preprocessDataTwo((double)commonError);
|
---|
302 | }
|
---|
303 |
|
---|
304 | public RankAnalysis(long[][] values, long commonError){
|
---|
305 | Matrix matv = new Matrix(values);
|
---|
306 | this.values = matv.getArrayCopy();
|
---|
307 | this.errorType = 2;
|
---|
308 | this.preprocessDataTwo((double)commonError);
|
---|
309 | }
|
---|
310 |
|
---|
311 | public RankAnalysis(int[][] values, int commonError){
|
---|
312 | Matrix matv = new Matrix(values);
|
---|
313 | this.values = matv.getArrayCopy();
|
---|
314 | this.errorType = 2;
|
---|
315 | this.preprocessDataTwo((double)commonError);
|
---|
316 | }
|
---|
317 |
|
---|
318 | public RankAnalysis(BigDecimal[][] values, BigDecimal commonError){
|
---|
319 | Matrix matv = new Matrix(values);
|
---|
320 | this.values = matv.getArrayCopy();
|
---|
321 | this.errorType = 2;
|
---|
322 | this.preprocessDataTwo(commonError.doubleValue());
|
---|
323 | }
|
---|
324 |
|
---|
325 | public RankAnalysis(BigInteger[][] values, BigInteger commonError){
|
---|
326 | Matrix matv = new Matrix(values);
|
---|
327 | this.values = matv.getArrayCopy();
|
---|
328 | this.errorType = 2;
|
---|
329 | this.preprocessDataTwo(commonError.doubleValue());
|
---|
330 | }
|
---|
331 |
|
---|
332 | public RankAnalysis(ArrayMaths[] values, double commonError){
|
---|
333 | Matrix matv = new Matrix(values);
|
---|
334 | this.values = matv.getArrayCopy();
|
---|
335 | this.errorType = 2;
|
---|
336 | this.preprocessDataTwo(commonError);
|
---|
337 | }
|
---|
338 |
|
---|
339 | public RankAnalysis(ArrayList<Object>[] values, double commonError){
|
---|
340 | Matrix matv = new Matrix(values);
|
---|
341 | this.values = matv.getArrayCopy();
|
---|
342 | this.errorType = 2;
|
---|
343 | this.preprocessDataTwo(commonError);
|
---|
344 | }
|
---|
345 |
|
---|
346 | public RankAnalysis(Vector<Object>[] values, double commonError){
|
---|
347 | Matrix matv = new Matrix(values);
|
---|
348 | this.values = matv.getArrayCopy();
|
---|
349 | this.errorType = 2;
|
---|
350 | this.preprocessDataTwo(commonError);
|
---|
351 | }
|
---|
352 |
|
---|
353 | public RankAnalysis(Matrix values, double commonError){
|
---|
354 | this.values = values.getArrayCopy();
|
---|
355 | this.errorType = 2;
|
---|
356 | this.preprocessDataTwo(commonError);
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 |
|
---|
361 | // No errors supplied
|
---|
362 | public RankAnalysis(double[][] values){
|
---|
363 | this.values = Conv.copy(values);
|
---|
364 | this.errorType = 3;
|
---|
365 | this.preprocessDataThree();
|
---|
366 | }
|
---|
367 |
|
---|
368 | public RankAnalysis(float[][] values){
|
---|
369 | Matrix matv = new Matrix(values);
|
---|
370 | this.values = matv.getArrayCopy();
|
---|
371 | this.errorType = 3;
|
---|
372 | this.preprocessDataThree();
|
---|
373 | }
|
---|
374 |
|
---|
375 | public RankAnalysis(long[][] values){
|
---|
376 | Matrix matv = new Matrix(values);
|
---|
377 | this.values = matv.getArrayCopy();
|
---|
378 | this.errorType = 3;
|
---|
379 | this.preprocessDataThree();
|
---|
380 | }
|
---|
381 |
|
---|
382 | public RankAnalysis(int[][] values){
|
---|
383 | Matrix matv = new Matrix(values);
|
---|
384 | this.values = matv.getArrayCopy();
|
---|
385 | this.errorType = 3;
|
---|
386 | this.preprocessDataThree();
|
---|
387 | }
|
---|
388 |
|
---|
389 | public RankAnalysis(BigDecimal[][] values){
|
---|
390 | Matrix matv = new Matrix(values);
|
---|
391 | this.values = matv.getArrayCopy();
|
---|
392 | this.errorType = 3;
|
---|
393 | this.preprocessDataThree();
|
---|
394 | }
|
---|
395 |
|
---|
396 | public RankAnalysis(BigInteger[][] values){
|
---|
397 | Matrix matv = new Matrix(values);
|
---|
398 | this.values = matv.getArrayCopy();
|
---|
399 | this.errorType = 3;
|
---|
400 | this.preprocessDataThree();
|
---|
401 | }
|
---|
402 |
|
---|
403 | public RankAnalysis(ArrayMaths[] values){
|
---|
404 | Matrix matv = new Matrix(values);
|
---|
405 | this.values = matv.getArrayCopy();
|
---|
406 | this.errorType = 3;
|
---|
407 | this.preprocessDataThree();
|
---|
408 | }
|
---|
409 |
|
---|
410 | public RankAnalysis(ArrayList<Object>[] values){
|
---|
411 | Matrix matv = new Matrix(values);
|
---|
412 | this.values = matv.getArrayCopy();
|
---|
413 | this.errorType = 3;
|
---|
414 | this.preprocessDataThree();
|
---|
415 | }
|
---|
416 |
|
---|
417 | public RankAnalysis(Vector<Object>[] values){
|
---|
418 | Matrix matv = new Matrix(values);
|
---|
419 | this.values = matv.getArrayCopy();
|
---|
420 | this.errorType = 3;
|
---|
421 | this.preprocessDataThree();
|
---|
422 | }
|
---|
423 |
|
---|
424 | public RankAnalysis(Matrix values){
|
---|
425 | this.values = values.getArrayCopy();
|
---|
426 | this.errorType = 3;
|
---|
427 | this.preprocessDataThree();
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 |
|
---|
432 |
|
---|
433 | // Convets common error per row to individual errors for all rows
|
---|
434 | private double[][] oneToTwo(double[] errors, int nCols){
|
---|
435 | int nRows = errors.length;
|
---|
436 | double[][] ret = new double[nRows][nCols];
|
---|
437 | for(int i=0; i<nRows; i++){
|
---|
438 | for(int j=0; j<nCols; j++){
|
---|
439 | ret[i][j] = errors[i];
|
---|
440 | }
|
---|
441 | }
|
---|
442 | return ret;
|
---|
443 | }
|
---|
444 |
|
---|
445 |
|
---|
446 | // Preprocess data with individual errors supplied directly
|
---|
447 | // or after common row error converted to individual errors by private method oneToTwo()
|
---|
448 | private void preprocessDataOne(){
|
---|
449 | // Check row and column lengths
|
---|
450 | this.numberOfRows = this.values.length;
|
---|
451 | this.numberOfColumns = this.values[0].length;
|
---|
452 | for(int i=1; i<this.numberOfRows; i++){
|
---|
453 | if(this.values[i].length!=this.numberOfColumns)throw new IllegalArgumentException("All rows of the value matrix must be of the same length");
|
---|
454 | }
|
---|
455 | for(int i=0; i<this.numberOfRows; i++){
|
---|
456 | if(this.errors[i].length!=this.numberOfColumns)throw new IllegalArgumentException("All rows of the error matrix must be of the same length as those of the value matrix");
|
---|
457 | }
|
---|
458 | this.diagonalLength = this.numberOfRows;
|
---|
459 | if(this.numberOfRows>this.numberOfColumns)this.diagonalLength = this.numberOfColumns;
|
---|
460 |
|
---|
461 | // Convert errors to variances
|
---|
462 | for(int i=0; i<this.numberOfRows; i++){
|
---|
463 | for(int j=0; j<this.numberOfColumns; j++){
|
---|
464 | this.errors[i][j] *= this.errors[i][j];
|
---|
465 | }
|
---|
466 | }
|
---|
467 | }
|
---|
468 |
|
---|
469 | // Preprocess data witha single common error supplied
|
---|
470 | private void preprocessDataTwo(double commonError){
|
---|
471 | // Check row and column lengths
|
---|
472 | this.numberOfRows = this.values.length;
|
---|
473 | this.numberOfColumns = this.values[0].length;
|
---|
474 | for(int i=1; i<this.numberOfRows; i++){
|
---|
475 | if(this.values[i].length!=this.numberOfColumns)throw new IllegalArgumentException("All rows of the value matrix must be of the same length");
|
---|
476 | }
|
---|
477 | this.diagonalLength = this.numberOfRows;
|
---|
478 | if(this.numberOfRows>this.numberOfColumns)this.diagonalLength = this.numberOfColumns;
|
---|
479 |
|
---|
480 | // Fill errors matrix
|
---|
481 | this.errors = new double[this.numberOfRows][this.numberOfColumns];
|
---|
482 | for(int i=0; i<this.numberOfRows; i++){
|
---|
483 | for(int j=0; j<this.numberOfColumns; j++){
|
---|
484 | this.errors[i][j] = commonError*commonError;
|
---|
485 | }
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | // Preprocess data with no errors supplied
|
---|
490 | // Each error substituted by a very rough estimate of the potential rounding error
|
---|
491 | private void preprocessDataThree(){
|
---|
492 | // Check row and column lengths
|
---|
493 | this.numberOfRows = this.values.length;
|
---|
494 | this.numberOfColumns = this.values[0].length;
|
---|
495 | for(int i=1; i<this.numberOfRows; i++){
|
---|
496 | if(this.values[i].length!=this.numberOfColumns)throw new IllegalArgumentException("All rows of the value matrix must be of the same length");
|
---|
497 | }
|
---|
498 | this.diagonalLength = this.numberOfRows;
|
---|
499 | if(this.numberOfRows>this.numberOfColumns)this.diagonalLength = this.numberOfColumns;
|
---|
500 |
|
---|
501 | // Fill errors matrix
|
---|
502 | this.errors = new double[this.numberOfRows][this.numberOfColumns];
|
---|
503 | double error = 0.0;
|
---|
504 | for(int i=0; i<this.numberOfRows; i++){
|
---|
505 | for(int j=0; j<this.numberOfColumns; j++){
|
---|
506 | error = Math.pow(10.0, Math.floor(Math.log10(Math.abs(this.values[i][j]))))*5.0E-16;
|
---|
507 | this.errors[i][j] = error*error;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | // Missing error options
|
---|
513 | // Use error row mean to replace a missing error
|
---|
514 | // This is the default option
|
---|
515 | public void useErrorRowMean(){
|
---|
516 | this.rowOption = true;
|
---|
517 | }
|
---|
518 |
|
---|
519 | // Use error column mean to replace a missing error
|
---|
520 | public void useErrorColumnMean(){
|
---|
521 | this.rowOption = false;
|
---|
522 | }
|
---|
523 |
|
---|
524 | // Return number of replaced missing errors
|
---|
525 | public int nMissingErrors(){
|
---|
526 | return this.numberOfMissingErrors;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | // Rank analysis
|
---|
531 | private void rankAnalysis(){
|
---|
532 |
|
---|
533 | // Check errors for negative values and missing values (entered as NaN)
|
---|
534 |
|
---|
535 | // Row means and negative errors multiplied by -1
|
---|
536 | this.errorRowMeans = new double[this.numberOfRows];
|
---|
537 | this.errorColumnMeans = new double[this.numberOfColumns];
|
---|
538 | this.numberOfMissingErrors = 0;
|
---|
539 | for(int i=0; i<this.numberOfRows; i++){
|
---|
540 | int counter = 0;
|
---|
541 | for(int j=0; j<this.numberOfColumns; j++){
|
---|
542 | if(!Double.isNaN(this.errors[i][j])){
|
---|
543 | if(this.errors[i][j]<0.0)this.errors[i][j] *= -1.0;
|
---|
544 | this.errorRowMeans[i] += this.errors[i][j];
|
---|
545 | counter++;
|
---|
546 | }
|
---|
547 | else{
|
---|
548 | this.numberOfMissingErrors++;
|
---|
549 | }
|
---|
550 | }
|
---|
551 | this.errorRowMeans[i] /= counter;
|
---|
552 | }
|
---|
553 |
|
---|
554 | // Column means
|
---|
555 | for(int i=0; i<this.numberOfColumns; i++){
|
---|
556 | int counter = 0;
|
---|
557 | for(int j=0; j<this.numberOfRows; j++){
|
---|
558 | if(!Double.isNaN(this.errors[j][i])){
|
---|
559 | this.errorColumnMeans[i] += this.errors[j][i];
|
---|
560 | counter++;
|
---|
561 | }
|
---|
562 | }
|
---|
563 | this.errorColumnMeans[i] /= counter;
|
---|
564 | }
|
---|
565 |
|
---|
566 | // missing errors replaced by the row or column mean excluding the missing values (see missingErrorOption);
|
---|
567 | if(this.numberOfMissingErrors>0){
|
---|
568 | for(int i=0; i<this.numberOfRows; i++){
|
---|
569 | for(int j=0; j<this.numberOfColumns; j++){
|
---|
570 | if(Double.isNaN(this.errors[i][j])){
|
---|
571 | if(this.rowOption){
|
---|
572 | this.errors[i][j] = errorRowMeans[i];
|
---|
573 | }
|
---|
574 | else{
|
---|
575 | this.errors[i][j] = errorColumnMeans[i];
|
---|
576 | }
|
---|
577 | }
|
---|
578 | }
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | // Matrix reduction
|
---|
583 |
|
---|
584 | this.reducedValues = this.values;
|
---|
585 | this.reducedErrors = this.errors;
|
---|
586 | Matrix matv0 = new Matrix(this.reducedValues);
|
---|
587 | Matrix mate0 = new Matrix(this.reducedErrors);
|
---|
588 | int nn = this.diagonalLength - 1;
|
---|
589 |
|
---|
590 | for(int i=0; i<nn; i++){
|
---|
591 | matv0 = new Matrix(this.reducedValues);
|
---|
592 |
|
---|
593 | // Isolate sub-matrix
|
---|
594 | int nrow = this.numberOfRows-i;
|
---|
595 | int ncol = this.numberOfColumns - i;
|
---|
596 | Matrix mat1 = matv0.getSubMatrix(i, i, this.numberOfRows-1, numberOfColumns-1);
|
---|
597 | double[][] subv = mat1.getArrayCopy();
|
---|
598 |
|
---|
599 | // Get pivot indices
|
---|
600 | int[] max = mat1.pivot();
|
---|
601 | int pivotI = max[0]+i;
|
---|
602 | int pivotJ = max[1]+i;
|
---|
603 |
|
---|
604 | // Swap rows
|
---|
605 | double[] holdv1 = this.reducedValues[i];
|
---|
606 | double[] holde1 = this.reducedErrors[i];
|
---|
607 | this.reducedValues[i] = this.reducedValues[pivotI];
|
---|
608 | this.reducedErrors[i] = this.reducedErrors[pivotI];
|
---|
609 | this.reducedValues[pivotI] = holdv1;
|
---|
610 | this.reducedErrors[pivotI] = holde1;
|
---|
611 |
|
---|
612 | // Swap columns
|
---|
613 | double holdv2 = 0.0;
|
---|
614 | double holde2 = 0.0;
|
---|
615 | for(int j=0; j<numberOfRows; j++){
|
---|
616 | holdv2 = this.reducedValues[j][i];
|
---|
617 | holde2 = this.reducedErrors[j][i];
|
---|
618 | this.reducedValues[j][i] = this.reducedValues[j][pivotJ];
|
---|
619 | this.reducedErrors[j][i] = this.reducedErrors[j][pivotJ];
|
---|
620 | this.reducedValues[j][pivotJ] = holdv2;
|
---|
621 | this.reducedErrors[j][pivotJ] = holde2;
|
---|
622 | }
|
---|
623 |
|
---|
624 | // Reduce sub-matrix
|
---|
625 | Matrix matValueHold = new Matrix(this.reducedValues);
|
---|
626 | Matrix matErrorHold = new Matrix(this.reducedErrors);
|
---|
627 | double[][] valueHold = matValueHold.getArrayCopy();
|
---|
628 | double[][] errorHold = matErrorHold.getArrayCopy();
|
---|
629 |
|
---|
630 | for(int j=i+1; j<this.numberOfRows; j++){
|
---|
631 | for(int k=i; k<this.numberOfColumns; k++){
|
---|
632 | double ratio1 = 1.0;
|
---|
633 | if(this.reducedValues[j][i]!=this.reducedValues[i][i])ratio1 = this.reducedValues[j][i]/this.reducedValues[i][i];
|
---|
634 | valueHold[j][k] = this.reducedValues[j][k] - ratio1*this.reducedValues[i][k];
|
---|
635 | double hold = this.reducedErrors[j][k] + this.reducedErrors[i][k]*ratio1*ratio1;
|
---|
636 | double ratio2 = 1.0;
|
---|
637 | if(this.reducedValues[i][k]!=this.reducedValues[i][i])ratio2 = this.reducedValues[i][k]/this.reducedValues[i][i];
|
---|
638 | hold += this.reducedErrors[j][i]*ratio2*ratio2;
|
---|
639 | errorHold[j][k] = hold + this.reducedErrors[i][i]*ratio1*ratio1*ratio2*ratio2;
|
---|
640 | }
|
---|
641 | }
|
---|
642 | matValueHold = new Matrix(valueHold);
|
---|
643 | matErrorHold = new Matrix(errorHold);
|
---|
644 | this.reducedValues = matValueHold.getArrayCopy();
|
---|
645 | this.reducedErrors = matErrorHold.getArrayCopy();
|
---|
646 | }
|
---|
647 |
|
---|
648 | // Convert errors to standard deviations
|
---|
649 | for(int i=0; i<this.numberOfRows; i++){
|
---|
650 | for(int j=0; j<this.numberOfColumns; j++){
|
---|
651 | this.reducedErrors[i][j] = Math.sqrt(this.reducedErrors[i][j]);
|
---|
652 | }
|
---|
653 | }
|
---|
654 |
|
---|
655 | // Fill zero elements
|
---|
656 | for(int i=1; i<this.diagonalLength; i++){
|
---|
657 | for(int j=0; j<i; j++){
|
---|
658 | this.reducedValues[i][j] = 0.0;
|
---|
659 | this.reducedErrors[i][j] = 0.0;
|
---|
660 | }
|
---|
661 | }
|
---|
662 |
|
---|
663 | if(this.diagonalLength<this.numberOfRows){
|
---|
664 | for(int i=this.diagonalLength; i<this.numberOfRows; i++){
|
---|
665 | for(int j=0; j<this.numberOfColumns; j++){
|
---|
666 | this.reducedValues[i][j] = 0.0;
|
---|
667 | this.reducedErrors[i][j] = 0.0;
|
---|
668 | }
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | // store diagonals and calculate probability values
|
---|
673 | this.reducedValuesDiagonal = new double[this.diagonalLength];
|
---|
674 | this.reducedErrorsDiagonal = new double[this.diagonalLength];
|
---|
675 | this.reducedValueOverError = new double[this.diagonalLength];
|
---|
676 | this.probabilityValues = new double[this.diagonalLength];
|
---|
677 | this.mcMullen = new double[this.numberOfRows];
|
---|
678 |
|
---|
679 | for(int i=0; i<this.diagonalLength; i++){
|
---|
680 | this.reducedValuesDiagonal[i] = this.reducedValues[i][i];
|
---|
681 | this.reducedErrorsDiagonal[i] = this.reducedErrors[i][i];
|
---|
682 | this.reducedValueOverError[i] = Math.abs(this.reducedValuesDiagonal[i]/this.reducedErrorsDiagonal[i]);
|
---|
683 | this.probabilityValues[i] = 1.0 - Stat.gaussianCDF(0.0, 1.0, -this.reducedValueOverError[i], this.reducedValueOverError[i]);
|
---|
684 | }
|
---|
685 |
|
---|
686 | // calculate criteria of McMullen, Jaskunas and Tinoco
|
---|
687 | for(int i=0; i<this.numberOfRows; i++){
|
---|
688 | double sum = 0.0;
|
---|
689 | for(int j=i; j<this.numberOfColumns; j++){
|
---|
690 | sum += this.reducedValues[i][j]*this.reducedValues[i][j];
|
---|
691 | }
|
---|
692 | this.mcMullen[i] = Math.sqrt(sum)/(this.numberOfColumns-i);
|
---|
693 | }
|
---|
694 |
|
---|
695 | this.rankAnalysisDone = true;
|
---|
696 | }
|
---|
697 |
|
---|
698 | // Return an analysis to a text file
|
---|
699 | public void analysis(){
|
---|
700 | this.analysis("RankAnalysisOutput.txt");
|
---|
701 | }
|
---|
702 |
|
---|
703 | public void analysis(String fileName){
|
---|
704 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
705 |
|
---|
706 | int posdot = fileName.indexOf(".");
|
---|
707 | if(posdot==-1)fileName = fileName + ".txt";
|
---|
708 |
|
---|
709 | FileOutput fout = new FileOutput(fileName);
|
---|
710 | fout.println("Rank Analysis");
|
---|
711 | fout.println("File name: " + fileName);
|
---|
712 | Date d = new Date();
|
---|
713 | String day = DateFormat.getDateInstance().format(d);
|
---|
714 | String tim = DateFormat.getTimeInstance().format(d);
|
---|
715 | fout.println("Program executed at " + tim + " on " + day);
|
---|
716 | fout.println();
|
---|
717 |
|
---|
718 | fout.println("Number of rows " + this.numberOfRows);
|
---|
719 | fout.println("Number of columns " + this.numberOfColumns);
|
---|
720 | if(this.numberOfMissingErrors>0){
|
---|
721 | fout.println("Number of substituted missing errors" + this.numberOfMissingErrors);
|
---|
722 | if(this.rowOption){
|
---|
723 | fout.println("Row means used as the substituted value/s");
|
---|
724 | }
|
---|
725 | else{
|
---|
726 | fout.println("Column means used as the substituted value/s");
|
---|
727 | }
|
---|
728 | }
|
---|
729 | fout.println();
|
---|
730 |
|
---|
731 | switch(this.errorType){
|
---|
732 | case 0: fout.println("Matrix of individual errors supplied");
|
---|
733 | break;
|
---|
734 | case 1: fout.println("Common error for all elements in each each row supplied");
|
---|
735 | break;
|
---|
736 | case 2: fout.println("Single common error for all elements in the matrix supplied");
|
---|
737 | break;
|
---|
738 | case 3: fout.println("No errors supplied - estimate of the rounding errors used");
|
---|
739 | }
|
---|
740 | fout.println();
|
---|
741 |
|
---|
742 | int field1 = 30;
|
---|
743 | int field2 = 15;
|
---|
744 | int trunc = 4;
|
---|
745 | if(this.errorType!=3){
|
---|
746 | fout.print("Reduced", field2);
|
---|
747 | fout.print("Reduced", field2);
|
---|
748 | fout.print("V/E Ratio", field2);
|
---|
749 | fout.print("P-value", field2);
|
---|
750 | fout.println("McMullen");
|
---|
751 |
|
---|
752 | fout.print("Value", field2);
|
---|
753 | fout.print("Error", field2);
|
---|
754 | fout.print(" ", field2);
|
---|
755 | fout.print(" ", field2);
|
---|
756 | fout.println("rms");
|
---|
757 |
|
---|
758 | fout.print("Diagonal (V)", field2);
|
---|
759 | fout.print("Diagonal (E)", field2);
|
---|
760 | fout.print(" ", field2);
|
---|
761 | fout.print(" ", field2);
|
---|
762 | fout.println(" ");
|
---|
763 | }
|
---|
764 | else{
|
---|
765 | fout.print("Reduced", field2);
|
---|
766 | fout.print("Reduced", field2);
|
---|
767 | fout.print("V/E Ratio", field2);
|
---|
768 | fout.print("P-value", field2);
|
---|
769 | fout.println("McMullen");
|
---|
770 |
|
---|
771 | fout.print("Value", field2);
|
---|
772 | fout.print("Estimated", field2);
|
---|
773 | fout.print(" ", field2);
|
---|
774 | fout.print(" ", field2);
|
---|
775 | fout.println("rms");
|
---|
776 |
|
---|
777 | fout.print("Diagonal (V)", field2);
|
---|
778 | fout.print("Rounding", field2);
|
---|
779 | fout.print(" ", field2);
|
---|
780 | fout.print(" ", field2);
|
---|
781 | fout.println(" ");
|
---|
782 |
|
---|
783 | fout.print(" ", field2);
|
---|
784 | fout.print("Error (E)", field2);
|
---|
785 | fout.print(" ", field2);
|
---|
786 | fout.print(" ", field2);
|
---|
787 | fout.println(" ");
|
---|
788 | }
|
---|
789 |
|
---|
790 | for(int i=0; i<this.diagonalLength; i++){
|
---|
791 | fout.print(Fmath.truncate(this.reducedValuesDiagonal[i], trunc), field2);
|
---|
792 | fout.print(Fmath.truncate(this.reducedErrorsDiagonal[i], trunc), field2);
|
---|
793 | fout.print(Fmath.truncate(this.reducedValueOverError[i], trunc), field2);
|
---|
794 | fout.print(Fmath.truncate(this.probabilityValues[i], trunc), field2);
|
---|
795 | fout.println(Fmath.truncate(this.mcMullen[i], trunc));
|
---|
796 | }
|
---|
797 |
|
---|
798 | System.out.println("Analysis written to text file " + fileName);
|
---|
799 |
|
---|
800 | fout.close();
|
---|
801 | }
|
---|
802 |
|
---|
803 |
|
---|
804 | // Return original values matrix
|
---|
805 | public double[][] originalValues(){
|
---|
806 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
807 | return this.values;
|
---|
808 | }
|
---|
809 |
|
---|
810 | // Return original error matrix
|
---|
811 | public double[][] originalErrors(){
|
---|
812 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
813 | return this.errors;
|
---|
814 | }
|
---|
815 |
|
---|
816 | // Return reduced values matrix
|
---|
817 | public double[][] reducedValues(){
|
---|
818 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
819 | return this.reducedValues;
|
---|
820 | }
|
---|
821 |
|
---|
822 | // Return reduced errors matrix
|
---|
823 | public double[][] reducedErrors(){
|
---|
824 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
825 | return this.reducedErrors;
|
---|
826 | }
|
---|
827 |
|
---|
828 | // Return reduced values diagonal
|
---|
829 | public double[] reducedValuesDiagonal(){
|
---|
830 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
831 | return this.reducedValuesDiagonal;
|
---|
832 | }
|
---|
833 |
|
---|
834 | // Return reduced errors diagonal
|
---|
835 | public double[] reducedErrorsDiagonal(){
|
---|
836 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
837 | return this.reducedErrorsDiagonal;
|
---|
838 | }
|
---|
839 |
|
---|
840 | // Return reduced value over reduced errors diagonal
|
---|
841 | public double[] reducedRatiosDiagonal(){
|
---|
842 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
843 | return this.reducedValueOverError;
|
---|
844 | }
|
---|
845 |
|
---|
846 | // Return probabilty values diagonal
|
---|
847 | public double[] probabilityValues(){
|
---|
848 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
849 | return this.probabilityValues;
|
---|
850 | }
|
---|
851 |
|
---|
852 | // Return McMullen values
|
---|
853 | public double[] mcMullenValues(){
|
---|
854 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
855 | return this.mcMullen;
|
---|
856 | }
|
---|
857 |
|
---|
858 | // return number of rows
|
---|
859 | public int nRows(){
|
---|
860 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
861 | return this.numberOfRows;
|
---|
862 | }
|
---|
863 |
|
---|
864 | // return number of columns
|
---|
865 | public int nColumns(){
|
---|
866 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
867 | return this.numberOfColumns;
|
---|
868 | }
|
---|
869 |
|
---|
870 | // return number of diagonal elements
|
---|
871 | public int nDiagonalElements(){
|
---|
872 | if(!this.rankAnalysisDone)this.rankAnalysis();
|
---|
873 | return this.diagonalLength;
|
---|
874 | }
|
---|
875 |
|
---|
876 | } |
---|