Answered by:
Building a Bayesian Network with arbitrary no. of incomming nodes
Question

Hi, How do I build a bayesian network with arbitrary no. of incomming nodes? Is there a generalized way of representing the CPT / prior / posterior variables?
E.g. , in the WaterSprinkler example, the incomming nodes are know and the datatype of their CPT and posterior variables are hardcoded.
Variable<Dirichlet> ProbCloudyPrior; // Zero Incoming Nodes VariableArray<Dirichlet> CPTRainPrior; // One Incomming Node VariableArray<VariableArray<Dirichlet>, Dirichlet[][]> CPTWetGrassPrior; // Two Incomming Nodes VariableArray<VariableArray<VariableArray<Dirichlet>, Dirichlet[][][]>> CPTThree; // Three Incomming Nodes
In my case, I do not know how many incoming nodes I have during compiletime. It's decided by the user during runtime.Is there a way to generalize the above data structure?Any pointers / workaround would be appreciated.ThanksTuesday, January 17, 2012 10:23 AM
Answers

This is more of a C# question than an Infer.NET question. It is essentially the same problem as manipulating jagged C# arrays whose depth is decided at runtime. The short answer is that you have to use generics and reflection.
Jagged VariableArrays in Infer.NET are all represented by the generic type VariableArray<TItem,TArray>. You can create a jagged VariableArray of arbitrary depth by repeatedly calling Variable.Array. Indexing a jagged VariableArray of unknown depth is more difficult. If you index it once, you get an object of type TItem but you will not know how deep this is without using reflection. So you will have to reflect on TItem to determine if it is itself of type VariableArray<T1,T2> and obtain the identity of T1. If this isn't clear, then try manipulating C# jagged arrays whose depth is decided at runtime and you'll see what I mean.
 Marked as answer by [ERROR] Wednesday, January 18, 2012 6:54 AM
Tuesday, January 17, 2012 5:07 PMOwner
All replies

This is more of a C# question than an Infer.NET question. It is essentially the same problem as manipulating jagged C# arrays whose depth is decided at runtime. The short answer is that you have to use generics and reflection.
Jagged VariableArrays in Infer.NET are all represented by the generic type VariableArray<TItem,TArray>. You can create a jagged VariableArray of arbitrary depth by repeatedly calling Variable.Array. Indexing a jagged VariableArray of unknown depth is more difficult. If you index it once, you get an object of type TItem but you will not know how deep this is without using reflection. So you will have to reflect on TItem to determine if it is itself of type VariableArray<T1,T2> and obtain the identity of T1. If this isn't clear, then try manipulating C# jagged arrays whose depth is decided at runtime and you'll see what I mean.
 Marked as answer by [ERROR] Wednesday, January 18, 2012 6:54 AM
Tuesday, January 17, 2012 5:07 PMOwner 
Thanks for the detailed answer. I'll try this outWednesday, January 18, 2012 6:55 AM