Package org.encog.ml.hmm

Examples of org.encog.ml.hmm.HiddenMarkovModel


    double [][] covariance1 = { {1, 2}, {1, 4} };
   
    double [] mean2 = {0.5, 0.25};
    double [][] covariance2 = { {4, 2}, {3, 4} };
   
    HiddenMarkovModel hmm = new HiddenMarkovModel(2);
   
    hmm.setPi(0, 0.8);
    hmm.setPi(1, 0.2);
   
    hmm.setStateDistribution(0, new ContinousDistribution(mean1,covariance1));
    hmm.setStateDistribution(1, new ContinousDistribution(mean2,covariance2));
   
    hmm.setTransitionProbability(0, 1, 0.05);
    hmm.setTransitionProbability(0, 0, 0.95);
    hmm.setTransitionProbability(1, 0, 0.10);
    hmm.setTransitionProbability(1, 1, 0.90);
   
    return hmm;
  }
View Full Code Here


    return hmm;
  }
 
  static HiddenMarkovModel buildDiscHMM()
  { 
    HiddenMarkovModel hmm =
      new HiddenMarkovModel(2, 2);
   
    hmm.setPi(0, 0.95);
    hmm.setPi(1, 0.05);
   
    hmm.setStateDistribution(0, new DiscreteDistribution(new double[][] { { 0.95, 0.05 } }));
    hmm.setStateDistribution(1, new DiscreteDistribution(new double[][] { { 0.20, 0.80 } }));
   
    hmm.setTransitionProbability(0, 1, 0.05);
    hmm.setTransitionProbability(0, 0, 0.95);
    hmm.setTransitionProbability(1, 0, 0.10);
    hmm.setTransitionProbability(1, 1, 0.90);
   
    return hmm;
  }
View Full Code Here

      Assert.assertTrue(e<0.01);
  }
 
  public void testDiscPersistEG()
  {
    HiddenMarkovModel sourceHMM = buildDiscHMM();

    EncogDirectoryPersistence.saveObject(EG_FILENAME, sourceHMM);
    HiddenMarkovModel resultHMM = (HiddenMarkovModel)EncogDirectoryPersistence.loadObject(EG_FILENAME);

    validate(resultHMM,sourceHMM);
  }
View Full Code Here

    validate(resultHMM,sourceHMM);
  }
 
  public void testDiscPersistSerial() throws IOException, ClassNotFoundException
  {
    HiddenMarkovModel sourceHMM = buildDiscHMM();
   
    SerializeObject.save(SERIAL_FILENAME, sourceHMM);
    HiddenMarkovModel resultHMM = (HiddenMarkovModel)SerializeObject.load(SERIAL_FILENAME);
       
    validate(resultHMM,sourceHMM);
  }
View Full Code Here

    validate(resultHMM,sourceHMM);
  }
 
  public void testContPersistEG()
  {
    HiddenMarkovModel sourceHMM = buildContHMM();

    EncogDirectoryPersistence.saveObject(EG_FILENAME, sourceHMM);
    HiddenMarkovModel resultHMM = (HiddenMarkovModel)EncogDirectoryPersistence.loadObject(EG_FILENAME);

    validate(resultHMM,sourceHMM);
  }
View Full Code Here

    validate(resultHMM,sourceHMM);
  }
 
  public void testContPersistSerial() throws IOException, ClassNotFoundException
  {
    HiddenMarkovModel sourceHMM = buildContHMM();
   
    SerializeObject.save(SERIAL_FILENAME, sourceHMM);
    HiddenMarkovModel resultHMM = (HiddenMarkovModel)SerializeObject.load(SERIAL_FILENAME);
       
    validate(resultHMM,sourceHMM);
  }
View Full Code Here

    return false;
  }

  @Override
  public void iteration() {
    HiddenMarkovModel nhmm;
    try {
      nhmm = this.method.clone();
    } catch (final CloneNotSupportedException e) {
      throw new InternalError();
    }

    final double allGamma[][][] = new double[this.training
        .getSequenceCount()][][];
    final double aijNum[][] = new double[this.method.getStateCount()][this.method
        .getStateCount()];
    final double aijDen[] = new double[this.method.getStateCount()];

    Arrays.fill(aijDen, 0.0);
    for (int i = 0; i < this.method.getStateCount(); i++) {
      Arrays.fill(aijNum[i], 0.);
    }

    int g = 0;
    for (final MLDataSet obsSeq : this.training.getSequences()) {
      final ForwardBackwardCalculator fbc = generateForwardBackwardCalculator(
          obsSeq, this.method);

      final double xi[][][] = estimateXi(obsSeq, fbc, this.method);
      final double gamma[][] = allGamma[g++] = estimateGamma(xi, fbc);

      for (int i = 0; i < this.method.getStateCount(); i++) {
        for (int t = 0; t < (obsSeq.size() - 1); t++) {
          aijDen[i] += gamma[t][i];

          for (int j = 0; j < this.method.getStateCount(); j++) {
            aijNum[i][j] += xi[t][i][j];
          }
        }
      }
    }

    for (int i = 0; i < this.method.getStateCount(); i++) {
      if (aijDen[i] == 0.0) {
        for (int j = 0; j < this.method.getStateCount(); j++) {
          nhmm.setTransitionProbability(i, j,
              this.method.getTransitionProbability(i, j));
        }
      } else {
        for (int j = 0; j < this.method.getStateCount(); j++) {
          nhmm.setTransitionProbability(i, j, aijNum[i][j]
              / aijDen[i]);
        }
      }
    }

    /* compute pi */
    for (int i = 0; i < this.method.getStateCount(); i++) {
      nhmm.setPi(i, 0.);
    }

    for (int o = 0; o < this.training.getSequenceCount(); o++) {
      for (int i = 0; i < this.method.getStateCount(); i++) {
        nhmm.setPi(
            i,
            nhmm.getPi(i)
                + (allGamma[o][0][i] / this.training
                    .getSequenceCount()));
      }
    }

    /* compute pdfs */
    for (int i = 0; i < this.method.getStateCount(); i++) {

      final double[] weights = new double[this.training.size()];
      double sum = 0.;
      int j = 0;

      int o = 0;
      for (final MLDataSet obsSeq : this.training.getSequences()) {
        for (int t = 0; t < obsSeq.size(); t++, j++) {
          sum += weights[j] = allGamma[o][t][i];
        }
        o++;
      }

      for (j--; j >= 0; j--) {
        weights[j] /= sum;
      }

      final StateDistribution opdf = nhmm.getStateDistribution(i);
      opdf.fit(this.training, weights);
    }

    this.method = nhmm;
  }
View Full Code Here

    return this.done;
  }

  @Override
  public void iteration() {
    final HiddenMarkovModel hmm = this.modelHMM.cloneStructure();

    learnPi(hmm);
    learnTransition(hmm);
    learnOpdf(hmm);
View Full Code Here

TOP

Related Classes of org.encog.ml.hmm.HiddenMarkovModel

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.