/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="GaussianFromMeanAndVarianceOp"]/message_doc[@name="AverageLogFactor(Gaussian, double, double)"]/*'/> public static double AverageLogFactor([Proper] Gaussian sample, double mean, double variance) { return(GaussianOp.AverageLogFactor(sample, mean, 1.0 / variance)); }
public void SparseGaussianListFactor() { SparseGaussianList.DefaultTolerance = 1e-10; var calcSuffix = ": calculation differs between sparse and dense"; var sparsitySuffix = ": result is not sparse as expected"; var calcErrMsg = ""; var sparsityErrMsg = ""; var tolerance = 1e-10; Rand.Restart(12347); int listSize = 50; // True distribution for the means var sparseMeanDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(1, 2), tolerance); sparseMeanDist[3] = Gaussian.FromMeanAndPrecision(4, 5); sparseMeanDist[6] = Gaussian.FromMeanAndPrecision(7, 8); var meanDist = sparseMeanDist.ToArray(); var sparseMeanPoint = SparseList <double> .Constant(listSize, 0.1); sparseMeanPoint[3] = 0.7; sparseMeanPoint[6] = 0.8; var meanPoint = sparseMeanPoint.ToArray(); // True distribution for the precisions var sparsePrecDist = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(1.1, 1.2), tolerance); sparsePrecDist[3] = Gamma.FromShapeAndRate(2.3, 2.4); sparsePrecDist[6] = Gamma.FromShapeAndRate(3.4, 4.5); var precDist = sparsePrecDist.ToArray(); var sparsePrecPoint = SparseList <double> .Constant(listSize, 0.1); sparsePrecPoint[3] = 5.6; sparsePrecPoint[6] = 0.5; var precPoint = sparsePrecPoint.ToArray(); var sparseSampleDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 1.5), tolerance); sparseSampleDist[3] = Gaussian.FromMeanAndPrecision(-0.5, 2.0); sparseSampleDist[9] = Gaussian.FromMeanAndPrecision(1.6, 0.4); var sampleDist = sparseSampleDist.ToArray(); var sparseSamplePoint = SparseList <double> .Constant(listSize, 0.5); sparseSamplePoint[3] = 0.1; sparseSamplePoint[9] = 2.3; var samplePoint = sparseSamplePoint.ToArray(); var toSparseSampleDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(-0.2, 0.3), tolerance); toSparseSampleDist[3] = Gaussian.FromMeanAndPrecision(2.1, 3.2); toSparseSampleDist[4] = Gaussian.FromMeanAndPrecision(1.3, 0.7); var toSampleDist = toSparseSampleDist.ToArray(); var toSparsePrecDist = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.3, 3.4), tolerance); toSparsePrecDist[3] = Gamma.FromShapeAndRate(3.4, 4.5); toSparsePrecDist[4] = Gamma.FromShapeAndRate(5.6, 6.7); var toPrecDist = toSparsePrecDist.ToArray(); // --------------------------- // Check average log factor // --------------------------- calcErrMsg = "Average log factor" + calcSuffix; // Dist, dist, dist var sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanDist, sparsePrecDist); var avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanDist[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Dist, dist, point sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanDist, sparsePrecPoint); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Dist, point, dist sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanPoint, sparsePrecDist); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanPoint[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Dist, point, point sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanPoint, sparsePrecPoint); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, dist, dist sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanDist, sparsePrecDist); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanDist[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, dist, point sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanDist, sparsePrecPoint); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, point, dist sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecDist); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanPoint[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, point, point sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint); avgLog = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // --------------------------- // Check log average factor // --------------------------- calcErrMsg = "Log average factor" + calcSuffix; var sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist); var logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Dist, dist, point sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanDist, sparsePrecPoint); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Dist, point, dist sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparsePrecDist); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanPoint[i], precDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Dist, point, point sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanPoint, sparsePrecPoint); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Point, dist, dist sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Point, dist, point sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanDist, sparsePrecPoint); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Point, point, dist sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecDist); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanPoint[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // Point, point, point sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint); logAvg = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // --------------------------- // Check log evidence ratio // --------------------------- calcErrMsg = "Log evidence ratio" + calcSuffix; var sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparseSampleDist, toSparsePrecDist); var evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanDist[i], precDist[i], toSampleDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Dist, dist, point sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanDist, sparsePrecPoint); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Dist, point, dist sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparseSampleDist, toSparsePrecDist); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanPoint[i], precDist[i], toSampleDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Dist, point, point sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanPoint, sparsePrecPoint); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, dist, dist sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, dist, point sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanDist, sparsePrecPoint); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanDist[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, point, dist sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanPoint, sparsePrecDist); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanPoint[i], precDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, point, point sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint); evidRat = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanPoint[i], precPoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // --------------------------- // Check SampleAverageConditional // --------------------------- calcErrMsg = "SampleAverageConditional" + calcSuffix; sparsityErrMsg = "SampleAverageConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseSampleAvgConditional); var sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i])); TAssert.True(3 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparsePrecDist, sparseSampleAvgConditional); sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(sampleDist[i], meanPoint[i], precDist[i], toPrecDist[i])); TAssert.True(3 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseMeanDist, sparsePrecPoint, sparseSampleAvgConditional); sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(meanDist[i], precPoint[i])); TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseMeanPoint, sparsePrecPoint, sparseSampleAvgConditional); sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(meanPoint[i], precPoint[i])); TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); // --------------------------- // Check MeanAverageConditional // --------------------------- calcErrMsg = "MeanAverageConditional" + calcSuffix; sparsityErrMsg = "MeanAverageConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseMeanAvgConditional); var meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i])); TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg); sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseMeanAvgConditional); meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i])); TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg); sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSampleDist, sparsePrecPoint, sparseMeanAvgConditional); meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(sampleDist[i], precPoint[i])); TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg); sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance); sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSamplePoint, sparsePrecPoint, sparseMeanAvgConditional); meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(samplePoint[i], precPoint[i])); TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg); // --------------------------- // Check PrecisionAverageConditional // --------------------------- calcErrMsg = "PrecisionAverageConditional" + calcSuffix; sparsityErrMsg = "PrecisionAverageConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance); sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, sparsePrecAvgConditional); var precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(sampleDist[i], meanDist[i], precDist[i])); TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg); sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance); sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSamplePoint, sparseMeanDist, sparsePrecDist, sparsePrecAvgConditional); precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(Gaussian.PointMass(samplePoint[i]), meanDist[i], precDist[i])); TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg); sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance); sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSampleDist, sparseMeanPoint, sparsePrecDist, sparsePrecAvgConditional); precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(sampleDist[i], Gaussian.PointMass(meanPoint[i]), precDist[i])); TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg); sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance); sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSamplePoint, sparseMeanPoint, sparsePrecAvgConditional); precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(samplePoint[i], meanPoint[i])); TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg); }
/// <summary>Computations that depend on the observed value of numberOfIterationsDecreased and vGamma__1 and vGaussian__1 and vDirichlet1</summary> /// <param name="numberOfIterations">The number of times to iterate each loop</param> private void Changed_numberOfIterationsDecreased_vGamma__1_vGaussian__1_vDirichlet1(int numberOfIterations) { if (this.Changed_numberOfIterationsDecreased_vGamma__1_vGaussian__1_vDirichlet1_iterationsDone == numberOfIterations) { return; } // Create array for 'vdouble__3_marginal' Forwards messages. this.vdouble__3_marginal_F = new DistributionStructArray <Gaussian, double>(2); for (int index2 = 0; index2 < 2; index2++) { this.vdouble__3_marginal_F[index2] = Gaussian.Uniform(); } this.vVector1_marginal_F = ArrayHelper.MakeUniform <Dirichlet>(this.VDirichlet1); Discrete vint9_F = ArrayHelper.MakeUniform <Discrete>(Discrete.Uniform(2)); Discrete[] vint9_selector_uses_B = default(Discrete[]); // Create array for 'vint9_selector_uses' Backwards messages. vint9_selector_uses_B = new Discrete[3]; for (int _ind = 0; _ind < 3; _ind++) { vint9_selector_uses_B[_ind] = ArrayHelper.MakeUniform <Discrete>(Discrete.Uniform(2)); } DistributionStructArray <Bernoulli, bool>[] vint9_selector_cases_uses_B = default(DistributionStructArray <Bernoulli, bool>[]); // Create array for 'vint9_selector_cases_uses' Backwards messages. vint9_selector_cases_uses_B = new DistributionStructArray <Bernoulli, bool> [2]; for (int _ind = 0; _ind < 2; _ind++) { // Create array for 'vint9_selector_cases_uses' Backwards messages. vint9_selector_cases_uses_B[_ind] = new DistributionStructArray <Bernoulli, bool>(2); for (int _iv = 0; _iv < 2; _iv++) { vint9_selector_cases_uses_B[_ind][_iv] = Bernoulli.Uniform(); } } DistributionStructArray <Bernoulli, bool>[] vint9_selector_cases_depth1_uses_B = default(DistributionStructArray <Bernoulli, bool>[]); // Create array for 'vint9_selector_cases_depth1_uses' Backwards messages. vint9_selector_cases_depth1_uses_B = new DistributionStructArray <Bernoulli, bool> [2]; for (int _iv = 0; _iv < 2; _iv++) { // Create array for 'vint9_selector_cases_depth1_uses' Backwards messages. vint9_selector_cases_depth1_uses_B[_iv] = new DistributionStructArray <Bernoulli, bool>(10); for (int _ind = 0; _ind < 10; _ind++) { vint9_selector_cases_depth1_uses_B[_iv][_ind] = Bernoulli.Uniform(); } } // Create array for replicates of 'vdouble17__index2__F' DistributionStructArray <Gaussian, double> vdouble17__index2__F = new DistributionStructArray <Gaussian, double>(2); for (int index2 = 0; index2 < 2; index2++) { vdouble17__index2__F[index2] = Gaussian.Uniform(); } // Message from use of 'vdouble17' Gaussian vdouble17_use_B = Gaussian.Uniform(); // Message to marginal of 'vdouble17__index2_' // Create array for replicates of 'vdouble17__index2__marginal_F' DistributionStructArray <Gaussian, double> vdouble17__index2__marginal_F = new DistributionStructArray <Gaussian, double>(2); for (int index2 = 0; index2 < 2; index2++) { vdouble17__index2__marginal_F[index2] = Gaussian.Uniform(); } // Create array for replicates of 'vdouble__4_index2__B' DistributionStructArray <Gamma, double> vdouble__4_index2__B = new DistributionStructArray <Gamma, double>(2); for (int index2 = 0; index2 < 2; index2++) { vdouble__4_index2__B[index2] = Gamma.Uniform(); } // Create array for 'vdouble__4_marginal' Forwards messages. this.vdouble__4_marginal_F = new DistributionStructArray <Gamma, double>(2); for (int index2 = 0; index2 < 2; index2++) { this.vdouble__4_marginal_F[index2] = Gamma.Uniform(); } DistributionStructArray <Bernoulli, bool> vint9_selector_cases_depth1_B = default(DistributionStructArray <Bernoulli, bool>); // Create array for 'vint9_selector_cases_depth1' Backwards messages. vint9_selector_cases_depth1_B = new DistributionStructArray <Bernoulli, bool>(2); for (int _iv = 0; _iv < 2; _iv++) { vint9_selector_cases_depth1_B[_iv] = Bernoulli.Uniform(); } DistributionStructArray <Bernoulli, bool> vint9_selector_cases_B = default(DistributionStructArray <Bernoulli, bool>); // Create array for 'vint9_selector_cases' Backwards messages. vint9_selector_cases_B = new DistributionStructArray <Bernoulli, bool>(2); for (int _iv = 0; _iv < 2; _iv++) { vint9_selector_cases_B[_iv] = Bernoulli.Uniform(); } this.vint9_marginal_F = ArrayHelper.MakeUniform <Discrete>(Discrete.Uniform(2)); // Create array for replicates of 'vdouble__3_index2__B' DistributionStructArray <Gaussian, double> vdouble__3_index2__B = new DistributionStructArray <Gaussian, double>(2); for (int index2 = 0; index2 < 2; index2++) { vdouble__3_index2__B[index2] = Gaussian.Uniform(); // Message to 'vdouble__3_marginal' from Variable factor this.vdouble__3_marginal_F[index2] = VariableVmpOp.MarginalAverageLogarithm <Gaussian>(this.vdouble__3_use_B[index2], this.VGaussian__1[index2], this.vdouble__3_marginal_F[index2]); // Message to 'vdouble__4_marginal' from Variable factor this.vdouble__4_marginal_F[index2] = VariableVmpOp.MarginalAverageLogarithm <Gamma>(this.vdouble__4_use_B[index2], this.VGamma__1[index2], this.vdouble__4_marginal_F[index2]); // Message to 'vdouble17__index2_' from Gaussian factor vdouble17__index2__F[index2] = GaussianOp.SampleAverageLogarithm(this.vdouble__3_marginal_F[index2], this.vdouble__4_marginal_F[index2]); // Message to 'vdouble17__index2__marginal' from Variable factor vdouble17__index2__marginal_F[index2] = VariableVmpOp.MarginalAverageLogarithm <Gaussian>(vdouble17_use_B, vdouble17__index2__F[index2], vdouble17__index2__marginal_F[index2]); } // Message to 'vVector1_marginal' from Variable factor this.vVector1_marginal_F = VariableVmpOp.MarginalAverageLogarithm <Dirichlet>(this.vVector1_use_B, this.VDirichlet1, this.vVector1_marginal_F); // Message to 'vint9' from Discrete factor vint9_F = DiscreteFromDirichletOp.SampleAverageLogarithm(this.vVector1_marginal_F, vint9_F); // Message to 'vint9_marginal' from Variable factor this.vint9_marginal_F = VariableVmpOp.MarginalAverageLogarithm <Discrete>(this.vint9_selector_B, vint9_F, this.vint9_marginal_F); for (int iteration = this.Changed_numberOfIterationsDecreased_vGamma__1_vGaussian__1_vDirichlet1_iterationsDone; iteration < numberOfIterations; iteration++) { for (int index2 = 0; index2 < 2; index2++) { // Message to 'vdouble__4_marginal' from Variable factor this.vdouble__4_marginal_F[index2] = VariableVmpOp.MarginalAverageLogarithm <Gamma>(this.vdouble__4_use_B[index2], this.VGamma__1[index2], this.vdouble__4_marginal_F[index2]); // Message to 'vdouble__3_index2_' from Gaussian factor vdouble__3_index2__B[index2] = GaussianOp.MeanAverageLogarithm(vdouble17__index2__marginal_F[index2], this.vdouble__4_marginal_F[index2]); // Message to 'vdouble__3_use' from EnterOne factor this.vdouble__3_use_B[index2] = GateEnterOneOp <double> .ValueAverageLogarithm <Gaussian>(vdouble__3_index2__B[index2], this.vint9_marginal_F, index2, this.vdouble__3_use_B[index2]); // Message to 'vdouble__3_marginal' from Variable factor this.vdouble__3_marginal_F[index2] = VariableVmpOp.MarginalAverageLogarithm <Gaussian>(this.vdouble__3_use_B[index2], this.VGaussian__1[index2], this.vdouble__3_marginal_F[index2]); // Message to 'vdouble17__index2_' from Gaussian factor vdouble17__index2__F[index2] = GaussianOp.SampleAverageLogarithm(this.vdouble__3_marginal_F[index2], this.vdouble__4_marginal_F[index2]); // Message to 'vdouble17__index2__marginal' from Variable factor vdouble17__index2__marginal_F[index2] = VariableVmpOp.MarginalAverageLogarithm <Gaussian>(vdouble17_use_B, vdouble17__index2__F[index2], vdouble17__index2__marginal_F[index2]); // Message to 'vint9_selector_cases_depth1_uses' from Variable factor vint9_selector_cases_depth1_uses_B[index2][8] = Bernoulli.FromLogOdds(VariableVmpOp.AverageLogFactor <Gaussian>(vdouble17__index2__marginal_F[index2])); // Message to 'vint9_selector_cases_depth1_uses' from Gaussian factor vint9_selector_cases_depth1_uses_B[index2][7] = Bernoulli.FromLogOdds(GaussianOp.AverageLogFactor(vdouble17__index2__marginal_F[index2], this.vdouble__3_marginal_F[index2], this.vdouble__4_marginal_F[index2])); } for (int _iv = 0; _iv < 2; _iv++) { // Message to 'vint9_selector_cases_depth1' from Replicate factor vint9_selector_cases_depth1_B[_iv] = ReplicateOp.DefAverageLogarithm <Bernoulli>(vint9_selector_cases_depth1_uses_B[_iv], vint9_selector_cases_depth1_B[_iv]); // Message to 'vint9_selector_cases_uses' from Copy factor vint9_selector_cases_uses_B[0][_iv] = ArrayHelper.SetTo <Bernoulli>(vint9_selector_cases_uses_B[0][_iv], vint9_selector_cases_depth1_B[_iv]); } // Message to 'vint9_selector_cases' from Replicate factor vint9_selector_cases_B = ReplicateOp.DefAverageLogarithm <DistributionStructArray <Bernoulli, bool> >(vint9_selector_cases_uses_B, vint9_selector_cases_B); // Message to 'vint9_selector_uses' from CasesInt factor vint9_selector_uses_B[0] = IntCasesOp.IAverageLogarithm(vint9_selector_cases_B, vint9_selector_uses_B[0]); // Message to 'vint9_selector' from Replicate factor this.vint9_selector_B = ReplicateOp.DefAverageLogarithm <Discrete>(vint9_selector_uses_B, this.vint9_selector_B); // Message to 'vVector1_marginal' from Variable factor this.vVector1_marginal_F = VariableVmpOp.MarginalAverageLogarithm <Dirichlet>(this.vVector1_use_B, this.VDirichlet1, this.vVector1_marginal_F); // Message to 'vint9' from Discrete factor vint9_F = DiscreteFromDirichletOp.SampleAverageLogarithm(this.vVector1_marginal_F, vint9_F); // Message to 'vint9_marginal' from Variable factor this.vint9_marginal_F = VariableVmpOp.MarginalAverageLogarithm <Discrete>(this.vint9_selector_B, vint9_F, this.vint9_marginal_F); for (int index2 = 0; index2 < 2; index2++) { // Message to 'vdouble__4_index2_' from Gaussian factor vdouble__4_index2__B[index2] = GaussianOp.PrecisionAverageLogarithm(vdouble17__index2__marginal_F[index2], this.vdouble__3_marginal_F[index2]); // Message to 'vdouble__4_use' from EnterOne factor this.vdouble__4_use_B[index2] = GateEnterOneOp <double> .ValueAverageLogarithm <Gamma>(vdouble__4_index2__B[index2], this.vint9_marginal_F, index2, this.vdouble__4_use_B[index2]); } // Message to 'vVector1_use' from Discrete factor this.vVector1_use_B = DiscreteFromDirichletOp.ProbsAverageLogarithm(this.vint9_marginal_F, this.vVector1_use_B); this.OnProgressChanged(new ProgressChangedEventArgs(iteration)); } for (int index2 = 0; index2 < 2; index2++) { // Message to 'vdouble__3_index2_' from Gaussian factor vdouble__3_index2__B[index2] = GaussianOp.MeanAverageLogarithm(vdouble17__index2__marginal_F[index2], this.vdouble__4_marginal_F[index2]); // Message to 'vdouble__3_use' from EnterOne factor this.vdouble__3_use_B[index2] = GateEnterOneOp <double> .ValueAverageLogarithm <Gaussian>(vdouble__3_index2__B[index2], this.vint9_marginal_F, index2, this.vdouble__3_use_B[index2]); // Message to 'vdouble__3_marginal' from Variable factor this.vdouble__3_marginal_F[index2] = VariableVmpOp.MarginalAverageLogarithm <Gaussian>(this.vdouble__3_use_B[index2], this.VGaussian__1[index2], this.vdouble__3_marginal_F[index2]); // Message to 'vint9_selector_cases_depth1_uses' from Gaussian factor vint9_selector_cases_depth1_uses_B[index2][7] = Bernoulli.FromLogOdds(GaussianOp.AverageLogFactor(vdouble17__index2__marginal_F[index2], this.vdouble__3_marginal_F[index2], this.vdouble__4_marginal_F[index2])); } for (int _iv = 0; _iv < 2; _iv++) { // Message to 'vint9_selector_cases_depth1' from Replicate factor vint9_selector_cases_depth1_B[_iv] = ReplicateOp.DefAverageLogarithm <Bernoulli>(vint9_selector_cases_depth1_uses_B[_iv], vint9_selector_cases_depth1_B[_iv]); // Message to 'vint9_selector_cases_uses' from Copy factor vint9_selector_cases_uses_B[0][_iv] = ArrayHelper.SetTo <Bernoulli>(vint9_selector_cases_uses_B[0][_iv], vint9_selector_cases_depth1_B[_iv]); } // Message to 'vint9_selector_cases' from Replicate factor vint9_selector_cases_B = ReplicateOp.DefAverageLogarithm <DistributionStructArray <Bernoulli, bool> >(vint9_selector_cases_uses_B, vint9_selector_cases_B); // Message to 'vint9_selector_uses' from CasesInt factor vint9_selector_uses_B[0] = IntCasesOp.IAverageLogarithm(vint9_selector_cases_B, vint9_selector_uses_B[0]); // Message to 'vint9_selector' from Replicate factor this.vint9_selector_B = ReplicateOp.DefAverageLogarithm <Discrete>(vint9_selector_uses_B, this.vint9_selector_B); // Message to 'vint9_marginal' from Variable factor this.vint9_marginal_F = VariableVmpOp.MarginalAverageLogarithm <Discrete>(this.vint9_selector_B, vint9_F, this.vint9_marginal_F); for (int index2 = 0; index2 < 2; index2++) { // Message to 'vdouble__4_index2_' from Gaussian factor vdouble__4_index2__B[index2] = GaussianOp.PrecisionAverageLogarithm(vdouble17__index2__marginal_F[index2], this.vdouble__3_marginal_F[index2]); // Message to 'vdouble__4_use' from EnterOne factor this.vdouble__4_use_B[index2] = GateEnterOneOp <double> .ValueAverageLogarithm <Gamma>(vdouble__4_index2__B[index2], this.vint9_marginal_F, index2, this.vdouble__4_use_B[index2]); // Message to 'vdouble__4_marginal' from Variable factor this.vdouble__4_marginal_F[index2] = VariableVmpOp.MarginalAverageLogarithm <Gamma>(this.vdouble__4_use_B[index2], this.VGamma__1[index2], this.vdouble__4_marginal_F[index2]); } // Message to 'vVector1_use' from Discrete factor this.vVector1_use_B = DiscreteFromDirichletOp.ProbsAverageLogarithm(this.vint9_marginal_F, this.vVector1_use_B); // Message to 'vVector1_marginal' from Variable factor this.vVector1_marginal_F = VariableVmpOp.MarginalAverageLogarithm <Dirichlet>(this.vVector1_use_B, this.VDirichlet1, this.vVector1_marginal_F); this.vdouble17_F = Gaussian.Uniform(); DistributionStructArray <Bernoulli, bool> vint9_selector_cases_F = default(DistributionStructArray <Bernoulli, bool>); // Create array for 'vint9_selector_cases' Forwards messages. vint9_selector_cases_F = new DistributionStructArray <Bernoulli, bool>(2); for (int _iv = 0; _iv < 2; _iv++) { vint9_selector_cases_F[_iv] = Bernoulli.Uniform(); } // Message to 'vint9_selector_cases' from CasesInt factor vint9_selector_cases_F = IntCasesOp.CasesAverageLogarithm <DistributionStructArray <Bernoulli, bool> >(this.vint9_marginal_F, vint9_selector_cases_F); // Message to 'vdouble17' from Exit factor this.vdouble17_F = GateExitOp <double> .ExitAverageLogarithm <Gaussian>(vint9_selector_cases_F, vdouble17__index2__marginal_F, this.vdouble17_F); this.Changed_numberOfIterationsDecreased_vGamma__1_vGaussian__1_vDirichlet1_iterationsDone = numberOfIterations; }