Package gov.sandia.cognition.math.matrix

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


      GaussianArHpWfParticle prevState, ObservedValue<Vector,?> data) {
    /*
     * Sample psi
     */
    final MultivariateGaussian priorPsi = prevState.getPsiSS();
    final Vector priorPsiSmpl = priorPsi.sample(this.getRandom());

    final KalmanFilter kf = prevState.getFilter().clone();

    /*
     * Update the filter parameters with the new psi.
     */
    final Matrix smplArTerms = MatrixFactory.getDefault().createDiagonal(
        priorPsiSmpl.subVector(
            priorPsiSmpl.getDimensionality()/2,
            priorPsiSmpl.getDimensionality() - 1));
    kf.getModel().setA(smplArTerms);

    final Vector smplOffsetTerm = priorPsiSmpl.subVector(0,
            priorPsiSmpl.getDimensionality()/2 - 1);
    kf.getModel().setState(smplOffsetTerm);
    kf.setCurrentInput(smplOffsetTerm);
 
    /*
 
View Full Code Here


              particle.getComponentPriorPredTotalLogLikelihood());

      final List<MultivariateGaussian> updatedComponentDists =
          (List<MultivariateGaussian>) ObjectUtil.cloneSmartElementsAsArrayList(particle
              .getDistributions());
      Vector updatedCounts;

      if (componentIndex == 0) {
        /*
         * This is the case in which we've sampled a new mixture component, so now we must create
         * it.
         */
        final MultivariateGaussian newComponentDist =
            new MultivariateGaussian(data, MatrixFactory.getDenseDefault().createMatrix(
                data.getDimensionality(), data.getDimensionality()));
        updatedComponentDists.add(newComponentDist);
        updatedCounts =
            VectorFactory.getDenseDefault().copyArray(
                Arrays.copyOf(particle.getCounts().toArray(), particle.getDistributionCount() + 1));
        updatedCounts.setElement(particle.getDistributionCount(), 1d);
      } else {
        /*
         * We've sampled an existing component, so updated the component's (sample) mean and
         * covariance.
         */
        updatedCounts = particle.getCounts().clone();
        final int adjComponentIndex = componentIndex - 1;
        final double oldComponentCount = updatedCounts.getElement(adjComponentIndex);
        final double updatedComponentCount = oldComponentCount + 1d;
        updatedCounts.setElement(adjComponentIndex, updatedComponentCount);

        final MultivariateGaussian sampledComponentDist =
            updatedComponentDists.get(adjComponentIndex);
        final Vector oldComponentMean = sampledComponentDist.getMean();
        final Vector updatedComponentMean =
            oldComponentMean.scale(oldComponentCount).plus(data).scale(1d / updatedComponentCount);
        final Matrix updatedComponentSS =
            sampledComponentDist
                .getCovariance()
                .plus(data.outerProduct(data))
                .plus(oldComponentMean.outerProduct(oldComponentMean).scale(oldComponentCount))
                .minus(
                    updatedComponentMean.outerProduct(updatedComponentMean).scale(
                        updatedComponentCount));

        sampledComponentDist.setCovariance(updatedComponentSS);
        sampledComponentDist.setMean(updatedComponentMean);
      }
View Full Code Here

        final Matrix postStatePrec = particle.getState().getCovarianceInverse().scale(
            sigma2SS.getShape()/sigma2SS.getScale());
        MultivariateStudentTDistribution postStateMarginal = new MultivariateStudentTDistribution(
            sigma2SS.getShape(),
            particle.getState().getMean(), postStatePrec);
        final Vector stateSample = postStateMarginal.sample(this.getRandom());

        final GaussianArHpWfParticle transStateTmp =
            new GaussianArHpWfParticle(particle.getPrevParticle(),
                particle.getFilter(),
                particle.getObs(),
View Full Code Here

        final double componentN = particle.getCounts().getElement(n);
        final double componentPriorPredDof =
            2d * centeringDist.getInverseWishart().getDegreesOfFreedom() + componentN
                - centeringDist.getInputDimensionality() + 1d;
        final Vector componentPriorPredMean =
            centeringDist.getGaussian().getMean().scale(kappa)
                .plus(component.getMean().scale(componentN)).scale(1d / (kappa + componentN));


        final Vector componentCenteringMeanDiff =
            centeringDist.getGaussian().getMean().minus(component.getMean());
        final Matrix componentD =
            component.getCovariance().plus(
                componentCenteringMeanDiff.outerProduct(componentCenteringMeanDiff).scale(
                    kappa * componentN / (kappa + componentN)));

        final Matrix componentPriorPredCovariance =
            centeringDist
                .getInverseWishart()
View Full Code Here

       * and replicate their beta sampling.
       * That would require a change here...
       */
      final double U = this.random.nextDouble();
      double lambdaSum = 0d;
      Vector sampledAugResponse = VectorFactory.getDefault().createVector(this.numCategories);
      for (int i = 0; i < this.numCategories; i++) {
  //        final Vector betaSample = particle.getLinearState().sample(this.random);
        final MultivariateGaussian predictivePrior = particle.getLinearState().clone();
        KalmanFilter kf = particle.getRegressionFilter(i);
        final Matrix G = kf.getModel().getA();
        predictivePrior.setMean(G.times(predictivePrior.getMean()));
        predictivePrior.setCovariance(
            G.times(predictivePrior.getCovariance()).times(G.transpose())
              .plus(kf.getModelCovariance()));
 
        // X * beta
        final double lambda = Math.exp(data.getObservedData().times(
            predictivePrior.getMean()).getElement(0));
        lambdaSum += lambda;
        final double dSampledAugResponse = -Math.log(
            -Math.log(U)/(1d+lambdaSum)
            - (data.getObservedValue().getElement(0) > 0d
                ? 0d : Math.log(this.random.nextDouble())/lambda));
        sampledAugResponse.setElement(i, dSampledAugResponse);
      }

      /*
       * Expand particle set over mixture components
       */
 
View Full Code Here

  private FruehwirthMultiParticle sufficientStatUpdate(
      FruehwirthMultiParticle priorParticle, ObservedValue<Vector, Matrix> data) {
    final FruehwirthMultiParticle updatedParticle = priorParticle.clone();
   
    final Vector sampledAugResponse = priorParticle.getAugResponseSample();
    final KalmanFilter filter = updatedParticle.getRegressionFilter(
        priorParticle.getCategoryId());
    final UnivariateGaussian evComponent = updatedParticle.EVcomponent;
    // TODO we should've already set this, so it might be redundant.
    filter.setMeasurementCovariance(
View Full Code Here

       * current states (beta and the global mean), i.e. a draw from (x_t, x_{t-1} | y_t). FIXME XXX
       * The above isn't currently being done
       */
      final MultivariateGaussian priorBetaSmoothedDist = getSmoothedPriorDist(particle.getPriorBeta(),
        augResponseDist, observation, particle.getPriorPredictiveMean());
      final Vector priorBetaSmoothedSample = priorBetaSmoothedDist.sample(random);
      final MultivariateGaussian postBetaSmoothedDist = getSmoothedPostDist(particle.getPriorBeta(),
        augResponseDist, observation, particle.getPriorPredictiveMean());
      final Vector postBetaSmoothedSample = postBetaSmoothedDist.sample(random);

      final Vector priorGlobalMeanSample = particle.getPriorBeta().getMean();
      final Vector postGlobalMeanSample = particle.getPriorBeta().sample(random);

      /*
       * Perform the actual Gaussian Bayes update. FIXME This is a very poor implementation.
       */
      mvGaussianBayesUpdate(augResponseDist, priorGlobalMeanSample,
          updatedBetaMean, observation.getObservedData());

      final Vector betaMeanError = postBetaSmoothedSample.minus(priorBetaSmoothedSample);
      final ScaledInverseGammaCovDistribution updatedBetaCov = particle.getPriorBetaCov().clone();
      updateCovariancePrior(updatedBetaCov, betaMeanError);
      final Matrix betaCovSmpl = updatedBetaCov.sample(random);
      Preconditions.checkState(betaCovSmpl.getElement(0, 0) >= 0d);
      updatedBetaMean.setCovariance(lambdaSamplesMatrix.times(betaCovSmpl
          .times(updatedBetaMean.getCovariance())));

      /*
       * Now, do the above for the the global mean term.
       */
      final MultivariateGaussian updatedGlobalMean =
          particle.getPriorBeta().times(particle.getAugmentedResponseDistribution());

      mvGaussianBayesUpdate(augResponseDist,
          observation.getObservedData().times(priorBetaSmoothedSample), updatedGlobalMean,
          MatrixFactory.getDenseDefault().createIdentity(
            augResponseDist.getInputDimensionality(), augResponseDist.getInputDimensionality()));

      final Vector globalMeanError = postGlobalMeanSample.minus(priorGlobalMeanSample);
      final ScaledInverseGammaCovDistribution updatedGlobalMeanCov =
          particle.getPriorBetaCov().clone();
      updateCovariancePrior(updatedGlobalMeanCov, globalMeanError);
      final Matrix globalMeanCovSmpl = updatedGlobalMeanCov.sample(random)
          .times(updatedGlobalMean.getCovariance());
View Full Code Here

  private MultivariateGaussian getSmoothedPostDist(MultivariateGaussian postBeta,
                                                   MultivariateGaussian augResponseDist,
                                                   ObservedValue<Vector, Matrix> observation,
                                                   Vector obsMeanAdj) {
    final Matrix C = postBeta.getCovariance();
    final Vector m = postBeta.getMean();
   
    // System design
    final Matrix F = observation.getObservedData();
    final Matrix G = MatrixFactory.getDefault().createIdentity(m.getDimensionality(), m.getDimensionality());
    final Matrix Omega = MatrixFactory.getDefault().createIdentity(m.getDimensionality(), m.getDimensionality());
   
    // Observation suff. stats
    final Matrix Sigma = augResponseDist.getCovariance();
    final Vector y = augResponseDist.getMean().minus(obsMeanAdj);
   
    final Vector a = G.times(m);
    final Matrix R = Omega;
   
    final Matrix W = F.times(Omega).times(F.transpose()).plus(Sigma);
    final Matrix FG = F.times(G);
    final Matrix A = FG.times(R).times(FG.transpose()).plus(W);
    final Matrix Wtil =
        A.transpose().solve(FG.times(R.transpose())).transpose();

    final Vector aSmooth = a.plus(Wtil.times(y.minus(FG.times(a))));
    final Matrix RSmooth =
        R.minus(Wtil.times(A).times(Wtil.transpose()));
   
    return new MultivariateGaussian(aSmooth, RSmooth);
  }
View Full Code Here

  private MultivariateGaussian getSmoothedPriorDist(MultivariateGaussian priorBeta,
                                                    MultivariateGaussian augResponseDist,
                                                    ObservedValue<Vector, Matrix> observation, Vector obsMeanAdj) {
    // Prior suff. stats
    final Matrix C = priorBeta.getCovariance();
    final Vector m = priorBeta.getMean();
   
    // System design
    final Matrix F = observation.getObservedData();
    final Matrix G = MatrixFactory.getDefault().createIdentity(m.getDimensionality(), m.getDimensionality());
    final Matrix Omega = MatrixFactory.getDefault().createIdentity(m.getDimensionality(), m.getDimensionality());
   
    // Observation suff. stats
    final Matrix Sigma = augResponseDist.getCovariance();
    final Vector y = augResponseDist.getMean().minus(obsMeanAdj);
   
    final Matrix W = F.times(Omega).times(F.transpose()).plus(Sigma);
    final Matrix FG = F.times(G);
    final Matrix A = FG.times(C).times(FG.transpose()).plus(W);
    final Matrix Wtil =
        A.transpose().solve(FG.times(C.transpose())).transpose();

    final Vector mSmooth = m.plus(Wtil.times(y.minus(FG.times(m))));
    final Matrix CSmooth =
        C.minus(Wtil.times(A).times(Wtil.transpose()));
    return new MultivariateGaussian(mSmooth, CSmooth);
  }
View Full Code Here

   * @param design
   * @return
   */
  private void mvGaussianBayesUpdate(MultivariateGaussian obsDist,
      Vector obsMeanComponentAdjustment, MultivariateGaussian prior, Matrix X) {
    final Vector m1 = prior.getMean();
    final Matrix c1inv = prior.getCovarianceInverse();

    final Vector m2 = obsDist.getMean().plus(obsMeanComponentAdjustment);
    final Matrix Xt = X.transpose();
    final Matrix c2inv = Xt.times(obsDist.getCovarianceInverse()).times(X);

    final Matrix Cinv = c1inv.plus(c2inv);
    final Matrix C = Cinv.inverse();

    final Vector m = C.times(c1inv.times(m1).plus(
      Xt.times(obsDist.getCovarianceInverse()).times(m2)));

    prior.setMean(m);
    prior.setCovariance(C);
  }
View Full Code Here

TOP

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

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.