Package gov.sandia.cognition.statistics.bayesian

Examples of gov.sandia.cognition.statistics.bayesian.KalmanFilter


    public GaussianArHpTransitionState update(
      GaussianArHpTransitionState predState) {

      final MultivariateGaussian posteriorState = predState.getState().clone();
      final DlmHiddenMarkovModel newHmm = predState.getHmm().clone();
      KalmanFilter kf = Iterables.get(newHmm.getStateFilters(),
          predState.getClassId());
      kf.update(posteriorState, predState.getObservation().getObservedValue());


      /*
       * The following are the parameter learning updates;
       * they can be done off-line, but we'll do them now.
       * TODO FIXME check that the input/offset thing is working!
       */
      final InverseGammaDistribution invScaleSS = predState.getInvScaleSS().clone();
      final List<MultivariateGaussian> systemOffsetsSS =
          ObjectUtil.cloneSmartElementsAsArrayList(predState.getPsiSS());

      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().get(predState.getClassId());
      final Vector phiPriorSmpl = priorPhi.sample(this.rng);
      final Vector xHdiff = postStateSample.minus(H.times(phiPriorSmpl));

      final double newN = invScaleSS.getShape() + 1d;
      final double d = invScaleSS.getScale() + xHdiff.dotProduct(xHdiff);
     
      invScaleSS.setScale(d);
      invScaleSS.setShape(newN);
     
      // FIXME TODO: crappy sampler
      final double newInvScaleSmpl = invScaleSS.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(),
              newInvScaleSmpl)));

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

      /*
       * Update offset and AR(1) prior(s).
       * Note that we divide out the previous inv scale param, since
       * we want to update A alone.
       */
      final Matrix priorAInv = priorPhi.getCovariance().scale(1d/predState.getInvScaleSample()).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.get(predState.getClassId());
      postPhi.setMean(postPhiMean);
      postPhi.setCovariance(postAInv.scale(newInvScaleSmpl));
     
      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);

      final Vector smplOffsetTerm = postPhiSmpl.subVector(0,
              postPhiSmpl.getDimensionality()/2 - 1);
      kf.getModel().setState(smplOffsetTerm);
      kf.setCurrentInput(smplOffsetTerm);
 
      final GaussianArHpTransitionState postState =
          new GaussianArHpTransitionState(newHmm,
              predState.getClassId(), predState.getObservation(),
              posteriorState, postStateSample, invScaleSS, systemOffsetsSS, newInvScaleSmpl);
View Full Code Here


      final DataDistribution<LogitMixParticle> initialParticles =
          CountedDataDistribution.create(true);
      for (int i = 0; i < numParticles; i++) {
       
        final MultivariateGaussian initialPriorState = initialPrior.clone();
        final KalmanFilter kf = this.initialFilter.clone();
        final int componentId = this.rng.nextInt(10);
        final UnivariateGaussian evDist = this.evDistribution.
            getDistributions().get(componentId);
       
        final LogitMixParticle particleMvgDPDist =
View Full Code Here

  @Override
  public GaussianEnsParticle clone() {
    GaussianEnsParticle clone = (GaussianEnsParticle) super.clone();
    clone.kf =
        new KalmanFilter(
            new LinearDynamicalSystem(
                this.kf.getModel().getA(),
                this.kf.getModel().getB(),
                this.kf.getModel().getC()),
            this.kf.getModelCovariance(),
View Full Code Here

        int numParticles) {
      final CountedDataDistribution<GaussianEnsParticle> initialParticles =
          CountedDataDistribution.create(numParticles, true);
      for (int i = 0; i < numParticles; i++) {

        final KalmanFilter thisKf = this.initialKf.clone();

        final GaussianEnsParticle particle =
            null;
//            new GaussianEnsParticle(thisKf,
//                ObservedValue.<Vector>create(0, null)
View Full Code Here

    clone.EVcomponent = this.EVcomponent;
    clone.previousParticle = this.previousParticle;
    // when do we ever need a deep copy?  we don't alter
    // the components of a kalman filter in place...
    clone.regressionFilter =
        new KalmanFilter(
            new LinearDynamicalSystem(
                this.regressionFilter.getModel().getA(),
                this.regressionFilter.getModel().getB(),
                this.regressionFilter.getModel().getC()),
            this.regressionFilter.getModelCovariance(),
View Full Code Here

      Matrix F, Matrix G, Matrix  modelCovariance,
      Random rng) {
    Preconditions.checkArgument(F.getNumRows() == 1);
    Preconditions.checkArgument(F.getNumColumns() == G.getNumRows());
    Preconditions.checkArgument(G.getNumColumns() == modelCovariance.getNumRows());
    this.initialFilter = new KalmanFilter(
            new LinearDynamicalSystem(
                G,
                MatrixFactory.getDefault().createMatrix(G.getNumRows(), G.getNumColumns()),
                F),
            modelCovariance,
View Full Code Here

  private LogitMixParticle sufficientStatUpdate(
      LogitMixParticle priorParticle, ObservedValue<Vector, Matrix> data) {

    final LogitMixParticle updatedParticle = priorParticle.clone();
    final KalmanFilter filter = updatedParticle.getRegressionFilter();

    final UnivariateGaussian evComponent = updatedParticle.EVcomponent;

    final boolean isOne = !data.getObservedValue().isZero();

    final int smplLowerIdx = DiscreteSamplingUtil.sampleIndexFromProportions(
        getRandom(), updatedParticle.getComponentLikelihoods());
//    final int smplLowerIdx = DiscreteSamplingUtil.sampleIndexFromProbabilities(
//        getRandom(), this.evDistribution.getPriorWeights());

    final UnivariateGaussian partComponent =
        this.evDistribution.getDistributions().get(smplLowerIdx);

    final double dsampledAugResponse = sampleAugResponse(
        updatedParticle.getPriorPredMean(),
        updatedParticle.getPriorPredCov(), isOne,
        partComponent);

    // TODO we should've already set this, so it might be redundant.
    filter.setMeasurementCovariance(
        MatrixFactory.getDefault().copyArray(new double[][] {{
          evComponent.getVariance() + partComponent.getVariance()}}));

    final Vector sampledAugResponse =
        VectorFactory.getDefault().copyValues(
            dsampledAugResponse
            - evComponent.getMean().doubleValue()
            - partComponent.getMean().doubleValue());

    updatedParticle.setAugResponseSample(sampledAugResponse);

    final MultivariateGaussian posteriorState = updatedParticle.getLinearState().clone();
    filter.update(posteriorState, sampledAugResponse);

    updatedParticle.setLinearState(posteriorState);
   
    return updatedParticle;
  }
View Full Code Here

      final DataDistribution<LogitMixParticle> initialParticles =
          CountedDataDistribution.create(true);
      for (int i = 0; i < numParticles; i++) {
       
        final MultivariateGaussian initialPriorState = initialPrior.clone();
        final KalmanFilter kf = this.initialFilter.clone();
        final int componentId = this.rng.nextInt(10);
        final UnivariateGaussian evDist = this.evDistribution.
            getDistributions().get(componentId);
       
        final LogitMixParticle particleMvgDPDist =
View Full Code Here

      double sourceTotalLogLikelihood = Double.NEGATIVE_INFINITY;

      final LogitMixParticle particle = particleEntry.getKey();

      final MultivariateGaussian predictivePrior = particle.getLinearState().clone();
      KalmanFilter kf = particle.getRegressionFilter();
      final Matrix G = kf.getModel().getA();
      final Matrix F = data.getObservedData();
      predictivePrior.setMean(G.times(predictivePrior.getMean()));
      predictivePrior.setCovariance(
          G.times(predictivePrior.getCovariance()).times(G.transpose())
            .plus(kf.getModelCovariance()));
      final Vector betaMean = predictivePrior.getMean();

      final int particleCount;
      if (particleEntry.getValue() instanceof MutableDoubleCount) {
        particleCount = ((MutableDoubleCount)particleEntry.getValue()).count;
View Full Code Here

    @Override
    public GaussianArHpWfParticle update(
      GaussianArHpWfParticle predState) {

      final MultivariateGaussian posteriorState = predState.getState().clone();
      final KalmanFilter kf = predState.getFilter().clone();
      kf.update(posteriorState, predState.getObservation().getObservedValue());

      /*
       * The following are the parameter learning updates;
       * they can be done off-line, but we'll do them now.
       * TODO FIXME check that the input/offset thing is working!
       */
      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);

      final Vector smplOffsetTerm = postPhiSmpl.subVector(0,
              postPhiSmpl.getDimensionality()/2 - 1);
      kf.getModel().setState(smplOffsetTerm);
      kf.setCurrentInput(smplOffsetTerm);
 
      final GaussianArHpWfParticle postState =
          new GaussianArHpWfParticle(kf, predState.getObservation(),
              posteriorState, postStateSample, scaleSS, systemOffsetsSS, newScaleSmpl, null);

View Full Code Here

TOP

Related Classes of gov.sandia.cognition.statistics.bayesian.KalmanFilter

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.