public void MaxTest2() { foreach (double max in new[] { 0.0, 2.0 }) { double oldm = double.NaN; double oldv = double.NaN; for (int i = 0; i < 300; i++) { Gaussian a = new Gaussian(System.Math.Pow(10, i), 177); Gaussian to_a = MaxGaussianOp.AAverageConditional(max, a, 0); Gaussian to_b = MaxGaussianOp.BAverageConditional(max, 0, a); Assert.Equal(to_a, to_b); if (max == 0) { Gaussian to_a2 = IsPositiveOp.XAverageConditional(false, a); double error = System.Math.Max(MMath.AbsDiff(to_a.MeanTimesPrecision, to_a2.MeanTimesPrecision, double.Epsilon), MMath.AbsDiff(to_a.Precision, to_a2.Precision, double.Epsilon)); //Trace.WriteLine($"{a} {to_a} {to_a2} {error}"); Assert.True(error < 1e-12); } //else Trace.WriteLine($"{a} {to_a}"); double m, v; to_a.GetMeanAndVariance(out m, out v); if (!double.IsNaN(oldm)) { Assert.True(v <= oldv); double olddiff = System.Math.Abs(max - oldm); double diff = System.Math.Abs(max - m); Assert.True(diff <= olddiff); } oldm = m; oldv = v; } } }
private void Max_APointMass(Gaussian max, Gaussian b) { double point = 3; Gaussian toPoint = MaxGaussianOp.AAverageConditional(max, Gaussian.PointMass(point), b); //Console.WriteLine($"{point} {toPoint} {toPoint.MeanTimesPrecision:g17} {toPoint.Precision:g17}"); if (max.IsPointMass && b.IsPointMass) { Gaussian toUniform = MaxGaussianOp.AAverageConditional(max, Gaussian.Uniform(), b); if (max.Point > b.Point) { Assert.Equal(toUniform, max); } else { Assert.Equal(toUniform, Gaussian.Uniform()); } } double oldDiff = double.PositiveInfinity; for (int i = 3; i < 100; i++) { Gaussian a = Gaussian.FromMeanAndPrecision(point, System.Math.Pow(10, i)); Gaussian to_a = MaxGaussianOp.AAverageConditional(max, a, b); double diff = toPoint.MaxDiff(to_a); //Console.WriteLine($"{a} {to_a} {to_a.MeanTimesPrecision:g17} {to_a.Precision:g17} {diff:g17}"); if (diff < 1e-14) { diff = 0; } Assert.True(diff <= oldDiff); oldDiff = diff; } }