Package gov.sandia.cognition.statistics.distribution

Examples of gov.sandia.cognition.statistics.distribution.MultivariateGaussian


              {0d, 0d},
              {0d, 0d}}),
          MatrixFactory.getDefault().copyArray(new double[][] {{0d}})   
        );
   
    final MultivariateGaussian trueInitialPrior = new MultivariateGaussian(
        VectorFactory.getDefault().copyValues(-0.7d, 0.9d),
        MatrixFactory.getDefault().copyArray(new double[][] {
            {1d, 0d},
            {0d, 1d}}));
    final int N = 100;
    final List<ObservedValue<Vector, Matrix>> observations = Lists.newArrayList();

    List<SimObservedValue<Vector, Matrix, Vector>> dlmSamples = DlmUtils.sampleDlm(
        rng, N, trueInitialPrior, initialFilter);
    for (SimObservedValue<Vector, Matrix, Vector> samplePair : dlmSamples) {
      final double ev1Upper = -Math.log(-Math.log(rng.nextDouble()));
      final double upperUtility = samplePair.getObservedValue().getElement(0) +
          ev1Upper;
      final double lowerUtility = -Math.log(-Math.log(rng.nextDouble()));
      final double obs = (upperUtility > lowerUtility) ? 1d : 0d;
      observations.add(
          SimObservedValue.<Vector, Matrix, LogitTrueState>create(
              VectorFactory.getDefault().copyValues(obs),
              samplePair.getObservedData(),
              new LogitTrueState(samplePair.getTrueState(), upperUtility, ev1Upper, lowerUtility)));
    }

    /*
     * Create and initialize the PL filter
     */
    final MultivariateGaussian initialPrior = new MultivariateGaussian(
        VectorFactory.getDefault().copyValues(0d, 0d),
        MatrixFactory.getDefault().copyArray(new double[][] {
            {10d, 0d},
            {0d, 10d}}));
    final Matrix F = MatrixFactory.getDefault().copyArray(new double[][] {
View Full Code Here


          MatrixFactory.getDefault().copyArray(new double[][] {
              {0d}}),
          MatrixFactory.getDefault().copyArray(new double[][] {{0d}})   
        );
   
    final MultivariateGaussian trueInitialPrior = new MultivariateGaussian(
        VectorFactory.getDefault().copyValues(0.9d),
        MatrixFactory.getDefault().copyArray(new double[][] {
            {0d}}));
    final int N = 2000;
    final List<ObservedValue<Vector, Matrix>> observations = Lists.newArrayList();

    List<SimObservedValue<Vector, Matrix, Vector>> dlmSamples = DlmUtils.sampleDlm(
        rng, N, trueInitialPrior, initialFilter,
        new Function<Matrix, Matrix>() {
          @Override
          public Matrix apply(Matrix input) {
            final double curVal;
            if (input == null)
              curVal = -1d;
            else
              curVal = input.getElement(0, 0);
            return MatrixFactory.getDefault().copyArray(
                new double[][] {{
                curVal + 2d/N
                }});
          }
        });
    for (SimObservedValue<Vector, Matrix, Vector> samplePair : dlmSamples) {
      final double ev1Upper = -Math.log(-Math.log(rng.nextDouble()));
      final double upperUtility = samplePair.getObservedValue().getElement(0) +
          ev1Upper;
      final double lowerUtility = -Math.log(-Math.log(rng.nextDouble()));
      final double obs = (upperUtility > lowerUtility) ? 1d : 0d;
      observations.add(
          SimObservedValue.<Vector, Matrix, LogitTrueState>create(
              VectorFactory.getDefault().copyValues(obs),
              samplePair.getObservedData(),
              new LogitTrueState(samplePair.getTrueState(), upperUtility, ev1Upper, lowerUtility)));
    }

    /*
     * Create and initialize the PL filter
     */
    final MultivariateGaussian initialPrior = new MultivariateGaussian(
        VectorFactory.getDefault().copyValues(0d),
        MatrixFactory.getDefault().copyArray(new double[][] {
            {100d}}));
    final Matrix F = MatrixFactory.getDefault().copyArray(new double[][] {
        {1d}});
View Full Code Here

          MatrixFactory.getDefault().copyArray(new double[][] {
              {0d}}),
          MatrixFactory.getDefault().copyArray(new double[][] {{0d}})   
        );
   
    final MultivariateGaussian trueInitialPrior = new MultivariateGaussian(
        VectorFactory.getDefault().copyValues(3.7d),
        MatrixFactory.getDefault().copyArray(new double[][] {
            {0d}}));
    final int T = 3000;
    final List<ObservedValue<Vector, Matrix>> observations = Lists.newArrayList();

    List<SimObservedValue<Vector, Matrix, Vector>> dlmSamples = DlmUtils.sampleDlm(
        rng, T, trueInitialPrior, initialFilter);
    int t = 0;
    for (SimObservedValue<Vector, Matrix, Vector> samplePair : dlmSamples) {
      final double ev1Upper = -Math.log(-Math.log(rng.nextDouble()));
      final double upperUtility = samplePair.getObservedValue().getElement(0) +
          ev1Upper;
      final double lowerUtility = -Math.log(-Math.log(rng.nextDouble()));
      final double obs = (upperUtility > lowerUtility) ? 1d : 0d;
      observations.add(
          SimObservedValue.<Vector, Matrix, LogitTrueState>create(
              t++,
              VectorFactory.getDefault().copyValues(obs),
              samplePair.getObservedData(),
              new LogitTrueState(samplePair.getTrueState(), upperUtility, ev1Upper, lowerUtility)));
    }

    /*
     * Create and initialize the PL filter
     */
    final MultivariateGaussian initialPrior = new MultivariateGaussian(
        VectorFactory.getDefault().copyValues(0d),
        MatrixFactory.getDefault().copyArray(new double[][] {
            {1000d}}));
    final Matrix F = MatrixFactory.getDefault().copyArray(new double[][] {
        {1d}});
View Full Code Here

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

      final MvGaussianDPDistribution updatedParticle =
          new MvGaussianDPDistribution(updatedComponentDists, particle.getCenteringDistribution(),
              particle.getAlpha(), updatedCounts);
View Full Code Here

  protected GaussianArHpWfParticle propagate(
      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);
 
    /*
     * Perform the Kalman update to get the posterior state suff. stats.
     */
    final InverseGammaDistribution priorSigma2 = prevState.getSigma2SS().clone();
    final double sigma2Sample = priorSigma2.sample(this.getRandom());
    MultivariateGaussian posteriorState = prevState.getState().clone();
    // TODO FIXME gross hack!
//    posteriorState.getCovariance().scaleEquals(sigma2Sample);
//    kf.setMeasurementCovariance(Iy.scale(sigma2Sample));
//    kf.setModelCovariance(Ix.scale(sigma2Sample));
    kf.predict(posteriorState);
View Full Code 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));
View Full Code Here

                this.regressionFilter.getModel().getB(),
                this.regressionFilter.getModel().getC()),
            this.regressionFilter.getModelCovariance(),
            this.regressionFilter.getMeasurementCovariance());
    // same here
    clone.linearState = new MultivariateGaussian(
        this.linearState.getMean(), this.linearState.getCovariance());
    clone.augResponseSample = this.augResponseSample;
    clone.priorPredMean = this.priorPredMean;
    clone.priorPredCov = this.priorPredCov;
    clone.compLikelihoods = this.compLikelihoods;
View Full Code Here

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

    final MultivariateGaussian posteriorState = updatedParticle.getLinearState();
    filter.update(posteriorState, sampledAugResponse);
   
    return updatedParticle;
  }
View Full Code Here

      /*
       * TODO when using FS aug sampling, we should probably go all the way
       * and replicate their beta sampling.
       * That would require a change here...
       */
      final MultivariateGaussian predictivePrior = particle.getLinearState().clone();
      KalmanFilter kf = particle.getRegressionFilter(particle.getCategoryId());
      final Matrix G = kf.getModel().getA();
      predictivePrior.setMean(G.times(predictivePrior.getMean()));
      predictivePrior.setCovariance(
          G.times(predictivePrior.getCovariance()).times(G.transpose())
            .plus(kf.getModelCovariance()));
      final Matrix F = kf.getModel().getC();

      final UnivariateGaussian evComponent = particle.getEVcomponent();
      final double predPriorObsMean = F.times(predictivePrior.getMean()).getElement(0)
          + evComponent.getMean();
      final double predPriorObsCov = F.times(predictivePrior.getCovariance()).times(F.transpose())
          .plus(kf.getMeasurementCovariance()).getElement(0, 0);
      particle.setPriorPredMean(predPriorObsMean);
      particle.setPriorPredCov(predPriorObsCov);

      double logLikelihood = UnivariateGaussian.PDF.logEvaluate(
View Full Code Here

     * Propagate
     */
    final DataDistribution<PolyaGammaLogitDistribution> updatedDist =
        new DefaultDataDistribution<PolyaGammaLogitDistribution>();
    for (final PolyaGammaLogitDistribution particle : resampledParticles) {
      final MultivariateGaussian augResponseDist = particle.getAugmentedResponseDistribution();

      final MultivariateGaussian updatedBetaMean = particle.getPriorBeta().clone();

      final List<Double> lambdaSamples =
          new ExponentialDistribution(2).sample(random, updatedBetaMean.getInputDimensionality());
      final Matrix lambdaSamplesMatrix =
          MatrixFactory.getDenseDefault().createDiagonal(
              VectorFactory.getDenseDefault().copyValues(lambdaSamples));
     
      /*
       * To perform the following updates, we need smoothed joint samples of the previous and
       * 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());
      Preconditions.checkState(globalMeanCovSmpl.getElement(0, 0) > 0d);
      updatedGlobalMean.setCovariance(globalMeanCovSmpl);

      final PolyaGammaLogitDistribution updatedParticle =
          new PolyaGammaLogitDistribution(updatedGlobalMean, updatedGlobalMeanCov);

      updatedDist.increment(updatedParticle);
View Full Code Here

TOP

Related Classes of gov.sandia.cognition.statistics.distribution.MultivariateGaussian

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.