Ejemplo n.º 1
0
		/// <summary>
		/// Constructs an LDA model
		/// </summary>
		/// <param name="sizeVocab">Size of vocabulary</param>
		/// <param name="numTopics">Number of topics</param>
		public LDAPredictionModel(int sizeVocab, int numTopics)
		{
			SizeVocab = sizeVocab;
			NumTopics = numTopics;
			PredictionSparsity = Sparsity.Sparse;

			//---------------------------------------------
			// The model
			//---------------------------------------------
			Range W = new Range(SizeVocab).Named("W");
			Range T = new Range(NumTopics).Named("T");

			ThetaPrior = Variable.New<Dirichlet>().Named("ThetaPrior");
			PhiPrior = Variable.Array<Dirichlet>(T).Named("PhiPrior").Attrib(new ValueRange(W));
			Theta = Variable.New<Vector>().Named("Theta");
			Phi = Variable.Array<Vector>(T).Named("Phi");
			Theta = Variable.Random<Vector, Dirichlet>(ThetaPrior);
			Phi[T] = Variable.Random<Vector, Dirichlet>(PhiPrior[T]);

			Word = Variable.New<int>().Named("Word");
			Word.SetSparsity(PredictionSparsity);
			var topic = Variable.Discrete(Theta).Attrib(new ValueRange(T)).Named("topic");
			using (Variable.Switch(topic))
				Word.SetTo(Variable.Discrete(Phi[topic]));
			Engine = new InferenceEngine(new VariationalMessagePassing());
			Engine.Compiler.ShowWarnings = false;
		}
Ejemplo n.º 2
0
        public DistributionArray <Bernoulli> TestBPM_LabelNoise(double initCount, out double noiseEstimate)
        {
            noiseEstimate = double.NaN;
            // data
            int K = xtrain[0].Count;
            // Create target y
            VariableArray <bool> y = Variable.Observed(ytrain).Named("y");
            Variable <Vector>    w = Variable.Random(new VectorGaussian(Vector.Zero(K),
                                                                        PositiveDefiniteMatrix.Identity(K))).Named("w");
            var ProbTrue  = Variable.Beta(1, initCount).Named("TPR");
            var ProbFalse = Variable.Beta(initCount, 1).Named("FNR");
            var mean      = Variable.GaussianFromMeanAndPrecision(0, .1).Named("mean");

            BayesPointMachine_LabelNoise(xtrain, w, y, ProbTrue, ProbFalse, mean);
            InferenceEngine engine        = new InferenceEngine();
            VectorGaussian  wPosterior    = engine.Infer <VectorGaussian>(w);
            Beta            probTruePost  = engine.Infer <Beta>(ProbTrue);
            Beta            probFalsePost = engine.Infer <Beta>(ProbFalse);
            //Console.WriteLine("Dist over w=\n" + wPosterior);
            //Console.WriteLine("Dist over p_t=\n" + probTruePost);
            //Console.WriteLine("Dist over p_f=\n" + probFalsePost);
            var meanPost            = engine.Infer <Gaussian>(mean);
            VariableArray <bool> yt = Variable.Array <bool>(new Range(ytest.Length)).Named("ytest");

            BayesPointMachine_LabelNoise(xtest, Variable.Random(wPosterior).Named("w"), yt, Variable.Random(probTruePost), Variable.Random(probFalsePost),
                                         Variable.Random(meanPost));
            return(engine.Infer <DistributionArray <Bernoulli> >(yt));
        }
Ejemplo n.º 3
0
        public void AlexJames()
        {
            // Data about tasks: true = correct, false = incorrect
            bool[] TaskA_Math = { false, true, true, true, true, true, true, true, false, false };

            // Range for A
            int   numA       = TaskA_Math.Length;
            Range taskARange = new Range(numA);

            VariableArray <bool> data = Variable.Array <bool>(taskARange);

            // The prior
            var taskA_performance = Variable.Beta(1, 1);

            // The likelihood model
            data[taskARange] = Variable.Bernoulli(taskA_performance).ForEach(taskARange);

            // Attach data
            data.ObservedValue = TaskA_Math;

            // Inference engine (EP)
            InferenceEngine engine = new InferenceEngine();

            // Infer probability of Task A Math so can predict Task B_Math etc.
            Console.WriteLine("isCorrect = {0}", engine.Infer <Beta>(taskA_performance).GetMean());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructs a new Glass/Sprinkler/Rain model
        /// </summary>
        public TesteModel()
        {
            // Set up the ranges
            NumberOfExamples = Variable.New <int>().Named("NofE");
            Range N = new Range(NumberOfExamples).Named("N");

            // Embora todas as variáveis neste exemplo tenham apenas 2 estados (true/false),
            // o exemplo é formulado de uma forma que mostra como se estender a vários estados
            Range C = new Range(2).Named("C");
            Range S = new Range(2).Named("S");


            // Definir as prioridades e os parâmetros
            ProbCloudyPrior = Variable.New <Dirichlet>().Named("ProbCloudyPrior");
            ProbCloudy      = Variable <Vector> .Random(ProbCloudyPrior).Named("ProbCloudy");

            ProbCloudy.SetValueRange(C);

            // Tabela de probabilidade do sprinkler condicionada estar nublado
            CPTSprinklerPrior = Variable.Array <Dirichlet>(C).Named("CPTSprinklerPrior");
            CPTSprinkler      = Variable.Array <Vector>(C).Named("CPTSprinkler");
            CPTSprinkler[C]   = Variable <Vector> .Random(CPTSprinklerPrior[C]);

            CPTSprinkler.SetValueRange(S);



            // Definir as principais variáveis
            Cloudy    = Variable.Array <int>(N).Named("Cloudy");
            Cloudy[N] = Variable.Discrete(ProbCloudy).ForEach(N);
            Sprinkler = AddChildFromOneParent(Cloudy, CPTSprinkler).Named("Sprinkler");
        }
Ejemplo n.º 5
0
        public SentimentIndex()
            : base("Sentiment Index")
        {
            numberOfTrainingItems = Variable.New<int>();
            var rangeOfTrainingItems = new Range(numberOfTrainingItems);
            trainingInputs = Variable.Array<Vector>(rangeOfTrainingItems);
            trainingOutputs = Variable.Array<bool>(rangeOfTrainingItems);

            weights = Variable.Random(new VectorGaussian(Vector.Zero(numberOfFeatures), PositiveDefiniteMatrix.Identity(numberOfFeatures)));

            using (Variable.ForEach(rangeOfTrainingItems))
            {
                trainingOutputs[rangeOfTrainingItems] = Variable.IsPositive(Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(weights, trainingInputs[rangeOfTrainingItems]), noise));
            }

            trainingEngine = new InferenceEngine();
            trainingEngine.ShowProgress = false;

            numberOfTestingItems = Variable.New<int>();
            var rangeOfTestingItems = new Range(numberOfTestingItems);
            testingInputs = Variable.Array<Vector>(rangeOfTestingItems);
            testingOutputs = Variable.Array<bool>(rangeOfTestingItems);

            weightsPosteriorDistribution = Variable.New<VectorGaussian>();
            var testWeights = Variable<Vector>.Random(weightsPosteriorDistribution);

            using (Variable.ForEach(rangeOfTestingItems))
            {
                testingOutputs[rangeOfTestingItems] = Variable.IsPositive(Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(testWeights, testingInputs[rangeOfTestingItems]), noise));
            }

            testingEngine = new InferenceEngine();
            testingEngine.ShowProgress = false;
        }
        // This factor is not included as a Variable.IndexOfMaximum shortcut
        // because you generally get better results with the expanded version, when part of a larger model.
        // Eventually the factor should be deprecated.
        public static Variable <int> IndexOfMaximum(VariableArray <double> array)
        {
            var p = Variable <int> .Factor(MMath.IndexOfMaximumDouble, array).Named("p");

            p.SetValueRange(array.Range);
            return(p);
        }
Ejemplo n.º 7
0
        public void ArgumentTest()
        {
            using (var map = new MemoryMapStream())
            {
                using (var array = new VariableArray <string>(map, 1024, 10))
                {
                    Assert.AreEqual(10, array.Length);
                    Assert.Catch <ArgumentOutOfRangeException>(() =>
                    {
                        array[1001] = 10.ToString();
                    });
                    Assert.Catch <ArgumentOutOfRangeException>(() =>
                    {
                        array[-1] = 10.ToString();
                    });

                    string value;
                    Assert.Catch <ArgumentOutOfRangeException>(() =>
                    {
                        value = array[1001];
                    });
                    Assert.Catch <ArgumentOutOfRangeException>(() =>
                    {
                        value = array[-1];
                    });
                }
            }
        }
Ejemplo n.º 8
0
		public ClinicalTrialModel()
		{
			numberPlacebo = Variable.New<int>().Named("numberPlacebo");
			numberTreated = Variable.New<int>().Named("numberTreated");
			Range P = new Range(numberPlacebo);
			Range T = new Range(numberTreated);
			placeboGroupOutcomes = Variable.Observed<bool>(new bool[] { true }, P).Named("placeboGroupOutcomes");
			treatedGroupOutcomes = Variable.Observed<bool>(new bool[] { true }, T).Named("treatedGroupOutcomes");

			isEffective = Variable.Bernoulli(0.2).Named("isEffective");
			using (Variable.If(isEffective))
			{
				// Model if treatment is effective
				probIfPlacebo = Variable.Beta(1, 1).Named("probIfPlacebo");
				placeboGroupOutcomes[P] = Variable.Bernoulli(probIfPlacebo).ForEach(P);
				probIfTreated = Variable.Beta(1, 1).Named("probIfTreated");
				treatedGroupOutcomes[T] = Variable.Bernoulli(probIfTreated).ForEach(T);
			}
			using (Variable.IfNot(isEffective))
			{
				// Model if treatment is not effective
				Variable<double> probAll = Variable.Beta(1, 1).Named("probAll");
				placeboGroupOutcomes[P] = Variable.Bernoulli(probAll).ForEach(P);
				treatedGroupOutcomes[T] = Variable.Bernoulli(probAll).ForEach(T);
			}
		}
Ejemplo n.º 9
0
        public DistributionArray <Bernoulli> TestNaiveBayes(double a, out double noiseEstimate)
        {
            noiseEstimate = double.NaN;
            int K = xtrain[0].Count;
            // Create target y
            VariableArray <bool> y        = Variable.Observed(ytrain).Named("y");
            Variable <Vector>    meanTrue = Variable.Random(new VectorGaussian(Vector.Zero(K),
                                                                               PositiveDefiniteMatrix.Identity(K))).Named("m1");
            Variable <Vector> meanFalse = Variable.Random(new VectorGaussian(Vector.Zero(K),
                                                                             PositiveDefiniteMatrix.Identity(K))).Named("m2");
            var precTrue  = Variable.Random(new Wishart(a, PositiveDefiniteMatrix.Identity(K)));
            var precFalse = Variable.Random(new Wishart(a, PositiveDefiniteMatrix.Identity(K)));

            NaiveBayes(xtrain, meanTrue, meanFalse, precTrue, precFalse, y);
            //InferenceEngine.DefaultEngine.Compiler.UseSerialSchedules = true;
            InferenceEngine      engine        = new InferenceEngine(new VariationalMessagePassing());
            var                  meanTruePost  = engine.Infer <VectorGaussian>(meanTrue);
            var                  meanFalsePost = engine.Infer <VectorGaussian>(meanFalse);
            var                  precTruePost  = engine.Infer <Wishart>(precTrue);
            var                  precFalsePost = engine.Infer <Wishart>(precFalse);
            var                  testRange     = new Range(ytest.Length);
            VariableArray <bool> yt            = Variable.Array <bool>(testRange).Named("ytest");

            yt[testRange] = Variable.Bernoulli(0.5).ForEach(testRange);
            NaiveBayes(xtest, Variable.Random(meanTruePost), Variable.Random(meanFalsePost), Variable.Random <PositiveDefiniteMatrix>(precTruePost),
                       Variable.Random <PositiveDefiniteMatrix>(precFalsePost), yt);
            return(engine.Infer <DistributionArray <Bernoulli> >(yt));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Constructs an LDA model
        /// </summary>
        /// <param name="sizeVocab">Size of vocabulary</param>
        /// <param name="numTopics">Number of topics</param>
        public LDAPredictionModel(int sizeVocab, int numTopics)
        {
            SizeVocab          = sizeVocab;
            NumTopics          = numTopics;
            PredictionSparsity = Sparsity.Sparse;

            //---------------------------------------------
            // The model
            //---------------------------------------------
            var W = new Range(SizeVocab).Named("W");
            var T = new Range(NumTopics).Named("T");

            ThetaPrior = Variable.New <Dirichlet>().Named("ThetaPrior");
            PhiPrior   = Variable.Array <Dirichlet>(T).Named("PhiPrior").Attrib(new ValueRange(W));
            Theta      = Variable.New <Vector>().Named("Theta");
            Phi        = Variable.Array <Vector>(T).Named("Phi");
            Theta      = Variable.Random <Vector, Dirichlet>(ThetaPrior);
            Phi[T]     = Variable.Random <Vector, Dirichlet>(PhiPrior[T]);

            Word = Variable.New <int>().Named("Word");
            Word.SetSparsity(PredictionSparsity);
            var topic = Variable.Discrete(Theta).Attrib(new ValueRange(T)).Named("topic");

            using (Variable.Switch(topic))
            {
                Word.SetTo(Variable.Discrete(Phi[topic]));
            }

            Engine = new InferenceEngine(new VariationalMessagePassing());
            Engine.Compiler.ShowWarnings = false;
        }
Ejemplo n.º 11
0
        public void Mixture1()
        {
            double[] data            = { 7 };
            int      T               = data.Length;
            VariableArray <double> x = Variable.Constant(data).Named("data");
            Range             i      = x.Range;
            Variable <Vector> D      = Variable.Dirichlet(new double[] { 1, 1 });

            VariableArray <int> c = Variable.Array <int>(i);

            using (Variable.ForEach(i))
            {
                c[i] = Variable.Discrete(D);
                using (Variable.Case(c[i], 0))
                {
                    x[i] = Variable.GaussianFromMeanAndPrecision(5.5, 1);
                }
                using (Variable.Case(c[i], 1))
                {
                    x[i] = Variable.GaussianFromMeanAndPrecision(8, 1);
                }
            }

            InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing());

            double[] DVibesResult = new double[] { -1.15070203880043, -0.67501576717763 };
            VmpTests.TestDirichletMoments(ie, D, DVibesResult);
            double[][] cVibesResult = new double[T][];
            cVibesResult[0] = new double[] { 0.24961241199438, 0.75038758800562 };
            VmpTests.TestDiscrete(ie, c, cVibesResult);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Constructs the model.
        /// </summary>
        public new void ConstructModel()
        {
            this.numberOfPeople    = Variable.New <int>().Named("numPeople").Attrib(new DoNotInfer());
            this.numberOfSkills    = Variable.New <int>().Named("numSkills").Attrib(new DoNotInfer());
            this.numberOfQuestions = Variable.New <int>().Named("numQuestions").Attrib(new DoNotInfer());

            this.People    = new Range(this.numberOfPeople).Named("people");
            this.Skills    = new Range(this.numberOfSkills).Named("skills");
            this.Questions = new Range(this.numberOfQuestions).Named("questions");

            this.numberOfSkillsForEachQuestion = Variable.Array <int>(this.Questions).Named("numSkillsForQuestions").Attrib(new DoNotInfer());
            var questionsSkills = new Range(this.numberOfSkillsForEachQuestion[this.Questions]).Named("questionsXskills");

            this.skillsForQuestion = Variable.Array(Variable.Array <int>(questionsSkills), this.Questions).Named("skillsForQuestion").Attrib(new DoNotInfer());

            this.hasSkills = Variable.Array(Variable.Array <bool>(this.Skills), this.People).Named("hasSkills");
            this.hasSkills[this.People][this.Skills] = Variable.Bernoulli(0.5).ForEach(this.People, this.Skills);

            this.isCorrect = Variable.Array(Variable.Array <bool>(this.Questions), this.People).Named("isCorrect");

            using (Variable.ForEach(this.People))
            {
                using (Variable.ForEach(this.Questions))
                {
                    var skillsForThisQuestion = Variable.Subarray(this.hasSkills[this.People], this.skillsForQuestion[this.Questions]).Named("skillsForThisQuestion");
                    this.isCorrect[this.People][this.Questions] = Factors.NoisyAllTrue(skillsForThisQuestion, 0.5, 0.5);
                }
            }
        }
Ejemplo n.º 13
0
            public BpmPredict()
            {
                nFeatures = Variable.New <int>().Named("nFeatures");
                Range feature = new Range(nFeatures).Named("feature");

                w = Variable.Array <double>(feature).Named("w");
                //VariableArray<Gaussian> wPrior = Variable.Array<Gaussian>(feature).Named("wPrior");
                //w[feature] = Variable<double>.Random(wPrior[feature]);
                wPrior = Variable.New <GaussianArray>().Named("wPrior");
                w.SetTo(Variable <double[]> .Random(wPrior));
                biasPrior = Variable.New <Gaussian>().Named("biasPrior");
                bias      = Variable <double> .Random(biasPrior).Named("bias");

                xValueCount = Variable.New <int>().Named("xValueCount");
                Range userFeature = new Range(xValueCount).Named("userFeature");

                xValues  = Variable.Array <double>(userFeature).Named("xValues");
                xIndices = Variable.Array <int>(userFeature).Named("xIndices");
                y        = Variable.New <bool>().Named("y");
                VariableArray <double> product = Variable.Array <double>(userFeature).Named("product");
                VariableArray <double> wSparse = Variable.Subarray(w, xIndices);

                product[userFeature] = xValues[userFeature] * wSparse[userFeature];
                Variable <double> score = Variable.Sum(product).Named("score");

                y      = (Variable.GaussianFromMeanAndVariance(score + bias, 1.0) > 0);
                engine = new InferenceEngine();
                engine.Compiler.FreeMemory   = false;
                engine.Compiler.ReturnCopies = false;
                engine.OptimiseForVariables  = new IVariable[] { y };
                engine.ModelName             = "BpmPredict";
            }
Ejemplo n.º 14
0
        public SoftmaxCategoricalOneParentNonShared(ModelNodeNonShared node)
        {
            this.node = node;
            Range parentRange = null;

            foreach (var parent in node.parents)
            {
                if (parent.distributionType == DistributionType.Categorical)
                {
                    parentRange = parent.states;
                }
            }
            Bprior = Variable.Array(Variable.Array <VectorGaussian>(parentRange), node.states).Named(node.name + "CoefficientsPrior");
            Bprior.ObservedValue = (node.original.distributions as SoftmaxOneParentCategorical).Bprior;

            B = Variable.Array(Variable.Array <Vector>(parentRange), node.states).Named(node.name + "B");
            B[node.states][parentRange] = Variable <Vector> .Random(Bprior[node.states][parentRange]);

            mPrior = Variable.Array(Variable.Array <Gaussian>(parentRange), node.states).Named(node.name + "mPrior");
            mPrior.ObservedValue = (node.original.distributions as SoftmaxOneParentCategorical).mPrior;

            m = Variable.Array(Variable.Array <double>(parentRange), node.states).Named(node.name + "m");
            m[node.states][parentRange] = Variable <double> .Random(mPrior[node.states][parentRange]);


            Variable.ConstrainEqualRandom(B[node.states.SizeAsInt - 1][parentRange], VectorGaussian.PointMass(Vector.Zero(node.parents.Count - 1)));
            Variable.ConstrainEqualRandom(m[node.states.SizeAsInt - 1][parentRange], Gaussian.PointMass(0));
        }
Ejemplo n.º 15
0
        public void ArgumentTest()
        {
            using (var map = new MemoryMapStream())
            {
                using (var array = new VariableArray<string>(map, 1024, 10))
                {
                    Assert.AreEqual(10, array.Length);
                    Assert.Catch<ArgumentOutOfRangeException>(() =>
                    {
                        array[1001] = 10.ToString();
                    });
                    Assert.Catch<ArgumentOutOfRangeException>(() =>
                    {
                        array[-1] = 10.ToString();
                    });

                    string value;
                    Assert.Catch<ArgumentOutOfRangeException>(() =>
                    {
                        value = array[1001];
                    });
                    Assert.Catch<ArgumentOutOfRangeException>(() =>
                    {
                        value = array[-1];
                    });
                }
            }
        }
Ejemplo n.º 16
0
        public ClinicalTrialModel()
        {
            numberPlacebo = Variable.New <int>().Named("numberPlacebo");
            numberTreated = Variable.New <int>().Named("numberTreated");
            Range P = new Range(numberPlacebo);
            Range T = new Range(numberTreated);

            placeboGroupOutcomes = Variable.Observed <bool>(new bool[] { true }, P).Named("placeboGroupOutcomes");
            treatedGroupOutcomes = Variable.Observed <bool>(new bool[] { true }, T).Named("treatedGroupOutcomes");

            isEffective = Variable.Bernoulli(0.2).Named("isEffective");
            using (Variable.If(isEffective))
            {
                // Model if treatment is effective
                probIfPlacebo           = Variable.Beta(1, 1).Named("probIfPlacebo");
                placeboGroupOutcomes[P] = Variable.Bernoulli(probIfPlacebo).ForEach(P);
                probIfTreated           = Variable.Beta(1, 1).Named("probIfTreated");
                treatedGroupOutcomes[T] = Variable.Bernoulli(probIfTreated).ForEach(T);
            }

            using (Variable.IfNot(isEffective))
            {
                // Model if treatment is not effective
                Variable <double> probAll = Variable.Beta(1, 1).Named("probAll");
                placeboGroupOutcomes[P] = Variable.Bernoulli(probAll).ForEach(P);
                treatedGroupOutcomes[T] = Variable.Bernoulli(probAll).ForEach(T);
            }

            engine.Compiler.WriteSourceFiles = false;
            engine.Compiler.GenerateInMemory = true;
        }
Ejemplo n.º 17
0
        public DistributionArray <Bernoulli> TestBPM_NoiseVariancePlusLabel(double noiseRate, out double noiseEstimate)
        {
            int K = xtrain[0].Count;
            // Create target y
            VariableArray <bool> y = Variable.Observed(ytrain).Named("y");
            Variable <Vector>    w = Variable.Random(new VectorGaussian(Vector.Zero(K),
                                                                        PositiveDefiniteMatrix.Identity(K))).Named("w");
            var ProbTrue  = Variable.Beta(1, 10).Named("TPR");
            var ProbFalse = Variable.Beta(10, 1).Named("FNR");

            var mean  = Variable.GaussianFromMeanAndPrecision(0, .1);
            var noise = Variable.GammaFromShapeAndRate(2, noiseRate);

            //var noise = Variable.Random(Gamma.PointMass(.1));
            BayesPointMachine_NoiseVariancePlusLabel(xtrain, w, y, mean, noise, ProbTrue, ProbFalse);
            InferenceEngine engine     = new InferenceEngine();
            VectorGaussian  wPosterior = engine.Infer <VectorGaussian>(w);
            var             noisePost  = engine.Infer <Gamma>(noise);

            noiseEstimate = noisePost.GetMean();
            Beta probTruePost  = engine.Infer <Beta>(ProbTrue);
            Beta probFalsePost = engine.Infer <Beta>(ProbFalse);
            //Console.WriteLine("Dist over w=\n" + wPosterior);
            //Console.WriteLine("Dist over noise=\n" + noisePost);
            var meanPost            = engine.Infer <Gaussian>(mean);
            VariableArray <bool> yt = Variable.Array <bool>(new Range(ytest.Length)).Named("ytest");

            BayesPointMachine_NoiseVariancePlusLabel(xtest, Variable.Random(wPosterior).Named("w"), yt, Variable.Random(meanPost), Variable.Random(noisePost),
                                                     Variable.Random(probTruePost), Variable.Random(probFalsePost));
            return(engine.Infer <DistributionArray <Bernoulli> >(yt));
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Initialises the states randomly.
        /// </summary>
        public void InitialiseStatesRandomly()
        {
            VariableArray <Discrete> zinit = Variable <Discrete> .Array(T);

            zinit.ObservedValue = Util.ArrayInit(T.SizeAsInt, t => Discrete.PointMass(Rand.Int(K.SizeAsInt), K.SizeAsInt));
            States[T].InitialiseTo(zinit[T]);
        }
Ejemplo n.º 19
0
        public virtual void CreateModel()
        {
            NumComponents = 2;
            Range ComponentRange = new Range(NumComponents);

            InferenceEngine = new InferenceEngine(new VariationalMessagePassing());
            InferenceEngine.ShowProgress = false;

            AverageTimePriors  = Variable.Array <Gaussian>(ComponentRange);
            TrafficNoisePriors = Variable.Array <Gamma>(ComponentRange);
            AverageTime        = Variable.Array <double>(ComponentRange);
            TrafficNoise       = Variable.Array <double>(ComponentRange);

            using (Variable.ForEach(ComponentRange))
            {
                AverageTime[ComponentRange] = Variable <double> .Random(AverageTimePriors[ComponentRange]);

                TrafficNoise[ComponentRange] = Variable <double> .Random(TrafficNoisePriors[ComponentRange]);
            }

            // Mixing coefficients
            MixingPrior        = Variable.New <Dirichlet>();
            MixingCoefficients = Variable <Vector> .Random(MixingPrior);

            MixingCoefficients.SetValueRange(ComponentRange);
        }
Ejemplo n.º 20
0
        // STATUS [ July 24, 2019 ]: this works
        // * See: https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/matchup-app-infer-net
        public void RunInference(int [] winnerData, int[] loserData)
        {
            // * Check if there are equal # of ints in 'winnerData' and 'loserData'
            ConfirmThatWinnerAndLoserLengthsMatch(winnerData, loserData);

            // * Define the statistical model as a probabilistic program
            // * game: the number of Games (i.e., matchups)
            // * player: the number of Players (i.e., managers)
            Range game   = new Range(winnerData.Length);
            Range player = new Range(winnerData.Concat(loserData).Max() + 1);

            PrintGameAndPlayerInfo(game, player);

            // * playerSkills[player] = vdouble[]0[index1]
            // * playerSkills[player].Definition: GaussianFromMeanAndVariance(vdouble1,vdouble2)
            VariableArray <double> playerSkills = Variable.Array <double>(player);

            playerSkills[player] = Variable.GaussianFromMeanAndVariance(6, 9).ForEach(player);

            VariableArray <int> winners = Variable.Array <int>(game);   // * winners = vint[]0
            VariableArray <int> losers  = Variable.Array <int>(game);   // * losers = vint[]1

            using (Variable.ForEach(game))
            {
                // * The player performance is a noisy version of their skill
                // * winnerPerformance = vdouble8
                // * loserPerformance  = vdouble11
                Variable <double> winnerPerformance = Variable.GaussianFromMeanAndVariance(playerSkills[winners[game]], 1.0);
                Variable <double> loserPerformance  = Variable.GaussianFromMeanAndVariance(playerSkills[losers[game]], 1.0);

                // * The winner performed better in this game
                Variable.ConstrainTrue(winnerPerformance > loserPerformance);
            }

            // * Attach the data to the model
            // * winners.ObservedValue & losers.ObservedValue = System.Int32[]
            // * > Includes all #s in initial data set (e.g., 'winnerData)
            winners.ObservedValue = winnerData;
            losers.ObservedValue  = loserData;

            // * Run inference
            InferenceEngine inferenceEngine = new InferenceEngine();

            // * inferredSkills = Microsoft.ML.Probabilistic.Distributions.Gaussian[] with score for each manager
            // * E.g., 0: Gaussian(9.517, 3.926)
            Gaussian[] inferredSkills = inferenceEngine.Infer <Gaussian[]>(playerSkills);

            // * The inferred skills are uncertain, which is captured in their variance
            // * orderedPlayerSkills is IOrderedEnumerable<<anonymous type: int Player, Gaussian Skill>>
            // * orderedPlayerSkills is System.Linq.OrderedEnumerable`2[<>f__AnonymousType0`2[System.Int32,Microsoft.ML.Probabilistic.Distributions.Gaussian],System.Double]
            var orderedPlayerSkills = inferredSkills
                                      .Select((s, i) => new { Player = i, Skill = s, })
                                      .OrderByDescending(ps => ps.Skill.GetMean());

            // * playerSkill is <anonymous type: int Player, Gaussian Skill>>
            // * playerSkill.Player is player numbers (0, 1, 2, 3, etc)
            // * playerSkill.Skill is a skill for each player
            // * > E.g., Gaussian(9.517, 3.926), Gaussian(4.955, 3.503)
            orderedPlayerSkills.ToList().ForEach((playerSkill) => C.WriteLine($"Player {playerSkill.Player} skill: {playerSkill.Skill}"));
        }
Ejemplo n.º 21
0
        public void Run()
        {
            // data
            double[] incomes = { 63, 16, 28, 55, 22, 20 };
            double[] ages    = { 38, 23, 40, 27, 18, 40 };
            bool[]   willBuy = { true, false, true, true, false, false };

            // Create target y
            VariableArray <bool> y = Variable.Observed(willBuy).Named("y");
            Variable <Vector>    w = Variable.Random(new VectorGaussian(Vector.Zero(3), PositiveDefiniteMatrix.Identity(3))).Named("w");

            BayesPointMachine(incomes, ages, w, y);

            InferenceEngine engine = new InferenceEngine();

            if (!(engine.Algorithm is Algorithms.GibbsSampling))
            {
                VectorGaussian wPosterior = engine.Infer <VectorGaussian>(w);
                Console.WriteLine("Dist over w=\n" + wPosterior);

                double[]             incomesTest = { 58, 18, 22 };
                double[]             agesTest    = { 36, 24, 37 };
                VariableArray <bool> ytest       = Variable.Array <bool>(new Range(agesTest.Length)).Named("ytest");
                BayesPointMachine(incomesTest, agesTest, Variable.Random(wPosterior).Named("w"), ytest);
                Console.WriteLine("output=\n" + engine.Infer(ytest));
            }
            else
            {
                Console.WriteLine("This model has a non-conjugate factor, and therefore cannot use Gibbs sampling");
            }
        }
Ejemplo n.º 22
0
        public virtual void CreateModel()
        {
            engine = new InferenceEngine();

            nExperiments = Variable.New <int>().Named("numExperiments");
            nTrials      = Variable.New <int>().Named("numTrials");

            experimentRange = new Range(nExperiments).Named("experiment");

            selectorPrior = Variable.New <Beta>();
            successAPrior = Variable.New <Beta>();
            successBPrior = Variable.New <Beta>();

            probSelection = Variable.Random <double, Beta>(selectorPrior).Named("probSelection");
            probSuccessA  = Variable.Random <double, Beta>(successAPrior).Named("probSuccessA");
            probSuccessB  = Variable.Random <double, Beta>(successBPrior).Named("probSuccessB");

            data = Variable.Array <int>(experimentRange).Named("data");

            using (Variable.ForEach(experimentRange))
            {
                Variable <bool> select = Variable.Bernoulli(probSelection);

                using (Variable.If(select))
                    data[experimentRange] = Variable.Binomial(nTrials, probSuccessA);
                using (Variable.IfNot(select))
                    data[experimentRange] = Variable.Binomial(nTrials, probSuccessB);
            }
        }
Ejemplo n.º 23
0
        public ClinicalTrialModel()
        {
            numberControl = Variable.New <int>().Named("numberControl");
            numberTreated = Variable.New <int>().Named("numberTreated");
            Range controlGroup = new Range(numberControl).Named("control group");
            Range treatedGroup = new Range(numberTreated).Named("treated group");

            recoveredControl = Variable.Array <bool>(controlGroup).Named("recoveredControl");
            recoveredTreated = Variable.Array <bool>(treatedGroup).Named("recoveredTreated");

            // Whether the treatment is effective. Use uniform prior.
            model = Variable.Bernoulli(0.5).Named("model");
            using (Variable.If(model))
            {
                // Model if treatment is effective. Use uniform priors.
                probControl = Variable.Beta(1, 1).Named("probControl");
                recoveredControl[controlGroup] = Variable.Bernoulli(probControl).ForEach(controlGroup);
                probTreated = Variable.Beta(1, 1).Named("probTreated");
                recoveredTreated[treatedGroup] = Variable.Bernoulli(probTreated).ForEach(treatedGroup);
            }

            using (Variable.IfNot(model))
            {
                // Model if treatment is not effective. Use uniform prior.
                probRecovery = Variable.Beta(1, 1).Named("probRecovery");
                recoveredControl[controlGroup] = Variable.Bernoulli(probRecovery).ForEach(controlGroup);
                recoveredTreated[treatedGroup] = Variable.Bernoulli(probRecovery).ForEach(treatedGroup);
            }

            Engine = new InferenceEngine()
            {
                ShowProgress = false
            };
        }
Ejemplo n.º 24
0
        public void MixtureWithConstantSelector()
        {
            Range Trange             = new Range(2).Named("Trange");
            VariableArray <double> x = Variable.Constant(new double[] { 1, 15 }, Trange).Named("x");

            Range Krange = new Range(2).Named("Krange");
            VariableArray <double> means = Variable.Array <double>(Krange);

            means[Krange] = Variable.GaussianFromMeanAndPrecision(2, 1).ForEach(Krange);

            VariableArray <int> c = Variable.Constant(new int[] { 0, 1 }, Trange).Named("c");

            c[Trange] = Variable.DiscreteUniform(Krange).ForEach(Trange);

            using (Variable.ForEach(Trange))
            {
                using (Variable.Switch(c[Trange]))
                {
                    x[Trange] = Variable.GaussianFromMeanAndPrecision(means[c[Trange]], 1);
                }
            }
            InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing());

            Console.WriteLine(ie.Infer(means));
        }
Ejemplo n.º 25
0
        public DistributionArray <Bernoulli> TestBPM_NoisePrecisionVMP(double noiseRate, out double noiseEstimate)
        {
            int K = xtrain[0].Count;
            // Create target y
            VariableArray <bool> y = Variable.Observed(ytrain).Named("y");
            Variable <Vector>    w = Variable.VectorGaussianFromMeanAndPrecision(Vector.Zero(K),
                                                                                 Variable.WishartFromShapeAndScale(.1, PositiveDefiniteMatrix.Identity(K))).Named("w");
            var mean  = Variable.GaussianFromMeanAndPrecision(0, .1);
            var noise = Variable.GammaFromShapeAndRate(2, noiseRate);

            //var noise = Variable.Random(Gamma.PointMass(.1));
            BayesPointMachine_NoisePrecisionVMP(xtrain, w, y, mean, noise);
            InferenceEngine engine     = new InferenceEngine(new VariationalMessagePassing());
            VectorGaussian  wPosterior = engine.Infer <VectorGaussian>(w);
            var             noisePost  = engine.Infer <Gamma>(noise);

            noiseEstimate = 1.0 / noisePost.GetMean();
            //Console.WriteLine("Dist over w=\n" + wPosterior);
            //Console.WriteLine("Dist over noise=\n" + noisePost);
            var meanPost            = engine.Infer <Gaussian>(mean);
            VariableArray <bool> yt = Variable.Array <bool>(new Range(ytest.Length)).Named("ytest");

            BayesPointMachine_NoisePrecisionVMP(xtest, Variable.Random(wPosterior).Named("w"), yt, Variable.Random(meanPost), Variable.Random(noisePost));
            return(engine.Infer <DistributionArray <Bernoulli> >(yt));
        }
        public void Run()
        {
            // Sample data from standard Gaussian
            double[] data = new double[100];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = Rand.Normal(0, 1);
            }

            // Create mean and precision random variables
            Variable <double> mean      = Variable.GaussianFromMeanAndVariance(0, 100).Named("mean");
            Variable <double> precision = Variable.GammaFromShapeAndScale(1, 1).Named("precision");

            Range dataRange          = new Range(data.Length).Named("n");
            VariableArray <double> x = Variable.Array <double>(dataRange).Named("x");

            x[dataRange]    = Variable.GaussianFromMeanAndPrecision(mean, precision).ForEach(dataRange);
            x.ObservedValue = data;

            InferenceEngine engine = new InferenceEngine();

            // Retrieve the posterior distributions
            Console.WriteLine("mean=" + engine.Infer(mean));
            Console.WriteLine("prec=" + engine.Infer(precision));
        }
Ejemplo n.º 27
0
        public void amin()
        {
            int   X_No    = 5;
            int   Z_No    = 3;
            Range X_Range = new Range(X_No).Named("X_Range");
            Range Z_Range = new Range(Z_No).Named("Z_Range");

            int[][] Index = new int[][] { new int[] { 0, 1, 3 },
                                          new int[] { 1, 2, 4 },
                                          new int[] { 2, 4 } };
            int[] sizes = new int[Z_No];
            for (int i = 0; i < Z_No; i++)
            {
                sizes[i] = Index[i].Length;
            }
            VariableArray <int> sizesVar = Variable.Constant(sizes, Z_Range);
            Range Index_Range            = new Range(sizesVar[Z_Range]).Named("Index_Range");
            VariableArray <VariableArray <int>, int[][]> indexVar = Variable.Array(Variable.Array <int>(Index_Range), Z_Range).Named("indexVar");

            indexVar.ObservedValue = Index;
            VariableArray <double> Var_X = Variable.Array <double>(X_Range).Named("Var_X");
            VariableArray <double> Var_Z = Variable.Array <double>(Z_Range);

            Var_X[X_Range] = Variable.GaussianFromMeanAndPrecision(0, 1).ForEach(X_Range);
            Var_Z[Z_Range] = Variable.Sum(Variable.GetItems(Var_X, indexVar[Z_Range]));
            InferenceEngine ie = new InferenceEngine();

            Console.WriteLine(ie.Infer(Var_Z));
        }
Ejemplo n.º 28
0
        public void MultinomialMarginalPrototype()
        {
            VariableArray <int> x  = Variable.Multinomial(1000, Vector.FromArray(new double[] { 0.1, 0.9 }));
            InferenceEngine     ie = new InferenceEngine();

            Console.WriteLine(ie.Infer(x));
        }
Ejemplo n.º 29
0
        public void CompareToArrayTest()
        {
            var randomGenerator = new System.Random(66707770); // make this deterministic

            using (var map = new MemoryMapStream())
            {
                using (var array = new VariableArray <string>(map, 1024, 1000))
                {
                    var arrayExpected = new string[1000];

                    for (uint i = 0; i < 1000; i++)
                    {
                        if (randomGenerator.Next(4) >= 2)
                        { // add data.
                            arrayExpected[i] = i.ToString();
                            array[i]         = i.ToString();
                        }
                        else
                        {
                            arrayExpected[i] = int.MaxValue.ToString();
                            array[i]         = int.MaxValue.ToString();
                        }
                        Assert.AreEqual(arrayExpected[i], array[i]);
                    }

                    for (var i = 0; i < 1000; i++)
                    {
                        Assert.AreEqual(arrayExpected[i], array[i]);
                    }
                }
            }
        }
Ejemplo n.º 30
0
        public override void Execute(ExecutionEnvironment environment)
        {
            DemandArgs(2);

            string name = Arguments[0].Value.StringValue;

            int length = -1;
            if(Arguments[1].Value.Type == HVMType.Variable)
            {
                Variable v = environment.LocalScope.ResolveAny(Arguments[1].Value.StringValue);
                if(v == null)
                {
                    throw new OpCodeArgumentException(1, HVMType.Variable, this);
                }

                VariableItem vi = v as VariableItem;
                if(vi == null)
                {
                    throw new OpCodeArgumentException(1, HVMType.Variable, this);
                }

                length = vi.Value.IntegerValue;
            }
            else
            {
                length = Arguments[1].Value.IntegerValue;
            }

            VariableArray va = new VariableArray(name, length);
            environment.LocalScope.Add(va);
        }
Ejemplo n.º 31
0
        public DistributionArray <Bernoulli> TestNaiveBayesDiagonalHierarchical(double a, out double noiseEstimate)
        {
            noiseEstimate = double.NaN;
            var xtrainArray = xtrain.Select(i => i.ToArray()).ToArray();
            var xtestArray  = xtest.Select(i => i.ToArray()).ToArray();
            int K           = xtrain[0].Count;
            var k           = new Range(K);
            // Create target y
            VariableArray <bool> y = Variable.Observed(ytrain).Named("y");
            var meanTrue           = Variable.Array <double>(k);

            meanTrue[k] = Variable.GaussianFromMeanAndPrecision(
                Variable.GaussianFromMeanAndPrecision(0, 1),
                Variable.GammaFromShapeAndRate(.1, .1)).ForEach(k);
            var precTrue = Variable.Array <double>(k);

            precTrue[k] = Variable.GammaFromShapeAndRate(
                1,
                Variable.GammaFromShapeAndRate(.1, .1)).ForEach(k);
            var meanFalse = Variable.Array <double>(k);

            meanFalse[k] = Variable.GaussianFromMeanAndPrecision(
                Variable.GaussianFromMeanAndPrecision(0, 1),
                Variable.GammaFromShapeAndRate(.1, .1)).ForEach(k);
            var precFalse = Variable.Array <double>(k);

            precFalse[k] = Variable.GammaFromShapeAndRate(
                1,
                Variable.GammaFromShapeAndRate(.1, .1)).ForEach(k);
            NaiveBayesDiagonal(xtrainArray, meanTrue, meanFalse, precTrue, precFalse, y);
            //InferenceEngine.DefaultEngine.Compiler.UseSerialSchedules = true;
            InferenceEngine      engine        = new InferenceEngine(new VariationalMessagePassing());
            var                  meanTruePost  = engine.Infer <Gaussian[]>(meanTrue);
            var                  meanFalsePost = engine.Infer <Gaussian[]>(meanFalse);
            var                  precTruePost  = engine.Infer <Gamma[]>(precTrue);
            var                  precFalsePost = engine.Infer <Gamma[]>(precFalse);
            var                  testRange     = new Range(ytest.Length);
            VariableArray <bool> yt            = Variable.Array <bool>(testRange).Named("ytest");

            yt[testRange] = Variable.Bernoulli(0.5).ForEach(testRange);
            var meanTruePrior  = Variable.Observed <Gaussian>(meanTruePost, k);
            var precTruePrior  = Variable.Observed <Gamma>(precTruePost, k);
            var meanFalsePrior = Variable.Observed <Gaussian>(meanFalsePost, k);
            var precFalsePrior = Variable.Observed <Gamma>(precFalsePost, k);

            meanTrue    = Variable.Array <double>(k);
            meanTrue[k] = Variable <double> .Random(meanTruePrior[k]);

            precTrue    = Variable.Array <double>(k);
            precTrue[k] = Variable <double> .Random(precTruePrior[k]);

            meanFalse    = Variable.Array <double>(k);
            meanFalse[k] = Variable <double> .Random(meanFalsePrior[k]);

            precFalse    = Variable.Array <double>(k);
            precFalse[k] = Variable <double> .Random(precFalsePrior[k]);

            NaiveBayesDiagonal(xtestArray, meanTrue, meanFalse, precTrue, precFalse, yt);
            return(engine.Infer <DistributionArray <Bernoulli> >(yt));
        }
Ejemplo n.º 32
0
        public void GPClassificationTest4()
        {
            bool[]   yData = new bool[] { false, false, true, false };
            double[] xData = new double[]
            {
                -1.555555555555556, -1.111111111111111, -0.2222222222222223, 1.555555555555555
            };
            Vector[]             xVec     = Array.ConvertAll(xData, v => Vector.Constant(1, v));
            Vector[]             basis    = new Vector[] { Vector.Zero(1) };
            IKernelFunction      kf       = new SquaredExponential(0.0);
            SparseGPFixed        sgpf     = new SparseGPFixed(kf, basis);
            Variable <bool>      evidence = Variable.Bernoulli(0.5).Named("evidence");
            IfBlock              block    = Variable.If(evidence);
            Variable <IFunction> f        = Variable.Random <IFunction>(new SparseGP(sgpf)).Named("f");
            Range item = new Range(xVec.Length).Named("item");
            VariableArray <Vector> x = Variable.Array <Vector>(item).Named("x");

            x.ObservedValue = xVec;
            VariableArray <bool> y = Variable.Array <bool>(item).Named("y");

            y.ObservedValue = yData;
            VariableArray <double> h = Variable.Array <double>(item).Named("h");

            h[item] = Variable.FunctionEvaluate(f, x[item]);
            y[item] = (h[item] > 0);
            block.CloseBlock();

            InferenceEngine engine        = new InferenceEngine();
            SparseGP        sgp           = engine.Infer <SparseGP>(f);
            Vector          alphaExpected = Vector.FromArray(new double[] { 0.409693797629808 });

            Console.WriteLine("alpha = {0} should be {1}", sgp.Alpha, alphaExpected);
        }
Ejemplo n.º 33
0
        public void MissingDataGaussianTest()
        {
            Variable <double>      mean      = Variable.GaussianFromMeanAndVariance(0, 100).Named("mean");
            Variable <double>      precision = Variable.GammaFromShapeAndScale(1, 1).Named("precision");
            Variable <int>         n         = Variable.New <int>().Named("n");
            Range                  i         = new Range(n).Named("i");
            VariableArray <double> x         = Variable.Array <double>(i).Named("x");

            using (Variable.ForEach(i))
            {
                using (Variable.If(x[i] > 0))
                {
                    x[i] = Variable.GaussianFromMeanAndPrecision(mean, precision);
                }
            }
            x.ObservedValue = new double[] { -1, 5.0, -1, 7.0, -1 };
            n.ObservedValue = x.ObservedValue.Length;

            InferenceEngine engine = new InferenceEngine(new VariationalMessagePassing());
            //Console.WriteLine(engine.Infer(isMissing));
            Gaussian meanExpected      = Gaussian.FromMeanAndVariance(5.9603207170807826, 0.66132138200164436);
            Gamma    precisionExpected = Gamma.FromShapeAndRate(2, 2.6628958274937107);
            Gaussian meanActual        = engine.Infer <Gaussian>(mean);
            Gamma    precisionActual   = engine.Infer <Gamma>(precision);

            Console.WriteLine("mean = {0} should be {1}", meanActual, meanExpected);
            Console.WriteLine("precision = {0} should be {1}", precisionActual, precisionExpected);
            Assert.True(meanExpected.MaxDiff(meanActual) < 1e-10);
            Assert.True(precisionExpected.MaxDiff(precisionActual) < 1e-10);
        }
Ejemplo n.º 34
0
        public override void AddParents(int numberOfEntries)
        {
            Observed = Variable.Array <int>(N).Named(node.name);
            Observed.SetValueRange(node.states);
            Range parentRange = new Range(node.parents.Count);

            x = Variable.Array <Vector>(N).Named("x" + node.name);
            for (int ii = 0; ii < numberOfEntries; ii++)
            {
                VariableArray <double> row = Variable <double> .Array(parentRange);

                for (int jj = 0; jj < node.parents.Count; jj++)
                {
                    row[jj] = Variable.GaussianFromMeanAndPrecision((node.parents[jj].distributions.ObservedNumerical[ii]), 1);
                }
                x[ii] = Variable.Vector(row);
            }

            var g = Variable.Array(Variable.Array <double>(node.states), N).Named("g" + node.name);

            g[N][node.states] = Variable.InnerProduct(B[node.states], (x[N])) + m[node.states];
            var p = Variable.Array <Vector>(N).Named("p" + node.name);

            p[N] = Variable.Softmax(g[N]);
            using (Variable.ForEach(N))
                Observed[N] = Variable.Discrete(p[N]);
        }
Ejemplo n.º 35
0
        public void Mixture2()
        {
            double[] data            = { .5, 12, 11 };
            int      T               = data.Length;
            VariableArray <double> x = Variable.Constant(data).Named("data");
            Range             i      = x.Range;
            Variable <Vector> D      = Variable.Dirichlet(new double[] { 1, 1 });

            VariableArray <int> c = Variable.Array <int>(i);

            using (Variable.ForEach(i))
            {
                c[i] = Variable.Discrete(D);
                using (Variable.Case(c[i], 0))
                {
                    x[i] = Variable.GaussianFromMeanAndPrecision(5, 1);
                }
                using (Variable.Case(c[i], 1))
                {
                    x[i] = Variable.GaussianFromMeanAndPrecision(10, 1);
                }
            }

            InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing());

            double[] DVibesResult = new double[] { -1.08333333348476, -0.58333333335936 };
            VmpTests.TestDirichletMoments(ie, D, DVibesResult);
            double[][] cVibesResult = new double[T][];
            cVibesResult[0] = new double[] { 1.00000000000000, 0.00000000000000 };
            cVibesResult[1] = new double[] { 0.00000000000000, 1.00000000000000 };
            cVibesResult[2] = new double[] { 0.00000000000000, 1.00000000000000 };
            VmpTests.TestDiscrete(ie, c, cVibesResult);
        }
Ejemplo n.º 36
0
		public override void CreateModel()
		{
			base.CreateModel();
			NumTrips = Variable.New<int>();
			Range tripRange = new Range(NumTrips);
			TravelTimes = Variable.Array<double>(tripRange);
			using (Variable.ForEach(tripRange))
			{
				TravelTimes[tripRange] = Variable.GaussianFromMeanAndPrecision(AverageTime, TrafficNoise);
			}
		}
Ejemplo n.º 37
0
		public BPMDataVars(
			Variable<int> nUser, Range user, 
			VariableArray<VariableArray<int>, int[][]> xIndices,
			VariableArray<int> xValueCount,
			VariableArray<VariableArray<double>, double[][]> xValues)
		{
			this.nItem = nUser;
			this.item = user;
			this.xValueCount = xValueCount;
			this.xValues = xValues;
			this.xIndices = xIndices;
		}
Ejemplo n.º 38
0
		public Inference(int numRanks)
		{
			nRanks = numRanks;
			probNextIfNotClick = Variable.New<double>();
			probNextIfClickNotRel = Variable.New<double>();
			probNextIfClickRel = Variable.New<double>();
			nUsers = Variable.New<int>();
			Range u = new Range(nUsers);

			appeal = new Variable<double>[nRanks];
			relevance = new Variable<double>[nRanks];
			examine = new VariableArray<bool>[nRanks];
			click = new VariableArray<bool>[nRanks];
			isRel = new VariableArray<bool>[nRanks];

			// user independent variables
			for (int d = 0; d < nRanks; d++)
			{
				appeal[d] = Variable.Beta(1, 1);
				relevance[d] = Variable.Beta(1, 1);
			}

			// Main model code
			for (int d = 0; d < nRanks; d++)
			{
				examine[d] = Variable.Array<bool>(u);
				click[d] = Variable.Array<bool>(u);
				isRel[d] = Variable.Array<bool>(u);
				if (d == 0)
					examine[d][u] = Variable.Bernoulli(1).ForEach(u);
				else
					using (Variable.ForEach(u))
					{
						var nextIfClick = Variable.New<bool>();
						using (Variable.If(isRel[d-1][u]))
							nextIfClick.SetTo(Variable.Bernoulli(probNextIfClickRel));
						using (Variable.IfNot(isRel[d-1][u]))
							nextIfClick.SetTo(Variable.Bernoulli(probNextIfClickNotRel));
						var nextIfNotClick = Variable.Bernoulli(probNextIfNotClick);
						var next = 
							(((!click[d - 1][u]) & nextIfNotClick) | (click[d - 1][u]  & nextIfClick));
						examine[d][u] = examine[d - 1][u] & next;
								  
					}

				using (Variable.ForEach(u))
				{
					click[d][u] = examine[d][u] & Variable.Bernoulli(appeal[d]);
					isRel[d][u] = click[d][u] & Variable.Bernoulli(relevance[d]);
				}
			}
			ie = new InferenceEngine();
		}
Ejemplo n.º 39
0
		public void BayesPointMachine(double[] incomes, double[] ages,Variable<Vector> w, VariableArray<bool> y)
		{
			// Create x vector, augmented by 1
			Range j = y.Range.Named("person");
			Vector[] xdata = new Vector[incomes.Length];
			for (int i = 0; i < xdata.Length; i++) 
				xdata[i] = Vector.FromArray(incomes[i], ages[i], 1);
			VariableArray<Vector> x = Variable.Observed(xdata,j).Named("x");

			// Bayes Point Machine
			double noise = 0.1;
			y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]).Named("innerProduct"),noise)>0;
		}
Ejemplo n.º 40
0
 public void ZeroSizeTest()
 {
     using (var map = new MemoryMapStream())
     {
         using (var array = new VariableArray<string>(map, 1024, 0))
         {
             Assert.AreEqual(0, array.Length);
         }
         using (var array = new VariableArray<string>(map, 1024, 100))
         {
             array.Resize(0);
             Assert.AreEqual(0, array.Length);
         }
     }
 }
Ejemplo n.º 41
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="numOfClasses">The number of classes.</param>
        /// <param name="noise">The noise level</param>
        protected VectorsTestModel(int numOfClasses, double noise)
            : base(numOfClasses, noise)
        {
            // Initialize the infer engine
            this.Engine = new InferenceEngine();

            // Initialize the variable for number of vectors and the range
            this.numOfVectors = Variable.New<int>();
            this.range = new Range(this.numOfVectors);

            // Initialize the variable array for feature vectors
            this.featureVectors = Variable.Array<Vector>(this.range);

            // Initialize the variable array for test results
            this.modelOutput = Variable.Array<int>(this.range);
        }
Ejemplo n.º 42
0
		/// <summary>
		/// Compute class scores for sparse BPM
		/// </summary>
		/// <param name="w">Weight array per class</param>
		/// <param name="xValues">Array of values</param>
		/// <param name="xIndices">Array of indices</param>
		/// <param name="itemFeature">Feature range</param>
		/// <param name="noisePrec">Noise precision</param>
		/// <returns></returns>
		public static Variable<double>[] ComputeClassScores(
			VariableArray<double>[] w, VariableArray<double> xValues,
			VariableArray<int> xIndices, Range itemFeature, double noisePrec)
		{
			int nClass = w.Length;
			Variable<double>[] score = new Variable<double>[nClass];
			Variable<double>[] scorePlusNoise = new Variable<double>[nClass];
			for (int c = 0; c < nClass; c++)
			{
				VariableArray<double> wSparse = Variable.Subarray<double>(w[c], xIndices);
				VariableArray<double> product = Variable.Array<double>(itemFeature);
				product[itemFeature] = xValues[itemFeature] * wSparse[itemFeature];
				score[c] = Variable.Sum(product);
				scorePlusNoise[c] = Variable.GaussianFromMeanAndPrecision(score[c], noisePrec);
			}
			return scorePlusNoise;
		}
Ejemplo n.º 43
0
        public static void BayesPointMachine(ArrayList trainingData, Variable<Vector> w, VariableArray<double> y)
        {
            // Create x vector, augmented by 1
            Range j = y.Range.Named("person");
            Vector[] xdata = new Vector[((double[])(trainingData[0])).Length];

            for (int i = 0; i < xdata.Length; i++) {
                double[] argumentList = new double[trainingData.Count+1];
                for (int k = 0; k < trainingData.Count; k++)
                    argumentList[k] = ((double[])(trainingData[k]))[i];
                argumentList[trainingData.Count] = 1;
                xdata[i] = Vector.FromArray(argumentList);
            }
            VariableArray<Vector> x = Variable.Observed(xdata, j).Named("x");

            // Bayes Point Machine
            double noise = 0.1;
            y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]).Named("innerProduct"), noise);
        }
Ejemplo n.º 44
0
		/// <summary>
		/// Model constructor
		/// </summary>
		public BayesianPCAModel()
		{
			// The various dimensions will be set externally...
			vN = Variable.New<int>().Named("NumObs");
			vD = Variable.New<int>().Named("NumFeats");
			vM = Variable.New<int>().Named("MaxComponents");
			rN = new Range(vN).Named("N");
			rD = new Range(vD).Named("D");
			rM = new Range(vM).Named("M");
			// ... as will the data
			vData = Variable.Array<double>(rN, rD).Named("data");
			// ... and the priors
			priorAlpha = Variable.New<Gamma>().Named("PriorAlpha");
			priorMu = Variable.New<Gaussian>().Named("PriorMu");
			priorPi = Variable.New<Gamma>().Named("PriorPi");
			// Mixing matrix. Each row is drawn from a Gaussian with zero mean and
			// a precision which will be learnt. This is a form of Automatic
			// Relevance Determination (ARD). The larger the precisions become, the
			// less important that row in the mixing matrix is in explaining the data
			vAlpha = Variable.Array<double>(rM).Named("Alpha");
			vW = Variable.Array<double>(rM, rD).Named("W");
			vAlpha[rM] = Variable.Random<double, Gamma>(priorAlpha).ForEach(rM);
			vW[rM, rD] = Variable.GaussianFromMeanAndPrecision(0, vAlpha[rM]).ForEach(rD);
			// Latent variables are drawn from a standard Gaussian
			vZ = Variable.Array<double>(rN, rM).Named("Z");
			vZ[rN, rM] = Variable.GaussianFromMeanAndPrecision(0.0, 1.0).ForEach(rN, rM);
			// Multiply the latent variables with the mixing matrix...
			vT = Variable.MatrixMultiply(vZ, vW).Named("T");
			// ... add in a bias ...
			vMu = Variable.Array<double>(rD).Named("mu");
			vMu[rD] = Variable.Random<double, Gaussian>(priorMu).ForEach(rD);
			vU = Variable.Array<double>(rN, rD).Named("U");
			vU[rN, rD] = vT[rN, rD] + vMu[rD];
			// ... and add in some observation noise ...
			vPi = Variable.Array<double>(rD).Named("pi");
			vPi[rD] = Variable.Random<double, Gamma>(priorPi).ForEach(rD);
			// ... to give the likelihood of observing the data
			vData[rN, rD] = Variable.GaussianFromMeanAndPrecision(vU[rN, rD], vPi[rD]);
			// Inference engine
			engine = new InferenceEngine();
			return;
		}
Ejemplo n.º 45
0
		public override void CreateModel()
		{
			base.CreateModel();

			NumTrips = Variable.New<int>();
			Range tripRange = new Range(NumTrips);
			TravelTimes = Variable.Array<double>(tripRange);
			ComponentIndices = Variable.Array<int>(tripRange);

			using (Variable.ForEach(tripRange))
			{
				ComponentIndices[tripRange] = Variable.Discrete(MixingCoefficients);
				using (Variable.Switch(ComponentIndices[tripRange]))
				{
					TravelTimes[tripRange].SetTo(
						Variable.GaussianFromMeanAndPrecision(
							AverageTime[ComponentIndices[tripRange]],
							TrafficNoise[ComponentIndices[tripRange]]));
				}
			}
		}
Ejemplo n.º 46
0
		/// <summary>
		/// Constructs a new Glass/Sprinkler/Rain model
		/// </summary>
		public WetGlassSprinklerRainModel()
		{
			// Set up the ranges
			NumberOfExamples = Variable.New<int>().Named("NofE");
			Range N = new Range(NumberOfExamples).Named("N");

			// Although all the variables in this example have just 2 states (true/false),
			// the example is formulated in a way that shows how to extend to multiple states
			Range C = new Range(2).Named("C");
			Range S = new Range(2).Named("S");
			Range R = new Range(2).Named("R");
			Range W = new Range(2).Named("W");

			// Define the priors and the parameters
			ProbCloudyPrior = Variable.New<Dirichlet>().Named("ProbCloudyPrior");
			ProbCloudy = Variable<Vector>.Random(ProbCloudyPrior).Named("ProbCloudy");
			ProbCloudy.SetValueRange(C);
			// Sprinkler probability table conditioned on cloudiness
			CPTSprinklerPrior = Variable.Array<Dirichlet>(C).Named("CPTSprinklerPrior");
			CPTSprinkler = Variable.Array<Vector>(C).Named("CPTSprinkler");
			CPTSprinkler[C] = Variable<Vector>.Random(CPTSprinklerPrior[C]);
			CPTSprinkler.SetValueRange(S);
			// Rain probability table conditioned on cloudiness
			CPTRainPrior = Variable.Array<Dirichlet>(C).Named("CPTRainPrior");
			CPTRain = Variable.Array<Vector>(C).Named("CPTRain"); ;
			CPTRain[C] = Variable<Vector>.Random(CPTRainPrior[C]);
			CPTRain.SetValueRange(R);
			// Wet grass probability table conditioned on sprinkler and rain
			CPTWetGrassPrior = Variable.Array(Variable.Array<Dirichlet>(R), S).Named("CPTWetGrassPrior");
			CPTWetGrass = Variable.Array(Variable.Array<Vector>(R), S).Named("CPTWetGrass");
			CPTWetGrass[S][R] = Variable<Vector>.Random(CPTWetGrassPrior[S][R]);
			CPTWetGrass.SetValueRange(W);

			// Define the primary variables
			Cloudy = Variable.Array<int>(N).Named("Cloudy");
			Cloudy[N] = Variable.Discrete(ProbCloudy).ForEach(N);
			Sprinkler = AddChildFromOneParent(Cloudy, CPTSprinkler).Named("Sprinkler");
			Rain = AddChildFromOneParent(Cloudy, CPTRain).Named("Rain");
			WetGrass = AddChildFromTwoParents(Sprinkler, Rain, CPTWetGrass).Named("WetGrass");
		}
Ejemplo n.º 47
0
		public virtual void CreateModel()
		{
			NumComponents = 2;
			Range ComponentRange = new Range(NumComponents);
			InferenceEngine = new InferenceEngine(new VariationalMessagePassing());
			InferenceEngine.ShowProgress = false;

			AverageTimePriors = Variable.Array<Gaussian>(ComponentRange);
			TrafficNoisePriors = Variable.Array<Gamma>(ComponentRange);
			AverageTime = Variable.Array<double>(ComponentRange);
			TrafficNoise = Variable.Array<double>(ComponentRange);

			using (Variable.ForEach(ComponentRange))
			{
				AverageTime[ComponentRange] = Variable.Random<double, Gaussian>(AverageTimePriors[ComponentRange]);
				TrafficNoise[ComponentRange] = Variable.Random<double, Gamma>(TrafficNoisePriors[ComponentRange]);
			}

			//Mixing coefficients
			MixingPrior = Variable.New<Dirichlet>();
			MixingCoefficients = Variable<Vector>.Random(MixingPrior);
			MixingCoefficients.SetValueRange(ComponentRange);
		}
        public BayesPointMachine(int nFeatures, double noise)
        {
            // Training model
            nTrain = Variable.New<int>().Named("nTrain");
            Range trainItem = new Range(nTrain).Named("trainItem");
            trainingLabels = Variable.Array<bool>(trainItem).Named("trainingLabels");
            trainingItems = Variable.Array<Vector>(trainItem).Named("trainingItems");
            weights = Variable.Random(new VectorGaussian(Vector.Zero(nFeatures),
                PositiveDefiniteMatrix.Identity(nFeatures))).Named("weights");
            trainingLabels[trainItem] = Variable.IsPositive(Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(weights, trainingItems[trainItem]), noise));

            // Testing model
            nTest = Variable.New<int>().Named("nTest");
            Range testItem = new Range(nTest).Named("testItem");
            testItems = Variable.Array<Vector>(testItem).Named("testItems");
            testLabels = Variable.Array<bool>(testItem).Named("testLabels");
            if (singleModel)
            {
                testLabels[testItem] = Variable.IsPositive(Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(weights, testItems[testItem]), noise));

                testEngine = new InferenceEngine(new ExpectationPropagation());
                testEngine.NumberOfIterations = 2;
            }
            else
            {
                weightPosterior = Variable.New<VectorGaussian>().Named("weightPosterior");
                Variable<Vector> testWeights = Variable<Vector>.Random(weightPosterior);
                testLabels[testItem] = Variable.IsPositive(Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(testWeights, testItems[testItem]), noise));

                trainEngine = new InferenceEngine(new ExpectationPropagation());
                trainEngine.ShowProgress = false;
                trainEngine.NumberOfIterations = 5;
                testEngine = new InferenceEngine(new ExpectationPropagation());
                testEngine.ShowProgress = false;
                testEngine.NumberOfIterations = 1;
            }
        }
Ejemplo n.º 49
0
        /// <summary>
        /// Initializes the ranges of the variables.
        /// </summary>
        /// <param name="taskCount">The number of tasks.</param>
        /// <param name="labelCount">The number of labels.</param>
        protected virtual void DefineVariablesAndRanges(int taskCount, int labelCount)
        {
            WorkerCount = Variable.New<int>().Named("WorkerCount");
            n = new Range(taskCount).Named("n");
            c = new Range(labelCount).Named("c");
            k = new Range(WorkerCount).Named("k");

            // The tasks for each worker
            WorkerTaskCount = Variable.Array<int>(k).Named("WorkerTaskCount");
            kn = new Range(WorkerTaskCount[k]).Named("kn");
            WorkerTaskIndex = Variable.Array(Variable.Array<int>(kn), k).Named("WorkerTaskIndex");
            WorkerTaskIndex.SetValueRange(n);
            WorkerLabel = Variable.Array(Variable.Array<int>(kn), k).Named("WorkerLabel");

            // The background probability vector
            BackgroundLabelProbPrior = Variable.New<Dirichlet>().Named("BackgroundLabelProbPrior");
            BackgroundLabelProb = Variable<Vector>.Random(BackgroundLabelProbPrior).Named("BackgroundLabelProb");
            BackgroundLabelProb.SetValueRange(c);

            // The confusion matrices for each worker
            ConfusionMatrixPrior = Variable.Array(Variable.Array<Dirichlet>(c), k).Named("ConfusionMatrixPrior");
            WorkerConfusionMatrix = Variable.Array(Variable.Array<Vector>(c), k).Named("ConfusionMatrix");
            WorkerConfusionMatrix[k][c] = Variable<Vector>.Random(ConfusionMatrixPrior[k][c]);
            WorkerConfusionMatrix.SetValueRange(c);

            // The unobserved 'true' label for each task
            TrueLabel = Variable.Array<int>(n).Attrib(QueryTypes.Marginal).Attrib(QueryTypes.MarginalDividedByPrior).Named("Truth");
            TrueLabelConstraint = Variable.Array<Discrete>(n).Named("TruthConstraint");
            // Constraint for online learning
            TrueLabel[n] = Variable.Discrete(BackgroundLabelProb).ForEach(n);
            Variable.ConstrainEqualRandom(TrueLabel[n], TrueLabelConstraint[n]);
            // The worker labels
            WorkerLabel = Variable.Array(Variable.Array<int>(kn), k).Named("WorkerLabel");
        }
Ejemplo n.º 50
0
        public void CompareToArrayTest()
        {
            var randomGenerator = new System.Random(66707770); // make this deterministic 

            using (var map = new MemoryMapStream())
            {
                using (var array = new VariableArray<string>(map, 1024, 1000))
                {
                    var arrayExpected = new string[1000];

                    for (uint i = 0; i < 1000; i++)
                    {
                        if (randomGenerator.Next(4) >= 2)
                        { // add data.
                            arrayExpected[i] = i.ToString();
                            array[i] = i.ToString();
                        }
                        else
                        {
                            arrayExpected[i] = int.MaxValue.ToString();
                            array[i] = int.MaxValue.ToString();
                        }
                        Assert.AreEqual(arrayExpected[i], array[i]);
                    }

                    for (var i = 0; i < 1000; i++)
                    {
                        Assert.AreEqual(arrayExpected[i], array[i]);
                    }
                }
            }
        }
        private void DefineModel()
        {
            this.observationCount = Variable.New<int>().Named("observation_count");
            this.gridWidth = Variable.New<int>().Named("grid_width");
            this.gridHeight = Variable.New<int>().Named("grid_height");
            this.shapePartCount = Variable.New<int>().Named("shape_part_count");
            this.traitCount = Variable.New<int>().Named("trait_count");

            this.observationRange = new Range(this.observationCount).Named("observation_range");
            this.xyRange = new Range(2).Named("xy_range");
            this.widthRange = new Range(this.gridWidth).Named("width_range");
            this.heightRange = new Range(this.gridHeight).Named("height_range");
            this.shapePartRange = new Range(this.shapePartCount).Named("shape_part_range");
            this.traitRange = new Range(this.traitCount).Named("trait_range");

            this.shapeLocationMeanPrior = Variable.New<GaussianArray1D>().Named("shape_location_mean_prior");
            this.shapeLocationMean = Variable.Array<double>(this.xyRange).Named("shape_location_mean");
            this.shapeLocationMean.SetTo(Variable<double[]>.Random(this.shapeLocationMeanPrior));

            this.shapeLocationPrecisionPrior = Variable.New<GammaArray1D>().Named("shape_location_prec_prior");
            this.shapeLocationPrecision = Variable.Array<double>(this.xyRange).Named("shape_location_prec");
            this.shapeLocationPrecision.SetTo(Variable<double[]>.Random(this.shapeLocationPrecisionPrior));

            this.shapePartOffsetWeightPriors = Variable.New<GaussianArray3D>().Named("shape_part_offset_weight_prior");
            this.shapePartOffsetWeights = Variable.Array(Variable.Array(Variable.Array<double>(this.traitRange), this.xyRange), this.shapePartRange).Named("shape_part_offset_weights");
            this.shapePartOffsetWeights.SetTo(Variable<double[][][]>.Random(this.shapePartOffsetWeightPriors));

            this.shapePartLogScaleWeightPriors = Variable.New<GaussianArray3D>().Named("shape_part_scale_weight_prior");
            this.shapePartLogScaleWeights = Variable.Array(Variable.Array(Variable.Array<double>(this.traitRange), this.xyRange), this.shapePartRange).Named("shape_part_scale_weights");
            this.shapePartLogScaleWeights.SetTo(Variable<double[][][]>.Random(this.shapePartLogScaleWeightPriors));

            this.shapePartAngleWeightPriors = Variable.New<GaussianArray2D>().Named("shape_part_angle_weight_prior");
            this.shapePartAngleWeights = Variable.Array(Variable.Array<double>(this.traitRange), this.shapePartRange).Named("shape_part_angle_weights");
            this.shapePartAngleWeights.SetTo(Variable<double[][]>.Random(this.shapePartAngleWeightPriors));

            this.shapePartOffsetPrecisionPriors = Variable.New<GammaArray2D>().Named("shape_part_offset_prec_prior");
            this.shapePartOffsetPrecisions = Variable.Array(Variable.Array<double>(this.xyRange), this.shapePartRange).Named("shape_part_offset_prec");
            this.shapePartOffsetPrecisions.SetTo(Variable<double[][]>.Random(this.shapePartOffsetPrecisionPriors));

            this.shapePartLogScalePrecisionPriors = Variable.New<GammaArray2D>().Named("shape_part_scale_prec_prior");
            this.shapePartLogScalePrecisions = Variable.Array(Variable.Array<double>(this.xyRange), this.shapePartRange).Named("shape_part_scale_prec");
            this.shapePartLogScalePrecisions.SetTo(Variable<double[][]>.Random(this.shapePartLogScalePrecisionPriors));

            this.shapePartAnglePrecisionPriors = Variable.New<GammaArray1D>().Named("shape_part_angle_prec_prior");
            this.shapePartAnglePrecisions = Variable.Array<double>(this.shapePartRange).Named("shape_part_angle_prec");
            this.shapePartAnglePrecisions.SetTo(Variable<double[]>.Random(this.shapePartAnglePrecisionPriors));

            this.globalLogScalePrior = Variable.New<GaussianArray1D>().Named("global_log_scale_prior");
            this.globalLogScale = Variable.Array<double>(this.observationRange).Named("global_log_scale");
            this.globalLogScale.SetTo(Variable<double[]>.Random(this.globalLogScalePrior));

            this.shapeLocation = Variable.Array(Variable.Array<double>(this.xyRange), this.observationRange).Named("shape_location");

            this.shapePartLocation = Variable.Array(Variable.Array(Variable.Array<double>(this.xyRange), this.shapePartRange), this.observationRange).Named("shape_part_location");
            this.shapePartLocation.AddAttribute(new PointEstimate());

            this.shapePartOrientation = Variable.Array(Variable.Array<PositiveDefiniteMatrix>(this.shapePartRange), this.observationRange).Named("shape_part_orientation");
            this.shapePartOrientation.AddAttribute(new PointEstimate());

            this.shapeTraitsPrior = Variable.New<GaussianArray2D>().Named("shape_traits_prior"); // Needs to be observed in the derived classes
            this.shapeTraits = Variable.Array(Variable.Array<double>(this.traitRange), this.observationRange).Named("shape_traits");
            this.shapeTraits.SetTo(Variable<double[][]>.Random(this.shapeTraitsPrior));

            this.observationNoiseProbability = Variable.New<double>().Named("observation_noise_prob");

            this.pixelCoords = Variable.Array<Vector>(this.widthRange, this.heightRange).Named("pixel_coords");

            this.labels =
                Variable.Array<VariableArray2D<bool>, bool[][,]>(Variable.Array<bool>(this.widthRange, this.heightRange), this.observationRange)
                        .Named("labels");
            this.noisyLabels =
                Variable.Array<VariableArray2D<bool>, bool[][,]>(Variable.Array<bool>(this.widthRange, this.heightRange), this.observationRange)
                        .Named("noisy_labels");

            this.noisyLabelsConstraint =
                Variable.Array<VariableArray2D<Bernoulli>, Bernoulli[][,]>(Variable.Array<Bernoulli>(this.widthRange, this.heightRange), this.observationRange)
                        .Named("noisy_labels_constraint");

            using (var observationIter = Variable.ForEach(this.observationRange))
            {
                this.shapeLocation[this.observationRange][this.xyRange] = Variable.GaussianFromMeanAndPrecision(this.shapeLocationMean[this.xyRange], this.shapeLocationPrecision[this.xyRange]);

                using (Variable.ForEach(this.shapePartRange))
                {
                    const double productDamping = 0.5;

                    // Location

                    var shapePartOffsetMeanTraitWeightProducts = Variable.Array(Variable.Array<double>(this.traitRange), this.xyRange).Named("shape_part_offset_mean_products");
                    shapePartOffsetMeanTraitWeightProducts[this.xyRange][this.traitRange] =
                        Variable<double>.Factor(Factor.Product_SHG09, this.shapeTraits[this.observationRange][this.traitRange], this.shapePartOffsetWeights[this.shapePartRange][this.xyRange][this.traitRange]);
                    var shapePartOffsetMeanTraitWeightProductsDamped = Variable.Array(Variable.Array<double>(this.traitRange), this.xyRange).Named("shape_part_offset_mean_products_damped");
                    shapePartOffsetMeanTraitWeightProductsDamped[this.xyRange][this.traitRange] = Variable<double>.Factor(Damp.Forward<double>, shapePartOffsetMeanTraitWeightProducts[this.xyRange][this.traitRange], productDamping);

                    var shapePartOffsetMean = Variable.Array<double>(this.xyRange).Named("shape_part_offset_mean");
                    shapePartOffsetMean[this.xyRange] = Variable.Sum(shapePartOffsetMeanTraitWeightProductsDamped[this.xyRange]);
                    var shapePartOffset = Variable.GaussianFromMeanAndPrecision(
                        shapePartOffsetMean[this.xyRange], this.shapePartOffsetPrecisions[this.shapePartRange][this.xyRange]).Named("shape_part_offset");

                    this.shapePartLocation[this.observationRange][this.shapePartRange][this.xyRange] = this.shapeLocation[this.observationRange][this.xyRange] + shapePartOffset;

                    // Orientation

                    var shapePartLogScaleMeanTraitWeightProducts = Variable.Array(Variable.Array<double>(this.traitRange), this.xyRange).Named("shape_part_logscale_mean_products");
                    shapePartLogScaleMeanTraitWeightProducts[this.xyRange][this.traitRange] =
                        Variable<double>.Factor(Factor.Product_SHG09, this.shapeTraits[this.observationRange][this.traitRange], this.shapePartLogScaleWeights[this.shapePartRange][this.xyRange][this.traitRange]);
                    var shapePartLogScaleMeanTraitWeightProductsDamped = Variable.Array(Variable.Array<double>(this.traitRange), this.xyRange).Named("shape_part_logscale_mean_products_damped");
                    shapePartLogScaleMeanTraitWeightProductsDamped[this.xyRange][this.traitRange] = Variable<double>.Factor(Damp.Forward<double>, shapePartLogScaleMeanTraitWeightProducts[this.xyRange][this.traitRange], productDamping);

                    var shapePartLogScaleMean = Variable.Array<double>(this.xyRange).Named("shape_part_logscale_mean");
                    shapePartLogScaleMean[this.xyRange] = Variable.Sum(shapePartLogScaleMeanTraitWeightProductsDamped[this.xyRange]);
                    var shapePartLogScale = Variable.Array<double>(this.xyRange).Named("shape_part_logscale");
                    shapePartLogScale[this.xyRange] = Variable.GaussianFromMeanAndPrecision(
                        shapePartLogScaleMean[this.xyRange], this.shapePartLogScalePrecisions[this.shapePartRange][this.xyRange]);

                    var shapePartAngleMeanTraitWeightProducts = Variable.Array<double>(this.traitRange).Named("shape_part_angle_mean_products");
                    shapePartAngleMeanTraitWeightProducts[this.traitRange] =
                        Variable<double>.Factor(Factor.Product_SHG09, this.shapeTraits[this.observationRange][this.traitRange], this.shapePartAngleWeights[this.shapePartRange][this.traitRange]);
                    var shapePartAngleMeanTraitWeightProductsDamped = Variable.Array<double>(this.traitRange).Named("shape_part_angle_mean_products_damped");
                    shapePartAngleMeanTraitWeightProductsDamped[this.traitRange] = Variable<double>.Factor(Damp.Forward<double>, shapePartAngleMeanTraitWeightProducts[this.traitRange], productDamping);

                    var shapePartAngleMean = Variable.Sum(shapePartAngleMeanTraitWeightProductsDamped).Named("shape_part_angle_mean");
                    var shapePartAngle = Variable.GaussianFromMeanAndPrecision(
                        shapePartAngleMean, this.shapePartAnglePrecisions[this.shapePartRange]).Named("shape_part_angle");

                    this.shapePartOrientation[this.observationRange][this.shapePartRange] = Variable<PositiveDefiniteMatrix>.Factor(
                        ShapeFactors.MatrixFromAngleScale, shapePartLogScale[0] /*+ this.globalLogScale[observationRange]*/, shapePartLogScale[1] /*+ this.globalLogScale[observationRange]*/, shapePartAngle);
                    this.shapePartOrientation[this.observationRange][this.shapePartRange].AddAttribute(new MarginalPrototype(new Wishart(2)));
                }

                using (Variable.ForEach(this.widthRange))
                using (Variable.ForEach(this.heightRange))
                {
                    var labelsByPart = Variable.Array<bool>(this.shapePartRange).Named("labels_by_part");

                    using (Variable.ForEach(this.shapePartRange))
                    {
                       labelsByPart[this.shapePartRange] = Variable<bool>.Factor(
                            ShapeFactors.LabelFromShape,
                            this.pixelCoords[this.widthRange, this.heightRange],
                            this.shapePartLocation[this.observationRange][this.shapePartRange][0],
                            this.shapePartLocation[this.observationRange][this.shapePartRange][1],
                            this.shapePartOrientation[this.observationRange][this.shapePartRange]);
                    }

                    this.labels[this.observationRange][this.widthRange, this.heightRange] = Variable<bool>.Factor(Factors.AnyTrue, labelsByPart);

                    //using (Variable.Repeat(100))
                    {
                        using (Variable.If(this.labels[this.observationRange][this.widthRange, this.heightRange]))
                        {
                            this.noisyLabels[this.observationRange][this.widthRange, this.heightRange] =
                                !Variable.Bernoulli(this.observationNoiseProbability);
                        }

                        using (Variable.IfNot(this.labels[this.observationRange][this.widthRange, this.heightRange]))
                        {
                            this.noisyLabels[this.observationRange][this.widthRange, this.heightRange] =
                                Variable.Bernoulli(this.observationNoiseProbability);
                        }
                    }

                    //Variable.ConstrainEqualRandom(
                    //    this.noisyLabels[this.observationRange][this.widthRange, this.heightRange],
                    //    this.noisyLabelsConstraint[this.observationRange][this.widthRange, this.heightRange]);
                }
            }
        }
Ejemplo n.º 52
0
		/// <summary>
		/// Specify the training model
		/// </summary>
		/// <param name="s">The name of the training model</param>
		/// <param name="nChunks">The number of chunks</param>
		/// <returns>A <see cref="BPMVarsModelForTrain"/> instance</returns>
		private BPMVarsModelForTrain SpecifyTrainModel(string s, int nChunks)
		{
			// An array of feature vectors - their observed values will be
			// set by the calling program
			VariableArray<Vector>[] xValues = new VariableArray<Vector>[nClass];
			// The number of items for each component
			Variable<int>[] nItem = new Variable<int>[nClass];
			// Ranges over the items for each component
			Range[] item = new Range[nClass];

			// The model identifier for the shared variables
			Model model = new Model(nChunks).Named("model" + s);
			// The weight vector within a submodel
			Variable<Vector>[] wModel = new Variable<Vector>[nClass];
			for (int c = 0; c < nClass; c++)
			{
				// Get a copy of the shared weight vector variable for the submodel
				wModel[c] = w[c].GetCopyFor(model).Named("wModel_" + c + s);
			}

			// Loop over the components
			for (int c = 0; c < nClass; c++)
			{
				// The number of items for each component - set by the calling program
				nItem[c] = Variable.New<int>().Named("nItem_" + c + s);
				// The item range for each component
				item[c] = new Range(nItem[c]).Named("item_" + c + s);
				// The items for each component - set by the calling program
				xValues[c] = Variable.Array<Vector>(item[c]);
				using (Variable.ForEach(item[c]))
				{
					// The score for this item across all components
					Variable<double>[] score = BPMUtils.ComputeClassScores(wModel, xValues[c][item[c]], NoisePrec);
					// The constraint imposed by the observed component
					BPMUtils.ConstrainArgMax(c, score);
				}
			}
			// Store the variables 
			BPMVarsModelForTrain bpmVar = new BPMVarsModelForTrain();
			bpmVar.ie = new InferenceEngine();
			bpmVar.xValues = xValues;
			bpmVar.nItems = nItem;
			bpmVar.model = model;
			return bpmVar;
		}
Ejemplo n.º 53
0
		/// <summary>
		/// Helper method to add a child from two parents
		/// </summary>
		/// <param name="parent1">First parent (a variable array over a range of examples)</param>
		/// <param name="parent2">Second parent (a variable array over the same range)</param>
		/// <param name="cpt">Conditional probability table</param>
		/// <returns></returns>
		public static VariableArray<int> AddChildFromTwoParents(
			VariableArray<int> parent1,
			VariableArray<int> parent2,
			VariableArray<VariableArray<Vector>, Vector[][]> cpt)
		{
			var n = parent1.Range;
			var child = Variable.Array<int>(n);
			using (Variable.ForEach(n))
			using (Variable.Switch(parent1[n]))
			using (Variable.Switch(parent2[n]))
				child[n] = Variable.Discrete(cpt[parent1[n]][parent2[n]]);
			return child;
		}
Ejemplo n.º 54
0
		/// <summary>
		/// Constructs an LDA model
		/// </summary>
		/// <param name="sizeVocab">Size of vocabulary</param>
		/// <param name="numTopics">Number of topics</param>
		public LDATopicInferenceModel(
			int sizeVocab, 
			int numTopics)
		{
			SizeVocab = sizeVocab;
			NumTopics = numTopics;

			//---------------------------------------------
			// The model
			//---------------------------------------------
			NumWordsInDoc = Variable.New<int>().Named("NumWordsInDoc");
			Range W = new Range(SizeVocab).Named("W");
			Range T = new Range(NumTopics).Named("T");
			Range WInD = new Range(NumWordsInDoc).Named("WInD");

			Theta = Variable.New<Vector>().Named("Theta");
			ThetaPrior = Variable.New<Dirichlet>().Named("ThetaPrior");
			ThetaPrior.SetValueRange(T);
			Theta = Variable<Vector>.Random(ThetaPrior);
			PhiPrior = Variable.Array<Dirichlet>(T).Named("PhiPrior");
			PhiPrior.SetValueRange(W);
			Phi = Variable.Array<Vector>(T).Named("Phi");
			Phi[T] = Variable.Random<Vector, Dirichlet>(PhiPrior[T]);

			Words = Variable.Array<int>(WInD).Named("Words");
			WordCounts = Variable.Array<double>(WInD).Named("WordCounts");
			using (Variable.ForEach(WInD))
			{
				using (Variable.Repeat(WordCounts[WInD]))
				{
					var topic = Variable.Discrete(Theta).Attrib(new ValueRange(T)).Named("topic");
					topic.SetValueRange(T);
					using (Variable.Switch(topic))
						Words[WInD] = Variable.Discrete(Phi[topic]);
				}
			}
			Engine = new InferenceEngine(new VariationalMessagePassing());
			Engine.Compiler.ShowWarnings = false;
		}
Ejemplo n.º 55
0
        public void ResizeTests()
        {
            var randomGenerator = new System.Random(66707770); // make this deterministic 

            using (var map = new MemoryMapStream())
            {
                using (var array = new VariableArray<string>(map, 1024, 1000))
                {
                    var arrayExepected = new string[1000];

                    for (uint i = 0; i < 1000; i++)
                    {
                        if (randomGenerator.Next(4) >= 2)
                        { // add data.
                            arrayExepected[i] = i.ToString();
                            array[i] = i.ToString();
                        }
                        else
                        {
                            arrayExepected[i] = int.MaxValue.ToString();
                            array[i] = int.MaxValue.ToString();
                        }

                        Assert.AreEqual(arrayExepected[i], array[i]);
                    }

                    Array.Resize<string>(ref arrayExepected, 335);
                    array.Resize(335);

                    Assert.AreEqual(arrayExepected.Length, array.Length);
                    for (int i = 0; i < arrayExepected.Length; i++)
                    {
                        Assert.AreEqual(arrayExepected[i], array[i]);
                    }
                }

                using (var array = new VariableArray<string>(map, 1024, 1000))
                {
                    var arrayExpected = new string[1000];

                    for (uint i = 0; i < 1000; i++)
                    {
                        if (randomGenerator.Next(4) >= 1)
                        { // add data.
                            arrayExpected[i] = i.ToString();
                            array[i] = i.ToString();
                        }
                        else
                        {
                            arrayExpected[i] = int.MaxValue.ToString();
                            array[i] = int.MaxValue.ToString();
                        }

                        Assert.AreEqual(arrayExpected[i], array[i]);
                    }

                    Array.Resize<string>(ref arrayExpected, 1235);
                    var oldSize = array.Length;
                    array.Resize(1235);

                    Assert.AreEqual(arrayExpected.Length, array.Length);
                    for (int i = 0; i < arrayExpected.Length; i++)
                    {
                        Assert.AreEqual(arrayExpected[i], array[i], 
                            string.Format("Array element not equal at index: {0}. Expected {1}, found {2}",
                                i, array[i], arrayExpected[i]));
                    }
                }
            }
        }
Ejemplo n.º 56
0
		/// <summary>
		/// Specifies the training model
		/// </summary>
		/// <param name="s">The name of the training model</param>
		/// <returns>A <see cref="BPMVarsForTrain"/> instance</returns>
		private BPMVarsForTrain SpecifyTrainModel(string s)
		{
			// An array of feature vectors - their observed values will be
			// set by the calling program
			VariableArray<Vector>[] xValues = new VariableArray<Vector>[nClass];
			// The number of items for each component
			Variable<int>[] nItem = new Variable<int>[nClass];
			// Ranges over the items for each component
			Range[] item = new Range[nClass];
			// The weight vector for each component
			Variable<Vector>[] w = new Variable<Vector>[nClass];
			// The prior weight distributions for each component
			Variable<VectorGaussian>[] wInit = new Variable<VectorGaussian>[nClass];
			for (int c = 0; c < nClass; c++)
			{
				// The prior distributions will be set by the calling program
				wInit[c] = Variable.New<VectorGaussian>();
				// The weight vectors are drawn from the prior distribution
				w[c] = Variable<Vector>.Random(wInit[c]);
			}
			// Loop over the components
			for (int c = 0; c < nClass; c++)
			{
				// The number of items for each component - set by the calling program
				nItem[c] = Variable.New<int>().Named("nItem_" + c + s);
				// The item range for each component
				item[c] = new Range(nItem[c]).Named("item_" + c + s);
				// The items for each component - set by the calling program
				xValues[c] = Variable.Array<Vector>(item[c]);
				// Loop over the items
				using (Variable.ForEach(item[c]))
				{
					// The score for this item across all components
					Variable<double>[] score = BPMUtils.ComputeClassScores(w, xValues[c][item[c]], NoisePrec);
					// The constraint imposed by the observed component
					BPMUtils.ConstrainArgMax(c, score);
				}
			}
			// Store the variables 
			BPMVarsForTrain bpmVar = new BPMVarsForTrain();
			bpmVar.ie = new InferenceEngine();
			bpmVar.xValues = xValues;
			bpmVar.nItems = nItem;
			bpmVar.w = w;
			bpmVar.wInit = wInit;
			return bpmVar;
		}
Ejemplo n.º 57
0
			public Model(SharedVariableArray<Vector> w, Range c, int numChunks)
			{
				// Items.
				numItems = Variable.New<int>().Named("numItems");
				i = new Range(numItems).Named("i");
				i.AddAttribute(new Sequential());
	
				// The model identifier for the shared variables.
				model = new MicrosoftResearch.Infer.Models.Model(numChunks).Named("model");
				// The weight vector for each submodel.
				wModel = w.GetCopyFor(model).Named("wModel");

				noisePrecision = Variable.New<double>().Named("noisePrecision");

				// Arrays of <see cref="Vector"/>-valued items (feature vectors) and integer labels.
				x = Variable.Array<Vector>(i).Named("x");
				y = Variable.Array<int>(i).Named("y");

				// For all items...
				using (Variable.ForEach(i))
				{
					// ...compute the score of this item across all classes...
					score = BPMUtils.ComputeClassScores(wModel, x[i], noisePrecision);
					y[i] = Variable.DiscreteUniform(c);

					// ... and constrain the output.
					BPMUtils.ConstrainMaximum(y[i], score);
				}

				// Inference engine settings (EP).
				engine.Compiler.UseSerialSchedules = true;
				engine.ShowProgress = false;
			}
Ejemplo n.º 58
0
		// Generates data from the model
		void GenerateData(int numUsers, int numItems, int numTraits, int numObservations, int numLevels,
											VariableArray<int> userData, VariableArray<int> itemData,
											VariableArray<VariableArray<bool>, bool[][]> ratingData,
											Gaussian[][] userTraitsPrior, Gaussian[][] itemTraitsPrior,
											Gaussian[] userBiasPrior, Gaussian[] itemBiasPrior, Gaussian[][] userThresholdsPrior,
											double affinityNoiseVariance, double thresholdsNoiseVariance)
		{
			int[] generatedUserData = new int[numObservations];
			int[] generatedItemData = new int[numObservations];
			bool[][] generatedRatingData = new bool[numObservations][];

			// Sample model parameters from the priors
			Rand.Restart(12347);
			double[][] userTraits = Util.ArrayInit(numUsers, u => Util.ArrayInit(numTraits, t => userTraitsPrior[u][t].Sample()));
			double[][] itemTraits = Util.ArrayInit(numItems, i => Util.ArrayInit(numTraits, t => itemTraitsPrior[i][t].Sample()));
			double[] userBias = Util.ArrayInit(numUsers, u => userBiasPrior[u].Sample());
			double[] itemBias = Util.ArrayInit(numItems, i => itemBiasPrior[i].Sample());
			double[][] userThresholds = Util.ArrayInit(numUsers, u => Util.ArrayInit(numLevels, l => userThresholdsPrior[u][l].Sample()));

			// Repeat the model with fixed parameters
			HashSet<int> visited = new HashSet<int>();
			for (int observation = 0; observation < numObservations; observation++) {
				int user = Rand.Int(numUsers);
				int item = Rand.Int(numItems);

				int userItemPairID = user * numItems + item; // pair encoding
				if (visited.Contains(userItemPairID)) // duplicate generated
                {
					observation--; // reject pair
					continue;
				}
				visited.Add(userItemPairID);

				double[] products = Util.ArrayInit(numTraits, t => userTraits[user][t] * itemTraits[item][t]);
				double bias = userBias[user] + itemBias[item];
				double affinity = bias + products.Sum();
				double noisyAffinity = new Gaussian(affinity, affinityNoiseVariance).Sample();
				double[] noisyThresholds = Util.ArrayInit(numLevels, l => new Gaussian(userThresholds[user][l], thresholdsNoiseVariance).Sample());

				generatedUserData[observation] = user;
				generatedItemData[observation] = item;
				generatedRatingData[observation] = Util.ArrayInit(numLevels, l => noisyAffinity > noisyThresholds[l]);
			}

			userData.ObservedValue = generatedUserData;
			itemData.ObservedValue = generatedItemData;
			ratingData.ObservedValue = generatedRatingData;
		}
Ejemplo n.º 59
0
			public Model()
			{
				// Classes.
				numClasses = Variable.New<int>().Named("numClasses");
				c = new Range(numClasses).Named("c");

				// Items.
				numItems = Variable.New<int>().Named("numItems");
				i = new Range(numItems).Named("i");
				i.AddAttribute(new Sequential());

				// The prior distribution for weight vector for each class. When
				// <see cref="Test"/> is called, this is set to the posterior weight
				// distributions from <see cref="Train"/>.
				wPrior = Variable.Array<VectorGaussian>(c).Named("wPrior");

				// The weight vector for each class.
				w = Variable.Array<Vector>(c).Named("w");
				w[c] = Variable<Vector>.Random(wPrior[c]);

				noisePrecision = Variable.New<double>().Named("noisePrecision");

				// Arrays of <see cref="Vector"/>-valued items (feature vectors) and integer labels.
				x = Variable.Array<Vector>(i).Named("x");
				y = Variable.Array<int>(i).Named("y");

				// For all items...
				using (Variable.ForEach(i))
				{
					// ...compute the score of this item across all classes...
					score = BPMUtils.ComputeClassScores(w, x[i], noisePrecision);
					y[i] = Variable.DiscreteUniform(c);

					// ... and constrain the output.
					BPMUtils.ConstrainMaximum(y[i], score);
				}

				// Inference engine settings (EP).
				engine.Compiler.UseSerialSchedules = true;
				engine.ShowProgress = false;
			}
Ejemplo n.º 60
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BinaryModel"/> class.
        /// </summary>
        /// <param name="trainModel">If set to <c>true</c> train model.</param>
        /// <param name="showFactorGraph">If set to <c>true</c> show factor graph.</param>
        /// <param name="debug">If set to <c>true</c> debug.</param>
        public BinaryModel(bool trainModel, bool showFactorGraph = false, bool debug = false)
        {
            evidence = Variable.Bernoulli(0.5).Named("evidence");

            using (Variable.If(evidence))
            {
                numberOfResidents = Variable.New<int>().Named("numberOfResidents").Attrib(new DoNotInfer());
                numberOfFeatures = Variable.New<int>().Named("numberOfFeatures").Attrib(new DoNotInfer());

                var resident = new Range(numberOfResidents).Named("resident");
                var feature = new Range(numberOfFeatures).Named("feature");

                numberOfExamples = Variable.Array<int>(resident).Named("numberOfExamples").Attrib(new DoNotInfer());
                var example = new Range(numberOfExamples[resident]).Named("example").Attrib(new Sequential());

                noisePrecision = Variable.New<double>().Named("noisePrecision").Attrib(new DoNotInfer());

                weightPriorMeans = Variable.New<GaussianArray>().Named("weightPriorMeans").Attrib(new DoNotInfer());
                weightPriorPrecisions = Variable.New<GammaArray>().Named("weightPriorPrecisions").Attrib(new DoNotInfer());

                weightMeans = Variable.Array<double>(feature).Named("weightMeans");
                weightPrecisions = Variable.Array<double>(feature).Named("weightPrecisions");

                weightMeans.SetTo(Variable<double[]>.Random(weightPriorMeans));
                weightPrecisions.SetTo(Variable<double[]>.Random(weightPriorPrecisions));

                weights = Variable.Array(Variable.Array<double>(feature), resident).Named("weights");
                featureValues = Variable.Array(Variable.Array(Variable.Array<double>(feature), example), resident).Named("featureValues").Attrib(new DoNotInfer());

                //			if (!useBias)
                //			{
                //				thresholdPriors = Variable.New<GaussianArray>().Named("thresholdPrior").Attrib(new DoNotInfer());
                //				thresholds = Variable.Array<double>(resident).Named("threshold");
                //				thresholds.SetTo(Variable<double[]>.Random(thresholdPriors));
                //			}

                activities = Variable.Array(Variable.Array<bool>(example), resident).Named("activities");
                // activities[resident][example].AddAttribute(new MarginalPrototype(new Bernoulli()));

                using (Variable.ForEach(resident))
                {
                    var products = Variable.Array(Variable.Array<double>(feature), example).Named("products");
                    var scores = Variable.Array<double>(example).Named("scores");
                    var scoresPlusNoise = Variable.Array<double>(example).Named("scoresPlusNoise");

                    weights[resident][feature] = Variable.GaussianFromMeanAndPrecision(weightMeans[feature], weightPrecisions[feature]);

                    using (Variable.ForEach(example))
                    {
                        using (Variable.ForEach(feature))
                        {
                            products[example][feature] = weights[resident][feature] * featureValues[resident][example][feature];
                        }

                        scores[example] = Variable.Sum(products[example]).Named("score");
                        scoresPlusNoise[example] = Variable.GaussianFromMeanAndPrecision(scores[example], noisePrecision).Named("scorePlusNoise");

                        //					if (useBias)
                        {
                            activities[resident][example] = scoresPlusNoise[example] > 0;
                        }
                        //					else
                        //					{
                        //						var diff = (scoresPlusNoise[example] - thresholds[resident]).Named("diff");
                        //						activities[example][resident] = diff > 0;
                        //					}
                    }
                }
            }

            engine = new InferenceEngine
            {
                Algorithm = new ExpectationPropagation { DefaultNumberOfIterations = trainModel ? 10 : 1 },
                ShowFactorGraph = showFactorGraph,
                ShowProgress = false,
                // BrowserMode = BrowserMode.Never, // debug ? BrowserMode.OnError : BrowserMode.Never,
                ShowWarnings = debug
            };

            if (debug)
            {
                engine.Compiler.GenerateInMemory = false;
                engine.Compiler.WriteSourceFiles = true;
                engine.Compiler.IncludeDebugInformation = true;
                engine.Compiler.CatchExceptions = true;
            }

            #if USE_PRECOMPILED_ALGORITHM
            numberOfResidents.ObservedValue = default(int);
            numberOfExamples.ObservedValue = default(int);
            numberOfFeatures.ObservedValue = default(int);
            noisePrecision.ObservedValue = default(double);
            featureValues.ObservedValue = default(double[][][]);
            weightPriorMeans.ObservedValue = default(DistributionStructArray<Gaussian, double>); // (DistributionStructArray<Gaussian, double>)Distribution<double>.Array(default(Gaussian[]));
            weightPriorPrecisions.ObservedValue = default(DistributionStructArray<Gamma, double>); // (DistributionStructArray<Gamma, double>)Distribution<double>.Array(default(Gamma[]));
            activities.ObservedValue = default(bool[][]);

            if (trainModel)
            {
                activities.AddAttribute(new DoNotInfer());
                algorithm = engine.GetCompiledInferenceAlgorithm(new IVariable[] { weights, weightMeans, weightPrecisions });
            }
            else
            {
                activities.AddAttribute(QueryTypes.Marginal);
                algorithm = engine.GetCompiledInferenceAlgorithm(new IVariable[] { activities });
            }
            #endif
        }