private void ClutterWithLoops(Gaussian meanPrior, Gaussian noiseDist, double[] data) { bool evidence = Factor.Random(new Bernoulli(0.5)); if (evidence) { double prec = 1; double mixWeight = 0.5; double mean = Factor.Random(meanPrior); bool[] b = new bool[data.Length]; for (int i = 0; i < data.Length; i++) { b[i] = Factor.Bernoulli(mixWeight); double x; if (b[i]) { x = Factor.Gaussian(mean, prec); } else { x = Factor.Random(noiseDist); } Constrain.Equal(x, data[i]); } InferNet.Infer(mean, nameof(mean)); InferNet.Infer(b, nameof(b)); } InferNet.Infer(evidence, nameof(evidence)); }
private void BossPredictorModel(bool isRaining) { bool raining = Factor.Random(new Bernoulli(0.8)); bool coffee = Factor.Random(new Bernoulli(0.6)); bool notRaining = Factor.Not(raining); bool temp = Factor.Or(coffee, notRaining); bool goodMood; bool approvesTrip; if (temp) { goodMood = Factor.Bernoulli(0.9); } else { goodMood = Factor.Bernoulli(0.2); } if (goodMood) { approvesTrip = Factor.Bernoulli(0.9); } else { approvesTrip = Factor.Bernoulli(0.4); } Constrain.Equal(raining, isRaining); InferNet.Infer(approvesTrip, nameof(approvesTrip)); }
private void ClippedGaussianModel(double threshold) { double x = Factor.Random(new Gaussian(0, 1)); //Attrib.Var(x, new Algorithm(new VariationalMessagePassing())); double diff = Factor.Difference(x, threshold); InferNet.Infer(x, nameof(x)); bool h = Factor.IsPositive(diff); Constrain.Equal(true, h); }
private void SimpleGaussianModel() { double mean = Factor.Random(new Gaussian(0, 100)); double precision = Factor.Random(Gamma.FromShapeAndScale(1, 1)); double x1 = Gaussian.Sample(mean, precision); double x2 = Gaussian.Sample(mean, precision); Constrain.Equal(x1, 5.0); Constrain.Equal(x2, 7.0); InferNet.Infer(mean, nameof(mean)); InferNet.Infer(precision, nameof(precision)); }
/// <summary>Computations that do not depend on observed values</summary> private void Constant() { if (this.Constant_isDone) { return; } // Create array for 'vdouble__0_uses' Backwards messages. this.vdouble__0_uses_B = new DistributionStructArray <Gaussian, double> [2]; // Create array for 'vdouble__0_uses' Backwards messages. this.vdouble__0_uses_B[0] = new DistributionStructArray <Gaussian, double>(5); // Create array for 'vdouble__0_uses' Backwards messages. this.vdouble__0_uses_B[1] = new DistributionStructArray <Gaussian, double>(5); // Create array for 'vdouble__0_itemvint__1_index0_' Backwards messages. this.vdouble__0_itemvint__1_index0__B = new DistributionStructArray <Gaussian, double>(6); // Create array for 'vdouble__0_itemvint__0_index0_' Backwards messages. this.vdouble__0_itemvint__0_index0__B = new DistributionStructArray <Gaussian, double>(6); bool vbool0_reduced = default(bool); vbool0_reduced = true; Constrain.Equal <bool>(true, vbool0_reduced); this.Constant_isDone = true; }
private static void Clutter(Gaussian meanPrior, Gaussian noiseDist, double data1, double data2) { double prec = 1; double mixWeight = 0.5; bool evidence = Factor.Random(new Bernoulli(0.5)); if (evidence) { double mean = Factor.Random(meanPrior); bool b1 = Factor.Bernoulli(mixWeight); double x1; if (b1) { x1 = Factor.Gaussian(mean, prec); } else { x1 = Factor.Random(noiseDist); } Constrain.Equal(x1, data1); bool b2 = Factor.Bernoulli(mixWeight); double x2; if (b2) { x2 = Factor.Gaussian(mean, prec); } else { x2 = Factor.Random(noiseDist); } Constrain.Equal(x2, data2); InferNet.Infer(mean, nameof(mean)); InferNet.Infer(b1, nameof(b1)); InferNet.Infer(b2, nameof(b2)); } InferNet.Infer(evidence, nameof(evidence)); }
internal void Click5LabelModel( Gaussian inputScoreMean, Gamma inputScorePrec, Gamma inputJudgePrec, Gamma inputClickPrec, Gaussian[] inputThresh, Gaussian[][] clickObservations) { // Add variables outside plate - all variables outside the plate should // be marked as outputs because we will be communicating their messages // across chunks double scoreMean = Factor.Random(inputScoreMean); double scorePrec = Factor.Random(inputScorePrec); double judgePrec = Factor.Random(inputJudgePrec); double clickPrec = Factor.Random(inputClickPrec); double thresh0 = Factor.Random(inputThresh[0]); double thresh1 = Factor.Random(inputThresh[1]); double thresh2 = Factor.Random(inputThresh[2]); double thresh3 = Factor.Random(inputThresh[3]); double thresh4 = Factor.Random(inputThresh[4]); double thresh5 = Factor.Random(inputThresh[5]); //Attrib.AllVars(new DivideMessages(false), thresh0, thresh1, thresh2, thresh3, thresh4, thresh5); //Attrib.AllVars(new DivideMessages(false), scoreMean, scorePrec, judgePrec, clickPrec); // Plate 1 int n1 = clickObservations[0].Length; double[] scores1 = new double[n1]; double[] scoresJ1 = new double[n1]; double[] scoresC1 = new double[n1]; for (int i1 = 0; i1 < n1; i1++) { Attrib.Var(i1, new Sequential()); scores1[i1] = Factor.Gaussian(scoreMean, scorePrec); // click-based score scoresC1[i1] = Factor.Gaussian(scores1[i1], clickPrec); Constrain.EqualRandom(scoresC1[i1], clickObservations[0][i1]); // judged score scoresJ1[i1] = Factor.Gaussian(scores1[i1], judgePrec); bool h1 = Factor.IsBetween(scoresJ1[i1], thresh0, thresh1); Constrain.Equal(true, h1); } // Plate 2 int n2 = clickObservations[1].Length; double[] scores2 = new double[n2]; double[] scoresJ2 = new double[n2]; double[] scoresC2 = new double[n2]; for (int i2 = 0; i2 < n2; i2++) { Attrib.Var(i2, new Sequential()); scores2[i2] = Factor.Gaussian(scoreMean, scorePrec); // click-based score scoresC2[i2] = Factor.Gaussian(scores2[i2], clickPrec); Constrain.EqualRandom(scoresC2[i2], clickObservations[1][i2]); // judged score scoresJ2[i2] = Factor.Gaussian(scores2[i2], judgePrec); bool h2 = Factor.IsBetween(scoresJ2[i2], thresh1, thresh2); Constrain.Equal(true, h2); } // Plate 3 int n3 = clickObservations[2].Length; double[] scores3 = new double[n3]; double[] scoresJ3 = new double[n3]; double[] scoresC3 = new double[n3]; for (int i3 = 0; i3 < n3; i3++) { Attrib.Var(i3, new Sequential()); scores3[i3] = Factor.Gaussian(scoreMean, scorePrec); // click-based score scoresC3[i3] = Factor.Gaussian(scores3[i3], clickPrec); Constrain.EqualRandom(scoresC3[i3], clickObservations[2][i3]); // judged score scoresJ3[i3] = Factor.Gaussian(scores3[i3], judgePrec); bool h3 = Factor.IsBetween(scoresJ3[i3], thresh2, thresh3); Constrain.Equal(true, h3); } // Plate 4 int n4 = clickObservations[3].Length; double[] scores4 = new double[n4]; double[] scoresJ4 = new double[n4]; double[] scoresC4 = new double[n4]; for (int i4 = 0; i4 < n4; i4++) { Attrib.Var(i4, new Sequential()); scores4[i4] = Factor.Gaussian(scoreMean, scorePrec); // click-based score scoresC4[i4] = Factor.Gaussian(scores4[i4], clickPrec); Constrain.EqualRandom(scoresC4[i4], clickObservations[3][i4]); // judged score scoresJ4[i4] = Factor.Gaussian(scores4[i4], judgePrec); bool h4 = Factor.IsBetween(scoresJ4[i4], thresh3, thresh4); Constrain.Equal(true, h4); } // Plate 5 int n5 = clickObservations[4].Length; double[] scores5 = new double[n5]; double[] scoresJ5 = new double[n5]; double[] scoresC5 = new double[n5]; for (int i5 = 0; i5 < n5; i5++) { Attrib.Var(i5, new Sequential()); scores5[i5] = Factor.Gaussian(scoreMean, scorePrec); // click-based score scoresC5[i5] = Factor.Gaussian(scores5[i5], clickPrec); Constrain.EqualRandom(scoresC5[i5], clickObservations[4][i5]); // judged score scoresJ5[i5] = Factor.Gaussian(scores5[i5], judgePrec); bool h5 = Factor.IsBetween(scoresJ5[i5], thresh4, thresh5); Constrain.Equal(true, h5); } //Attrib.AllVars(new DivideMessages(false), scores1, scores2, scores3, scores4, scores5); //Attrib.AllVars(new DivideMessages(false), scoresC1, scoresC2, scoresC3, scoresC4, scoresC5); //Attrib.AllVars(new DivideMessages(false), scoresJ1, scoresJ2, scoresJ3, scoresJ4, scoresJ5); InferNet.Infer(scoreMean, nameof(scoreMean)); InferNet.Infer(scorePrec, nameof(scorePrec)); InferNet.Infer(judgePrec, nameof(judgePrec)); InferNet.Infer(clickPrec, nameof(clickPrec)); InferNet.Infer(thresh0, nameof(thresh0)); InferNet.Infer(thresh1, nameof(thresh1)); InferNet.Infer(thresh2, nameof(thresh2)); InferNet.Infer(thresh3, nameof(thresh3)); InferNet.Infer(thresh4, nameof(thresh4)); InferNet.Infer(thresh5, nameof(thresh5)); }