Package gov.sandia.cognition.math.matrix

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


      /*
       * First, we create a Gaussian distribution out of the augmented response Note: we're only
       * working in 1D right now (a single binary response), however, we'll want to move to multiple
       * binary response next. that's why matrices and vectors are being used here.
       */
      final Vector y = observation.getObservedValue();
      final Vector invOmegaSamples = VectorFactory.getDenseDefault().createVector(1);
      final Vector z = VectorFactory.getDenseDefault().createVector(1);
      /*
       * Now, we compute the predictive log odds, so that we can evaluate the likelihood.
       */
      final Matrix x = observation.getObservedData();
      final Vector phi = x.times(particle.getPriorBeta().getMean());
      for (int i = 0; i < y.getDimensionality(); i++) {
        final double omega = PolyaGammaLogitDistribution.sample(
          phi.getElement(i), rng);
        invOmegaSamples.setElement(i, 1d / omega);
        z.setElement(i, (y.getElement(i) - 0.5d) / omega);
      }
      final Matrix zCov = MatrixFactory.getDenseDefault().createDiagonal(invOmegaSamples);

View Full Code Here


      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 double k_t = data.getObservedValue().getElement(0) - 1d/2d;

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

    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}
 
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() + partComponent.getVariance()}}));

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

      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;
      } else {
View Full Code Here

  public static void main(String[] args) {


    final double trueGlobalMean = 30d;
    final Vector trueBetas =
        VectorFactory.getDenseDefault().copyArray(new double[] {0d});

    final Random rng = new Random(829351983l);
    /*
     * Sample test data from a binomial with log odds defined using the above parameters. Also, we
     * produce predictor values by generating the randomly.
     */
//    final MultivariateGaussian dataGeneratingDist =
//        new MultivariateGaussian(VectorFactory.getDenseDefault().copyArray(
//            new double[] {100d, -50d, 34d, 0d, 1e-3d}), MatrixFactory.getDenseDefault()
//            .createDiagonal(
//                VectorFactory.getDenseDefault().copyArray(new double[] {100d, 10d, 500d, 30d, 1d})));
    final List<ObservedValue<Vector, Matrix>> observations = Lists.newArrayList();
    for (int i = 0; i < 10000; i++) {
//      final Vector dataSample = dataGeneratingDist.sample(rng);
      final Vector dataSample = VectorFactory.getDenseDefault().copyArray(new
        double[] {1d});
      final double phi = Math.exp(-trueGlobalMean - dataSample.dotProduct(trueBetas));
      final double pi = 1d / (1d + phi);
      final Vector y = VectorFactory.getDenseDefault().createVector1D(rng.nextDouble() <= pi ? 1d : 0d);
      final Matrix dataDesign = MatrixFactory.getDenseDefault().copyRowVectors(dataSample);
      observations.add(ObservedValue.create(i, y, dataDesign));
    }

    /*
     * Instantiate PL filter by first providing prior parameters/distributions.
     */

    final Vector betaCovPriorMean = VectorFactory.getDefault().copyArray(
      new double[] {200d});
    final double betaPriorCovDof = 2 + betaCovPriorMean.getDimensionality();
    final ScaledInverseGammaCovDistribution priorBetaCov =
        new ScaledInverseGammaCovDistribution(betaCovPriorMean.getDimensionality()
          betaCovPriorMean.scale(betaPriorCovDof - 1d).getElement(0),
          betaPriorCovDof);
    final MultivariateGaussian priorBeta =
        new MultivariateGaussian(VectorFactory.getDenseDefault().copyArray(
            new double[] {0d}), priorBetaCov.getMean());

View Full Code Here

       */
      double rmseMean = 0d;

      for (Entry<LogitMixParticle, ? extends Number> particleEntry :
        currentMixtureDistribution.asMap().entrySet()) {
        final Vector trueState = dlmSamples.get(i).getTrueState();
        final Vector particleState = particleEntry.getKey().getLinearState().getMean();
        final double rse = Math.sqrt(trueState.minus(particleState).norm2());
//        System.out.println("true: " + trueState + ", particle:" + particleState);

        rmseMean += rse * Math.exp(particleEntry.getValue().doubleValue());
      }
View Full Code Here

  @Override
  public List<SimHmmObservedValue<Vector, Vector>> sample(Random random, int T) {
    List<SimHmmObservedValue<Vector, Vector>> results = Lists.newArrayList();

    int currentClass = DiscreteSamplingUtil.sampleIndexFromProbabilities(random, this.marginalClassProbs);
    Vector currentState = null;
    for (int i = 0; i < T; i++) {

      Vector classProbs = (currentState == null) ? this.marginalClassProbs :
        this.classTransProbs.getColumn(currentClass);

      currentClass = DiscreteSamplingUtil.sampleIndexFromProbabilities(random, classProbs);

      KalmanFilter currentFilter = this.stateFilters.get(currentClass);
      if (currentState == null) {
        currentState = currentFilter.createInitialLearnedObject().sample(random);
      } else {
        final Matrix G = currentFilter.getModel().getA();
        Matrix modelCovSqrt = CholeskyDecompositionMTJ.create(
            (DenseMatrix) currentFilter.getModelCovariance()).getR();
        currentState = MultivariateGaussian.sample(G.times(currentState), modelCovSqrt, random);
      }
      currentState.plusEquals(currentFilter.getModel().getB().times(
          currentFilter.getCurrentInput()));

      final Matrix F = currentFilter.getModel().getC();
      Vector observationMean = F.times(currentState);
      Matrix measurementCovSqrt = CholeskyDecompositionMTJ.create(
          (DenseMatrix) currentFilter.getMeasurementCovariance()).getR();
      Vector observation = MultivariateGaussian.sample(observationMean,
          measurementCovSqrt, random);
 
      results.add(new SimHmmObservedValue<Vector, Vector>(i, currentClass,
          currentState.clone(), observation));
    }
View Full Code Here

          hmm.computeForwardProbabilities(obsLikelihoodSequence, true);
 
      final List<Vector> jointProbs = Lists.newArrayList();
      for (int i = 0; i < forwardProbabilities.size() - 1; i++) {
        final WeightedValue<Vector> input = forwardProbabilities.get(i);
        final Vector prod =
            hmm.getTransitionProbability().times(input.getValue());
        jointProbs.add(prod.scale(1d / prod.norm1()));
      }
      jointProbs
          .add(Iterables.getLast(forwardProbabilities).getValue());
     
      return jointProbs;
View Full Code Here

  }

  @Override
  public MultivariateGaussian getEmissionFunction(MultivariateGaussian state, int classId) {
    KalmanFilter kf = this.stateFilters.get(classId);
    final Vector mean = kf.getModel().getC().times(state.getMean());
    final Matrix cov = kf.getModel().getC().times(state.getCovariance())
        .times(kf.getModel().getC().transpose())
        .plus(kf.getMeasurementCovariance());
    final MultivariateGaussian likelihood = new MultivariateGaussian(
        mean, cov);
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.