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);
}
}
}
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;
}
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;
}
}
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;
}
}
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);
}
Aggregations