Search in sources :

Example 1 with DoubleMatrix2D

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

the class IndTestMixedMultipleTTest method multiLL.

private double multiLL(DoubleMatrix2D coeffs, Node dep, List<Node> indep) {
    DoubleMatrix2D indepData = factory2D.make(internalData.subsetColumns(indep).getDoubleData().toArray());
    List<Node> depList = new ArrayList<>();
    depList.add(dep);
    DoubleMatrix2D depData = factory2D.make(internalData.subsetColumns(depList).getDoubleData().toArray());
    int N = indepData.rows();
    DoubleMatrix2D probs = Algebra.DEFAULT.mult(factory2D.appendColumns(factory2D.make(N, 1, 1.0), indepData), coeffs);
    probs = factory2D.appendColumns(factory2D.make(indepData.rows(), 1, 1.0), probs).assign(Functions.exp);
    double ll = 0;
    for (int i = 0; i < N; i++) {
        DoubleMatrix1D curRow = probs.viewRow(i);
        curRow.assign(Functions.div(curRow.zSum()));
        ll += Math.log(curRow.get((int) depData.get(i, 0)));
    }
    return ll;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) Node(edu.cmu.tetrad.graph.Node) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D)

Example 2 with DoubleMatrix2D

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

the class IndTestRegressionAD method determines.

// ==========================PRIVATE METHODS============================//
// /**
// * Return the p-value of the last calculated independence fact.
// *
// * @return this p-value.  When accessed through the IndependenceChecker
// *         interface, this p-value should only be considered to be a
// *         relative strength.
// */
// private double getRelativeStrength() {
// 
// // precondition:  pdf is the most recently used partial
// // correlation distribution function, and storedR is the most
// // recently calculated partial correlation.
// return 2.0 * Integrator.getArea(npdf, Math.abs(storedR), 9.0, 100);
// }
// /**
// * Computes that value x such that P(abs(N(0,1) > x) < alpha.  Note that
// * this is a two sided test of the null hypothesis that the Fisher's Z
// * value, which is distributed as N(0,1) is not equal to 0.0.
// */
// private double cutoffGaussian() {
// npdf = new NormalPdf();
// final double upperBound = 9.0;
// final double delta = 0.001;
// //        double alpha = this.alpha/2.0;    //Two sided test
// return CutoffFinder.getCutoff(npdf, upperBound, alpha, delta);
// }
// private int sampleSize() {
// return data.rows();
// }
public boolean determines(List<Node> zList, Node xVar) {
    if (zList == null) {
        throw new NullPointerException();
    }
    for (Node node : zList) {
        if (node == null) {
            throw new NullPointerException();
        }
    }
    int size = zList.size();
    int[] zCols = new int[size];
    int xIndex = getVariables().indexOf(xVar);
    for (int i = 0; i < zList.size(); i++) {
        zCols[i] = getVariables().indexOf(zList.get(i));
    }
    int[] zRows = new int[data.rows()];
    for (int i = 0; i < data.rows(); i++) {
        zRows[i] = i;
    }
    DoubleMatrix2D Z = data.viewSelection(zRows, zCols);
    DoubleMatrix1D x = data.viewColumn(xIndex);
    DoubleMatrix2D Zt = new Algebra().transpose(Z);
    DoubleMatrix2D ZtZ = new Algebra().mult(Zt, Z);
    DoubleMatrix2D G = new DenseDoubleMatrix2D(new TetradMatrix(ZtZ.toArray()).inverse().toArray());
    // Bug in Colt? Need to make a copy before multiplying to avoid
    // a ClassCastException.
    DoubleMatrix2D Zt2 = Zt.like();
    Zt2.assign(Zt);
    DoubleMatrix2D GZt = new Algebra().mult(G, Zt2);
    DoubleMatrix1D b_x = new Algebra().mult(GZt, x);
    DoubleMatrix1D xPred = new Algebra().mult(Z, b_x);
    DoubleMatrix1D xRes = xPred.copy().assign(x, Functions.minus);
    double SSE = xRes.aggregate(Functions.plus, Functions.square);
    boolean determined = SSE < 0.0001;
    if (determined) {
        StringBuilder sb = new StringBuilder();
        sb.append("Determination found: ").append(xVar).append(" is determined by {");
        for (int i = 0; i < zList.size(); i++) {
            sb.append(zList.get(i));
            if (i < zList.size() - 1) {
                sb.append(", ");
            }
        }
        sb.append("}");
        TetradLogger.getInstance().log("independencies", sb.toString());
    }
    return determined;
}
Also used : Algebra(cern.colt.matrix.linalg.Algebra) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) Node(edu.cmu.tetrad.graph.Node) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Example 3 with DoubleMatrix2D

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

the class IndTestFisherZGeneralizedInverse method determines.

public boolean determines(List<Node> zList, Node xVar) {
    if (zList == null) {
        throw new NullPointerException();
    }
    if (zList.isEmpty()) {
        return false;
    }
    for (Node node : zList) {
        if (node == null) {
            throw new NullPointerException();
        }
    }
    int size = zList.size();
    int[] zCols = new int[size];
    int xIndex = getVariables().indexOf(xVar);
    for (int i = 0; i < zList.size(); i++) {
        zCols[i] = getVariables().indexOf(zList.get(i));
    }
    int[] zRows = new int[data.rows()];
    for (int i = 0; i < data.rows(); i++) {
        zRows[i] = i;
    }
    DoubleMatrix2D Z = data.viewSelection(zRows, zCols);
    DoubleMatrix1D x = data.viewColumn(xIndex);
    DoubleMatrix2D Zt = new Algebra().transpose(Z);
    DoubleMatrix2D ZtZ = new Algebra().mult(Zt, Z);
    TetradMatrix _ZtZ = new TetradMatrix(ZtZ.toArray());
    TetradMatrix ginverse = _ZtZ.inverse();
    DoubleMatrix2D G = new DenseDoubleMatrix2D(ginverse.toArray());
    // DoubleMatrix2D G = MatrixUtils.ginverse(ZtZ);
    DoubleMatrix2D Zt2 = Zt.copy();
    DoubleMatrix2D GZt = new Algebra().mult(G, Zt2);
    DoubleMatrix1D b_x = new Algebra().mult(GZt, x);
    DoubleMatrix1D xPred = new Algebra().mult(Z, b_x);
    DoubleMatrix1D xRes = xPred.copy().assign(x, Functions.minus);
    double SSE = xRes.aggregate(Functions.plus, Functions.square);
    double variance = SSE / (data.rows() - (zList.size() + 1));
    // ChiSquare chiSquare = new ChiSquare(data.rows(),
    // PersistentRandomUtil.getInstance().getEngine());
    // 
    // double p = chiSquare.cdf(sum);
    // boolean determined = p < 1 - getAlternativePenalty();
    // 
    boolean determined = variance < getAlpha();
    if (determined) {
        StringBuilder sb = new StringBuilder();
        sb.append("Determination found: ").append(xVar).append(" is determined by {");
        for (int i = 0; i < zList.size(); i++) {
            sb.append(zList.get(i));
            if (i < zList.size() - 1) {
                sb.append(", ");
            }
        }
        sb.append("}");
        // sb.append(" p = ").append(nf.format(p));
        sb.append(" SSE = ").append(nf.format(SSE));
        TetradLogger.getInstance().log("independencies", sb.toString());
        System.out.println(sb);
    }
    return determined;
}
Also used : Algebra(cern.colt.matrix.linalg.Algebra) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) Node(edu.cmu.tetrad.graph.Node) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Example 4 with DoubleMatrix2D

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

the class GlassoRunner method execute.

// ===================PUBLIC METHODS OVERRIDING ABSTRACT================//
public void execute() {
    Object dataModel = getDataModel();
    Parameters params = getParams();
    if (dataModel instanceof DataSet) {
        DataSet dataSet = (DataSet) dataModel;
        DoubleMatrix2D cov = new DenseDoubleMatrix2D(dataSet.getCovarianceMatrix().toArray());
        Glasso glasso = new Glasso(cov);
        glasso.setMaxit((int) params.get("maxit", 10000));
        glasso.setIa(params.getBoolean("ia", false));
        glasso.setIs(params.getBoolean("is", false));
        glasso.setItr(params.getBoolean("itr", false));
        glasso.setIpen(params.getBoolean("ipen", false));
        glasso.setThr(params.getDouble("thr", 1e-4));
        glasso.setRhoAllEqual(1.0);
        Glasso.Result result = glasso.search();
        TetradMatrix wwi = new TetradMatrix(result.getWwi().toArray());
        List<Node> variables = dataSet.getVariables();
        Graph resultGraph = new EdgeListGraph(variables);
        for (int i = 0; i < variables.size(); i++) {
            for (int j = i + 1; j < variables.size(); j++) {
                if (wwi.get(i, j) != 0.0 && wwi.get(i, j) != 0.0) {
                    resultGraph.addUndirectedEdge(variables.get(i), variables.get(j));
                }
            }
        }
        setResultGraph(resultGraph);
    }
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Example 5 with DoubleMatrix2D

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

the class Ricf method fitConGraph.

/**
 * Fits a concentration graph. Coding algorithm #2 only.
 */
private FitConGraphResult fitConGraph(Graph graph, ICovarianceMatrix cov, int n, double tol) {
    DoubleFactory2D factory = DoubleFactory2D.dense;
    Algebra algebra = new Algebra();
    List<Node> nodes = graph.getNodes();
    String[] nodeNames = new String[nodes.size()];
    for (int i = 0; i < nodes.size(); i++) {
        Node node = nodes.get(i);
        if (!cov.getVariableNames().contains(node.getName())) {
            throw new IllegalArgumentException("Node in graph not in cov matrix: " + node);
        }
        nodeNames[i] = node.getName();
    }
    DoubleMatrix2D S = new DenseDoubleMatrix2D(cov.getSubmatrix(nodeNames).getMatrix().toArray());
    graph = graph.subgraph(nodes);
    List<List<Node>> cli = cliques(graph);
    int nc = cli.size();
    if (nc == 1) {
        return new FitConGraphResult(S, 0, 0, 1);
    }
    int k = S.rows();
    int it = 0;
    // Only coding alg #2 here.
    DoubleMatrix2D K = algebra.inverse(factory.diagonal(factory.diagonal(S)));
    int[] all = range(0, k - 1);
    while (true) {
        DoubleMatrix2D KOld = K.copy();
        it++;
        for (List<Node> aCli : cli) {
            int[] a = asIndices(aCli, nodes);
            int[] b = complement(all, a);
            DoubleMatrix2D a1 = S.viewSelection(a, a);
            DoubleMatrix2D a2 = algebra.inverse(a1);
            DoubleMatrix2D a3 = K.viewSelection(a, b);
            DoubleMatrix2D a4 = K.viewSelection(b, b);
            DoubleMatrix2D a5 = algebra.inverse(a4);
            DoubleMatrix2D a6 = K.viewSelection(b, a).copy();
            DoubleMatrix2D a7 = algebra.mult(a3, a5);
            DoubleMatrix2D a8 = algebra.mult(a7, a6);
            a2.assign(a8, PlusMult.plusMult(1));
            DoubleMatrix2D a9 = K.viewSelection(a, a);
            a9.assign(a2);
        }
        DoubleMatrix2D a32 = K.copy();
        a32.assign(KOld, PlusMult.plusMult(-1));
        double diff = algebra.norm1(a32);
        if (diff < tol)
            break;
    }
    DoubleMatrix2D V = algebra.inverse(K);
    int numNodes = graph.getNumNodes();
    int df = numNodes * (numNodes - 1) / 2 - graph.getNumEdges();
    double dev = lik(algebra.inverse(V), S, n, k);
    return new FitConGraphResult(V, dev, df, it);
}
Also used : Node(edu.cmu.tetrad.graph.Node) DoubleFactory2D(cern.colt.matrix.DoubleFactory2D) Endpoint(edu.cmu.tetrad.graph.Endpoint) Algebra(cern.colt.matrix.linalg.Algebra) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Aggregations

DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)137 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)39 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)37 Algebra (cern.colt.matrix.linalg.Algebra)16 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)13 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)13 Node (edu.cmu.tetrad.graph.Node)11 Graph (edu.cmu.tetrad.graph.Graph)8 Test (org.junit.Test)6 DoubleMatrixReader (ubic.basecode.io.reader.DoubleMatrixReader)6 StringMatrixReader (ubic.basecode.io.reader.StringMatrixReader)6 DataSet (edu.cmu.tetrad.data.DataSet)5 DoubleArrayList (cern.colt.list.DoubleArrayList)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)4 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)4 AbstractFormatter (cern.colt.matrix.impl.AbstractFormatter)3 RobustEigenDecomposition (dr.math.matrixAlgebra.RobustEigenDecomposition)3 Endpoint (edu.cmu.tetrad.graph.Endpoint)3 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)3 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)3