Search in sources :

Example 11 with DoubleMatrix2D

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

the class MGM method runTests1.

private static void runTests1() {
    try {
        // DoubleMatrix2D xIn = DoubleFactory2D.dense.make(loadDataSelect("/Users/ajsedgewick/tetrad/test_data", "med_test_C.txt"));
        // DoubleMatrix2D yIn = DoubleFactory2D.dense.make(loadDataSelect("/Users/ajsedgewick/tetrad/test_data", "med_test_D.txt"));
        // String path = MGM.class.getResource("test_data").getPath();
        String path = "/Users/ajsedgewick/tetrad_master/tetrad/tetrad-lib/src/main/java/edu/pitt/csb/mgm/test_data";
        System.out.println(path);
        DoubleMatrix2D xIn = DoubleFactory2D.dense.make(MixedUtils.loadDelim(path, "med_test_C.txt").getDoubleData().toArray());
        DoubleMatrix2D yIn = DoubleFactory2D.dense.make(MixedUtils.loadDelim(path, "med_test_D.txt").getDoubleData().toArray());
        int[] L = new int[24];
        Node[] vars = new Node[48];
        for (int i = 0; i < 24; i++) {
            L[i] = 2;
            vars[i] = new ContinuousVariable("X" + i);
            vars[i + 24] = new DiscreteVariable("Y" + i);
        }
        double lam = .2;
        MGM model = new MGM(xIn, yIn, new ArrayList<>(Arrays.asList(vars)), L, new double[] { lam, lam, lam });
        MGM model2 = new MGM(xIn, yIn, new ArrayList<>(Arrays.asList(vars)), L, new double[] { lam, lam, lam });
        System.out.println("Weights: " + Arrays.toString(model.weights.toArray()));
        DoubleMatrix2D test = xIn.copy();
        DoubleMatrix2D test2 = xIn.copy();
        long t = System.currentTimeMillis();
        for (int i = 0; i < 50000; i++) {
            test2 = xIn.copy();
            test.assign(test2);
        }
        System.out.println("assign Time: " + (System.currentTimeMillis() - t));
        t = System.currentTimeMillis();
        double[][] xArr = xIn.toArray();
        for (int i = 0; i < 50000; i++) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            // test = DoubleFactory2D.dense.make(xArr);
            test2 = xIn.copy();
            test = test2;
        }
        System.out.println("equals Time: " + (System.currentTimeMillis() - t));
        System.out.println("Init nll: " + model.smoothValue(model.params.toMatrix1D()));
        System.out.println("Init reg term: " + model.nonSmoothValue(model.params.toMatrix1D()));
        t = System.currentTimeMillis();
        model.learnEdges(700);
        // model.learn(1e-7, 700);
        System.out.println("Orig Time: " + (System.currentTimeMillis() - t));
        System.out.println("nll: " + model.smoothValue(model.params.toMatrix1D()));
        System.out.println("reg term: " + model.nonSmoothValue(model.params.toMatrix1D()));
        System.out.println("params:\n" + model.params);
        System.out.println("adjMat:\n" + model.adjMatFromMGM());
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 12 with DoubleMatrix2D

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

the class MGM method smooth.

/**
 * non-penalized -log(pseudolikelihood) this is the smooth function g(x) in prox gradient
 * this overloaded version calculates both nll and the smooth gradient at the same time
 * any value in gradOut will be replaced by the new calculations
 *
 * @param parIn
 * @param gradOutVec
 * @return
 */
public double smooth(DoubleMatrix1D parIn, DoubleMatrix1D gradOutVec) {
    // work with copy
    MGMParams par = new MGMParams(parIn, p, lsum);
    MGMParams gradOut = new MGMParams();
    for (int i = 0; i < par.betad.size(); i++) {
        if (par.betad.get(i) < 0)
            return Double.POSITIVE_INFINITY;
    }
    // beta=beta-diag(diag(beta));
    // for r=1:q
    // phi(Lsum(r)+1:Lsum(r+1),Lsum(r)+1:Lsum(r+1))=0;
    // end
    // beta=triu(beta); phi=triu(phi);
    // beta=beta+beta';
    // phi=phi+phi';
    upperTri(par.beta, 1);
    par.beta.assign(alg.transpose(par.beta), Functions.plus);
    for (int i = 0; i < q; i++) {
        par.phi.viewPart(lcumsum[i], lcumsum[i], l[i], l[i]).assign(0);
    }
    // ensure matrix is upper triangular
    upperTri(par.phi, 0);
    par.phi.assign(alg.transpose(par.phi), Functions.plus);
    // Xbeta=X*beta*diag(1./betad);
    DoubleMatrix2D divBetaD = factory2D.diagonal(factory1D.make(p, 1.0).assign(par.betad, Functions.div));
    DoubleMatrix2D xBeta = alg.mult(xDat, alg.mult(par.beta, divBetaD));
    // Dtheta=D*theta*diag(1./betad);
    DoubleMatrix2D dTheta = alg.mult(alg.mult(dDat, par.theta), divBetaD);
    // Squared loss
    // tempLoss =  (X-e*alpha1'-Xbeta-Dtheta) = -res (in gradient code)
    DoubleMatrix2D tempLoss = factory2D.make(n, xDat.columns());
    // wxprod=X*(theta')+D*phi+e*alpha2';
    DoubleMatrix2D wxProd = alg.mult(xDat, alg.transpose(par.theta));
    wxProd.assign(alg.mult(dDat, par.phi), Functions.plus);
    for (int i = 0; i < n; i++) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        for (int j = 0; j < xDat.columns(); j++) {
            tempLoss.set(i, j, xDat.get(i, j) - par.alpha1.get(j) - xBeta.get(i, j) - dTheta.get(i, j));
        }
        for (int j = 0; j < dDat.columns(); j++) {
            wxProd.set(i, j, wxProd.get(i, j) + par.alpha2.get(j));
        }
    }
    // sqloss=-n/2*sum(log(betad))+...
    // .5*norm((X-e*alpha1'-Xbeta-Dtheta)*diag(sqrt(betad)),'fro')^2;
    double sqloss = -n / 2.0 * par.betad.copy().assign(Functions.log).zSum() + .5 * Math.pow(alg.normF(alg.mult(tempLoss, factory2D.diagonal(par.betad.copy().assign(Functions.sqrt)))), 2);
    // ok now tempLoss = res
    tempLoss.assign(Functions.mult(-1));
    // gradbeta=X'*(res);
    gradOut.beta = alg.mult(alg.transpose(xDat), tempLoss);
    // gradbeta=gradbeta-diag(diag(gradbeta)); % zero out diag
    // gradbeta=tril(gradbeta)'+triu(gradbeta);
    DoubleMatrix2D lowerBeta = alg.transpose(lowerTri(gradOut.beta.copy(), -1));
    upperTri(gradOut.beta, 1).assign(lowerBeta, Functions.plus);
    // gradalpha1=diag(betad)*sum(res,1)';
    gradOut.alpha1 = alg.mult(factory2D.diagonal(par.betad), margSum(tempLoss, 1));
    // gradtheta=D'*(res);
    gradOut.theta = alg.mult(alg.transpose(dDat), tempLoss);
    // categorical loss
    /*catloss=0;
        wxprod=X*(theta')+D*phi+e*alpha2'; %this is n by Ltot
        for r=1:q
            wxtemp=wxprod(:,Lsum(r)+1:Lsum(r)+L(r));
            denom= logsumexp(wxtemp,2); %this is n by 1
            catloss=catloss-sum(wxtemp(sub2ind([n L(r)],(1:n)',Y(:,r))));
            catloss=catloss+sum(denom);
        end
        */
    double catloss = 0;
    for (int i = 0; i < yDat.columns(); i++) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        DoubleMatrix2D wxTemp = wxProd.viewPart(0, lcumsum[i], n, l[i]);
        // need to copy init values for calculating nll
        DoubleMatrix2D wxTemp0 = wxTemp.copy();
        // does this need to be done in log space??
        wxTemp.assign(Functions.exp);
        DoubleMatrix1D invDenom = factory1D.make(n, 1.0).assign(margSum(wxTemp, 2), Functions.div);
        wxTemp.assign(alg.mult(factory2D.diagonal(invDenom), wxTemp));
        for (int k = 0; k < n; k++) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            DoubleMatrix1D curRow = wxTemp.viewRow(k);
            DoubleMatrix1D curRow0 = wxTemp0.viewRow(k);
            catloss -= curRow0.get((int) yDat.get(k, i) - 1);
            catloss += logsumexp(curRow0);
            // wxtemp(sub2ind(size(wxtemp),(1:n)',Y(:,r)))=wxtemp(sub2ind(size(wxtemp),(1:n)',Y(:,r)))-1;
            curRow.set((int) yDat.get(k, i) - 1, curRow.get((int) yDat.get(k, i) - 1) - 1);
        }
    }
    // gradalpha2=sum(wxprod,1)';
    gradOut.alpha2 = margSum(wxProd, 1);
    // gradw=X'*wxprod;
    DoubleMatrix2D gradW = alg.mult(alg.transpose(xDat), wxProd);
    // gradtheta=gradtheta+gradw';
    gradOut.theta.assign(alg.transpose(gradW), Functions.plus);
    // gradphi=D'*wxprod;
    gradOut.phi = alg.mult(alg.transpose(dDat), wxProd);
    // end
    for (int i = 0; i < q; i++) {
        gradOut.phi.viewPart(lcumsum[i], lcumsum[i], l[i], l[i]).assign(0);
    }
    // gradphi=tril(gradphi)'+triu(gradphi);
    DoubleMatrix2D lowerPhi = alg.transpose(lowerTri(gradOut.phi.copy(), 0));
    upperTri(gradOut.phi, 0).assign(lowerPhi, Functions.plus);
    /*
        for s=1:p
            gradbetad(s)=-n/(2*betad(s))+1/2*norm(res(:,s))^2-res(:,s)'*(Xbeta(:,s)+Dtheta(:,s));
        end
         */
    gradOut.betad = factory1D.make(xDat.columns());
    for (int i = 0; i < p; i++) {
        gradOut.betad.set(i, -n / (2.0 * par.betad.get(i)) + alg.norm2(tempLoss.viewColumn(i)) / 2.0 - alg.mult(tempLoss.viewColumn(i), xBeta.viewColumn(i).copy().assign(dTheta.viewColumn(i), Functions.plus)));
    }
    gradOut.alpha1.assign(Functions.div((double) n));
    gradOut.alpha2.assign(Functions.div((double) n));
    gradOut.betad.assign(Functions.div((double) n));
    gradOut.beta.assign(Functions.div((double) n));
    gradOut.theta.assign(Functions.div((double) n));
    gradOut.phi.assign(Functions.div((double) n));
    gradOutVec.assign(gradOut.toMatrix1D());
    return (sqloss + catloss) / ((double) n);
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D)

Example 13 with DoubleMatrix2D

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

the class TetradAlgebra method solve.

public static TetradMatrix solve(TetradMatrix a, TetradMatrix b) {
    DoubleMatrix2D _a = new DenseDoubleMatrix2D(a.toArray());
    DoubleMatrix2D _b = new DenseDoubleMatrix2D(b.toArray());
    return new TetradMatrix(new Algebra().solve(_a, _b).toArray());
}
Also used : Algebra(cern.colt.matrix.linalg.Algebra) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Example 14 with DoubleMatrix2D

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

the class TestMatrix2D method doubleTest19.

/**
 */
public static void doubleTest19() {
    System.out.println("\n\n");
    DoubleMatrix2D A;
    int k;
    int uk;
    int lk;
    double[][] values5 = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
    A = Factory2D.make(values5);
    k = cern.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
    uk = cern.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
    lk = cern.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
    System.out.println("\n\nupperBandwidth=" + uk);
    System.out.println("lowerBandwidth=" + lk);
    System.out.println("bandwidth=" + k + " " + A);
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    double[][] values4 = { { 1, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 1 } };
    A = Factory2D.make(values4);
    k = cern.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
    uk = cern.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
    lk = cern.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
    System.out.println("\n\nupperBandwidth=" + uk);
    System.out.println("lowerBandwidth=" + lk);
    System.out.println("bandwidth=" + k + " " + A);
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    double[][] values1 = { { 1, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 1, 1 }, { 0, 0, 1, 1 } };
    A = Factory2D.make(values1);
    k = cern.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
    uk = cern.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
    lk = cern.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
    System.out.println("\n\nupperBandwidth=" + uk);
    System.out.println("lowerBandwidth=" + lk);
    System.out.println("bandwidth=" + k + " " + A);
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    double[][] values6 = { { 0, 1, 1, 1 }, { 0, 1, 1, 1 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 } };
    A = Factory2D.make(values6);
    k = cern.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
    uk = cern.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
    lk = cern.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
    System.out.println("\n\nupperBandwidth=" + uk);
    System.out.println("lowerBandwidth=" + lk);
    System.out.println("bandwidth=" + k + " " + A);
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    double[][] values7 = { { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 1, 1, 0, 0 }, { 1, 1, 1, 1 } };
    A = Factory2D.make(values7);
    k = cern.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
    uk = cern.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
    lk = cern.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
    System.out.println("\n\nupperBandwidth=" + uk);
    System.out.println("lowerBandwidth=" + lk);
    System.out.println("bandwidth=" + k + " " + A);
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    double[][] values2 = { { 1, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 1 }, { 1, 0, 1, 1 } };
    A = Factory2D.make(values2);
    k = cern.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
    uk = cern.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
    lk = cern.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
    System.out.println("\n\nupperBandwidth=" + uk);
    System.out.println("lowerBandwidth=" + lk);
    System.out.println("bandwidth=" + k + " " + A);
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    double[][] values3 = { { 1, 1, 1, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 1 }, { 0, 0, 1, 1 } };
    A = Factory2D.make(values3);
    k = cern.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
    uk = cern.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
    lk = cern.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
    System.out.println("\n\nupperBandwidth=" + uk);
    System.out.println("lowerBandwidth=" + lk);
    System.out.println("bandwidth=" + k + " " + A);
// System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 15 with DoubleMatrix2D

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

the class TestMatrix2D method doubleTest33.

/**
 */
public static void doubleTest33() {
    double nan = Double.NaN;
    double inf = Double.POSITIVE_INFINITY;
    double ninf = Double.NEGATIVE_INFINITY;
    double[][] data = { { ninf, nan } };
    /*
	{ 
		{ 1, 4, 0 },
		{ 6, 2, 5 },
		{ 0, 7, 3 },
		{ 0, 0, 8 },
		{ 0, 0, 0 },
		{ 0, 0, 0 }
	};
	*/
    DoubleMatrix2D x = new DenseDoubleMatrix2D(data);
    System.out.println("\n\n\n" + x);
    System.out.println("\n" + x.equals(ninf));
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

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