Search in sources :

Example 1 with Identity

use of org.apache.commons.math3.analysis.function.Identity in project deeplearning4j by deeplearning4j.

the class TestReconstructionDistributions method testGaussianLogProb.

@Test
public void testGaussianLogProb() {
    Nd4j.getRandom().setSeed(12345);
    int inputSize = 4;
    int[] mbs = new int[] { 1, 2, 5 };
    for (boolean average : new boolean[] { true, false }) {
        for (int minibatch : mbs) {
            INDArray x = Nd4j.rand(minibatch, inputSize);
            INDArray mean = Nd4j.randn(minibatch, inputSize);
            INDArray logStdevSquared = Nd4j.rand(minibatch, inputSize).subi(0.5);
            INDArray distributionParams = Nd4j.createUninitialized(new int[] { minibatch, 2 * inputSize });
            distributionParams.get(NDArrayIndex.all(), NDArrayIndex.interval(0, inputSize)).assign(mean);
            distributionParams.get(NDArrayIndex.all(), NDArrayIndex.interval(inputSize, 2 * inputSize)).assign(logStdevSquared);
            ReconstructionDistribution dist = new GaussianReconstructionDistribution("identity");
            double negLogProb = dist.negLogProbability(x, distributionParams, average);
            INDArray exampleNegLogProb = dist.exampleNegLogProbability(x, distributionParams);
            assertArrayEquals(new int[] { minibatch, 1 }, exampleNegLogProb.shape());
            //Calculate the same thing, but using Apache Commons math
            double logProbSum = 0.0;
            for (int i = 0; i < minibatch; i++) {
                double exampleSum = 0.0;
                for (int j = 0; j < inputSize; j++) {
                    double mu = mean.getDouble(i, j);
                    double logSigma2 = logStdevSquared.getDouble(i, j);
                    double sigma = Math.sqrt(Math.exp(logSigma2));
                    NormalDistribution nd = new NormalDistribution(mu, sigma);
                    double xVal = x.getDouble(i, j);
                    double thisLogProb = nd.logDensity(xVal);
                    logProbSum += thisLogProb;
                    exampleSum += thisLogProb;
                }
                assertEquals(-exampleNegLogProb.getDouble(i), exampleSum, 1e-6);
            }
            double expNegLogProb;
            if (average) {
                expNegLogProb = -logProbSum / minibatch;
            } else {
                expNegLogProb = -logProbSum;
            }
            //                System.out.println(expLogProb + "\t" + logProb + "\t" + (logProb / expLogProb));
            assertEquals(expNegLogProb, negLogProb, 1e-6);
            //Also: check random sampling...
            int count = minibatch * inputSize;
            INDArray arr = Nd4j.linspace(-3, 3, count).reshape(minibatch, inputSize);
            INDArray sampleMean = dist.generateAtMean(arr);
            INDArray sampleRandom = dist.generateRandom(arr);
        }
    }
}
Also used : GaussianReconstructionDistribution(org.deeplearning4j.nn.conf.layers.variational.GaussianReconstructionDistribution) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) GaussianReconstructionDistribution(org.deeplearning4j.nn.conf.layers.variational.GaussianReconstructionDistribution) ReconstructionDistribution(org.deeplearning4j.nn.conf.layers.variational.ReconstructionDistribution) ExponentialReconstructionDistribution(org.deeplearning4j.nn.conf.layers.variational.ExponentialReconstructionDistribution) BernoulliReconstructionDistribution(org.deeplearning4j.nn.conf.layers.variational.BernoulliReconstructionDistribution) Test(org.junit.Test)

Example 2 with Identity

use of org.apache.commons.math3.analysis.function.Identity in project FSensor by KalebKE.

the class FitPoints method generateRIV.

/**
 * Generates a vector from the identity matrix of r.
 *
 * @param subr centered algebraic form of the ellipsoid
 */
private RealVector generateRIV(RealMatrix subr) {
    riv = new ArrayRealVector(3);
    riv.setEntry(0, Math.abs(subr.getEntry(0, 0)));
    riv.setEntry(1, Math.abs(subr.getEntry(1, 1)));
    riv.setEntry(2, Math.abs(subr.getEntry(2, 2)));
    return riv;
}
Also used : ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Example 3 with Identity

use of org.apache.commons.math3.analysis.function.Identity in project gatk-protected by broadinstitute.

the class IntegerCopyNumberTransitionMatrix method getPaddedTransitionMatrix.

/**
     * Pad the transition matrix with extra hidden states. The old transition matrix will be embedded on the upper left
     * corner of an identity matrix. As a result, there will be no mixing between the existing states and the hidden
     * states.
     *
     * Note: padding is performed for convenience and the extra hidden states are meant to be inaccessible. It is the
     * user's responsibility to make sure that the prior probabilities prohibits the occupancy of these states.
     *
     * @param originalTransitionMatrix the original non-padded transition matrix
     * @param padding non-negative number of extra hidden states to pad
     * @return an instance of {@link IntegerCopyNumberTransitionMatrix}
     */
private static RealMatrix getPaddedTransitionMatrix(final RealMatrix originalTransitionMatrix, final int padding) {
    if (padding > 0) {
        final int maxCopyNumber = originalTransitionMatrix.getColumnDimension() - 1;
        final int newMaxCopyNumber = maxCopyNumber + padding;
        final RealMatrix paddedTransitionMatrix = MatrixUtils.createRealIdentityMatrix(newMaxCopyNumber + 1);
        for (int i = 0; i <= maxCopyNumber; i++) {
            for (int j = 0; j <= maxCopyNumber; j++) {
                paddedTransitionMatrix.setEntry(i, j, originalTransitionMatrix.getEntry(i, j));
            }
        }
        return paddedTransitionMatrix;
    } else {
        return originalTransitionMatrix;
    }
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix)

Example 4 with Identity

use of org.apache.commons.math3.analysis.function.Identity in project gatk by broadinstitute.

the class IntegerCopyNumberTransitionMatrix method getPaddedTransitionMatrix.

/**
     * Pad the transition matrix with extra hidden states. The old transition matrix will be embedded on the upper left
     * corner of an identity matrix. As a result, there will be no mixing between the existing states and the hidden
     * states.
     *
     * Note: padding is performed for convenience and the extra hidden states are meant to be inaccessible. It is the
     * user's responsibility to make sure that the prior probabilities prohibits the occupancy of these states.
     *
     * @param originalTransitionMatrix the original non-padded transition matrix
     * @param padding non-negative number of extra hidden states to pad
     * @return an instance of {@link IntegerCopyNumberTransitionMatrix}
     */
private static RealMatrix getPaddedTransitionMatrix(final RealMatrix originalTransitionMatrix, final int padding) {
    if (padding > 0) {
        final int maxCopyNumber = originalTransitionMatrix.getColumnDimension() - 1;
        final int newMaxCopyNumber = maxCopyNumber + padding;
        final RealMatrix paddedTransitionMatrix = MatrixUtils.createRealIdentityMatrix(newMaxCopyNumber + 1);
        for (int i = 0; i <= maxCopyNumber; i++) {
            for (int j = 0; j <= maxCopyNumber; j++) {
                paddedTransitionMatrix.setEntry(i, j, originalTransitionMatrix.getEntry(i, j));
            }
        }
        return paddedTransitionMatrix;
    } else {
        return originalTransitionMatrix;
    }
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix)

Example 5 with Identity

use of org.apache.commons.math3.analysis.function.Identity in project FSensor by KalebKE.

the class RotationKalmanFilter method correct.

/**
 * Correct the current state estimate with an actual measurement.
 *
 * @param z
 *            the measurement vector
 * @throws NullArgumentException
 *             if the measurement vector is {@code null}
 * @throws DimensionMismatchException
 *             if the dimension of the measurement vector does not fit
 * @throws SingularMatrixException
 *             if the covariance matrix could not be inverted
 */
public void correct(final RealVector z) throws NullArgumentException, DimensionMismatchException, SingularMatrixException {
    // sanity checks
    MathUtils.checkNotNull(z);
    if (z.getDimension() != measurementMatrix.getRowDimension()) {
        throw new DimensionMismatchException(z.getDimension(), measurementMatrix.getRowDimension());
    }
    // S = H * P(k) * H' + R
    RealMatrix s = measurementMatrix.multiply(errorCovariance).multiply(measurementMatrixT).add(measurementModel.getMeasurementNoise());
    // Inn = z(k) - H * xHat(k)-
    RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation));
    // calculate gain matrix
    // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1
    // K(k) = P(k)- * H' * S^-1
    // instead of calculating the inverse of S we can rearrange the formula,
    // and then solve the linear equation A x X = B with A = S', X = K' and
    // B = (H * P)'
    // K(k) * S = P(k)- * H'
    // S' * K(k)' = H * P(k)-'
    RealMatrix kalmanGain = new CholeskyDecomposition(s).getSolver().solve(measurementMatrix.multiply(errorCovariance.transpose())).transpose();
    // update estimate with measurement z(k)
    // xHat(k) = xHat(k)- + K * Inn
    stateEstimation = stateEstimation.add(kalmanGain.operate(innovation));
    // update covariance of prediction error
    // P(k) = (I - K * H) * P(k)-
    RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension());
    errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance);
}
Also used : CholeskyDecomposition(org.apache.commons.math3.linear.CholeskyDecomposition) DimensionMismatchException(org.apache.commons.math3.exception.DimensionMismatchException) MatrixDimensionMismatchException(org.apache.commons.math3.linear.MatrixDimensionMismatchException) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Aggregations

RealMatrix (org.apache.commons.math3.linear.RealMatrix)3 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)2 TreeRangeSet (com.google.common.collect.TreeRangeSet)1 AlignmentSegment (cz1.ngs.model.AlignmentSegment)1 Blast6Segment (cz1.ngs.model.Blast6Segment)1 Sequence (cz1.ngs.model.Sequence)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 BidiMap (org.apache.commons.collections4.BidiMap)1 DualHashBidiMap (org.apache.commons.collections4.bidimap.DualHashBidiMap)1 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)1 DimensionMismatchException (org.apache.commons.math3.exception.DimensionMismatchException)1 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)1 CholeskyDecomposition (org.apache.commons.math3.linear.CholeskyDecomposition)1