Package gov.sandia.cognition.math.matrix

Examples of gov.sandia.cognition.math.matrix.Matrix


    final int N = 100;

    final double[] a = {0.9d, 0.9d};
    final double[] sigma2 = {Math.pow(0.2d, 2), Math.pow(1.2d, 2)};
    final double sigma_y2 = Math.pow(0.3d, 2);
    Matrix modelCovariance1 = MatrixFactory.getDefault().copyArray(
        new double[][] {{Math.pow(0.2d, 2)}});
    Matrix modelCovariance2 = MatrixFactory.getDefault().copyArray(
        new double[][] {{Math.pow(1.2d, 2)}});
    Matrix measurementCovariance = MatrixFactory.getDefault().copyArray(
        new double[][] {{Math.pow(0.3d, 2)}});
    LinearDynamicalSystem model1 = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{0.9d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1}})
      );
    LinearDynamicalSystem model2 = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{0.9d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1}})
      );
    KalmanFilter trueKf1 = new KalmanFilter(model1, modelCovariance1, measurementCovariance);
    KalmanFilter trueKf2 = new KalmanFilter(model2, modelCovariance2, measurementCovariance);

    final UnivariateGaussian prior = new UnivariateGaussian(0d, sigma_y2);
    final UnivariateGaussian s1Likelihood = prior;
    final UnivariateGaussian s2Likelihood = s1Likelihood;
   
    Vector initialClassProbs = VectorFactory.getDefault()
            .copyArray(new double[] { 0.7d, 0.3d });
    Matrix classTransProbs = MatrixFactory.getDefault().copyArray(
                new double[][] { { 0.7d, 0.7d },
                    { 0.3d, 0.3d } });
   
    DlmHiddenMarkovModel dlmHmm = new DlmHiddenMarkovModel(
        Lists.newArrayList(trueKf1, trueKf2),
View Full Code Here


    final long seed = new Random().nextLong();
    final Random random = new Random(seed);
    log.info("seed=" + seed);

    final double trueSigma = Math.pow(0.2d, 2);
    Matrix modelCovariance1 = MatrixFactory.getDefault().copyArray(
        new double[][] {{trueSigma}});
    Matrix modelCovariance2 = MatrixFactory.getDefault().copyArray(
        new double[][] {{trueSigma}});
    Matrix measurementCovariance = MatrixFactory.getDefault().copyArray(
        new double[][] {{trueSigma}});

    List<Vector> truePsis = Lists.newArrayList(
        VectorFactory.getDefault().copyValues(3d, 0.2d),
        VectorFactory.getDefault().copyValues(1d, 0.9d));

    LinearDynamicalSystem model1 = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{truePsis.get(0).getElement(1)}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}})
      );
    LinearDynamicalSystem model2 = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{truePsis.get(1).getElement(1)}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}})
      );
    KalmanFilter trueKf1 = new KalmanFilter(model1, modelCovariance1, measurementCovariance);
    trueKf1.setCurrentInput(VectorFactory.getDefault().copyValues(truePsis.get(0).getElement(0)));
    KalmanFilter trueKf2 = new KalmanFilter(model2, modelCovariance2, measurementCovariance);
    trueKf2.setCurrentInput(VectorFactory.getDefault().copyValues(truePsis.get(1).getElement(0)));
   
    Vector initialClassProbs = VectorFactory.getDefault()
            .copyArray(new double[] { 0.7d, 0.3d });
    Matrix classTransProbs = MatrixFactory.getDefault().copyArray(
                new double[][] { { 0.7d, 0.7d },
                    { 0.3d, 0.3d } });
   
    DlmHiddenMarkovModel trueHmm1 = new DlmHiddenMarkovModel(
        Lists.newArrayList(trueKf1, trueKf2),
        initialClassProbs, classTransProbs);

    final double sigmaPriorMean = Math.pow(0.4, 2);
    final double sigmaPriorShape = 2d;
    final double sigmaPriorScale = sigmaPriorMean*(sigmaPriorShape + 1d);
    final InverseGammaDistribution sigmaPrior = new InverseGammaDistribution(sigmaPriorShape,
        sigmaPriorScale);
   
    final Vector phiMean1 = VectorFactory.getDefault().copyArray(new double[] {
        0d, 0.8d
    });
    final Matrix phiCov1 = MatrixFactory.getDefault().copyArray(new double[][] {
        {2d + 4d * sigmaPriorMean, 0d},
        { 0d, 4d * sigmaPriorMean}
    });
    final MultivariateGaussian phiPrior1 = new MultivariateGaussian(phiMean1, phiCov1);

    final Vector phiMean2 = VectorFactory.getDefault().copyArray(new double[] {
        0d, 0.1d
    });
    final Matrix phiCov2 = MatrixFactory.getDefault().copyArray(new double[][] {
        { 1d + 4d * sigmaPriorMean, 0d},
        { 0d, 4d * sigmaPriorMean}
    });
    final MultivariateGaussian phiPrior2 = new MultivariateGaussian(phiMean2, phiCov2);
   
View Full Code Here

        final Vector alphaTerm = psiSample.subVector(0,
            psiSample.getDimensionality()/2 - 1);
        thisKf.getModel().setState(alphaTerm);
        thisKf.setCurrentInput(alphaTerm);

        final Matrix betaTerm = MatrixFactory.getDefault().createDiagonal(
            psiSample.subVector(
                psiSample.getDimensionality()/2,
                psiSample.getDimensionality() - 1));
        thisKf.getModel().setA(betaTerm);

        final Matrix offsetIdent = MatrixFactory.getDefault().createIdentity(
            psiSample.getDimensionality()/2, psiSample.getDimensionality()/2);
        thisKf.getModel().setB(offsetIdent);

        final Matrix measIdent = Iy.clone();
        thisKf.setMeasurementCovariance(measIdent);

        final Matrix modelIdent = Ix.clone();
        thisKf.setModelCovariance(modelIdent);

        final MultivariateGaussian priorState = thisKf.createInitialLearnedObject();
        final MultivariateGaussian priorStateSmpler = thisKf.createInitialLearnedObject();
        priorStateSmpler.getCovariance().scaleEquals(sigma2Sample);
View Full Code Here

       * they can be done off-line, but we'll do them now.
       * TODO FIXME check that the input/offset thing is working!
       */

      final int xDim = posteriorState.getInputDimensionality();
      final Matrix H = MatrixFactory.getDefault().createMatrix(xDim, xDim * 2);
      H.setSubMatrix(0, 0, Ix);
      H.setSubMatrix(0, xDim,
          // x_{t-1}
          MatrixFactory.getDefault().createDiagonal(predState.getStateSample()));

      final InverseGammaDistribution sigma2SS = predState.getSigma2SS().clone();
      // TODO FIXME matrix inverse!!
      final Matrix postStatePrec = posteriorState.getCovarianceInverse().scale(
          sigma2SS.getShape()/sigma2SS.getScale());
      MultivariateStudentTDistribution postStateMarginal = new MultivariateStudentTDistribution(
          sigma2SS.getShape(),
          posteriorState.getMean(), postStatePrec);
      final Vector postStateSample = postStateMarginal.sample(this.rng);
     
      final Vector psiPriorSmpl = predState.getPsiSample();
      // x_t
      final Vector xHdiff = postStateSample.minus(H.times(psiPriorSmpl));

      /*
       * 1. Update the sigma2 sufficient stats.
       */
      final double newN = sigma2SS.getShape() + 1d;
      final double d = sigma2SS.getScale() + xHdiff.dotProduct(xHdiff);
      sigma2SS.setScale(d);
      sigma2SS.setShape(newN);
     
      /*
       * 2. Update psi sufficient stats. (i.e. offset and AR(1)).
       *
       * Note that we divide out the previous scale param, since
       * we want to update A alone.
       * TODO FIXME inverse!  ewww.
       */
      final Matrix priorAInv = predState.getPsiSS().getCovarianceInverse();
      /*
       * TODO FIXME: we don't have a generalized outer product, so we're only
       * supporting the 1d case for now.
       */
      final Vector Hv = H.convertToVector();
      /*
       * TODO FIXME inverse!  ewww.
       */
      final Matrix postAInv = priorAInv.plus(Hv.outerProduct(Hv)).inverse();
      final Vector postPsiMean = postAInv.times(priorAInv.times(psiPriorSmpl).plus(
          H.transpose().times(postStateSample)));
      final MultivariateGaussian postPsi = predState.getPsiSS().clone();
      postPsi.setMean(postPsiMean);
      postPsi.setCovariance(postAInv);
     
View Full Code Here

      final KalmanFilter kf = transState.getFilter();
      /*
       * Construct the measurement prior predictive likelihood
       */
      final Vector mPriorPredMean = kf.getModel().getC().times(priorPredState.getMean());
      final Matrix mPriorPredCov = kf.getModel().getC().times(priorPredState.getCovariance())
          .times(kf.getModel().getC().transpose())
          .plus(kf.getMeasurementCovariance());
      final MultivariateGaussian mPriorPredDist = new MultivariateGaussian(
          mPriorPredMean, mPriorPredCov);

View Full Code Here

        final Vector offsetTerm = systemSample.subVector(0,
            systemSample.getDimensionality()/2 - 1);
        thisKf.getModel().setState(offsetTerm);
        thisKf.setCurrentInput(offsetTerm);

        final Matrix A = MatrixFactory.getDefault().createDiagonal(
            systemSample.subVector(
                systemSample.getDimensionality()/2,
                systemSample.getDimensionality() - 1));
        thisKf.getModel().setA(A);

        final Matrix offsetIdent = MatrixFactory.getDefault().createIdentity(
            systemSample.getDimensionality()/2, systemSample.getDimensionality()/2);
        thisKf.getModel().setB(offsetIdent);

        final Matrix measIdent = MatrixFactory.getDefault().createIdentity(
            thisKf.getModel().getOutputDimensionality(),
            thisKf.getModel().getOutputDimensionality());
        thisKf.setMeasurementCovariance(measIdent.scale(scaleSample));

        final Matrix modelIdent = MatrixFactory.getDefault().createIdentity(
            thisKf.getModel().getStateDimensionality(),
            thisKf.getModel().getStateDimensionality());
        thisKf.setModelCovariance(modelIdent.scale(scaleSample));

        final MultivariateGaussian priorState = thisKf.createInitialLearnedObject();
        final Vector priorStateSample = priorState.sample(this.rng);

        final GaussianArHpWfParticle particle =
View Full Code Here

       */
      final InverseGammaDistribution scaleSS = predState.getSigma2SS().clone();
      final MultivariateGaussian systemOffsetsSS = predState.getPsiSS().clone();

      final int xDim = posteriorState.getInputDimensionality();
      final Matrix Ij = MatrixFactory.getDefault().createIdentity(xDim, xDim);
      final Matrix H = MatrixFactory.getDefault().createMatrix(xDim, xDim * 2);
      H.setSubMatrix(0, 0, Ij);
      H.setSubMatrix(0, xDim, MatrixFactory.getDefault().createDiagonal(predState.getStateSample()));
      final Vector postStateSample = posteriorState.sample(this.rng);
      final MultivariateGaussian priorPhi = predState.getPsiSS();
      final Vector phiPriorSmpl = priorPhi.sample(this.rng);
      final Vector xHdiff = postStateSample.minus(H.times(phiPriorSmpl));

      final double newN = scaleSS.getShape() + 1d;
      final double d = scaleSS.getScale() + xHdiff.dotProduct(xHdiff);
     
      scaleSS.setScale(d);
      scaleSS.setShape(newN);
     
      // FIXME TODO: crappy sampler
      final double newScaleSmpl = scaleSS.sample(this.rng);
     
      /*
       * Update state and measurement covariances, which
       * have a strict dependency in this model (equality).
       */
      kf.setMeasurementCovariance(MatrixFactory.getDefault().createDiagonal(
          VectorFactory.getDefault().createVector(kf.getModel().getOutputDimensionality(),
              newScaleSmpl)));

      kf.setModelCovariance(MatrixFactory.getDefault().createDiagonal(
          VectorFactory.getDefault().createVector(kf.getModel().getStateDimensionality(),
              newScaleSmpl)));

      /*
       * Update offset and AR(1) prior(s).
       * Note that we divide out the previous scale param, since
       * we want to update A alone.
       */
      final Matrix priorAInv = priorPhi.getCovariance().scale(1d/predState.getSigma2Sample()).inverse();
      /*
       * TODO FIXME: we don't have a generalized outer product, so we're only
       * supporting the 1d case for now.
       */
      final Vector Hv = H.convertToVector();
      final Matrix postAInv = priorAInv.plus(Hv.outerProduct(Hv)).inverse();
      // TODO FIXME: ewww.  inverse.
      final Vector postPhiMean = postAInv.times(priorAInv.times(phiPriorSmpl).plus(
          H.transpose().times(postStateSample)));
      final MultivariateGaussian postPhi = systemOffsetsSS;
      postPhi.setMean(postPhiMean);
      postPhi.setCovariance(postAInv.scale(newScaleSmpl));
     
      final Vector postPhiSmpl = postPhi.sample(this.rng);
      final Matrix smplArTerms = MatrixFactory.getDefault().createDiagonal(
          postPhiSmpl.subVector(
              postPhiSmpl.getDimensionality()/2,
              postPhiSmpl.getDimensionality() - 1));
      kf.getModel().setA(smplArTerms);

View Full Code Here

    final Random random = new Random(seed);
    log.info("seed=" + seed);
    ExtSamplingUtils.log.setLevel(Level.INFO);

    final double trueSigma2 = Math.pow(0.2d, 2);
    Matrix modelCovariance = MatrixFactory.getDefault().copyArray(
        new double[][] {{trueSigma2}});
    Matrix measurementCovariance = MatrixFactory.getDefault().copyArray(
        new double[][] {{trueSigma2}});

    Vector truePsi = VectorFactory.getDefault().copyValues(3d, 0.2d);

    LinearDynamicalSystem dlm = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{truePsi.getElement(1)}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}})
      );
    KalmanFilter trueKf = new KalmanFilter(dlm, modelCovariance, measurementCovariance);
    trueKf.setCurrentInput(VectorFactory.getDefault().copyValues(truePsi.getElement(0)));
   
    final double sigmaPriorMean = Math.pow(0.4, 2);
    final double sigmaPriorShape = 2d;
    final double sigmaPriorScale = sigmaPriorMean*(sigmaPriorShape - 1d);
    final InverseGammaDistribution sigmaPrior = new InverseGammaDistribution(sigmaPriorShape,
        sigmaPriorScale);
   
    final Vector phiMean = VectorFactory.getDefault().copyArray(new double[] {
        0d, 0.8d
    });
    final Matrix phiCov = MatrixFactory.getDefault().copyArray(new double[][] {
        {2d + 4d * sigmaPriorMean, 0d},
        { 0d, 4d * sigmaPriorMean}
    });
    final MultivariateGaussian phiPrior = new MultivariateGaussian(phiMean, phiCov);

View Full Code Here

    final int N = 10000;

    final double[] a = {0.9d, 0.9d};
    final double[] sigma2 = {Math.pow(0.2d, 2), Math.pow(1.2d, 2)};
    final double sigma_y2 = Math.pow(0.3d, 2);
    Matrix modelCovariance1 = MatrixFactory.getDefault().copyArray(
        new double[][] {{Math.pow(0.2d, 2)}});
    Matrix modelCovariance2 = MatrixFactory.getDefault().copyArray(
        new double[][] {{Math.pow(1.2d, 2)}});
    Matrix measurementCovariance = MatrixFactory.getDefault().copyArray(
        new double[][] {{Math.pow(0.3d, 2)}});
    LinearDynamicalSystem model1 = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{0.9d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1}})
      );
    LinearDynamicalSystem model2 = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{0.9d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1}})
      );
    KalmanFilter trueKf1 = new KalmanFilter(model1, modelCovariance1, measurementCovariance);
    KalmanFilter trueKf2 = new KalmanFilter(model2, modelCovariance2, measurementCovariance);

    final UnivariateGaussian prior = new UnivariateGaussian(0d, sigma_y2);
    final UnivariateGaussian s1Likelihood = prior;
    final UnivariateGaussian s2Likelihood = s1Likelihood;
   
    Vector initialClassProbs = VectorFactory.getDefault()
            .copyArray(new double[] { 0.7d, 0.3d });
    Matrix classTransProbs = MatrixFactory.getDefault().copyArray(
                new double[][] { { 0.7d, 0.7d },
                    { 0.3d, 0.3d } });
   
    DlmHiddenMarkovModel dlmHmm = new DlmHiddenMarkovModel(
        Lists.newArrayList(trueKf1, trueKf2),
View Full Code Here

    final long seed = new Random().nextLong();
    final Random random = new Random(seed);
    log.info("seed=" + seed);

    final double trueSigma = Math.pow(0.2d, 2);
    Matrix modelCovariance1 = MatrixFactory.getDefault().copyArray(
        new double[][] {{trueSigma}});
    Matrix modelCovariance2 = MatrixFactory.getDefault().copyArray(
        new double[][] {{trueSigma}});
    Matrix measurementCovariance = MatrixFactory.getDefault().copyArray(
        new double[][] {{trueSigma}});

    List<Vector> truePsis = Lists.newArrayList(
        VectorFactory.getDefault().copyValues(3d, 0.2d),
        VectorFactory.getDefault().copyValues(-1d, 0.9d));

    LinearDynamicalSystem model1 = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{truePsis.get(0).getElement(1)}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}})
      );
    LinearDynamicalSystem model2 = new LinearDynamicalSystem(
        MatrixFactory.getDefault().copyArray(new double[][] {{truePsis.get(1).getElement(1)}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}}),
        MatrixFactory.getDefault().copyArray(new double[][] {{1d}})
      );
    KalmanFilter trueKf1 = new KalmanFilter(model1, modelCovariance1, measurementCovariance);
    trueKf1.setCurrentInput(VectorFactory.getDefault().copyValues(truePsis.get(0).getElement(0)));
    KalmanFilter trueKf2 = new KalmanFilter(model2, modelCovariance2, measurementCovariance);
    trueKf2.setCurrentInput(VectorFactory.getDefault().copyValues(truePsis.get(1).getElement(0)));
   
    Vector initialClassProbs = VectorFactory.getDefault()
            .copyArray(new double[] { 0.5d, 0.5d });
    Matrix classTransProbs = MatrixFactory.getDefault().copyArray(
                new double[][] { { 0.5d, 0.5d },
                    { 0.5d, 0.5d } });
   
    DlmHiddenMarkovModel trueHmm1 = new DlmHiddenMarkovModel(
        Lists.newArrayList(trueKf1, trueKf2),
        initialClassProbs, classTransProbs);

    final double sigmaPriorMean = Math.pow(0.4, 2);
    final double sigmaPriorShape = 2d;
    final double sigmaPriorScale = sigmaPriorMean*(sigmaPriorShape + 1d);
    final InverseGammaDistribution sigmaPrior = new InverseGammaDistribution(sigmaPriorShape,
        sigmaPriorScale);
   
    final Vector phiMean1 = VectorFactory.getDefault().copyArray(new double[] {
        0d, 0.8d
    });
    final Matrix phiCov1 = MatrixFactory.getDefault().copyArray(new double[][] {
        {2d + 4d * sigmaPriorMean, 0d},
        { 0d, 4d * sigmaPriorMean}
    });
    final MultivariateGaussian phiPrior1 = new MultivariateGaussian(phiMean1, phiCov1);

    final Vector phiMean2 = VectorFactory.getDefault().copyArray(new double[] {
        0d, 0.1d
    });
    final Matrix phiCov2 = MatrixFactory.getDefault().copyArray(new double[][] {
        { 1d + 4d * sigmaPriorMean, 0d},
        { 0d, 4d * sigmaPriorMean}
    });
    final MultivariateGaussian phiPrior2 = new MultivariateGaussian(phiMean2, phiCov2);
   
View Full Code Here

TOP

Related Classes of gov.sandia.cognition.math.matrix.Matrix

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.