Setting initial mu and variance for Factorial HMM
lonerzzz opened this issue · 3 comments
Hello,
I had raised an issue last year regarding the use of initialization values for the HMMs, but I see there are currently few contributors.
I am now attempting to play with the source to allow setting of the mu and variance of the Factorial HMM for the internal binary states instead of relying on random initialization, but I am not sure what variables I should be changing. The different distributions are held in lists and I am not sure of the nomenclature of the variables that should be altered. There are variables labelled NormalGamma_Parameter{binaryHiddenVar0 = 0, binaryHiddenVar1 = 0}_0 for example which would seem to hold the desired values. Can someone point me in the direction of the variables to be changed so that I can play with it further?
What I am attempting to do overall is add an initQs method that works in parallel to the resetQs method to populate the Node statistics in the MessagePassingAlgorithm and DynamicModel classes.
Lastly, is it valid to be initializing the starter values for the time0 and timeT values?
If I am going in entirely the wrong direction, I can play elsewhere if given direction.
Thanks,
Jason
Hi, thanks for coming back here.
I've been checking your last issue. Let me ask you a couple of questions about the problem you are seeking to solve. In your last issue, you asked for a way to set the initial normal values for an HMM. And you posted the following piece of code,
`
IOHMM.initializeLearning();
list = IOHMM.getModel().getConditionalDistributionsTimeT();
np = (Normal_MultinomialParents)list.get( 0 );
normalList = np.getNormalDistributions();
Normal normal = normalList.get( 0 );
normal.setMean( 0 );
normal.setVariance( 32 );
`
My question is if you are seeking to fix the distributions of the hidden Normal variables at time 0? Please, try to be a bit more precise.
Thanks
Thanks for the response.
The work on the IOHMM is not quite the same problem so less relevant.
What I am trying to do now with the FHMM is to set the initial states that correspond to the binary hidden variables. For example with a periodic square wave of amplitude 10 with some light injected noise and mean duty cycle of 50%, I am running the following code:
DataStream<DynamicDataInstance> dataStream1 = DynamicDataStreamLoader.loadFromFile( "H://samples/sample1.arff" );
FactorialHMM factorialHMM = new FactorialHMM( dataStream1.getAttributes() );
factorialHMM.setNumHidden( hiddenCount );
factorialHMM.getLearningAlgorithm().setSeed( seed ); //Accessor added to allow setting of seed
System.out.println( factorialHMM.getDynamicDAG() );
factorialHMM.updateModel( dataStream1 );
System.out.println(factorialHMM.getModel());
and getting the following output with a hiddenCount of 1 and seed of 0 where the mu and variance are not optimal with each state of the hidden variable roughly equivalent:
Dynamic DAG at Time 0
MeasurementList has 1 parent(s): {binaryHiddenVar0}
binaryHiddenVar0 has 0 parent(s): {}
Dynamic DAG at Time T
MeasurementList has 1 parent(s): {binaryHiddenVar0}
binaryHiddenVar0 has 1 parent(s): {binaryHiddenVar0_Interface}
N Iter: 4, elbo:-13.245793416436968
N Iter: 3, elbo:-338.1776284263548
Dynamic Bayesian Network Time 0:
P(MeasurementList | binaryHiddenVar0) follows a Normal|Multinomial
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar0 = 0}
Normal [ mu = 0.4743521319525647, var = 0.6666666666741669 ] | {binaryHiddenVar0 = 1}
P(binaryHiddenVar0) follows a Multinomial
[ 0.4, 0.6 ]
Dynamic Bayesian Network Time T:
P(MeasurementList | binaryHiddenVar0) follows a Normal|Multinomial
Normal [ mu = 5.04989738113568, var = 24.154741894366406 ] | {binaryHiddenVar0 = 0}
Normal [ mu = 4.4225959070546565, var = 23.87533109803503 ] | {binaryHiddenVar0 = 1}
P(binaryHiddenVar0 | binaryHiddenVar0_Interface) follows a Multinomial|Multinomial
[ 0.4960909048464801, 0.5039090951535199 ] | {binaryHiddenVar0_Interface = 0}
[ 0.49233879439776024, 0.5076612056022398 ] | {binaryHiddenVar0_Interface = 1}
or getting the following output with a hiddenCount of 2 and seed of 0 which is still not optimal for mu and variance:
Dynamic DAG at Time 0
MeasurementList has 2 parent(s): {binaryHiddenVar0, binaryHiddenVar1}
binaryHiddenVar0 has 0 parent(s): {}
binaryHiddenVar1 has 0 parent(s): {}
Dynamic DAG at Time T
MeasurementList has 2 parent(s): {binaryHiddenVar0, binaryHiddenVar1}
binaryHiddenVar0 has 1 parent(s): {binaryHiddenVar0_Interface}
binaryHiddenVar1 has 1 parent(s): {binaryHiddenVar1_Interface}
N Iter: 9, elbo:-13.938940596996913
N Iter: 3, elbo:-373.7033033558986
Dynamic Bayesian Network Time 0:
P(MeasurementList | binaryHiddenVar0 , binaryHiddenVar1) follows a Normal|Multinomial
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar1 = 0, binaryHiddenVar0 = 0}
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar1 = 0, binaryHiddenVar0 = 1}
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar1 = 1, binaryHiddenVar0 = 0}
Normal [ mu = 0.4743521319525647, var = 0.6666666666741669 ] | {binaryHiddenVar1 = 1, binaryHiddenVar0 = 1}
P(binaryHiddenVar0) follows a Multinomial
[ 0.4, 0.6 ]
P(binaryHiddenVar1) follows a Multinomial
[ 0.4, 0.6 ]
Dynamic Bayesian Network Time T:
P(MeasurementList | binaryHiddenVar0 , binaryHiddenVar1) follows a Normal|Multinomial
Normal [ mu = 4.683744413824284, var = 23.19851345266765 ] | {binaryHiddenVar1 = 0, binaryHiddenVar0 = 0}
Normal [ mu = 4.725785543461045, var = 23.290710520321134 ] | {binaryHiddenVar1 = 0, binaryHiddenVar0 = 1}
Normal [ mu = 4.738404118189752, var = 23.194795349715882 ] | {binaryHiddenVar1 = 1, binaryHiddenVar0 = 0}
Normal [ mu = 4.7804934931903285, var = 23.28410271604163 ] | {binaryHiddenVar1 = 1, binaryHiddenVar0 = 1}
P(binaryHiddenVar0 | binaryHiddenVar0_Interface) follows a Multinomial|Multinomial
[ 0.4902305992691898, 0.5097694007308102 ] | {binaryHiddenVar0_Interface = 0}
[ 0.49020570645090367, 0.5097942935490963 ] | {binaryHiddenVar0_Interface = 1}
P(binaryHiddenVar1 | binaryHiddenVar1_Interface) follows a Multinomial|Multinomial
[ 0.5042686201568135, 0.49573137984318644 ] | {binaryHiddenVar1_Interface = 0}
[ 0.5042993073275475, 0.4957006926724526 ] | {binaryHiddenVar1_Interface = 1}
in contrast to the following output with a hiddenCount of 2 and Random seed of 1 which gives values as expected for the mu and variance given the known square wave input:
Dynamic DAG at Time 0
MeasurementList has 2 parent(s): {binaryHiddenVar0, binaryHiddenVar1}
binaryHiddenVar0 has 0 parent(s): {}
binaryHiddenVar1 has 0 parent(s): {}
Dynamic DAG at Time T
MeasurementList has 2 parent(s): {binaryHiddenVar0, binaryHiddenVar1}
binaryHiddenVar0 has 1 parent(s): {binaryHiddenVar0_Interface}
binaryHiddenVar1 has 1 parent(s): {binaryHiddenVar1_Interface}
N Iter: 8, elbo:-13.938940596996913
N Iter: 37, elbo:-95.31822032672022
Dynamic Bayesian Network Time 0:
P(MeasurementList | binaryHiddenVar0 , binaryHiddenVar1) follows a Normal|Multinomial
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar1 = 0, binaryHiddenVar0 = 0}
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar1 = 0, binaryHiddenVar0 = 1}
Normal [ mu = 0.4743521319525647, var = 0.6666666666741669 ] | {binaryHiddenVar1 = 1, binaryHiddenVar0 = 0}
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar1 = 1, binaryHiddenVar0 = 1}
P(binaryHiddenVar0) follows a Multinomial
[ 0.6, 0.4 ]
P(binaryHiddenVar1) follows a Multinomial
[ 0.4, 0.6 ]
Dynamic Bayesian Network Time T:
P(MeasurementList | binaryHiddenVar0 , binaryHiddenVar1) follows a Normal|Multinomial
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar1 = 0, binaryHiddenVar0 = 0}
Normal [ mu = 0.0, var = 1.0 ] | {binaryHiddenVar1 = 0, binaryHiddenVar0 = 1}
Normal [ mu = -0.01678463774996772, var = 0.11878710198491312 ] | {binaryHiddenVar1 = 1, binaryHiddenVar0 = 0}
Normal [ mu = 9.986603117680877, var = 0.12934896574776703 ] | {binaryHiddenVar1 = 1, binaryHiddenVar0 = 1}
P(binaryHiddenVar0 | binaryHiddenVar0_Interface) follows a Multinomial|Multinomial
[ 0.9107142857142857, 0.08928571428571429 ] | {binaryHiddenVar0_Interface = 0}
[ 0.09803921568627451, 0.9019607843137255 ] | {binaryHiddenVar0_Interface = 1}
P(binaryHiddenVar1 | binaryHiddenVar1_Interface) follows a Multinomial|Multinomial
[ 0.5, 0.5 ] | {binaryHiddenVar1_Interface = 0}
[ 0.019417475728155338, 0.9805825242718447 ] | {binaryHiddenVar1_Interface = 1}
Because of these results, I would like to set appropriate starting values for the Normal mu and variance for those situations where the approximate hidden state value is known such that convergence is possible.
In terms of code, I have looked at what the random Node initialization was setting for the different distributions and was attempting to put in place a parallel mechanism where desired parameters such as the mean and variance can be set. I have made the assumption that the 'natural' parameters in the different EF_* distributions are what I would set.
I have found a few bugs along the way that I would be happy to fix. as well.
If I am still not clear, please let me know what would make it easier to clarify.
Thanks,
Jason
Hi, I was wondering if I had provided sufficient information on this for you to understand my intent.
Thanks,
Jason