locked
A confusing problem from Clinical Trial RRS feed

  • Question

  • I repeat the Clinical Trial in the user guide with my modified code and I met this problem. The outcome varies from the position where priors are inserted. I wonder what the difference is between the following two models. Thanks for your viewing.

    The code with expected results is shown below:

                //initialize observation
                VariableArray<bool> controlGroup = Variable.Observed(new bool[] { false, false, true, false, false });  
                Range i=controlGroup.Range;
                VariableArray<bool> treatGroup = Variable.Observed(new bool[] { true, false, true, true, true });
                Range j=treatGroup.Range;
    
                Variable<bool> isEffective=Variable.Bernoulli(0.5);
                Variable<double> probControl, probTreat, probAll;
     
    
    
                using(Variable.If(isEffective)){
                    probTreat = Variable.Beta(1, 1);            //insert prior here
                    probControl = Variable.Beta(1, 1);          //insert prior here
                    controlGroup[i]=Variable.Bernoulli(probControl).ForEach(i);
                    treatGroup[j]=Variable.Bernoulli(probTreat).ForEach(j);
                }
    
                using(Variable.IfNot(isEffective)){
                    probAll = Variable.Beta(1, 1);              //insert prior here
                    controlGroup[i]=Variable.Bernoulli(probAll).ForEach(i);
                    treatGroup[j]=Variable.Bernoulli(probAll).ForEach(j);
                }
    
                InferenceEngine ie=new InferenceEngine();       //initialize Inference Engine
                if(!(ie.Algorithm is GibbsSampling)){
                    Console.WriteLine("Probability of good outcome if control = " + ie.Infer<Beta>(probControl).GetMean());
                    Console.WriteLine("Probability of good outcome if given treatment = " + ie.Infer<Beta>(probTreat).GetMean());
                    Console.ReadLine();
                }


    The outcome is:

    Probability of good outcome if control = 0.285714

    Probability of good outcome if given treatment = 0.714286

    After the prior position changed, the code with unexpected results is shown below:

                //initialize observation
                VariableArray<bool> controlGroup = Variable.Observed(new bool[] { false, false, true, false, false });  
                Range i=controlGroup.Range;
                VariableArray<bool> treatGroup = Variable.Observed(new bool[] { true, false, true, true, true });
                Range j=treatGroup.Range;
    
                Variable<bool> isEffective=Variable.Bernoulli(0.5);
                Variable<double> probControl, probTreat, probAll;
                probTreat = Variable.Beta(1, 1);            //insert prior here
                probControl = Variable.Beta(1, 1);          //insert prior here
                probAll = Variable.Beta(1, 1);              //insert prior here
    
                using(Variable.If(isEffective)){
                    controlGroup[i]=Variable.Bernoulli(probControl).ForEach(i);
                    treatGroup[j]=Variable.Bernoulli(probTreat).ForEach(j);
                }
    
                using(Variable.IfNot(isEffective)){
                    controlGroup[i]=Variable.Bernoulli(probAll).ForEach(i);
                    treatGroup[j]=Variable.Bernoulli(probAll).ForEach(j);
                }
    
                InferenceEngine ie=new InferenceEngine();       //initialize Inference Engine
                if(!(ie.Algorithm is GibbsSampling)){
                    Console.WriteLine("Probability of good outcome if control = " + ie.Infer<Beta>(probControl).GetMean());
                    Console.WriteLine("Probability of good outcome if given treatment = " + ie.Infer<Beta>(probTreat).GetMean());
                    Console.ReadLine();
                }

    The outcome is:

    Probability of good outcome if control = 0.338235

    Probability of good outcome if given treatment = 0.661765





    • Edited by Weipeng0098 Sunday, August 28, 2016 6:53 AM Format
    Sunday, August 28, 2016 6:48 AM

All replies

  • Infer.NET does not know where a variable is declared.  It only knows where a variable is defined (in this case, by the call to Variable.Beta).  So when you move the call to Variable.Beta outside the 'if' then the variable is moved outside of the corresponding gate in the factor graph, and this changes the answer under approximate inference.
    Sunday, August 28, 2016 2:31 PM
    Owner