locked
Exponents of a variable in the model RRS feed

  • Question

  • Hello infer.NET support team,

    I have a model in with a variable to the power of two or three in it. Running the model I get this error:

    An unhandled exception of type 'System.Exception' occurred in Infer.Runtime.dll
    Additional information: x is nan

    Can you give some guides on this please? How can I change the priors to not get this error? 

    Here 's a simple example of what I try to do:

    public static double testFunction(double x) {
                return (0.25 * x + 0.5 * x * x);
            }
    
            public static Variable<double> testFunction(Variable<double> x)
            {
                return (0.25 * x + 0.5 * x * x);
            }
    
            static void Main(string[] args)
            {
                // training data
                int numData = 10;
                double[] data = new double[]{1, 3.2, 2, 4.3, 2.2, 0.2, 5.6, 2.7, 0.6, 0.11};
                double[] outputData = new double[numData];
                double coef = 0.9;
                for (int i = 0; i < numData; i++) {
                    outputData[i] = testFunction(data[i]) * coef + Gaussian.FromMeanAndVariance(0, 0.0025).Sample();
                }
    
                // model
                Range range = new Range(numData);
                VariableArray<double> inputArr = Variable.Array<double>(range);
                VariableArray<double> outputArray = Variable.Array<double>(range);
                Variable<double> coefVar = Variable.GaussianFromMeanAndPrecision(0, 0.01);
                Variable<double> precision = Variable<double>.GammaFromShapeAndRate(1, 10);
                using (Variable.ForEach(range)) {
                    outputArray[range] = Variable.GaussianFromMeanAndPrecision(testFunction(inputArr[range] * coefVar), precision); 
                }
                inputArr.ObservedValue = data;
                outputArray.ObservedValue = outputData;
                InferenceEngine ie = new InferenceEngine(new ExpectationPropagation());
                Console.WriteLine("coef possterior: " + ie.Infer<Gaussian>(coefVar));
    
                Console.ReadLine();
            }

    Sunday, February 15, 2015 5:35 PM

Answers

  • The data is not sampled from the model. In the definition of outputData[range] move the multiplication by coefVar outside the test function call.

    -Y-

    • Marked as answer by RazinR Friday, February 20, 2015 3:09 PM
    Thursday, February 19, 2015 6:48 PM

All replies

  • This is an issue with the product factor under EP.  Use a different inference algorithm or try adding the following line:

                ie.Compiler.GivePriorityTo(typeof(GaussianProductOp_SHG09));
    

    Tuesday, February 17, 2015 5:43 PM
    Owner
  • Thanks. It seems using either approaches, it does not converge even after thousands of iterations. And the final answer is also not close to real value of the variable which I'm trying to infer.

    I wonder if there is something special wrong in my model. Can you please have  a look at my code?

    static void Main(string[] args)
            {
                // training data
                int numData = 100;
                var rand = new Random();
                double[] data = new double[numData] ;
                for (int i = 0; i < numData; i++) {
                    data[i] = rand.Next(10);
                }
                double[] outputData = new double[numData];
                double coef = 0.9;
                for (int i = 0; i < numData; i++)
                {
                    outputData[i] = testFunction(data[i]) * coef + Gaussian.FromMeanAndVariance(0, 0.0025).Sample();// noise is also added
                }
    
                // model
                Range range = new Range(numData);
                VariableArray<double> inputArr = Variable.Array<double>(range);
                VariableArray<double> outputArray = Variable.Array<double>(range);
                Variable<double> coefVar = Variable.GaussianFromMeanAndPrecision(0, 0.01);
                Variable<double> precision = Variable<double>.GammaFromShapeAndRate(1, 10);
                using (Variable.ForEach(range))
                {
                    outputArray[range] = Variable.GaussianFromMeanAndPrecision(testFunction(inputArr[range] * coefVar), precision);
                }
                inputArr.ObservedValue = data;
                outputArray.ObservedValue = outputData;
                InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing());
                //InferenceEngine ie = new InferenceEngine(new ExpectationPropagation());
                ie.NumberOfIterations = 3100;
                //ie.Compiler.GivePriorityTo(typeof(GaussianProductOp_SHG09));
                Console.WriteLine("coef possterior: " + ie.Infer<Gaussian>(coefVar));
    
                Console.ReadLine();
            }
    

    Wednesday, February 18, 2015 5:02 PM
  • The data is not sampled from the model. In the definition of outputData[range] move the multiplication by coefVar outside the test function call.

    -Y-

    • Marked as answer by RazinR Friday, February 20, 2015 3:09 PM
    Thursday, February 19, 2015 6:48 PM