public class MarkovChainVariate extends RandomVariateBase implements DiscreteRandomVariate
Instances of this class generate values from a Markov Chain with the given transition probabilities on the state space [0,..n-1] where n is the size of the transition matrix.
IMPORTANT: if the initial initialState is not specified, then it defaults to
0. If this is not a valid initialState, then it should be explicitly called
or specified in the call to RandomVariateFactory
.
Modifier and Type | Field and Description |
---|---|
protected int |
currentState |
private static double |
EPSILON |
private int |
initialState |
private static java.util.logging.Logger |
LOGGER |
private double[][] |
transitionMatrix |
private java.util.Map<java.lang.Integer,DiscreteRandomVariate> |
transitions |
rng
Constructor and Description |
---|
MarkovChainVariate() |
Modifier and Type | Method and Description |
---|---|
static double[][] |
deepCopy(double[][] matrix) |
double |
generate()
Generate a random variate having this class's distribution.
|
int |
generateInt() |
int |
getCurrentState() |
int |
getInitialState() |
java.lang.Object[] |
getParameters()
Returns the array of parameters as an Object[].
|
static double[] |
getSteadyState(double[][] matrix)
This uses the power approach to finding the steady-state distribution.
|
double[][] |
getTransitionMatrix() |
static boolean |
isSquare(double[][] matrix)
'
|
static double[][] |
matrixSquare(double[][] matrix) |
static java.lang.String |
matrixToString(double[][] matrix) |
static void |
normalize(double[][] matrix)
This normalizes the given matrix in-place, so beware.
|
static double |
score(double[][] matrix)
The score is the sum of the max - min of each column
|
void |
setInitialState(int initialState) |
void |
setParameters(java.lang.Object... params)
The first argument should be the transition matrix as a
double[][] . |
void |
setTransitionMatrix(double[][] transitionMatrix)
This normalizes the given transitionMatrix so that each row is a discrete
probability distribution.
|
java.lang.String |
toString() |
getRandomNumber, setRandomNumber
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getRandomNumber, setRandomNumber
private static final java.util.logging.Logger LOGGER
private static final double EPSILON
private double[][] transitionMatrix
private final java.util.Map<java.lang.Integer,DiscreteRandomVariate> transitions
private int initialState
protected int currentState
public double generate()
RandomVariate
generate
in interface RandomVariate
public void setParameters(java.lang.Object... params)
The first argument should be the transition matrix as a
The second (optional) argumen is an integer that is the initial
initialState.double[][]
. Because of the peculiarities of varargs, it must
be wrapped in an Object[]
array.
setParameters
in interface RandomVariate
params
- Given parametersjava.lang.IllegalArgumentException
- is params.length is not 1 or 2java.lang.IllegalArgumentException
- if params[0] is not a
double[][]
arrayjava.lang.IllegalArgumentException
- if params.length == 2 and params[1] is
not an Integerpublic java.lang.Object[] getParameters()
RandomVariate
getParameters
in interface RandomVariate
public int generateInt()
generateInt
in interface DiscreteRandomVariate
public double[][] getTransitionMatrix()
public void setTransitionMatrix(double[][] transitionMatrix)
transitionMatrix
- the transitionMatrix to setjava.lang.IllegalArgumentException
- if given transitionMatrix is not squarepublic int getInitialState()
public void setInitialState(int initialState)
initialState
- the row to setpublic int getCurrentState()
public static double[][] deepCopy(double[][] matrix)
matrix
- Given matrixpublic static void normalize(double[][] matrix)
matrix
- Given matrixjava.lang.IllegalArgumentException
- if it finds an entry that is negativepublic static boolean isSquare(double[][] matrix)
matrix
- Given double[][] arraypublic static double[] getSteadyState(double[][] matrix)
matrix
- Given matrixpublic static double[][] matrixSquare(double[][] matrix)
matrix
- Given double[][] arraypublic static double score(double[][] matrix)
matrix
- Given double[][] arraypublic static java.lang.String matrixToString(double[][] matrix)
matrix
- Given double[][] arraypublic java.lang.String toString()
toString
in class java.lang.Object