How do I access the values of the prior distribution?

The engine.
Post Reply
ermutarra
Posts: 17
Joined: Fri Apr 24, 2009 1:19 pm

How do I access the values of the prior distribution?

Post by ermutarra »

Hi,

I have managed to read the data from a file, learn the naive bayes network and perform inference. After I have performed the inference I want to see what the probability distribution is for the parent node. The code I have for this is pasted below, but it gives a segmentation fault... Does anybody know what's wrong with it and/or the best way to know the values of the new distribution? Also, what is the difference between the checked and unchecked value? Which should I use?

DSL_Dmatrix* probmatrix = theNet.GetNode(gaithandle)->Value()->GetMatrix();
DSL_sysCoordinates coords(*probmatrix);
theNames = theNet.GetNode(gaithandle)->Definition()->GetOutcomesNames();
coords.GoFirst();
int gaitindex;
for( int i=1; i<=NUM_GAITS; i++ ){
sprintf(s,"State_%d",i);
gaitindex = theNames->FindPosition(s);
coords.GoTo(gaitindex);
printf("Gait \t %d \t coord index \t %d \t checked \t %f \t unchecked \t %f \n",i,gaitindex,coords.CheckedValue(),coords.UncheckedValue());
}

Thank you very much for the help!
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: How do I access the values of the prior distribution?

Post by shooltz[BayesFusion] »

Also, what is the difference between the checked and unchecked value? Which should I use?
Probably neither :) It's quite old option, but still survives in the docs. You can simply iterate over any DSL_Dmatrix with zero-based index. If you need to use multi-dimensional coords, specify them with DSL_intArray.

Anyway, priors should be acessed with a snippet like the one below - note a call to Definition(), not Value():

DSL_Dmatrix* probmatrix = theNet.GetNode(gaithandle)->Definition()->GetMatrix();
int count = probmatrix->GetSize();
for (int i = 0; i < count; i ++) printf("%d %g\n", i, (*probmatrix));
ermutarra
Posts: 17
Joined: Fri Apr 24, 2009 1:19 pm

Post by ermutarra »

This works great!!

Thank you very much.

PS: Could I suggest that you update the docs and the tutorials? We would give you so much less hassle if they were up to date :)
ermutarra
Posts: 17
Joined: Fri Apr 24, 2009 1:19 pm

Post by ermutarra »

By the way, if I use

DSL_Dmatrix* probmatrix = theNet.GetNode(gaithandle)->Value()->GetMatrix();

instead of "Definition", then I get the posterior distribution, right?
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Post by shooltz[BayesFusion] »

ermutarra wrote:By the way, if I use

DSL_Dmatrix* probmatrix = theNet.GetNode(gaithandle)->Value()->GetMatrix();

instead of "Definition", then I get the posterior distribution, right?
Yes. The numbers in this matrix will be only if you call DSL_network::UpdateBeliefs first, and inference will succeed. To ensure that posteriors you're accessing are valid, use this:

Code: Select all

DSL_nodeValue *v = net.GetNode(handle);
if (v->IsValueValid())
{
   DSL_Dmatrix *mtx = v->GetMatrix();
   // ... read contents of mtx here
}
ermutarra
Posts: 17
Joined: Fri Apr 24, 2009 1:19 pm

Post by ermutarra »

Hi Shooltz,

You mention in a previous post of this thread that I should use DSL_intArray in I need to use multi-dimensional coords.

Say I have learned my model with Smilearn. It is a naive bayes classifier. So I can see the prior for the parent node as you pointed out in the previous post. Great!

Now I want to see the CPT for one of the children nodes, how do I do this? Could you provide a code snippet? And how can I see the states of the child node? I get the list in the form of an DSL_idArray but I dont know how to access the individual states.

The code I have at the moment is:

probmatrix = theNet.GetNode(disthandle)->Definition()->GetMatrix();
int count = (probmatrix->GetSize())/NUM_GAITS;
int n=0; double probs[NUM_GAITS][count];
for( int i=0; i<NUM_GAITS; i++ ){
for( int j=0; j<count; j++ ){
probs[j] = (*probmatrix)[n];
n++;
}
}
DSL_idArray *theNames;
theNames = theNet.GetNode(disthandle)->Definition()->GetOutcomesNames();
file << endl << "D\\G ";
for( int i=0; i<NUM_GAITS; i++ ){ file << i << " "; }; file << endl;
for( int j=0; j<count; j++ ){
file << theNames->GetId(j) << " "; // HOW DO I GET WHAT STATE THIS PROB corresponds to???
for( int i=0; i<NUM_GAITS; i++ ){
file << probs[j] << " ";
}
file << endl;
}


Thank you very much!!
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Post by shooltz[BayesFusion] »

ermutarra wrote:The code I have at the moment is:

probmatrix = theNet.GetNode(disthandle)->Definition()->GetMatrix();
int count = (probmatrix->GetSize())/NUM_GAITS;
int n=0; double probs[NUM_GAITS][count];
I don't think this will compile. You'll need constant expression as array dimension; the 'count' variable is not constant.
And how can I see the states of the child node? I get the list in the form of an DSL_idArray but I dont know how to access the individual states.


Please clarify what do you mean by 'accessing the individual states'. Are you interested in the probabilities for i-th state for different parent configurations?
ermutarra
Posts: 17
Joined: Fri Apr 24, 2009 1:19 pm

Post by ermutarra »

I actually managed to get that code to work.

The method I was looking for to know which state the probability corresponds to is
theNames->Subscript(j)

So all is well, I was just wondering whether there is an easier/shorter way to accomplish the same thing as that code. But I'm not very bothered about it now that it works.

Thanks!
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Post by shooltz[BayesFusion] »

ermutarra wrote:The method I was looking for to know which state the probability corresponds to is
theNames->Subscript(j)
You can use (*theNames)[j] as well. Or use the reference (not pointer) to DSL_idArray:

const DSL_idArray &theNames = *(theNet.GetNode(disthandle)->Definition()->GetOutcomesNames());
cout << theNames[j];
Post Reply