/*
*   Class   RankAnalysis
*
*   USAGE:  Matrix Rank Analysis
*
*   WRITTEN BY: Dr Michael Thomas Flanagan
*
*   DATE:    August -  September 2008
*   UPDATE:  12 October 2008
*
*   DOCUMENTATION:
*   See Michael Thomas Flanagan's Java library web page:
*   http://www.ee.ucl.ac.uk/~mflanaga/java/RankAnalysis.html
*   http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*   Copyright (c) 2008 Michael Thomas Flanagan
*
*   PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* 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
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/

package flanagan.analysis;

import flanagan.analysis.Stat;
import flanagan.analysis.Cronbach;
import flanagan.math.Fmath;
import flanagan.math.Conv;
import flanagan.math.Matrix;
import flanagan.math.ArrayMaths;
import flanagan.io.FileOutput;

import java.util.ArrayList;
import java.util.Vector;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.text.*;

public class RankAnalysis{

    private double[][] values = null;                   // matrix of values whose rank is required
    private double[][] errors = null;                   // matrix of errors of the values

    private double[] valuesDiagonal = null;             // diagonal of values whose rank is required
    private double[] errorsDiagonal = null;             // diagonal of errors of the values

    private double[][] reducedValues = null;            // reduced matrix of values whose rank is required
    private double[][] reducedErrors = null;            // reduced matrix of standard deviations of the values

    private double[] reducedValuesDiagonal = null;      // diagonal of reduced values
    private double[] reducedErrorsDiagonal = null;      // diagonal of reduved errors
    private double[] reducedValueOverError = null;      // ratio of reduced value diagonal over reduced error diagonal
    private double[] probabilityValues = null;          // P-values for above ratios
    private double[] mcMullen = null;                   // Criteria of McMullen, Jaskunas and Tinoco

    private int numberOfRows = 0;                       // number of rows
    private int numberOfColumns = 0;                    // number of columns
    private int diagonalLength = 0;                     // length of diagonal

    private int errorType = 3;                          // = 0  matrix of individual errors supplied
                                                        // = 1  common error for all elements in each each row supplied
                                                        // = 2  single common error for all elements in the matrix supplied
                                                        // = 3  no error/s supplied

    private double[] errorRowMeans = null;              // means of the rows of errors
    private double[] errorColumnMeans = null;           // means of the columns of errors

    private int numberOfMissingErrors = 0;              // number of missing errors (entered as NaN)
    private boolean rowOption = true;                   // = true - missing errors replaced by the appropriate row mean
                                                        // = false - missing errors replaced by the appropriate column mean

    private boolean rankAnalysisDone = false;           // = true when rank analysis performed


    // CONSTRUCTORS
    // Individual error for each value
    public RankAnalysis(double[][] values, double[][] errors){
        this.values = Conv.copy(values);
        this.errors = Conv.copy(errors);
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(float[][] values, float[][] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        Matrix mate = new Matrix(errors);
        this.errors = mate.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(long[][] values, long[][] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        Matrix mate = new Matrix(errors);
        this.errors = mate.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(int[][] values, int[][] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        Matrix mate = new Matrix(errors);
        this.errors = mate.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(BigDecimal[][] values, BigDecimal[][] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        Matrix mate = new Matrix(errors);
        this.errors = mate.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(BigInteger[][] values, BigInteger[][] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        Matrix mate = new Matrix(errors);
        this.errors = mate.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(ArrayMaths[] values, ArrayMaths[] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        Matrix mate = new Matrix(errors);
        this.errors = mate.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(ArrayList[] values, ArrayList[] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        Matrix mate = new Matrix(errors);
        this.errors = mate.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(Vector[] values, Vector[] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        Matrix mate = new Matrix(errors);
        this.errors = mate.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }

    public RankAnalysis(Matrix values, Matrix errors){
        this.values = values.getArrayCopy();
        this.errors = errors.getArrayCopy();
        this.errorType = 0;
        this.preprocessDataOne();
    }



    // Common error for each row
    public RankAnalysis(double[][] values, double[] errors){
        this.values = Conv.copy(values);
        this.errors = this.oneToTwo(Conv.copy(errors), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(float[][] values, float[] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }


    public RankAnalysis(long[][] values, long[] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(int[][] values, int[] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(BigDecimal[][] values, BigDecimal[] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(BigInteger[][] values, BigInteger[] errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(ArrayMaths[] values, ArrayMaths errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errors = this.oneToTwo(errors.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(ArrayList[] values, ArrayList errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(Vector[] values, Vector errors){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(Scores values){
        this.values = values.usedScoresAsRowPerItem();
        Matrix mat = new Matrix(this.values);
        double[] errors = mat.rowStandardDeviations();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(Cronbach values){
        this.values = values.usedScoresAsRowPerItem();
        Matrix mat = new Matrix(this.values);
        double[] errors = mat.rowStandardDeviations();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    public RankAnalysis(PCA values){
        this.values = values.usedScoresAsRowPerItem();
        Matrix mat = new Matrix(this.values);
        double[] errors = mat.rowStandardDeviations();
        ArrayMaths ame = new ArrayMaths(errors);
        this.errors = this.oneToTwo(ame.array(), this.values[0].length);
        this.errorType = 1;
        this.preprocessDataOne();
    }

    // Common error for all values
    public RankAnalysis(double[][] values, double commonError){
        this.values = Conv.copy(values);
        this.errorType = 2;
        this.preprocessDataTwo(commonError);
    }

    public RankAnalysis(float[][] values, float commonError){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo((double)commonError);
    }

    public RankAnalysis(long[][] values, long commonError){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo((double)commonError);
    }

    public RankAnalysis(int[][] values, int commonError){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo((double)commonError);
    }

    public RankAnalysis(BigDecimal[][] values, BigDecimal commonError){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo(commonError.doubleValue());
    }

    public RankAnalysis(BigInteger[][] values, BigInteger commonError){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo(commonError.doubleValue());
    }

    public RankAnalysis(ArrayMaths[] values, double commonError){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo(commonError);
    }

    public RankAnalysis(ArrayList[] values, double commonError){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo(commonError);
    }

    public RankAnalysis(Vector[] values, double commonError){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo(commonError);
    }

    public RankAnalysis(Matrix values, double commonError){
        this.values = values.getArrayCopy();
        this.errorType = 2;
        this.preprocessDataTwo(commonError);
    }



    // No errors supplied
    public RankAnalysis(double[][] values){
        this.values = Conv.copy(values);
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(float[][] values){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(long[][] values){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(int[][] values){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(BigDecimal[][] values){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(BigInteger[][] values){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(ArrayMaths[] values){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(ArrayList[] values){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(Vector[] values){
        Matrix matv = new Matrix(values);
        this.values = matv.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }

    public RankAnalysis(Matrix values){
        this.values = values.getArrayCopy();
        this.errorType = 3;
        this.preprocessDataThree();
    }




    // Convets common error per row to individual errors for all rows
    private double[][] oneToTwo(double[] errors, int nCols){
        int nRows = errors.length;
        double[][] ret = new double[nRows][nCols];
        for(int i=0; ithis.numberOfColumns)this.diagonalLength = this.numberOfColumns;

        // Convert errors to variances
        for(int i=0; ithis.numberOfColumns)this.diagonalLength = this.numberOfColumns;

        // Fill errors matrix
        this.errors = new double[this.numberOfRows][this.numberOfColumns];
        for(int i=0; ithis.numberOfColumns)this.diagonalLength = this.numberOfColumns;

        // Fill errors matrix
        this.errors = new double[this.numberOfRows][this.numberOfColumns];
        double error = 0.0;
        for(int i=0; i0){
            for(int i=0; i0){
            fout.println("Number of substituted missing errors" + this.numberOfMissingErrors);
            if(this.rowOption){
                fout.println("Row means used as the substituted value/s");
            }
            else{
                fout.println("Column means used as the substituted value/s");
            }
        }
        fout.println();

        switch(this.errorType){
            case 0: fout.println("Matrix of individual errors supplied");
                    break;
            case 1: fout.println("Common error for all elements in each each row supplied");
                    break;
            case 2: fout.println("Single common error for all elements in the matrix supplied");
                    break;
            case 3: fout.println("No errors supplied - estimate of the rounding errors used");
        }
        fout.println();

        int field1 = 30;
        int field2 = 15;
        int trunc = 4;
        if(this.errorType!=3){
            fout.print("Reduced", field2);
            fout.print("Reduced", field2);
            fout.print("V/E Ratio", field2);
            fout.print("P-value", field2);
            fout.println("McMullen");

            fout.print("Value", field2);
            fout.print("Error", field2);
            fout.print("    ", field2);
            fout.print("    ", field2);
            fout.println("rms");

            fout.print("Diagonal (V)", field2);
            fout.print("Diagonal (E)", field2);
            fout.print("   ", field2);
            fout.print("   ", field2);
            fout.println("   ");
        }
        else{
            fout.print("Reduced", field2);
            fout.print("Reduced", field2);
            fout.print("V/E Ratio", field2);
            fout.print("P-value", field2);
            fout.println("McMullen");

            fout.print("Value", field2);
            fout.print("Estimated", field2);
            fout.print("    ", field2);
            fout.print("    ", field2);
            fout.println("rms");

            fout.print("Diagonal (V)", field2);
            fout.print("Rounding", field2);
            fout.print("   ", field2);
            fout.print("   ", field2);
            fout.println("   ");

            fout.print("   ", field2);
            fout.print("Error (E)", field2);
            fout.print("   ", field2);
            fout.print("   ", field2);
            fout.println("   ");
        }

        for(int i=0; i 
 

Ads help maintain this website.