📄 markovgenerator.java
字号:
/* jahmm package - v0.3.1 *//* * Copyright (c) 2004, Jean-Marc Francois. * * This file is part of Jahmm. * Jahmm is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Jahmm is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jahmm; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package be.ac.ulg.montefiore.run.jahmm.toolbox;import java.util.*;import be.ac.ulg.montefiore.run.jahmm.*;/** * Generates Markovian observations given a HMM. */public class MarkovGenerator { private final Hmm hmm; private int stateNb; /** * Initializes a Markovian generator. The generator is ready to generate * a new {@link #observation() observation}. * * @param hmm An Hidden Markov Model that perfectly models the sequences * generated by this object. */ public MarkovGenerator(Hmm hmm) { if (hmm == null) throw new IllegalArgumentException(); this.hmm = hmm; newSequence(); } /** * Generates a new (pseudo) random observation. * * @return The generated observation. */ public Observation observation() { Observation o = hmm.getOpdf(stateNb).generate(); double rand = Math.random(); for (int j = 0; j < hmm.nbStates() - 1; j++) if ((rand -= hmm.getAij(stateNb, j)) < 0) { stateNb = j; return o; } stateNb = hmm.nbStates() - 1; return o; } /** * Generates a new (pseudo) random observation sequence and start * a new one. * * @param length The length of the sequence. * @return An observation sequence. */ public Vector observationSequence(int length) { if (length <= 0) throw new IllegalArgumentException(); Vector sequence = new Vector(); while (length-- > 0) sequence.add(observation()); newSequence(); return sequence; } /** * Finds a new state according to the initial (pi) probabilities of each * state. */ public void newSequence() { double rand = Math.random(), current = 0.; for (int i = 0; i < hmm.nbStates() - 1; i++) { current += hmm.getPi(i); if (current > rand) { stateNb = i; return; } } stateNb = hmm.nbStates() - 1; } /** * Get the state number of the current state. * * @return A state number. */ public int stateNb() { return stateNb; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -