final long seed = new Random().nextLong();
final Random random = new Random(seed);
log.info("seed=" + seed);
final double trueSigma = Math.pow(0.2d, 2);
Matrix modelCovariance1 = MatrixFactory.getDefault().copyArray(
new double[][] {{trueSigma}});
Matrix modelCovariance2 = MatrixFactory.getDefault().copyArray(
new double[][] {{trueSigma}});
Matrix measurementCovariance = MatrixFactory.getDefault().copyArray(
new double[][] {{trueSigma}});
List<Vector> truePsis = Lists.newArrayList(
VectorFactory.getDefault().copyValues(3d, 0.2d),
VectorFactory.getDefault().copyValues(-1d, 0.9d));
LinearDynamicalSystem model1 = new LinearDynamicalSystem(
MatrixFactory.getDefault().copyArray(new double[][] {{truePsis.get(0).getElement(1)}}),
MatrixFactory.getDefault().copyArray(new double[][] {{1d}}),
MatrixFactory.getDefault().copyArray(new double[][] {{1d}})
);
LinearDynamicalSystem model2 = new LinearDynamicalSystem(
MatrixFactory.getDefault().copyArray(new double[][] {{truePsis.get(1).getElement(1)}}),
MatrixFactory.getDefault().copyArray(new double[][] {{1d}}),
MatrixFactory.getDefault().copyArray(new double[][] {{1d}})
);
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.5d, 0.5d });
Matrix classTransProbs = MatrixFactory.getDefault().copyArray(
new double[][] { { 0.5d, 0.5d },
{ 0.5d, 0.5d } });
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);