Package gov.sandia.cognition.math.matrix

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


  public <N, H extends GenericHMM<N,?,?>, T extends HmmTransitionState<N, H>> void evaluate(
      int replication, SimHmmObservedValue<Vector, Vector> obs,
      DataDistribution<T> distribution) {

    final Vector trueState = obs.getState();
    RingAccumulator<Vector> stateMean = new RingAccumulator<Vector>();
    // rediculous hack to get around java bug
    for (T particle : distribution.getDomain()) {
      final double particleWeight = distribution.getFraction(particle);
      Object tmpParticle = particle;
View Full Code Here


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

    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);
   
    List<MultivariateGaussian> priorPhis = Lists.newArrayList(phiPrior1, phiPrior2);

    final HmmPlFilter<DlmHiddenMarkovModel, GaussianArHpTransitionState, Vector> wfFilter =
        new GaussianArHpHmmPLFilter(trueHmm1, sigmaPrior, priorPhis, random, true);


    final String path;
    if (args.length == 0)
      path = ".";
    else
      path = args[0];
    String outputFilename = path + "/hmm-nar-wf-rs-10000-class-errors-m1.csv";

    final int K = 5;
    final int T = 700;
    final int N = 1000;

    /*
     * Note: replications are over the same set of simulated observations.
     */
    List<SimHmmObservedValue<Vector, Vector>> simulation = trueHmm1.sample(random, T);

    wfFilter.setNumParticles(N);
    wfFilter.setResampleOnly(false);

    CSVWriter writer = new CSVWriter(new FileWriter(outputFilename), ',');
    String[] header = "rep,t,filter.type,measurement.type,resample.type,measurement".split(",");
    writer.writeNext(header);

    GaussianArHmmClassEvaluator wfClassEvaluator = new GaussianArHmmClassEvaluator("wf-pl",
        writer);
    GaussianArHmmRmseEvaluator wfRmseEvaluator = new GaussianArHmmRmseEvaluator("wf-pl",
        writer);
    GaussianArHmmPsiLearningEvaluator wfPsiEvaluator = new GaussianArHmmPsiLearningEvaluator("wf-pl",
        truePsis, writer);

    RingAccumulator<MutableDouble> wfLatency =
        new RingAccumulator<MutableDouble>();
    Stopwatch wfWatch = new Stopwatch();


    for (int k = 0; k < K; k++) {
      log.info("Processing replication " + k);
      CountedDataDistribution<GaussianArHpTransitionState> wfDistribution =
          (CountedDataDistribution<GaussianArHpTransitionState>) wfFilter.getUpdater().createInitialParticles(N);


      final long numPreRuns = -1l;//wfDistribution.getMaxValueKey().getTime();
     
      /*
       * Recurse through the particle filter
       */
      for (int i = 0; i < T; i++) {
 
        final double x = simulation.get(i).getClassId();
        final Vector y = simulation.get(i).getObservedValue();

        if (i > numPreRuns) {

          if (i > 0) {
            wfWatch.reset();
View Full Code Here

      /*
       * Construct the measurement prior predictive likelihood
       * t_n (H*m^psi, d*C^psi/n)
       */
      final Vector mPriorPredMean = H.times(priorPsi.getMean());
      final Matrix mPriorPredCov = H.times(priorPsi.getCovariance()).times(H.transpose())
          .plus(Iy.scale(2d));

      // TODO FIXME
      final Matrix stPriorPredPrec = mPriorPredCov.inverse().scale(
View Full Code Here

        final KalmanFilter thisKf = this.initialKf.clone();
        final MultivariateGaussian thisPsiPrior = initialPriorPsi.clone();
        // TODO FIXME use t-distribution
        final MultivariateGaussian thisPsiPriorSmpler = thisPsiPrior.clone();
        thisPsiPriorSmpler.getCovariance().scaleEquals(sigma2Sample);
        final Vector psiSample = thisPsiPriorSmpler.sample(this.rng);

        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);
        final Vector priorStateSample = priorStateSmpler.sample(this.rng);


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

      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

   
            /*
             * Compute and output RS forward errors
             */
            ResampleType rsResampleType = rsDistribution.getMaxValueKey().getResampleType();
            Vector rsStateProbDiffs = computeStateDiffs(i, hmm.getNumStates(), rsDistribution, forwardResults);
            String[] rsLine = {Integer.toString(k), Integer.toString(i), "p(x_t=0|y^t)",
               rsResampleType.toString(),
               Double.toString(rsStateProbDiffs.getElement(0))};
            writer.writeNext(rsLine);
            log.info("rsStateProbDiffs=" + rsStateProbDiffs);
   
     
            if (i > numPreRuns) {
              wfFilter.update(wfDistribution, obsState);
       
              RingAccumulator<MutableDouble> pfAtTRate = new RingAccumulator<MutableDouble>();
              for (P state : wfDistribution.getDomain()) {
                final double err = (x == state.getClassId()) ? wfDistribution.getFraction(state) : 0d;
                pfAtTRate.accumulate(new MutableDouble(err));
              }
              pfRunningRate.accumulate(new MutableDouble(pfAtTRate.getSum()));
       
              ResampleType wfResampleType = wfDistribution.getMaxValueKey().getResampleType();
              Vector wfStateProbDiffs = computeStateDiffs(i, hmm.getNumStates(), wfDistribution, forwardResults);
              String[] wfLine = {Integer.toString(k), Integer.toString(i), "p(x_t=0|y^t)", "water-filling",
                 wfResampleType.toString(),
                 Double.toString(wfStateProbDiffs.getElement(0))};
              writer.writeNext(wfLine);
              log.info("wfStateProbDiffs=" + wfStateProbDiffs);
   
            }
     
          }
   
          log.info("viterbiRate:" + viterbiRate.getMean());
          log.info("pfRunningRate:" + pfRunningRate.getMean());
     
    //      RingAccumulator<MutableDouble> pfRate2 = new RingAccumulator<MutableDouble>();
    //      for (HMMTransitionState<Integer> state : distribution.getDomain()) {
    //        final double chainLogLikelihood = distribution.getLogFraction(state);
    //        RingAccumulator<MutableDouble> pfAtTimeRate = new RingAccumulator<MutableDouble>();
    //        for (int i = 0; i < T; i++) {
    //          final double x = DoubleMath.roundToInt(sample.getSecond().get(i), RoundingMode.HALF_EVEN);
    //          final double err;
    //          if (i < T - 1) {
    //            final WeightedValue<Integer> weighedState = state.getStateHistory().get(i);
    //            err = (x == weighedState.getValue() ? 1d : 0d);
    //          } else {
    //            err = (x == state.getState() ? 1d : 0d);
    //          }
    //          pfAtTimeRate.accumulate(new MutableDouble(err));
    //        }
    //        pfRate2.accumulate(new MutableDouble(pfAtTimeRate.getMean().doubleValue()
    //            * Math.exp(chainLogLikelihood)));
    //      }
    //      log.info("pfChainRate:" + pfRate2.getSum());
   
          /*
           * Loop through the smoothed trajectories and compute the
           * class probabilities for each state.
           */
          for (int t = 0; t < T; t++) {
   
            CountedDataDistribution<Integer> wfStateSums = new CountedDataDistribution<Integer>(true);
            CountedDataDistribution<Integer> rsStateSums = new CountedDataDistribution<Integer>(true);
            if (t < T - 1) {
              for (HmmTransitionState<T, H> state : wfDistribution.getDomain()) {
                final WeightedValue<Integer> weighedState = state.getStateHistory().get(t);
                wfStateSums.increment(weighedState.getValue(), weighedState.getWeight());
              }
              for (HmmTransitionState<T, H> state : rsDistribution.getDomain()) {
                final WeightedValue<Integer> weighedState = state.getStateHistory().get(t);
                rsStateSums.increment(weighedState.getValue(), weighedState.getWeight());
              }
            } else {
              for (P state : wfDistribution.getDomain()) {
                wfStateSums.adjust(state.getClassId(), wfDistribution.getLogFraction(state), wfDistribution.getCount(state));
              }
              for (P state : rsDistribution.getDomain()) {
                rsStateSums.adjust(state.getClassId(), rsDistribution.getLogFraction(state), rsDistribution.getCount(state));
              }
            }
   
            Vector wfStateProbDiffs = VectorFactory.getDefault().createVector(hmm.getNumStates());
            Vector rsStateProbDiffs = VectorFactory.getDefault().createVector(hmm.getNumStates());
            for (int j = 0; j < hmm.getNumStates(); j++) {
              /*
               * Sometimes all the probability goes to one class...
               */
              final double wfStateProb;
              if (!wfStateSums.getDomain().contains(j))
                wfStateProb = 0d;
              else
                wfStateProb = wfStateSums.getFraction(j);
              wfStateProbDiffs.setElement(j, gammas.get(t).getElement(j) - wfStateProb);
   
              final double rsStateProb;
              if (!rsStateSums.getDomain().contains(j))
                rsStateProb = 0d;
              else
                rsStateProb = rsStateSums.getFraction(j);
              rsStateProbDiffs.setElement(j, gammas.get(t).getElement(j) - rsStateProb);
            }
            String[] wfLine = {Integer.toString(k), Integer.toString(t), "p(x_t=0|y^T)", "water-filling",
                  wfDistribution.getMaxValueKey().getResampleType().toString(),
                  Double.toString(wfStateProbDiffs.getElement(0))};
            writer.writeNext(wfLine);
            String[] rsLine = {Integer.toString(k), Integer.toString(t), "p(x_t=0|y^T)", "resample",
                rsDistribution.getMaxValueKey().getResampleType().toString(),
                Double.toString(rsStateProbDiffs.getElement(0))};
            writer.writeNext(rsLine);
          }
        }
        writer.close();
      }
View Full Code Here

      CountedDataDistribution<Integer> stateSums = new CountedDataDistribution<Integer>(true);
      for (P state : wfDistribution.getDomain()) {
        stateSums.adjust(state.getClassId(), wfDistribution.getLogFraction(state), wfDistribution.getCount(state));
      }
     
      Vector stateProbDiffs = VectorFactory.getDefault().createVector(S);
      for (int j = 0; j < S; j++) {
        /*
         * Sometimes all the probability goes to one class...
         */
        final double stateProb;
        if (!stateSums.getDomain().contains(j))
          stateProb = 0d;
        else
          stateProb = stateSums.getFraction(j);
        stateProbDiffs.setElement(j, trueDists.get(t).getElement(j) - stateProb);
      }
   
      return stateProbDiffs;
    }
View Full Code Here

      final MultivariateGaussian priorPredState = transState.getState();
      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 double scaleSample = thisPriorScale.sample(this.rng);

        final MultivariateGaussian thisPriorOffset = initialPriorPsi.clone();

        final Vector systemSample = thisPriorOffset.sample(this.rng);
        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 =
            new GaussianArHpWfParticle(thisKf,
                ObservedValue.<Vector>create(0, null), priorState,
                priorStateSample,
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.