Java Code Examples for org.apache.commons.math3.linear.Array2DRowRealMatrix

The following are top voted examples for showing how to use org.apache.commons.math3.linear.Array2DRowRealMatrix. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: Machine-Learning-End-to-Endguide-for-Java-developers   File: MathExamples.java   View source code 6 votes vote down vote up
public void apacheCommonsExample() {
    double[][] A = {
        {0.1950, 0.0311},
        {0.3588, 0.2203},
        {0.1716, 0.5931},
        {0.2105, 0.3242}};

    double[][] B = {
        {0.0502, 0.9823, 0.9472},
        {0.5732, 0.2694, 0.916}};

    RealMatrix aRealMatrix = new Array2DRowRealMatrix(A);
    RealMatrix bRealMatrix = new Array2DRowRealMatrix(B);

    RealMatrix cRealMatrix = aRealMatrix.multiply(bRealMatrix);
    System.out.println();
    for (int i = 0; i < cRealMatrix.getRowDimension(); i++) {
        System.out.println(cRealMatrix.getRowVector(i));
    }
}
 
Example 2
Project: FSensor   File: RotationMeasurementModel.java   View source code 6 votes vote down vote up
public RotationMeasurementModel()
{
	super();

	// H = measurementMatrix
	measurementMatrix = new Array2DRowRealMatrix(new double[][]
	{
	{ 1, 0, 0, 0 },
	{ 0, 1, 0, 0 },
	{ 0, 0, 1, 0 },
	{ 0, 0, 0, 1 } });

	// R = measurementNoise
	measurementNoise = new Array2DRowRealMatrix(new double[][]
	{
	{ noiseCoefficient, 0, 0, 0 },
	{ 0, noiseCoefficient, 0, 0 },
	{ 0, 0, noiseCoefficient, 0 },
	{ 0, 0, 0, noiseCoefficient } });
}
 
Example 3
Project: Java-Data-Science-Made-Easy   File: MathExamples.java   View source code 6 votes vote down vote up
public void apacheCommonsExample() {
    double[][] A = {
        {0.1950, 0.0311},
        {0.3588, 0.2203},
        {0.1716, 0.5931},
        {0.2105, 0.3242}};

    double[][] B = {
        {0.0502, 0.9823, 0.9472},
        {0.5732, 0.2694, 0.916}};

    RealMatrix aRealMatrix = new Array2DRowRealMatrix(A);
    RealMatrix bRealMatrix = new Array2DRowRealMatrix(B);

    RealMatrix cRealMatrix = aRealMatrix.multiply(bRealMatrix);
    System.out.println();
    for (int i = 0; i < cRealMatrix.getRowDimension(); i++) {
        System.out.println(cRealMatrix.getRowVector(i));
    }
}
 
Example 4
Project: Java-for-Data-Science   File: MathExamples.java   View source code 6 votes vote down vote up
public void apacheCommonsExample() {
    double[][] A = {
        {0.1950, 0.0311},
        {0.3588, 0.2203},
        {0.1716, 0.5931},
        {0.2105, 0.3242}};

    double[][] B = {
        {0.0502, 0.9823, 0.9472},
        {0.5732, 0.2694, 0.916}};

    RealMatrix aRealMatrix = new Array2DRowRealMatrix(A);
    RealMatrix bRealMatrix = new Array2DRowRealMatrix(B);

    RealMatrix cRealMatrix = aRealMatrix.multiply(bRealMatrix);
    System.out.println();
    for (int i = 0; i < cRealMatrix.getRowDimension(); i++) {
        System.out.println(cRealMatrix.getRowVector(i));
    }
}
 
Example 5
Project: BNPMix.java   File: Generator.java   View source code 6 votes vote down vote up
public RealMatrix nextWishart(double df, Cholesky invscale) {
  int d = invscale.getL().getColumnDimension();
  Array2DRowRealMatrix A = new Array2DRowRealMatrix(d,d);
  ArrayRealVector v = new ArrayRealVector(d);
  for (int i=0; i<d; i++) {
    v.setEntry(i, sqrt(nextChiSquared(df-i)));
    for (int j=0; j<i; j++) {
      v.setEntry(j, 0.0);
    }
    for (int j=i+1; j<d; j++) {
      v.setEntry(j, nextGaussian());
    }
    A.setColumnVector(i, invscale.solveLT(v));
  }
  return A.multiply(A.transpose());
}
 
Example 6
Project: BNPMix.java   File: Generator.java   View source code 6 votes vote down vote up
public static void main(String[] args) {
  // test wishart
  double[][] s = {{2.0,1.0,0.0},{1.0,2.0,1.0},{0.0,1.0,2.0}};
  RealMatrix S = new Array2DRowRealMatrix(s);
  Cholesky C = new Cholesky(S);
  double df = 2.4;
  RealMatrix sum = new Array2DRowRealMatrix(3,3);
  for (int i=0; i<100000; i++) {
    RealMatrix sample = generator.nextWishart(df, C);
    sum = sum.add(sample);
  }
  sum = sum.scalarMultiply(1.0/100000.0);
  System.out.println(sum.getRowVector(0));
  System.out.println(sum.getRowVector(1));
  System.out.println(sum.getRowVector(2));
}
 
Example 7
Project: oryx2   File: LinearSystemSolver.java   View source code 6 votes vote down vote up
/**
 * @param data dense matrix represented in row-major form
 * @return solver for the system Ax = b
 */
static Solver getSolver(double[][] data) {
  if (data == null) {
    return null;
  }
  RealMatrix M = new Array2DRowRealMatrix(data, false);
  double infNorm = M.getNorm();
  double singularityThreshold = infNorm * SINGULARITY_THRESHOLD_RATIO;
  RRQRDecomposition decomposition = new RRQRDecomposition(M, singularityThreshold);
  DecompositionSolver solver = decomposition.getSolver();
  if (solver.isNonSingular()) {
    return new Solver(solver);
  }
  // Otherwise try to report apparent rank
  int apparentRank = decomposition.getRank(0.01); // Better value?
  log.warn("{} x {} matrix is near-singular (threshold {}). Add more data or decrease the " +
           "number of features, to <= about {}",
           M.getRowDimension(), 
           M.getColumnDimension(),
           singularityThreshold,
           apparentRank);
  throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank);
}
 
Example 8
Project: morpheus-core   File: XDataFrameLeastSquares.java   View source code 6 votes vote down vote up
/**
 * Creates the X design matrix for this regression model
 * @return  the X design matrix
 */
RealMatrix createX() {
    final int n = frame.rows().count();
    final int offset = hasIntercept() ? 1 : 0;
    final int p = hasIntercept() ? regressors.size() + 1 : regressors.size();
    final int[] colIndexes = regressors.stream().mapToInt(k -> frame.cols().ordinalOf(k)).toArray();
    final RealMatrix x = new Array2DRowRealMatrix(n, p);
    for (int i = 0; i < n; ++i) {
        x.setEntry(i, 0, 1d);
        for (int j = offset; j < p; ++j) {
            final double value = frame.data().getDouble(i, colIndexes[j - offset]);
            x.setEntry(i, j, value);
        }
    }
    return x;
}
 
Example 9
Project: hdf5-java-bindings   File: HDF5LibraryUnitTest.java   View source code 6 votes vote down vote up
@Test
public void testCreateLargeMatrix(){
    // Creates a large PoN of junk values and simply tests that these can be written and read.

    // Make a big, fake set of read counts.
    final int numRows = 2500000;
    final int numCols = 10;
    final double mean = 3e-7;
    final double sigma = 1e-9;

    final RealMatrix bigCounts = createMatrixOfGaussianValues(numRows, numCols, mean, sigma);
    final File tempOutputHD5 = Utils.createTempFile("big-ol-", ".hd5");
    final HDF5File hdf5File = new HDF5File(tempOutputHD5, HDF5File.OpenMode.CREATE);
    final String hdf5Path = "/test/m";
    hdf5File.makeDoubleMatrix(hdf5Path, bigCounts.getData());
    hdf5File.close();

    final HDF5File hdf5FileForReading = new HDF5File(tempOutputHD5, HDF5File.OpenMode.READ_ONLY);
    final double[][] result = hdf5FileForReading.readDoubleMatrix(hdf5Path);
    final RealMatrix resultAsRealMatrix = new Array2DRowRealMatrix(result);
    Assert.assertTrue(resultAsRealMatrix.getRowDimension() == numRows);
    Assert.assertTrue(resultAsRealMatrix.getColumnDimension() == numCols);
    final RealMatrix readMatrix = new Array2DRowRealMatrix(result);
    assertEqualsMatrix(readMatrix, bigCounts, false);
}
 
Example 10
Project: Fog-Computing-Mobile-Architecture   File: PowerVmSelectionPolicyMaximumCorrelation.java   View source code 6 votes vote down vote up
/**
 * Gets the correlation coefficients.
 * 
 * @param data the data
 * @return the correlation coefficients
 */
protected List<Double> getCorrelationCoefficients(final double[][] data) {
	int n = data.length;
	int m = data[0].length;
	List<Double> correlationCoefficients = new LinkedList<Double>();
	for (int i = 0; i < n; i++) {
		double[][] x = new double[n - 1][m];
		int k = 0;
		for (int j = 0; j < n; j++) {
			if (j != i) {
				x[k++] = data[j];
			}
		}

		// Transpose the matrix so that it fits the linear model
		double[][] xT = new Array2DRowRealMatrix(x).transpose().getData();

		// RSquare is the "coefficient of determination"
		correlationCoefficients.add(MathUtil.createLinearRegression(xT,
				data[i]).calculateRSquared());
	}
	return correlationCoefficients;
}
 
Example 11
Project: incubator-hivemall   File: MatrixUtilsTest.java   View source code 6 votes vote down vote up
@Test
public void testToeplitz() {
    RealMatrix[] c = new RealMatrix[] {new Array2DRowRealMatrix(new double[] {1}),
            new Array2DRowRealMatrix(new double[] {2}),
            new Array2DRowRealMatrix(new double[] {3})};
    RealMatrix[][] A = MatrixUtils.toeplitz(c, 3);
    // 1  2  3
    // 2  1  2
    // 3  2  1
    Assert.assertArrayEquals(new RealMatrix[] {new Array2DRowRealMatrix(new double[] {1}),
            new Array2DRowRealMatrix(new double[] {2}),
            new Array2DRowRealMatrix(new double[] {3})}, A[0]);
    Assert.assertArrayEquals(new RealMatrix[] {new Array2DRowRealMatrix(new double[] {2}),
            new Array2DRowRealMatrix(new double[] {1}),
            new Array2DRowRealMatrix(new double[] {2})}, A[1]);
    Assert.assertArrayEquals(new RealMatrix[] {new Array2DRowRealMatrix(new double[] {3}),
            new Array2DRowRealMatrix(new double[] {2}),
            new Array2DRowRealMatrix(new double[] {1})}, A[2]);
}
 
Example 12
Project: incubator-hivemall   File: MatrixUtilsTest.java   View source code 6 votes vote down vote up
@Test
public void testFlatten2D() {
    RealMatrix[] m1 = new RealMatrix[] {
            new Array2DRowRealMatrix(new double[][] {new double[] {1, 2, 3},
                    new double[] {4, 5, 6}}),
            new Array2DRowRealMatrix(new double[][] {new double[] {7, 8, 9},
                    new double[] {10, 11, 12}}),
            new Array2DRowRealMatrix(new double[][] {new double[] {13, 14, 15},
                    new double[] {16, 17, 18}})};
    double[] actual = MatrixUtils.flatten(m1);
    double[] expected = new double[18];
    for (int i = 0; i < expected.length; i++) {
        expected[i] = i + 1;
    }
    Assert.assertArrayEquals(expected, actual, 0.d);
}
 
Example 13
Project: incubator-hivemall   File: MatrixUtilsTest.java   View source code 6 votes vote down vote up
@Test
public void testUnflatten2D() {
    double[] data = new double[24];
    for (int i = 0; i < data.length; i++) {
        data[i] = i + 1;
    }
    RealMatrix[] actual = MatrixUtils.unflatten(data, 2, 3, 4);

    RealMatrix[] expected = new RealMatrix[] {
            new Array2DRowRealMatrix(new double[][] {new double[] {1, 2, 3},
                    new double[] {4, 5, 6}}),
            new Array2DRowRealMatrix(new double[][] {new double[] {7, 8, 9},
                    new double[] {10, 11, 12}}),
            new Array2DRowRealMatrix(new double[][] {new double[] {13, 14, 15},
                    new double[] {16, 17, 18}}),
            new Array2DRowRealMatrix(new double[][] {new double[] {19, 20, 21},
                    new double[] {22, 23, 24}})};

    Assert.assertArrayEquals(expected, actual);
}
 
Example 14
Project: incubator-hivemall   File: MatrixUtilsTest.java   View source code 6 votes vote down vote up
@Test
public void testPower1() {
    RealMatrix A = new Array2DRowRealMatrix(new double[][] {new double[] {1, 2, 3},
            new double[] {4, 5, 6}});

    double[] x = new double[3];
    x[0] = Math.random();
    x[1] = Math.random();
    x[2] = Math.random();

    double[] u = new double[2];
    double[] v = new double[3];

    double s = MatrixUtils.power1(A, x, 2, u, v);

    SingularValueDecomposition svdA = new SingularValueDecomposition(A);

    Assert.assertArrayEquals(svdA.getU().getColumn(0), u, 0.001d);
    Assert.assertArrayEquals(svdA.getV().getColumn(0), v, 0.001d);
    Assert.assertEquals(svdA.getSingularValues()[0], s, 0.001d);
}
 
Example 15
Project: incubator-hivemall   File: MatrixUtilsTest.java   View source code 6 votes vote down vote up
@Test
public void testLanczosTridiagonalization() {
    // Symmetric matrix
    RealMatrix C = new Array2DRowRealMatrix(new double[][] {new double[] {1, 2, 3, 4},
            new double[] {2, 1, 4, 3}, new double[] {3, 4, 1, 2}, new double[] {4, 3, 2, 1}});

    // naive initial vector
    double[] a = new double[] {1, 1, 1, 1};

    RealMatrix actual = new Array2DRowRealMatrix(new double[4][4]);
    MatrixUtils.lanczosTridiagonalization(C, a, actual);

    RealMatrix expected = new Array2DRowRealMatrix(new double[][] {new double[] {40, 60, 0, 0},
            new double[] {60, 10, 120, 0}, new double[] {0, 120, 10, 120},
            new double[] {0, 0, 120, 10}});

    Assert.assertEquals(expected, actual);
}
 
Example 16
Project: incubator-hivemall   File: MatrixUtilsTest.java   View source code 6 votes vote down vote up
@Test
public void testTridiagonalEigen() {
    // Tridiagonal Matrix
    RealMatrix T = new Array2DRowRealMatrix(new double[][] {new double[] {40, 60, 0, 0},
            new double[] {60, 10, 120, 0}, new double[] {0, 120, 10, 120},
            new double[] {0, 0, 120, 10}});

    double[] eigvals = new double[4];
    RealMatrix eigvecs = new Array2DRowRealMatrix(new double[4][4]);

    MatrixUtils.tridiagonalEigen(T, 2, eigvals, eigvecs);

    RealMatrix actual = eigvecs.multiply(eigvecs.transpose());

    RealMatrix expected = new Array2DRowRealMatrix(new double[4][4]);
    for (int i = 0; i < 4; i++) {
        expected.setEntry(i, i, 1);
    }

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            Assert.assertEquals(expected.getEntry(i, j), actual.getEntry(i, j), 0.001d);
        }
    }
}
 
Example 17
Project: classifyRSAkey   File: LeastSquaresFitPriorProbabilityEstimator.java   View source code 6 votes vote down vote up
public Pair<RealVector, RealMatrix> value(final RealVector aprioriApproximation) {

            int maskCount = allMaskValues.size();
            int groupCount = table.getGroupsNames().size();

            RealMatrix aprioriApproximationMatrix = new Array2DRowRealMatrix(aprioriApproximation.getDimension(), 1);
            aprioriApproximationMatrix.setColumn(0, aprioriApproximation.toArray());

            RealVector approximation = maskProbabilitiesForSources.multiply(aprioriApproximationMatrix).getColumnVector(0);
            approximation = normalizeVector(approximation);
            RealVector differences = observedFrequencies.subtract(approximation);

            RealMatrix jacobian = new Array2DRowRealMatrix(maskCount, groupCount);
            for (int i = 0; i < groupCount; i++) {
                RealVector partialDifferences = observedFrequencies.subtract(maskProbabilitiesForSources.getColumnVector(i).mapMultiply(aprioriApproximation.getEntry(i)));
                jacobian.setColumn(i, partialDifferences.toArray());//.ebeDivide(differences).toArray());
                //if (i == 0) System.out.println(partialDifferences);
            }

            return new Pair<>(differences, jacobian);
        }
 
Example 18
Project: SME   File: OLSMultipleLinearRegression.java   View source code 6 votes vote down vote up
/**
 * <p>Compute the "hat" matrix.
 * </p>
 * <p>The hat matrix is defined in terms of the design matrix X
 *  by X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup>
 * </p>
 * <p>The implementation here uses the QR decomposition to compute the
 * hat matrix as Q I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the
 * p-dimensional identity matrix augmented by 0's.  This computational
 * formula is from "The Hat Matrix in Regression and ANOVA",
 * David C. Hoaglin and Roy E. Welsch,
 * <i>The American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp. 17-22.
 * </p>
 * <p>Data for the model must have been successfully loaded using one of
 * the {@code newSampleData} methods before invoking this method; otherwise
 * a {@code NullPointerException} will be thrown.</p>
 *
 * @return the hat matrix
 * @throws NullPointerException unless method {@code newSampleData} has been
 * called beforehand.
 */
public RealMatrix calculateHat() {
    // Create augmented identity matrix
    RealMatrix Q = qr.getQ();
    final int p = qr.getR().getColumnDimension();
    final int n = Q.getColumnDimension();
    // No try-catch or advertised NotStrictlyPositiveException - NPE above if n < 3
    Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n);
    double[][] augIData = augI.getDataRef();
    for (int i = 0; i < n; i++) {
        for (int j =0; j < n; j++) {
            if (i == j && i < p) {
                augIData[i][j] = 1d;
            } else {
                augIData[i][j] = 0d;
            }
        }
    }

    // Compute and return Hat matrix
    // No DME advertised - args valid if we get here
    return Q.multiply(augI).multiply(Q.transpose());
}
 
Example 19
Project: cy-dataseries   File: BSPlineRegressionSmoothingProvider.java   View source code 6 votes vote down vote up
@Override
public double[] smooth(double[] sourceX, double[] noisyY, double[] estimateX, double parameter) {
	int numDivisions = (int)Math.round(parameter);
	
	SmoothingHelper.SmoothingInput filteredInput = SmoothingHelper.filterInvalidValues(sourceX, noisyY);
	sourceX = filteredInput.getSourceX();
	noisyY = filteredInput.getSourceY();

	if(numDivisions >= sourceX.length) {
		throw new IllegalArgumentException("Cannot fit with " + numDivisions + " knots as the input data (after removing NAs) has < " + (numDivisions + 1) + " data points.");
	}

	
	double[][] sourceBasis = createBasis(numDivisions, splineDegree, sourceX);
	RealMatrix matrix = new Array2DRowRealMatrix(sourceBasis);		
	QRDecomposition decomposition = new QRDecomposition(matrix);
	RealVector coefficients = decomposition.getSolver().solve(new ArrayRealVector(noisyY));
	
	double[][] estimateBasis = createBasis((int)parameter, splineDegree, estimateX);
	RealMatrix estimateBasisMatrix = new Array2DRowRealMatrix(estimateBasis);
	
	double[] result = estimateBasisMatrix.transpose().preMultiply(coefficients).toArray();
	return result;
}
 
Example 20
Project: droidplanner-master   File: FitPoints.java   View source code 6 votes vote down vote up
/**
 * Translate the algebraic form of the ellipsoid to the center.
 * 
 * @param center
 *            vector containing the center of the ellipsoid.
 * @param a
 *            the algebraic form of the polynomial.
 * @return the center translated form of the algebraic ellipsoid.
 */
private RealMatrix translateToCenter(RealVector center, RealMatrix a)
{
	// Form the corresponding translation matrix.
	RealMatrix t = MatrixUtils.createRealIdentityMatrix(4);

	RealMatrix centerMatrix = new Array2DRowRealMatrix(1, 3);

	centerMatrix.setRowVector(0, center);

	t.setSubMatrix(centerMatrix.getData(), 3, 0);

	// Translate to the center.
	RealMatrix r = t.multiply(a).multiply(t.transpose());

	return r;
}
 
Example 21
Project: CloudSimDisk   File: PowerVmSelectionPolicyMaximumCorrelation.java   View source code 6 votes vote down vote up
/**
 * Gets the correlation coefficients.
 * 
 * @param data the data
 * @return the correlation coefficients
 */
protected List<Double> getCorrelationCoefficients(final double[][] data) {
	int n = data.length;
	int m = data[0].length;
	List<Double> correlationCoefficients = new LinkedList<Double>();
	for (int i = 0; i < n; i++) {
		double[][] x = new double[n - 1][m];
		int k = 0;
		for (int j = 0; j < n; j++) {
			if (j != i) {
				x[k++] = data[j];
			}
		}

		// Transpose the matrix so that it fits the linear model
		double[][] xT = new Array2DRowRealMatrix(x).transpose().getData();

		// RSquare is the "coefficient of determination"
		correlationCoefficients.add(MathUtil.createLinearRegression(xT,
				data[i]).calculateRSquared());
	}
	return correlationCoefficients;
}
 
Example 22
Project: nongfei-missionplane   File: FitPoints.java   View source code 6 votes vote down vote up
/**
 * Translate the algebraic form of the ellipsoid to the center.
 * 
 * @param center
 *            vector containing the center of the ellipsoid.
 * @param a
 *            the algebraic form of the polynomial.
 * @return the center translated form of the algebraic ellipsoid.
 */
private RealMatrix translateToCenter(RealVector center, RealMatrix a)
{
	// Form the corresponding translation matrix.
	RealMatrix t = MatrixUtils.createRealIdentityMatrix(4);

	RealMatrix centerMatrix = new Array2DRowRealMatrix(1, 3);

	centerMatrix.setRowVector(0, center);

	t.setSubMatrix(centerMatrix.getData(), 3, 0);

	// Translate to the center.
	RealMatrix r = t.multiply(a).multiply(t.transpose());

	return r;
}
 
Example 23
Project: clust4j   File: DataSet.java   View source code 6 votes vote down vote up
public DataSet(Array2DRowRealMatrix data, int[] labels, String[] hdrz, MatrixFormatter formatter, boolean copyData) {
	
	/*// we should allow this behavior...
	if(null == labels)
		throw new IllegalArgumentException("labels cannot be null");
	*/

	if(null == data)
		throw new IllegalArgumentException("data cannot be null");
	if(null == hdrz)
		this.headers = genHeaders(data.getColumnDimension());
	else 
		this.headers = VecUtils.copy(hdrz);
	
	
	// Check to make sure dims match up...
	if((null != labels) && labels.length != data.getRowDimension())
		throw new DimensionMismatchException(labels.length, data.getRowDimension());
	if(this.headers.length != data.getColumnDimension())
		throw new DimensionMismatchException(this.headers.length, data.getColumnDimension());
	
	this.data = copyData ? (Array2DRowRealMatrix)data.copy() : data;
	this.labels = VecUtils.copy(labels);
	this.formatter = null == formatter ? DEF_FORMATTER : formatter;
}
 
Example 24
Project: clust4j   File: KMeansTests.java   View source code 6 votes vote down vote up
/** Scale = false */
@Test
public void KMeansTest1() {
	final double[][] data = new double[][] {
		new double[] {0.005, 	 0.182751,  0.1284},
		new double[] {3.65816,   0.29518,   2.123316},
		new double[] {4.1234,    0.27395,   1.8900002}
	};
	
	final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(data);
	KMeans km = new KMeans(mat, 2).fit();
	
	assertTrue(km.getLabels()[0] == 0 && km.getLabels()[1] == 1);
	assertTrue(km.getLabels()[1] == km.getLabels()[2]);
	assertTrue(km.didConverge());
	//km.info("testing the kmeans logger");
}
 
Example 25
Project: gatk-protected   File: IntegerCopyNumberTransitionMatrixUnitTest.java   View source code 6 votes vote down vote up
@Test
public void testPadding() {
    final IntegerCopyNumberTransitionMatrix data = new IntegerCopyNumberTransitionMatrix(
            new Array2DRowRealMatrix(new double[][] {
                    {1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9}}), 2);
    final RealMatrix expected = new Array2DRowRealMatrix(
            new double[][]{
                    {1.0/12, 2.0/15, 3.0/18, 0, 0},
                    {4.0/12, 5.0/15, 6.0/18, 0, 0},
                    {7.0/12, 8.0/15, 9.0/18, 0, 0},
                    {0,      0,      0,      1, 0},
                    {0,      0,      0,      0, 1}});
    Assert.assertEquals(data.getTransitionMatrix().subtract(expected).getNorm(), 0, 1e-12);
}
 
Example 26
Project: clust4j   File: KMedoidsTests.java   View source code 6 votes vote down vote up
@Test
public void KMedoidsLoadTest1() {
	final Array2DRowRealMatrix mat = getRandom(400, 10); // need to reduce size for travis CI
	StandardScaler scaler = new StandardScaler().fit(mat);
	RealMatrix X = scaler.transform(mat);
	
	final boolean[] scale = new boolean[] {false, true};
	final int[] ks = new int[] {1,3,5};
	
	KMedoids km = null;
	for(boolean b : scale) {
		for(int k : ks) {
			km = new KMedoids(b ? X : mat, 
					new KMedoidsParameters(k)
						.setVerbose(true));
			km.fit();
		}
	}
}
 
Example 27
Project: clust4j   File: HierarchicalTests.java   View source code 6 votes vote down vote up
@Test
public void testKernel() {
	final double[][] data = new double[][] {
		new double[] {3.65816,   0.29518,   2.123316},
		new double[] {0.005, 	 0.182751,  0.1284},
		new double[] {4.1234,    0.27395,   1.8900002}
	};
	
	int[] labels;
	final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(data);
	for(Linkage linkage: HierarchicalAgglomerative.Linkage.values()) {
		HierarchicalAgglomerative hac = 
			new HierarchicalAgglomerative(mat,
				new HierarchicalAgglomerativeParameters()
						.setLinkage(linkage)
						.setMetric(new GaussianKernel())
						.setVerbose(false)).fit();
		
		labels = hac.getLabels();
		assertTrue(labels[0] == labels[2]);
	}
}
 
Example 28
Project: gatk-protected   File: PoNTestUtils.java   View source code 6 votes vote down vote up
/**
 * Reads a very basic tsv (numbers separated by tabs) into a RealMatrix.
 * <p>Very little error checking happens in this method</p>
 *
 * @param inputFile readable file.  Not {@code null}
 * @return never {@code null}
 */
public static RealMatrix readTsvIntoMatrix(final File inputFile) {
    IOUtils.canReadFile(inputFile);
    final List<double []> allData = new ArrayList<>();
    int ctr = 0;
    try {

        final CSVReader reader = new CSVReader(new FileReader(inputFile), '\t', CSVWriter.NO_QUOTE_CHARACTER);
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            ctr++;
            allData.add(Arrays.stream(nextLine).filter(s -> StringUtils.trim(s).length() > 0).map(s -> Double.parseDouble(StringUtils.trim(s))).mapToDouble(d -> d).toArray());
        }
    } catch (final IOException ioe) {
        Assert.fail("Could not open test file: " + inputFile, ioe);
    }
    final RealMatrix result = new Array2DRowRealMatrix(allData.size(), allData.get(0).length);
    for (int i = 0; i < result.getRowDimension(); i++) {
        result.setRow(i, allData.get(i));
    }
    return result;
}
 
Example 29
Project: clust4j   File: PipelineTest.java   View source code 6 votes vote down vote up
@Test
public void testB() {
	final double[][] data = new double[][] {
		new double[] {0.005, 	 0.182751,  0.1284},
		new double[] {3.65816,   0.29518,   2.123316},
		new double[] {4.1234,    0.27395,   1.8900002}
	};
	
	final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(data);
	final KMedoidsParameters planner = new KMedoidsParameters(2).setVerbose(true);
	
	// Build the pipeline
	final UnsupervisedPipeline<KMedoids> pipe = new UnsupervisedPipeline<KMedoids>(planner, 
		new PreProcessor[]{
			new StandardScaler(), 
			new MeanImputation(new MeanImputation.MeanImputationPlanner().setVerbose(true)) // Will create a warning
		});
	
	@SuppressWarnings("unused")
	KMedoids km = pipe.fit(mat); 
	System.out.println();
}
 
Example 30
Project: clust4j   File: LoadTests.java   View source code 6 votes vote down vote up
@Test
public void testLargeMatrixMultiplicationTask() {
	// Force a massively distributed task... can take long time (if works...)!
	if(GlobalState.ParallelismConf.PARALLELISM_ALLOWED) {
		final int rows = GlobalState.ParallelismConf.MAX_SERIAL_VECTOR_LEN + 1;
		final int cols = 2;
		
		try {
			// 2 X 10,000,001
			Array2DRowRealMatrix A = TestSuite.getRandom(cols, rows);
			
			// 10,000,001 X 2
			Array2DRowRealMatrix B = TestSuite.getRandom(rows, cols);
			
			// Yield 2 X 2
			MatUtils.multiplyDistributed(A.getDataRef(), B.getDataRef());
		} catch(OutOfMemoryError e) {
			Log.info("could not complete large distributed multiplication due to heap space");
			fail();
		} finally { // don't want to fail tests just because of this...
			assertTrue(true);
		}
	}
}
 
Example 31
Project: clust4j   File: KMeansTests.java   View source code 6 votes vote down vote up
@Test
public void testLabelCentroidReordering() {
	final double[][] data = new double[][] {
		new double[] {0.000, 	 0.000,     0.000},
		new double[] {1.500,     1.500,     1.500},
		new double[] {3.000,     3.000,     3.000}
	};
		
	final KMeans km = new KMeans(new Array2DRowRealMatrix(data,false), 3).fit();

	// the labels should correspond to the index of centroid...
	assertTrue(VecUtils.equalsExactly(km.getLabels(), new int[]{ 0,1,2 }));
	final ArrayList<double[]> centroids = km.getCentroids();
	
	for(int i = 0; i < centroids.size(); i++) {
		assertTrue(VecUtils.equalsExactly(data[i], centroids.get(i)));
	}
}
 
Example 32
Project: Gprs_droidplanner   File: FitPoints.java   View source code 6 votes vote down vote up
/**
 * Translate the algebraic form of the ellipsoid to the center.
 * 
 * @param center
 *            vector containing the center of the ellipsoid.
 * @param a
 *            the algebraic form of the polynomial.
 * @return the center translated form of the algebraic ellipsoid.
 */
private RealMatrix translateToCenter(RealVector center, RealMatrix a)
{
	// Form the corresponding translation matrix.
	RealMatrix t = MatrixUtils.createRealIdentityMatrix(4);

	RealMatrix centerMatrix = new Array2DRowRealMatrix(1, 3);

	centerMatrix.setRowVector(0, center);

	t.setSubMatrix(centerMatrix.getData(), 3, 0);

	// Translate to the center.
	RealMatrix r = t.multiply(a).multiply(t.transpose());

	return r;
}
 
Example 33
Project: clust4j   File: TrainTestSplitTests.java   View source code 6 votes vote down vote up
@Test
public void testLowerBoundWithNoLabels() {
	int[] labels = null;
	DataSet set = new DataSet(
		new Array2DRowRealMatrix(new double[][]{
			new double[]{0,0,0},
			new double[]{1,1,1}
		}, false),
		
		labels // null
	);
	
	TrainTestSplit split = new TrainTestSplit(set, 0.8);
	assertTrue(split.getTrain().numRows() == 1);
	assertTrue(split.getTest().numRows() == 1);
	
	split = new TrainTestSplit(set, 0.1);
	assertTrue(split.getTrain().numRows() == 1);
	assertTrue(split.getTest().numRows() == 1);
}
 
Example 34
Project: clust4j   File: AffinityPropagationTests.java   View source code 6 votes vote down vote up
@Test
public void testOnIris() {
	Array2DRowRealMatrix iris = data;
	AffinityPropagation ap = new AffinityPropagation(iris, 
		new AffinityPropagationParameters()
				.setVerbose(true)).fit();
	
	final int[] expected = new LabelEncoder(new int[]{
		1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
		1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
		1, 0, 1, 0, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 2, 4, 3, 2,
		3, 4, 3, 4, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 3, 2, 2, 2, 3, 3, 3, 2,
		3, 3, 3, 3, 3, 2, 3, 3, 6, 4, 6, 6, 6, 5, 3, 5, 6, 5, 6, 4, 6, 4, 4,
		6, 6, 5, 5, 4, 6, 4, 5, 4, 6, 6, 4, 4, 6, 6, 5, 5, 6, 4, 4, 5, 6, 6,
		4, 6, 6, 6, 4, 6, 6, 6, 4, 6, 6, 4
	}).fit().getEncodedLabels();
	
	// Assert that the predicted labels are at least 90% in sync with sklearn
	// expected labels (give leeway for random state...)
	assertTrue(Precision.equals(ap.indexAffinityScore(expected), 1.0, 0.1));
}
 
Example 35
Project: gatk-protected   File: HDF5PCACoveragePoNCreationUtilsUnitTest.java   View source code 6 votes vote down vote up
@DataProvider(name="readCountOnlyWithDiverseShapeData")
public Object[][] readCountOnlyWithDiverseShapeData() {
    final List<Object[]> result = new ArrayList<>(4);
    final Random rdn = new Random(31);
    final int[] columnCounts = new int[] { 10, 100, 100, 200};
    final int[] targetCounts = new int[] { 100, 100, 200, 200 };
    for (int k = 0; k < columnCounts.length; k++) {
        final List<String> columnNames = IntStream.range(0, columnCounts[k]).mapToObj(i -> "sample_" + (i + 1)).collect(Collectors.toList());
        final List<Target> targets = IntStream.range(0, targetCounts[k]).mapToObj(i -> new Target("target_" + (i+1))).collect(Collectors.toList());
        final double[][] counts = new double[targetCounts[k]][columnCounts[k]];
        for (int i = 0; i < counts.length; i++) {
            for (int j = 0; j < counts[0].length; j++) {
                counts[i][j] = rdn.nextDouble();
            }
        }
        final ReadCountCollection readCounts = new ReadCountCollection(targets, columnNames,
                new Array2DRowRealMatrix(counts, false));
        result.add(new Object[]{readCounts });
    }
    return result.toArray(new Object[result.size()][]);
}
 
Example 36
Project: clust4j   File: KMedoidsTests.java   View source code 6 votes vote down vote up
@Test
public void KMedoidsTest5() {
	final double[][] data = new double[][] {
		new double[] {0.005, 	 0.182751,  0.1284},
		new double[] {3.65816,   0.29518,   2.123316},
		new double[] {4.1234,    0.0001,    1.8900002},
		new double[] {100,       200,       100}
	};
	
	final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(data);
	StandardScaler scaler = new StandardScaler().fit(mat);
	RealMatrix X = scaler.transform(mat);
	final boolean[] scale = new boolean[]{false, true};
	
	KMedoids km = null;
	for(boolean b : scale) {
		km = new KMedoids(b ? X : mat, new KMedoidsParameters(1));
		km.fit();
		assertTrue(km.didConverge());
	}
}
 
Example 37
Project: gatk-protected   File: ReadCountCollectionUtilsUnitTest.java   View source code 6 votes vote down vote up
@DataProvider(name="tooManyZerosData")
public Object[][] tooManyZerosData() {
    final double[] zeroProbabilities = new double[] { .001, .01, .02, 0.1 };
    final List<Object[]> result = new ArrayList<>();
    final Random rdn = new Random(13);
    final int columnCount = 100;
    final int targetCount = 100;
    final List<String> columnNames = IntStream.range(0, columnCount).mapToObj(i -> "sample_" + (i + 1)).collect(Collectors.toList());
    final List<Target> targets = IntStream.range(0, targetCount).mapToObj(i -> new Target("target_" + (i+1))).collect(Collectors.toList());
    for (final double zeroProbability : zeroProbabilities) {
        final double[][] counts = new double[columnCount][targetCount];
        for (int i = 0; i < counts.length; i++) {
            for (int j = 0; j < counts[0].length; j++) {
                counts[i][j] = rdn.nextDouble() <= zeroProbability ? 0.0 : rdn.nextDouble();
            }
        }
        final ReadCountCollection readCounts = new ReadCountCollection(targets, columnNames, new Array2DRowRealMatrix(counts, false));
        result.add(new Object[] { readCounts });
    }
    return result.toArray(new Object[result.size()][]);
}
 
Example 38
Project: gatk-protected   File: HDF5PCACoveragePoNCreationUtilsUnitTest.java   View source code 6 votes vote down vote up
@DataProvider(name="readCountAndPercentileData")
public Object[][] readCountAndPercentileData() {
    final double[] percentiles = new double[] { 1.0, 2.5, 5.0 , 10.0, 25.0 };
    final List<Object[]> result = new ArrayList<>();
    final Random rdn = new Random(13);
    final int columnCount = 100;
    final int targetCount = 100;
    final List<String> columnNames = IntStream.range(0, columnCount).mapToObj(i -> "sample_" + (i + 1)).collect(Collectors.toList());
    final List<Target> targets = IntStream.range(0, targetCount).mapToObj(i -> new Target("target_" + (i+1))).collect(Collectors.toList());
    for (final double percentile : percentiles) {
        final double[][] counts = new double[columnCount][targetCount];
        for (int i = 0; i < counts.length; i++) {
            for (int j = 0; j < counts[0].length; j++) {
                counts[i][j] = rdn.nextDouble();
            }
        }
        final ReadCountCollection readCounts = new ReadCountCollection(targets, columnNames,
                new Array2DRowRealMatrix(counts, false));
        result.add(new Object[]{readCounts, percentile});
    }
    return result.toArray(new Object[result.size()][]);
}
 
Example 39
Project: cloudsim-plus   File: PowerVmSelectionPolicyMaximumCorrelation.java   View source code 6 votes vote down vote up
/**
 * Gets the correlation coefficients.
 *
 * @param data the data
 * @return the correlation coefficients
 */
protected List<Double> getCorrelationCoefficients(final double[][] data) {
    final int n = data.length;
    final int m = data[0].length;
    final List<Double> correlationCoefficients = new LinkedList<>();
    for (int i = 0; i < n; i++) {
        final double[][] x = new double[n - 1][m];
        int k = 0;
        for (int j = 0; j < n; j++) {
            if (j != i) {
                x[k++] = data[j];
            }
        }

        // Transpose the matrix so that it fits the linear model
        final double[][] xT = new Array2DRowRealMatrix(x).transpose().getData();

        // RSquare is the "coefficient of determination"
        correlationCoefficients.add(
            MathUtil.createLinearRegression(xT, data[i]).calculateRSquared());
    }
    return correlationCoefficients;
}
 
Example 40
Project: MeteoInfoLib   File: LinalgUtil.java   View source code 6 votes vote down vote up
/**
 * Calculates the QR-decomposition of a matrix. The QR-decomposition of a
 * matrix A consists of two matrices Q and R that satisfy: A = QR, Q is
 * orthogonal (QTQ = I), and R is upper triangular. If A is m×n, Q is m×m
 * and R m×n.
 *
 * @param a Given matrix.
 * @return Result Q/R arrays.
 */
public static Array[] qr(Array a) {
    int m = a.getShape()[0];
    int n = a.getShape()[1];
    Array Qa = Array.factory(DataType.DOUBLE, new int[]{m, m});
    Array Ra = Array.factory(DataType.DOUBLE, a.getShape());
    double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a);
    RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
    QRDecomposition decomposition = new QRDecomposition(matrix);
    RealMatrix Q = decomposition.getQ();
    RealMatrix R = decomposition.getR();
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
            Qa.setDouble(i * m + j, Q.getEntry(i, j));
        }
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            Ra.setDouble(i * n + j, R.getEntry(i, j));
        }
    }

    return new Array[]{Qa, Ra};
}