public static WrappedGaussian AAverageConditional([SkipIfUniform] WrappedGaussian Product, double B, WrappedGaussian result) { result.Period = Product.Period/B; result.Gaussian = GaussianProductOp.AAverageConditional(Product.Gaussian, B); result.Normalize(); return result; }
public static WrappedGaussian AAverageConditional(double Product, double B, WrappedGaussian result) { if (B == 0) { if (Product != 0) throw new AllZeroException(); result.SetToUniform(); } else result.Point = Product / B; result.Normalize(); return result; }
public static WrappedGaussian ProductAverageLogarithm([SkipIfUniform] WrappedGaussian A, double B, WrappedGaussian result) { double m, v; A.Gaussian.GetMeanAndVariance(out m, out v); result.Gaussian.SetMeanAndVariance(B*m, B*B*v); double period = B*A.Period; if (period != result.Period) { double ratio = period / result.Period; double intRatio = Math.Round(ratio); if (Math.Abs(ratio - intRatio) > result.Period*1e-4) throw new ArgumentException("B*A.Period ("+period+") is not a multiple of result.Period ("+result.Period+")"); // if period is a multiple of result.Period, then wrapping to result.Period is equivalent to first wrapping to period, then to result.Period. } result.Normalize(); return result; }
// ---------------------------------------------------------------------------------------------------------------------- // WrappedGaussian // ---------------------------------------------------------------------------------------------------------------------- /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="DoublePlusOp"]/message_doc[@name="SumAverageConditional(WrappedGaussian, WrappedGaussian)"]/*'/> public static WrappedGaussian SumAverageConditional([SkipIfUniform] WrappedGaussian a, [SkipIfUniform] WrappedGaussian b) { if (a.Period != b.Period) { throw new ArgumentException("a.Period (" + a.Period + ") != b.Period (" + b.Period + ")"); } WrappedGaussian result = WrappedGaussian.Uniform(a.Period); result.Gaussian = SumAverageConditional(a.Gaussian, b.Gaussian); result.Normalize(); return(result); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="PlusWrappedGaussianOp"]/message_doc[@name="SumAverageLogarithm(WrappedGaussian, double)"]/*'/> public static WrappedGaussian SumAverageLogarithm([SkipIfUniform] WrappedGaussian a, double b) { return(PlusWrappedGaussianOp.SumAverageConditional(a, b)); }
public static double AverageLogFactor(WrappedGaussian sum) { return(0.0); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="PlusWrappedGaussianOp"]/message_doc[@name="BAverageConditional(WrappedGaussian, double)"]/*'/> public static WrappedGaussian BAverageConditional([SkipIfUniform] WrappedGaussian sum, double a) { return(AAverageConditional(sum, a)); }
public static Gaussian YAverageLogarithm([SkipIfUniform] VectorGaussian rotate, double angle) { return(YAverageLogarithm(rotate, WrappedGaussian.PointMass(angle))); }
public static WrappedGaussian AAverageLogarithm(double Product, double B, WrappedGaussian result) { return AAverageConditional(Product, B, result); }
public static WrappedGaussian AAverageLogarithm([SkipIfUniform] WrappedGaussian Product, double B, WrappedGaussian result) { if (Product.IsPointMass) return AAverageLogarithm(Product.Point, B, result); return AAverageConditional(Product, B, result); }
public static WrappedGaussian BAverageConditional([SkipIfUniform] WrappedGaussian Product, double A, WrappedGaussian result) { return AAverageConditional(Product, A, result); }
public static Gaussian YAverageLogarithm([SkipIfUniform] VectorGaussian rotate, [Proper] WrappedGaussian angle) { // for x ~ N(m,v): // E[cos(x)] = cos(m)*exp(-v/2) // E[sin(x)] = sin(m)*exp(-v/2) if (angle.Period != 2 * Math.PI) { throw new ArgumentException("angle.Period (" + angle.Period + ") != 2*PI (" + 2 * Math.PI + ")"); } double angleMean, angleVar; angle.Gaussian.GetMeanAndVariance(out angleMean, out angleVar); double expVar = Math.Exp(-0.5 * angleVar); double mCos = Math.Cos(angleMean) * expVar; double mSin = Math.Sin(angleMean) * expVar; if (rotate.Dimension != 2) { throw new ArgumentException("rotate.Dimension (" + rotate.Dimension + ") != 2"); } double prec = rotate.Precision[0, 0]; if (rotate.Precision[0, 1] != 0) { throw new ArgumentException("rotate.Precision is not diagonal"); } if (rotate.Precision[1, 1] != prec) { throw new ArgumentException("rotate.Precision is not spherical"); } #if false Vector rotateMean = rotate.GetMean(); double mean = -mSin * rotateMean[0] + mCos * rotateMean[1]; #else double rotateMean0 = rotate.MeanTimesPrecision[0] / rotate.Precision[0, 0]; double rotateMean1 = rotate.MeanTimesPrecision[1] / rotate.Precision[1, 1]; double mean = -mSin * rotateMean0 + mCos * rotateMean1; #endif return(Gaussian.FromMeanAndPrecision(mean, prec)); }
public void Initialize(bool skipStringDistributions = false) { // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing // you are trying to deserialize. this.pareto = new Pareto(1.2, 3.5); this.poisson = new Poisson(2.3); this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] { { 22, 21 }, { 21, 23 } })); this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] { { 16, 15 }, { 15, 17 } })); this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3)); this.pointMass = PointMass <double> .Create(1.1); this.gaussian = new Gaussian(11.0, 12.0); this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5); this.gamma = new Gamma(9.0, 10.0); this.gammaPower = new GammaPower(5.6, 2.8, 3.4); this.discrete = new Discrete(6.0, 7.0, 8.0); this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5); this.dirichlet = new Dirichlet(3.0, 4.0, 5.0); this.beta = new Beta(2.0, 1.0); this.binomial = new Binomial(5, 0.8); this.bernoulli = new Bernoulli(0.6); this.sparseBernoulliList = SparseBernoulliList.Constant(4, new Bernoulli(0.1)); this.sparseBernoulliList[1] = new Bernoulli(0.9); this.sparseBernoulliList[3] = new Bernoulli(0.7); this.sparseBetaList = SparseBetaList.Constant(5, new Beta(2.0, 2.0)); this.sparseBetaList[0] = new Beta(3.0, 4.0); this.sparseBetaList[1] = new Beta(5.0, 6.0); this.sparseGaussianList = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2)); this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4); this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6); this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0)); this.truncatedGamma = new TruncatedGamma(1.2, 2.3, 3.4, 4.5); this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8); this.wrappedGaussian = new WrappedGaussian(1.2, 2.3, 3.4); ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian }); vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian }); ga2D = Distribution <double> .Array(new[, ] { { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian } }); vga2D = Distribution <Vector> .Array(new[, ] { { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian } }); gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } }); vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } }); var gp = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0)); var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i)); this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis)); this.quantileEstimator = new QuantileEstimator(0.01); this.quantileEstimator.Add(5); this.outerQuantiles = OuterQuantiles.FromDistribution(3, this.quantileEstimator); this.innerQuantiles = InnerQuantiles.FromDistribution(3, this.outerQuantiles); if (!skipStringDistributions) { // String distributions can not be serialized by some formatters (namely BinaryFormatter) // That is fine because this combination is never used in practice this.stringDistribution1 = StringDistribution.String("aa") .Append(StringDistribution.OneOf("b", "ccc")).Append("dddd"); this.stringDistribution2 = new StringDistribution(); this.stringDistribution2.SetToProduct(StringDistribution.OneOf("a", "b"), StringDistribution.OneOf("b", "c")); } }
public static WrappedGaussian AngleAverageLogarithm([SkipIfUniform] VectorGaussian rotate, double x, double y, [Proper] WrappedGaussian angle) { if (rotate.Dimension != 2) { throw new ArgumentException("rotate.Dimension (" + rotate.Dimension + ") != 2"); } double rPrec = rotate.Precision[0, 0]; if (rotate.Precision[0, 1] != 0) { throw new ArgumentException("rotate.Precision is not diagonal"); } if (rotate.Precision[1, 1] != rPrec) { throw new ArgumentException("rotate.Precision is not spherical"); } Vector rotateMean = rotate.GetMean(); double a = x * rotateMean[0] + y * rotateMean[1]; double b = x * rotateMean[1] - y * rotateMean[0]; double c = Math.Sqrt(a * a + b * b) * rPrec; double angle0 = Math.Atan2(b, a); // the exact conditional is exp(c*cos(angle - angle0)) which is a von Mises distribution. // we will approximate this with a Gaussian lower bound that makes contact at the current angleMean. if (angle.Period != 2 * Math.PI) { throw new ArgumentException("angle.Period (" + angle.Period + ") != 2*PI (" + 2 * Math.PI + ")"); } double angleMean = angle.Gaussian.GetMean(); double angleDiff = angleMean - angle0; double df = -c *Math.Sin(angleDiff); double precision = c * Math.Abs(Math.Cos(angleDiff * 0.5)); // ensures a lower bound double meanTimesPrecision = angleMean * precision + df; if (double.IsNaN(meanTimesPrecision)) { throw new ApplicationException("result is nan"); } WrappedGaussian result = WrappedGaussian.Uniform(angle.Period); result.Gaussian = Gaussian.FromNatural(meanTimesPrecision, precision); return(result); }
public static WrappedGaussian AngleAverageLogarithm([SkipIfUniform] VectorGaussian rotate, [Proper] Gaussian x, [Proper] Gaussian y, [Proper] WrappedGaussian angle) { return(AngleAverageLogarithm(rotate, x.GetMean(), y.GetMean(), angle)); }
public static VectorGaussian RotateAverageLogarithm([SkipIfUniform] Gaussian x, [SkipIfUniform] Gaussian y, double angle, VectorGaussian result) { return(RotateAverageLogarithm(x, y, WrappedGaussian.PointMass(angle), result)); }
public static VectorGaussian RotateAverageLogarithm(double x, double y, [Proper] WrappedGaussian angle, VectorGaussian result) { return(RotateAverageLogarithm(Gaussian.PointMass(x), Gaussian.PointMass(y), angle, result)); }
public static VectorGaussian RotateAverageLogarithm([SkipIfUniform] Gaussian x, [SkipIfUniform] Gaussian y, [Proper] WrappedGaussian angle, VectorGaussian result) { // for x ~ N(m,v): // E[cos(x)] = cos(m)*exp(-v/2) // E[sin(x)] = sin(m)*exp(-v/2) if (angle.Period != 2 * Math.PI) { throw new ArgumentException("angle.Period (" + angle.Period + ") != 2*PI (" + 2 * Math.PI + ")"); } double angleMean, angleVar; angle.Gaussian.GetMeanAndVariance(out angleMean, out angleVar); double expHalfVar = Math.Exp(-0.5 * angleVar); double mCos = Math.Cos(angleMean) * expHalfVar; double mSin = Math.Sin(angleMean) * expHalfVar; double mCos2 = mCos * mCos; double mSin2 = mSin * mSin; // E[cos(x)^2] = 0.5 E[1+cos(2x)] = 0.5 (1 + cos(2m) exp(-2v)) // E[sin(x)^2] = E[1 - cos(x)^2] = 0.5 (1 - cos(2m) exp(-2v)) double expVar = expHalfVar * expHalfVar; // cos2m = cos(2m)*exp(-v) double cos2m = 2 * mCos2 - expVar; double mCosSqr = 0.5 * (1 + cos2m * expVar); double mSinSqr = 1 - mCosSqr; double mSinCos = mSin * mCos * expVar; if (result.Dimension != 2) { throw new ArgumentException("result.Dimension (" + result.Dimension + ") != 2"); } double mx, vx, my, vy; x.GetMeanAndVariance(out mx, out vx); y.GetMeanAndVariance(out my, out vy); Vector mean = Vector.Zero(2); mean[0] = mCos * mx - mSin * my; mean[1] = mSin * mx + mCos * my; double mx2 = mx * mx + vx; double my2 = my * my + vy; double mxy = mx * my; PositiveDefiniteMatrix variance = new PositiveDefiniteMatrix(2, 2); variance[0, 0] = mx2 * mCosSqr - 2 * mxy * mSinCos + my2 * mSinSqr - mean[0] * mean[0]; variance[0, 1] = (mx2 - my2) * mSinCos + mxy * (mCosSqr - mSinSqr) - mean[0] * mean[1]; variance[1, 0] = variance[0, 1]; variance[1, 1] = mx2 * mSinSqr + 2 * mxy * mSinCos + my2 * mCosSqr - mean[1] * mean[1]; result.SetMeanAndVariance(mean, variance); return(result); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="DoublePlusVmpOp"]/message_doc[@name="SumAverageLogarithm(double, WrappedGaussian)"]/*'/> public static WrappedGaussian SumAverageLogarithm(double a, [SkipIfUniform] WrappedGaussian b) { return(DoublePlusOp.SumAverageConditional(a, b)); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="DoublePlusVmpOp"]/message_doc[@name="BAverageLogarithm(WrappedGaussian, double)"]/*'/> public static WrappedGaussian BAverageLogarithm([SkipIfUniform] WrappedGaussian sum, double a) { return(AAverageLogarithm(sum, a)); }
public static WrappedGaussian ProductAverageLogarithm(double A, [SkipIfUniform] WrappedGaussian B, WrappedGaussian result) { return ProductAverageLogarithm(B, A, result); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="PlusWrappedGaussianOp"]/message_doc[@name="SumAverageConditional(double, WrappedGaussian)"]/*'/> public static WrappedGaussian SumAverageConditional(double a, [SkipIfUniform] WrappedGaussian b) { return(SumAverageConditional(b, a)); }
public static WrappedGaussian BAverageLogarithm([SkipIfUniform] WrappedGaussian Product, double A, WrappedGaussian result) { return AAverageLogarithm(Product, A, result); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="PlusWrappedGaussianOp"]/message_doc[@name="AAverageConditional(WrappedGaussian, WrappedGaussian)"]/*'/> public static WrappedGaussian AAverageConditional([SkipIfUniform] WrappedGaussian sum, [SkipIfUniform] WrappedGaussian b) { if (sum.Period != b.Period) { throw new ArgumentException("sum.Period (" + sum.Period + ") != b.Period (" + b.Period + ")"); } WrappedGaussian result = WrappedGaussian.Uniform(sum.Period); result.Gaussian = DoublePlusOp.AAverageConditional(sum.Gaussian, b.Gaussian); result.Normalize(); return(result); }
public static double AverageLogFactor(WrappedGaussian sum) { return 0.0; }
public void Initialize() { // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing // you are trying to deserialize. this.pareto = new Pareto(1.2, 3.5); this.poisson = new Poisson(2.3); this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] { { 22, 21 }, { 21, 23 } })); this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] { { 16, 15 }, { 15, 17 } })); this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3)); this.pointMass = PointMass <double> .Create(1.1); this.gaussian = new Gaussian(11.0, 12.0); this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5); this.gamma = new Gamma(9.0, 10.0); this.gammaPower = new GammaPower(5.6, 2.8, 3.4); this.discrete = new Discrete(6.0, 7.0, 8.0); this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5); this.dirichlet = new Dirichlet(3.0, 4.0, 5.0); this.beta = new Beta(2.0, 1.0); this.binomial = new Binomial(5, 0.8); this.bernoulli = new Bernoulli(0.6); this.sparseBernoulliList = SparseBernoulliList.Constant(4, new Bernoulli(0.1)); this.sparseBernoulliList[1] = new Bernoulli(0.9); this.sparseBernoulliList[3] = new Bernoulli(0.7); this.sparseBetaList = SparseBetaList.Constant(5, new Beta(2.0, 2.0)); this.sparseBetaList[0] = new Beta(3.0, 4.0); this.sparseBetaList[1] = new Beta(5.0, 6.0); this.sparseGaussianList = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2)); this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4); this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6); this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0)); this.truncatedGamma = new TruncatedGamma(1.2, 2.3, 3.4, 4.5); this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8); this.wrappedGaussian = new WrappedGaussian(1.2, 2.3, 3.4); ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian }); vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian }); ga2D = Distribution <double> .Array(new[, ] { { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian } }); vga2D = Distribution <Vector> .Array(new[, ] { { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian } }); gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } }); vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } }); var gp = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0)); var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i)); this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis)); this.quantileEstimator = new QuantileEstimator(0.01); this.quantileEstimator.Add(5); }