locked
Multiple continuous parents and a combination of continuous and discrete parents examples RRS feed

  • Question

  • Hello Everyone,

    I am new to Infer.NET and I would really appreciate it if you could either provide or point me to an existing example of how to implement the following two scenarios for RVs A, B, and C with A-->C, B-->C:

    1. All three variables are continuous (e.g. Gaussian)
    2. A and C are continuous and B is discrete

     Many thanks!

    Thursday, February 14, 2013 7:22 PM

Answers

  • Below are some examples. On the continuous case, if you have more than two variables to sum you should use a VariableArray and use the Sum factor.

    static void Main(string[] args)
    {
    	// ------------------------------
    	// Continuous example.
    	// ------------------------------
    	var A1 = Variable.GaussianFromMeanAndPrecision(0, 1);
    	var B1 = Variable.GaussianFromMeanAndPrecision(3, 4);
    	var C1 = Variable.New<double>();
    
    	C1 = A1 + B1;
    
    	// Example of inference: Observe C, infer A and B
    	C1.ObservedValue = 2.5;
    	var engine = new InferenceEngine();
    
    	Console.WriteLine(engine.Infer(A1));
    	Console.WriteLine(engine.Infer(B1));
    
    	// ------------------------------
    	// Continuous/discrete example.
    	// ------------------------------
    	var A2 = Variable.GaussianFromMeanAndPrecision(0, 1);
    	double[] discreteProbs = new double[] {0.4, 0.2, 0.3};
    	Range valueRange = new Range(discreteProbs.Length);
    	var B2 = Variable.Discrete(valueRange, discreteProbs);
    	B2.SetValueRange(valueRange);
    
    	// The values to sum conditioned on the discrete value.
    	var B2ValuesToSum = Variable.Array<double>(valueRange);
    	B2ValuesToSum.ObservedValue = new double[] { 1.0, 2.0, 3.0 };
    	var C2 = Variable.New<double>();
    
    	using (Variable.Switch(B2))
    		C2 = A2 + B2ValuesToSum[B2];
    
    	// Example of inference: Observe C, infer A and B
    	C2.ObservedValue = 2.5;
    
    	Console.WriteLine(engine.Infer(A2));
    	Console.WriteLine(engine.Infer(B2));
    }


    Tuesday, February 19, 2013 8:50 AM
    Owner

All replies

  • Hi Alex

    You have given the structure of the model but not model itself. An example of (1) would be C=A+B. An example of (2) would be if C = A if B == 0, C = -A if B == 1. There are lots of examples to do this kind of operation, but did you have models in mind?

    John

    Friday, February 15, 2013 9:41 AM
    Owner
  • Hi John,

    Apologies for the lack of context, I would like to see a linear combination in both cases (C=A+B).

    Thank you for quick response!

    Alex

    Friday, February 15, 2013 4:08 PM
  • Just to clarify a bit more, while this seems like a simple regression, the model I would like to build eventually is more complex and will include multiple "modules" of this sort. My final goal is inference.

    Thank you!

    Friday, February 15, 2013 11:30 PM
  • Below are some examples. On the continuous case, if you have more than two variables to sum you should use a VariableArray and use the Sum factor.

    static void Main(string[] args)
    {
    	// ------------------------------
    	// Continuous example.
    	// ------------------------------
    	var A1 = Variable.GaussianFromMeanAndPrecision(0, 1);
    	var B1 = Variable.GaussianFromMeanAndPrecision(3, 4);
    	var C1 = Variable.New<double>();
    
    	C1 = A1 + B1;
    
    	// Example of inference: Observe C, infer A and B
    	C1.ObservedValue = 2.5;
    	var engine = new InferenceEngine();
    
    	Console.WriteLine(engine.Infer(A1));
    	Console.WriteLine(engine.Infer(B1));
    
    	// ------------------------------
    	// Continuous/discrete example.
    	// ------------------------------
    	var A2 = Variable.GaussianFromMeanAndPrecision(0, 1);
    	double[] discreteProbs = new double[] {0.4, 0.2, 0.3};
    	Range valueRange = new Range(discreteProbs.Length);
    	var B2 = Variable.Discrete(valueRange, discreteProbs);
    	B2.SetValueRange(valueRange);
    
    	// The values to sum conditioned on the discrete value.
    	var B2ValuesToSum = Variable.Array<double>(valueRange);
    	B2ValuesToSum.ObservedValue = new double[] { 1.0, 2.0, 3.0 };
    	var C2 = Variable.New<double>();
    
    	using (Variable.Switch(B2))
    		C2 = A2 + B2ValuesToSum[B2];
    
    	// Example of inference: Observe C, infer A and B
    	C2.ObservedValue = 2.5;
    
    	Console.WriteLine(engine.Infer(A2));
    	Console.WriteLine(engine.Infer(B2));
    }


    Tuesday, February 19, 2013 8:50 AM
    Owner
  • Thank you John, so the operators "+", "-" are overwritten for the different types of variables, that is nice! And if I were interested in learning weights associated with the children, should I use the softmax feature as described here?

    (http://blogs.msdn.com/b/infernet_team_blog/archive/2011/09/30/new-features-in-infer-net-2-4-the-softmax-factor.aspx)

    Thank you!

    Thursday, February 21, 2013 12:05 AM
  • Hi Alex

    Operator overrides for random variables are described here. The factors and constraints section shows other functions that you can use to build up models. The softmax factor is just one of these building blocks which may or may not be suitable for your problem (it maps an array of random variables to a probability vector).

    You first need to decide what model it is that you want to build. This is often the trickiest part because every problem is different. To get the most out of your data you should consider building a custom model that designed based both on an analysis of your data and on domain knowledge. However, a standard model such as multinomial softmax regression may be a good starting point if it is appropriate for your data. There are a lot of other examples of different types of standard models in the Tutorials and Examples section. Also, make sure you check out Infer.Net 101 which shows some best practice for coding up models.

    This forum can provide help if the user guide is unclear about how to go about implementing your model, or if you run into difficulties or errors. But in general we cannot advise on what is an appropriate model for a specific problem/data set.

    John

    Thursday, February 21, 2013 11:58 AM
    Owner
  • Hi John,

    Many thanks for your responses here and to my other question! I am trying to get a sense of the flexibility/scalability of Infer.NET as a potential framework to test multiple models which motivated my questions above. I will be sure to ask about my specific model next time.

    Alex

    Monday, February 25, 2013 9:56 PM