|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjml.sequence.HMM
public class HMM
A Java implementation of basic Hidden Markov Model. Observation symbols and state symbols are discrete integers starting from 0. It is the responsibility of users to maintain the mapping from IDs to observation symbols and the mapping from IDs to state symbols.
Field Summary | |
---|---|
(package private) double[][] |
A
State transition probability matrix. |
(package private) double[][] |
B
Observation probability matrix. |
(package private) double |
epsilon
Convergence precision. |
(package private) int |
M
Number of distinct observation symbols per state. |
(package private) int |
maxIter
Maximal number of iterations. |
(package private) int |
N
Number of states in the model. |
(package private) int[][] |
Os
Observation sequences for training. |
(package private) double[] |
pi
Initial state distribution. |
(package private) int[][] |
Qs
Hidden state sequences for training data. |
Constructor Summary | |
---|---|
HMM()
Default constructor. |
|
HMM(int N,
int M)
Construct an HMM with default convergence precision being 1e-6 and maximal number of iterations being 1000. |
|
HMM(int N,
int M,
double epsilon,
int maxIter)
Construct an HMM. |
Method Summary | |
---|---|
double[][] |
allocateMatrix(int nRows,
int nCols)
Allocate memory for a 2D double array. |
double[] |
allocateVector(int n)
Allocate continuous memory block for a 1D double
array. |
int |
argmax(double[] V)
Compute the maximum argument. |
void |
assignVector(double[] V,
double v)
Assign a 1D double array by a real scalar. |
void |
assignVector(double[] V1,
double[] V2)
Element-wise assignment operation. |
void |
clearMatrix(double[][] M)
Clear all elements of a 2D double array to zero. |
void |
clearVector(double[] V)
Clear all elements of a 1D double array to zero. |
void |
divideAssign(double[] V,
double v)
Element-wise division and assignment operation. |
double |
evaluate(int[] O)
Compute P(O|Theta), the probability of the observation sequence given the model, by forward recursion with scaling. |
double |
evaluate2(int[] O)
Compute P(O|Theta), the probability of the observation sequence given the model, by forward recursion without scaling. |
void |
feedData(int[][] Os)
Feed observation sequences for training. |
void |
feedLabels(int[][] Qs)
Feed state sequences for training data. |
double[] |
genDiscreteDistribution(int n)
Generate a discrete distribution with sample size of n. |
static int[][][] |
generateDataSequences(int D,
int T_min,
int T_max,
double[] pi,
double[][] A,
double[][] B)
Generate observation sequences with hidden state sequences given model parameters and number of data sequences. |
double[][] |
getA()
|
double[][] |
getB()
|
double[] |
getPi()
|
private double[][] |
initializeA()
|
private double[][] |
initializeB()
|
private double[] |
initializePi()
|
void |
loadModel(java.lang.String filePath)
Load an HMM model from a file. |
static void |
main(java.lang.String[] args)
|
void |
plusAssign(double[] V1,
double[] V2)
Element-wise addition and assignment operation. |
int[] |
predict(int[] O)
Predict the best single state path for a given observation sequence using Viterbi algorithm with logarithms. |
int[] |
predict2(int[] O)
Predict the best single state path for a given observation sequence using Viterbi algorithm. |
void |
saveModel(java.lang.String filePath)
Save this HMM model to a file. |
void |
setA(double[][] A)
|
void |
setB(double[][] B)
|
void |
setOs(int[][] Os)
|
void |
setPi(double[] pi)
|
void |
setQs(int[][] Qs)
|
void |
showObservationSequence(int[] O)
Show an observation sequence. |
void |
showStateSequence(int[] Q)
Show a state sequence. |
double |
sum(double[] V)
Compute the sum of a 1D double array. |
void |
sum2one(double[] V)
Sum a 1D double array to one, i.e., V[i] = V[i] / sum(V). |
void |
timesAssign(double[] V,
double v)
Element-wise multiplication and assignment operation. |
void |
timesAssign(double[] V1,
double[] V2)
Element-wise multiplication and assignment operation. |
void |
train()
Inference the basic HMM with scaling. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
int[][] Os
int[][] Qs
int N
int M
double[] pi
double[][] A
double[][] B
double epsilon
int maxIter
Constructor Detail |
---|
public HMM()
public HMM(int N, int M, double epsilon, int maxIter)
N
- number of states in the modelM
- number of distinct observation symbols per stateepsilon
- convergence precisionmaxIter
- maximal number of iterationspublic HMM(int N, int M)
N
- number of states in the modelM
- number of distinct observation symbols per stateMethod Detail |
---|
public static void main(java.lang.String[] args)
args
- public void feedData(int[][] Os)
Os
- observation sequencespublic void feedLabels(int[][] Qs)
Qs
- state sequencespublic double evaluate2(int[] O)
O
- an observation sequence
public double evaluate(int[] O)
O
- an observation sequence
public int argmax(double[] V)
V
- a double
array
public int[] predict2(int[] O)
O
- an observation sequence
public int[] predict(int[] O)
O
- an observation sequence
public void train()
public double[] genDiscreteDistribution(int n)
n
- sample size
private double[][] initializeB()
private double[][] initializeA()
private double[] initializePi()
public void assignVector(double[] V, double v)
double
array by a real scalar.
V
- a 1D double
arrayv
- a real scalarpublic void clearVector(double[] V)
double
array to zero.
V
- a double
arraypublic void clearMatrix(double[][] M)
double
array to zero.
M
- a 2D double
arraypublic double[] allocateVector(int n)
double
array.
n
- number of elements to be allocated
double
array of length npublic double[][] allocateMatrix(int nRows, int nCols)
double
array.
nRows
- number of rowsnCols
- number of columns
double
arraypublic void divideAssign(double[] V, double v)
V
- a 1D double
arrayv
- a real scalarpublic void timesAssign(double[] V, double v)
V
- a 1D double
arrayv
- a real scalarpublic void timesAssign(double[] V1, double[] V2)
V1
- a 1D double
arrayV2
- a 1D double
arraypublic double sum(double[] V)
double
array.
V
- a 1D double
array
public void sum2one(double[] V)
double
array to one, i.e., V[i] = V[i] / sum(V).
V
- a 1D double
arraypublic void plusAssign(double[] V1, double[] V2)
V1
- a 1D double
arrayV2
- a 1D double
arraypublic void assignVector(double[] V1, double[] V2)
V1
- a 1D double
arrayV2
- a 1D double
arraypublic void saveModel(java.lang.String filePath)
filePath
- file path to save the modelpublic void loadModel(java.lang.String filePath)
filePath
- file Path to load an HMM model frompublic void setQs(int[][] Qs)
public void setOs(int[][] Os)
public void setPi(double[] pi)
public void setA(double[][] A)
public void setB(double[][] B)
public double[] getPi()
public double[][] getA()
public double[][] getB()
public void showStateSequence(int[] Q)
Q
- a state sequence represented by a 1D
int
arraypublic void showObservationSequence(int[] O)
O
- an observation sequence represented by a 1D
int
arraypublic static int[][][] generateDataSequences(int D, int T_min, int T_max, double[] pi, double[][] A, double[][] B)
D
- number of data sequences to be generatedT_min
- minimal sequence lengthT_max
- maximal sequence lengthpi
- initial state distributionA
- state transition probability matrixB
- observation probability matrix
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |