Search in sources :

Example 6 with DenseDoubleMatrix1D

use of cern.colt.matrix.impl.DenseDoubleMatrix1D in project Gemma by PavlidisLab.

the class LinkAnalysisServiceImpl method diagnoseCorrelationDistribution.

/**
 * Check properties of the distribution
 */
// Better readability
@SuppressWarnings("StatementWithEmptyBody")
private void diagnoseCorrelationDistribution(ExpressionExperiment ee, CoexpCorrelationDistribution corrDist) throws UnsuitableForAnalysisException {
    /*
         * Find the median, etc.
         */
    ByteArrayConverter bac = new ByteArrayConverter();
    double[] binCounts = bac.byteArrayToDoubles(corrDist.getBinCounts());
    int numBins = binCounts.length;
    DoubleMatrix1D histogram = new DenseDoubleMatrix1D(binCounts);
    // QC parameters; quantile, not correlation
    double lowerLimitofMiddle = 0.45;
    double upperLimitofMiddle = 0.55;
    double tailFraction = 0.1;
    // normalize
    histogram.assign(Functions.div(histogram.zSum()));
    double lowerTailDensity = 0.0;
    double upperTailDensity = 0.0;
    double median = 0.0;
    // cumulative
    double s = 0.0;
    double middleDensity = 0.0;
    for (int bin = 0; bin < histogram.size(); bin++) {
        // cumulate
        s += histogram.get(bin);
        /*
             * Perhaps these should be adjusted based on the sample size; for smaller data sets, more of the data is
             * going to be above 0.9 etc. But in practice this can't have a very big effect.
             */
        if (bin == (int) Math.floor(numBins * tailFraction)) {
            lowerTailDensity = s;
        } else if (bin == (int) Math.floor(numBins * (1.0 - tailFraction))) {
            upperTailDensity = 1.0 - s;
        } else if (bin > (int) Math.floor(lowerLimitofMiddle * numBins) && bin < (int) Math.floor(upperLimitofMiddle * numBins)) {
            middleDensity += histogram.get(bin);
        }
        if (s >= 0.2) {
        // firstQuintile = binToCorrelation( i, numBins );
        } else if (s >= 0.5) {
            median = this.binToCorrelation(bin, numBins);
        } else if (s >= 0.8) {
        // lastQuintile = binToCorrelation( i, numBins );
        }
    }
    String message = "";
    boolean bad = false;
    if (median > 0.2 || median < -0.2) {
        bad = true;
        message = "Correlation distribution fails QC: median far from center (" + median + ")";
    } else if (lowerTailDensity + upperTailDensity > middleDensity) {
        bad = true;
        message = "Correlation distribution fails QC: tails too heavy";
    }
    if (bad) {
        throw new UnsuitableForAnalysisException(ee, message);
    }
}
Also used : ByteArrayConverter(ubic.basecode.io.ByteArrayConverter) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D)

Example 7 with DenseDoubleMatrix1D

use of cern.colt.matrix.impl.DenseDoubleMatrix1D in project tdq-studio-se by Talend.

the class Formatter method demo2.

/**
 * Demonstrates how to use this class.
 */
public static void demo2() {
    // parameters
    double[] values = { // 5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MIN_VALUE, Double.MAX_VALUE
    5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0 / 0.0, Double.MIN_VALUE, Double.MAX_VALUE, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY // Double.MIN_VALUE, Double.MAX_VALUE //, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
    };
    // String[] formats =         {"%G", "%1.10G", "%f", "%1.2f", "%0.2e"};
    String[] formats = { "%G", "%1.19G" };
    // now the processing
    int size = formats.length;
    DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
    String[] strings = new String[size];
    for (int i = 0; i < size; i++) {
        String format = formats[i];
        strings[i] = new Formatter(format).toString(matrix);
        for (int j = 0; j < matrix.size(); j++) {
            System.out.println(String.valueOf(matrix.get(j)));
        }
    }
    System.out.println("original:\n" + new Formatter().toString(matrix));
    for (int i = 0; i < size; i++) {
        System.out.println("\nstring(" + formats[i] + "):\n" + strings[i]);
    }
}
Also used : AbstractFormatter(cern.colt.matrix.impl.AbstractFormatter) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D)

Example 8 with DenseDoubleMatrix1D

use of cern.colt.matrix.impl.DenseDoubleMatrix1D in project tdq-studio-se by Talend.

the class Sorting method zdemo3.

/**
 * Demonstrates advanced sorting.
 * Sorts by sinus of cell values.
 */
public static void zdemo3() {
    Sorting sort = quickSort;
    double[] values = { 0.5, 1.5, 2.5, 3.5 };
    DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
    cern.colt.function.DoubleComparator comp = new cern.colt.function.DoubleComparator() {

        public int compare(double a, double b) {
            double as = Math.sin(a);
            double bs = Math.sin(b);
            return as < bs ? -1 : as == bs ? 0 : 1;
        }
    };
    System.out.println("unsorted:" + matrix);
    DoubleMatrix1D sorted = sort.sort(matrix, comp);
    System.out.println("sorted  :" + sorted);
    // check whether it is really sorted
    sorted.assign(cern.jet.math.Functions.sin);
    /*
	sorted.assign(
		new cern.colt.function.DoubleFunction() {
			public double apply(double arg) { return Math.sin(arg); }
		}
	);
	*/
    System.out.println("sined  :" + sorted);
}
Also used : DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D)

Example 9 with DenseDoubleMatrix1D

use of cern.colt.matrix.impl.DenseDoubleMatrix1D in project tdq-studio-se by Talend.

the class Sorting method zdemo4.

/**
 * Demonstrates applying functions.
 */
protected static void zdemo4() {
    double[] values1 = { 0, 1, 2, 3 };
    double[] values2 = { 0, 2, 4, 6 };
    DoubleMatrix1D matrix1 = new DenseDoubleMatrix1D(values1);
    DoubleMatrix1D matrix2 = new DenseDoubleMatrix1D(values2);
    System.out.println("m1:" + matrix1);
    System.out.println("m2:" + matrix2);
    matrix1.assign(matrix2, cern.jet.math.Functions.pow);
    /*
	matrix1.assign(matrix2,
		new cern.colt.function.DoubleDoubleFunction() {
			public double apply(double x, double y) { return Math.pow(x,y); }
		}
	);
	*/
    System.out.println("applied:" + matrix1);
}
Also used : DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D)

Example 10 with DenseDoubleMatrix1D

use of cern.colt.matrix.impl.DenseDoubleMatrix1D in project tetrad by cmu-phil.

the class SemUpdater method getUpdatedSemIm.

/**
 * See http://en.wikipedia.org/wiki/Multivariate_normal_distribution.
 */
public SemIm getUpdatedSemIm() {
    Algebra algebra = new Algebra();
    // First manipulate the old semIm.
    SemIm manipulatedSemIm = getManipulatedSemIm();
    // Get out the means and implied covariances.
    double[] means = new double[manipulatedSemIm.getVariableNodes().size()];
    for (int i = 0; i < means.length; i++) {
        means[i] = manipulatedSemIm.getMean(manipulatedSemIm.getVariableNodes().get(i));
    }
    DoubleMatrix1D mu = new DenseDoubleMatrix1D(means);
    DoubleMatrix2D sigma = new DenseDoubleMatrix2D(manipulatedSemIm.getImplCovar(true).toArray());
    // Updating on x2 = a.
    SemEvidence evidence = getEvidence();
    List nodesInEvidence = evidence.getNodesInEvidence();
    int[] x2 = new int[nodesInEvidence.size()];
    DoubleMatrix1D a = new DenseDoubleMatrix1D(nodesInEvidence.size());
    for (int i = 0; i < nodesInEvidence.size(); i++) {
        Node _node = (Node) nodesInEvidence.get(i);
        x2[i] = evidence.getNodeIndex(_node);
    }
    for (int i = 0; i < nodesInEvidence.size(); i++) {
        int j = evidence.getNodeIndex((Node) nodesInEvidence.get(i));
        a.set(i, evidence.getProposition().getValue(j));
    }
    // x1 is all the variables.
    // int[] x1 = new int[sigma.rows() - x2.length];
    int[] x1 = new int[sigma.rows()];
    for (int i = 0; i < sigma.rows(); i++) {
        x1[i] = i;
    }
    // int index = -1;
    // for (int i = 0; i < sigma.rows(); i++) {
    // if (Arrays.binarySearch(x2, i) == -1) {
    // x1[++index] = i;
    // }
    // }
    // Calculate sigmaBar. (Don't know how to use it yet.)
    // DoubleMatrix2D sigma11 = sigma.viewSelection(x1, x1);
    DoubleMatrix2D sigma12 = sigma.viewSelection(x1, x2);
    DoubleMatrix2D sigma22 = sigma.viewSelection(x2, x2);
    // DoubleMatrix2D sigma21 = sigma.viewSelection(x2, x1);
    DoubleMatrix2D inv_sigma22 = algebra.inverse(sigma22);
    DoubleMatrix2D temp1 = algebra.mult(sigma12, inv_sigma22);
    // DoubleMatrix2D temp2 = algebra.times(temp1, sigma21.copy());
    // DoubleMatrix2D sigmaBar = sigma11.copy().assign(temp2, Functions.minus);
    // Calculate muBar.
    DoubleMatrix1D mu1 = mu.viewSelection(x1);
    DoubleMatrix1D mu2 = mu.viewSelection(x2);
    DoubleMatrix1D temp4 = a.copy().assign(mu2, Functions.minus);
    DoubleMatrix1D temp5 = algebra.mult(temp1, temp4);
    DoubleMatrix1D muBar = mu1.copy().assign(temp5, Functions.plus);
    // Estimate a SEM with this sigmaBar and muBar.
    // List variableNodes = manipulatedSemIm.getVariableNodes();
    // String[] varNames = new String[variableNodes.size()];
    // 
    // for (int i = 0; i < variableNodes.size(); i++) {
    // varNames[i] = ((Node) variableNodes.get(i)).getNode();
    // }
    // System.out.println(sigmaBar);
    // 
    // CovarianceMatrix covMatrix = new CovarianceMatrix(varNames,
    // sigmaBar, 100);
    // SemPm semPm = manipulatedSemIm.getEstIm();
    // SemEstimator estimator = new SemEstimator(covMatrix, semPm);
    // estimator.estimate();
    // SemIm semIm = estimator.getEstimatedSem();
    // semIm.setMeanValues(muBar.toArray());
    // return semIm;
    DoubleMatrix2D sigma2 = new DenseDoubleMatrix2D(manipulatedSemIm.getErrCovar().toArray());
    // }
    return manipulatedSemIm.updatedIm(new TetradMatrix(sigma2.toArray()), new TetradVector(muBar.toArray()));
}
Also used : Node(edu.cmu.tetrad.graph.Node) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) TetradVector(edu.cmu.tetrad.util.TetradVector) Algebra(cern.colt.matrix.linalg.Algebra) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) List(java.util.List) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D)

Aggregations

DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)12 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)12 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)6 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)6 Algebra (cern.colt.matrix.linalg.Algebra)5 Node (edu.cmu.tetrad.graph.Node)3 DoubleArrayList (cern.colt.list.DoubleArrayList)2 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)2 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)2 Endpoint (edu.cmu.tetrad.graph.Endpoint)2 Graph (edu.cmu.tetrad.graph.Graph)2 SemGraph (edu.cmu.tetrad.graph.SemGraph)2 AbstractFormatter (cern.colt.matrix.impl.AbstractFormatter)1 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)1 TetradVector (edu.cmu.tetrad.util.TetradVector)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)1 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)1