locked
beginner with infer.net RRS feed

  • Question

  • Hi every body

    I wonder why there is not a new result for w, when I set new observed values?

    Thanks!

    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                testc();
            }
    
            static void testc(){
                InferenceEngine inferenceEngine = new InferenceEngine();
                Variable<double> S = Variable.New<double>().Named("S");
                Variable<double> T = Variable.New<double>().Named("T");
                Variable<double> U = Variable.New<double>().Named("U");
                Variable<double> a = Variable.GaussianFromMeanAndVariance(0, 3).Named("a");
                Variable<double> c = Variable.GaussianFromMeanAndVariance(1.2, 2).Named("c");
                Variable<double> W = Variable.GaussianFromMeanAndVariance(1, 2).Named("w");
                U = T * a + S * c * W;            
    
                S.ObservedValue = 1;
                T.ObservedValue = 2;
                U.ObservedValue = 0.74;
                
    
                Console.WriteLine("W: " + inferenceEngine.Infer(W));
    
                Gaussian aPosterior = inferenceEngine.Infer<Gaussian>(a);
                Gaussian cPosterior = inferenceEngine.Infer<Gaussian>(c);
                Gaussian WPosterior = inferenceEngine.Infer<Gaussian>(W);
    
                a = Variable.Random(aPosterior);
                c = Variable.Random(cPosterior);
                W = Variable.Random(WPosterior);
                
                S.ObservedValue = 1.3;
                T.ObservedValue = 0.74;
                U.ObservedValue = 0.404;
                
                Console.WriteLine("W: " + inferenceEngine.Infer(W));
    
            }
        }
    }

    Sunday, August 10, 2014 9:45 PM

Answers

  • In the line W = Variable.Random(WPosterior); you have created a new definition for w and it no longer has the previous definition. i.e. it is now defined as a random variable whose prior is the result of the previous inference, and so the inference is correct.

    If you want to keep the model the same, best to encapsulate it in a class, and have the priors settable at run time: 

    public class TestModel
        {
            InferenceEngine inferenceEngine = new InferenceEngine();
            Variable<double> S = Variable.New<double>().Named("S");
            Variable<double> T = Variable.New<double>().Named("T");
            Variable<double> U = Variable.New<double>().Named("U");
            Variable<double> a = Variable.New<double>().Named("a");
            Variable<double> c = Variable.New<double>().Named("c");
            Variable<double> W = Variable.New<double>().Named("w");
            Variable<Gaussian> aPrior = Variable.New<Gaussian>().Named("aPrior");
            Variable<Gaussian> cPrior = Variable.New<Gaussian>().Named("cPrior");
            Variable<Gaussian> WPrior = Variable.New<Gaussian>().Named("WPrior");

            public class TestModelResults
            {
                public Gaussian aPosterior;
                public Gaussian cPosterior;
                public Gaussian WPosterior;
            }

            public void CreateModel()
            {
                this.a = Variable<double>.Random(aPrior);
                this.c = Variable<double>.Random(cPrior);
                this.W = Variable<double>.Random(WPrior);
                this.U = this.T * this.a + this.S * this.c * this.W;
            }

            public TestModelResults Run(
                double s,
                double t,
                double u,
                Gaussian aprior,
                Gaussian cprior,
                Gaussian wprior)
            {
                this.S.ObservedValue = s;
                this.T.ObservedValue = t;
                this.U.ObservedValue = u;
                this.aPrior.ObservedValue = aprior;
                this.cPrior.ObservedValue = cprior;
                this.WPrior.ObservedValue = wprior;

                return new TestModelResults()
                {
                    aPosterior = this.inferenceEngine.Infer<Gaussian>(this.a),
                    cPosterior = this.inferenceEngine.Infer<Gaussian>(this.c),
                    WPosterior = this.inferenceEngine.Infer<Gaussian>(this.W),
                };
            }
        }

    Then you can call as follows:

     
    var model = new TestModel();
    model.CreateModel();
    var posteriors = model.Run(
            1, 2, 0.74,
            Gaussian.FromMeanAndVariance(0, 3),
            Gaussian.FromMeanAndVariance(1.2, 2),
            Gaussian.FromMeanAndVariance(1, 2));

    Console.WriteLine(posteriors.WPosterior);

    posteriors = model.Run(
            1.3, 0.74, 0.404,
            posteriors.aPosterior,
            posteriors.cPosterior,
            posteriors.WPosterior);

    Console.WriteLine(posteriors.WPosterior);

    • Marked as answer by Capli19 Monday, August 11, 2014 9:58 AM
    Monday, August 11, 2014 9:29 AM
    Owner