/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="WishartFromShapeAndRateOp"]/message_doc[@name="RateAverageLogarithm(Wishart, double, Wishart)"]/*'/> public static Wishart RateAverageLogarithm([SkipIfUniform] Wishart sample, double shape, Wishart result) { int dimension = result.Dimension; result.Shape = shape + 0.5 * (dimension + 1); sample.GetMean(result.Rate); return(result); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ProductWishartOp"]/message_doc[@name="AAverageLogarithm(Wishart, double, Wishart)"]/*'/> public static Wishart AAverageLogarithm([SkipIfUniform] Wishart Product, double B, Wishart result) { if (Product.IsPointMass) { return(AAverageLogarithm(Product.Point, B, result)); } return(AAverageConditional(Product, B, result)); }
public void FailSCreateWishart(double nu, int order) { var matrix = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order); matrix[0, 0] = 0.0; var d = new Wishart(nu, matrix); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="WishartFromShapeAndRateOp"]/message_doc[@name="AverageLogFactor(Wishart, double, Wishart)"]/*'/> public static double AverageLogFactor([Proper] Wishart sample, double shape, [Proper] Wishart rate) { // factor = (a-(d+1)/2)*logdet(X) -tr(X*B) + a*logdet(B) - GammaLn(a,d) int dimension = sample.Dimension; return((shape - (dimension + 1) * 0.5) * sample.GetMeanLogDeterminant() - Matrix.TraceOfProduct(sample.GetMean(), rate.GetMean()) + shape * rate.GetMeanLogDeterminant() - MMath.GammaLn(shape, dimension)); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ProductWishartOp"]/message_doc[@name="BAverageConditional(Wishart, PositiveDefiniteMatrix)"]/*'/> public static Gamma BAverageConditional([SkipIfUniform] Wishart Product, PositiveDefiniteMatrix A) { if (Product.IsPointMass) { return(BAverageConditional(Product.Point, A)); } // det(ab)^(shape-(d+1)/2) exp(-tr(rate*(ab))) =propto b^((shape-(d+1)/2)*d) exp(-b*tr(rate*a)) return(Gamma.FromShapeAndRate(Product.Dimension * (Product.Shape - (Product.Dimension + 1) * 0.5) + 1, Matrix.TraceOfProduct(Product.Rate, A))); }
/// <summary>EP message to <c>b</c>.</summary> /// <param name="Product">Incoming message from <c>product</c>. Must be a proper distribution. If any element is uniform, the result will be uniform.</param> /// <param name="A">Constant value for <c>a</c>.</param> /// <returns>The outgoing EP message to the <c>b</c> argument.</returns> /// <remarks> /// <para>The outgoing message is a distribution matching the moments of <c>b</c> as the random arguments are varied. The formula is <c>proj[p(b) sum_(product) p(product) factor(product,a,b)]/p(b)</c>.</para> /// </remarks> /// <exception cref="ImproperMessageException"> /// <paramref name="Product" /> is not a proper distribution.</exception> public static Gamma BAverageConditional([SkipIfUniform] Wishart Product, PositiveDefiniteMatrix A) { if (Product.IsPointMass) { return(BAverageLogarithm(Product.Point, A)); } // (ab)^(shape-(d+1)/2) exp(-tr(rate*(ab))) return(Gamma.FromShapeAndRate(Product.Shape + (1 - (Product.Dimension + 1) * 0.5), Matrix.TraceOfProduct(Product.Rate, A))); }
private void VectorGaussianModel() { Vector mm = Vector.FromArray(0.1, 0.2); Vector m = Factor.Random(VectorGaussian.FromMeanAndPrecision(mm, PositiveDefiniteMatrix.Identity(2))); PositiveDefiniteMatrix p = Factor.Random(Wishart.FromShapeAndRate(2, 1.0, 1.0)); Vector c = Factor.VectorGaussian(m, p); InferNet.Infer(c, nameof(c)); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ProductWishartOp"]/message_doc[@name="ProductAverageConditional(Wishart, double, Wishart)"]/*'/> public static Wishart ProductAverageConditional([SkipIfUniform] Wishart A, double B, Wishart result) { if (A.IsPointMass) { return(ProductAverageConditional(A.Point, B, result)); } result.SetTo(A); result.Rate.Scale(1 / B); return(result); }
public void ValidateDensity(double nu, double density) { var matrix = Matrix <double> .Build.Dense(1, 1, 1.0); var x = Matrix <double> .Build.Dense(1, 1, 5.0); var d = new Wishart(nu, matrix); AssertHelpers.AlmostEqualRelative(density, d.Density(x), 16); }
public static Wishart ProductAverageConditional([SkipIfUniform] Wishart A, double B, Wishart result) { if (A.IsPointMass) { result.Rate.SetTo(A.Point); result.Rate.Scale(B); } else { result.SetTo(A); result.Rate.Scale(1/B); } return result; }
public void MP_GetItemWishart() { InferenceEngine engine = new InferenceEngine(); ModelDefinitionMethod meth = new ModelDefinitionMethod(GetItemWishartModel); var ca = engine.Compiler.CompileWithoutParams( declaringType, meth.Method, new AttributeRegistry <object, ICompilerAttribute>(true)); ca.Execute(1); Wishart cMarg = ca.Marginal <Wishart>("c"); DistributionArray <Wishart> dMarg = ca.Marginal <DistributionArray <Wishart> >("d"); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ProductWishartOp"]/message_doc[@name="BAverageLogarithm(Wishart, Wishart)"]/*'/> public static Gamma BAverageLogarithm([SkipIfUniform] Wishart Product, [Proper] Wishart A) { if (A.IsPointMass) { return(BAverageLogarithm(Product, A.Point)); } if (Product.IsPointMass) { return(BAverageLogarithm(Product.Point, A)); } return(BAverageConditional(Product, A.GetMean())); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="WishartFromShapeAndRateOp"]/message_doc[@name="LogAverageFactor(PositiveDefiniteMatrix, double, Wishart)"]/*'/> public static double LogAverageFactor(PositiveDefiniteMatrix sample, double shape, Wishart rate) { // f(X,a,B) = |X|^(a-c) |B|^a/Gamma_d(a) exp(-tr(BX)) = |X|^(-2c) Gamma_d(a+c)/Gamma_d(a) W(B; a+c, X) // p(B) = |B|^(a'-c) |B'|^(a')/Gamma_d(a') exp(-tr(BB')) // int_B f p(B) dB = |X|^(a-c) Gamma_d(a+a')/Gamma_d(a)/Gamma_d(a') |B'|^(a') |B'+X|^(-(a+a')) int dimension = sample.Rows; double c = 0.5 * (dimension + 1); Wishart to_rate = new Wishart(dimension); to_rate = RateAverageConditional(sample, shape, to_rate); return(rate.GetLogAverageOf(to_rate) - 2 * c * sample.LogDeterminant() + MMath.GammaLn(shape + c, dimension) - MMath.GammaLn(shape, dimension)); }
/// <summary>VMP message to <c>b</c>.</summary> /// <param name="Product">Incoming message from <c>product</c>. Must be a proper distribution. If any element is uniform, the result will be uniform.</param> /// <param name="A">Incoming message from <c>a</c>. Must be a proper distribution. If any element is uniform, the result will be uniform.</param> /// <returns>The outgoing VMP message to the <c>b</c> argument.</returns> /// <remarks> /// <para>The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except <c>b</c>. Because the factor is deterministic, <c>product</c> is integrated out before taking the logarithm. The formula is <c>exp(sum_(a) p(a) log(sum_product p(product) factor(product,a,b)))</c>.</para> /// </remarks> /// <exception cref="ImproperMessageException"> /// <paramref name="Product" /> is not a proper distribution.</exception> /// <exception cref="ImproperMessageException"> /// <paramref name="A" /> is not a proper distribution.</exception> public static Gamma BAverageLogarithm([SkipIfUniform] Wishart Product, [Proper] Wishart A) { if (A.IsPointMass) { return(BAverageLogarithm(Product, A.Point)); } if (Product.IsPointMass) { return(BAverageLogarithm(Product.Point, A)); } // (ab)^(shape-(d+1)/2) exp(-tr(rate*(ab))) return(Gamma.FromShapeAndRate(Product.Shape + (1 - (Product.Dimension + 1) * 0.5), Matrix.TraceOfProduct(Product.Rate, A.GetMean()))); }
public void CanGetS() { int order = 2; var matrix = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order); var d = new Wishart(1.0, matrix); for (int i = 0; i < order; i++) { for (int j = 0; j < order; j++) { Assert.AreEqual <double>(matrix[i, j], d.S[i, j]); } } }
public void ValidateMode(double nu, int order) { var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order)); var mode = d.Mode; for (int i = 0; i < d.S.RowCount; i++) { for (int j = 0; j < d.S.ColumnCount; j++) { Assert.AreEqual((nu - d.S.RowCount - 1.0) * d.S[i, j], mode[i, j]); } } }
public void ValidateVariance(double nu, int order) { var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order)); var variance = d.Variance; for (var i = 0; i < d.S.RowCount; i++) { for (var j = 0; j < d.S.ColumnCount; j++) { Assert.AreEqual(nu * (d.S[i, j] * d.S[i, j] + d.S[i, i] * d.S[j, j]), variance[i, j]); } } }
public void ValidateMean([Values(0.1, 1.0, 5.0)] double nu, [Values(2, 5)] int order) { var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order)); var mean = d.Mean; for (var i = 0; i < d.S.RowCount; i++) { for (var j = 0; j < d.S.ColumnCount; j++) { Assert.AreEqual(nu * d.S[i, j], mean[i, j]); } } }
public void ValidateMode(double nu, int order) { var d = new Wishart(nu, Matrix <double> .Build.RandomPositiveDefinite(order, 1)); var mode = d.Mode; for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual((nu - d.Scale.RowCount - 1.0) * d.Scale[i, j], mode[i, j]); } } }
public void ValidateVariance(double nu, int order) { var d = new Wishart(nu, Matrix <double> .Build.RandomPositiveDefinite(order, 1)); var variance = d.Variance; for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual(nu * ((d.Scale[i, j] * d.Scale[i, j]) + (d.Scale[i, i] * d.Scale[j, j])), variance[i, j]); } } }
public void ValidateMean(double nu, int order) { var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order)); var mean = d.Mean; for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual(nu * d.Scale[i, j], mean[i, j]); } } }
private static void ClusterizeAll(IReadOnlyList <float> series, int[] from, int[] to, ILogger logger) { foreach (var template in Wishart.GenerateTemplateForWishart(from, to)) { var vectors = ZVectorBuilder.Build(series, template, 0); var clusters = Clusterize(vectors); var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "clusters", $"{template[0]}-{template[1]}-{template[2]}-{template[3]}.json"); using (var writer = new StreamWriter(path)) { ServiceStack.Text.JsonSerializer.SerializeToWriter(clusters, writer); } } }
public void CanGetS() { const int Order = 2; var matrix = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(Order); var d = new Wishart(1.0, matrix); for (var i = 0; i < Order; i++) { for (var j = 0; j < Order; j++) { Assert.AreEqual(matrix[i, j], d.Scale[i, j]); } } }
private static void wishart_test08() //****************************************************************************80 // // Purpose: // // WISHART_TEST08 samples the unit Wishart and unit Wishart inverse matrices. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 11 October 2013 // // Author: // // John Burkardt // { Console.WriteLine(""); Console.WriteLine("WISHART_TEST08:"); Console.WriteLine(" Verify that, if using the same set of random numbers,"); Console.WriteLine(" inverse(W) = M,"); Console.WriteLine(" where"); Console.WriteLine(" W = Wishart.wishart_unit_sample ( n, df );"); Console.WriteLine(" M = Wishart.wishart_unit_sample_inverse ( n, df );"); // // Set the parameters. // int n = 5; int df = 8; double[] w = Wishart.wishart_unit_sample(n, df); double[] m = Wishart.wishart_unit_sample_inverse(n, df); // // Compute W * M. // double[] wm = typeMethods.r8mat_mm_new(n, n, n, w, m); // // Compare M * W to I. // double[] ident = typeMethods.r8mat_identity_new(n); double diff = typeMethods.r8mat_norm_fro_affine(n, n, wm, ident); Console.WriteLine(""); Console.WriteLine(" Frobenius norm of error is " + diff + ""); }
public void CanCreateWishart([Values(0.1, 1.0, 5.0)] double nu, [Values(2, 5)] int order) { var matrix = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order); var d = new Wishart(nu, matrix); Assert.AreEqual(nu, d.Nu); for (var i = 0; i < d.S.RowCount; i++) { for (var j = 0; j < d.S.ColumnCount; j++) { Assert.AreEqual(matrix[i, j], d.S[i, j]); } } }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="WishartFromShapeAndRateOp_Laplace2"]/message_doc[@name="LogAverageFactor(Wishart, double, Wishart, Wishart)"]/*'/> public static double LogAverageFactor(Wishart sample, double shape, Wishart rate, [Fresh] Wishart to_sample) { // int_R f(Y,R) p(R) dR = |Y|^(a-c) |Y+B_r|^-(a+a_r) Gamma_d(a+a_r)/Gamma_d(a)/Gamma_d(a_r) |B_r|^a_r int dim = sample.Dimension; double c = 0.5 * (dim + 1); double shape2 = shape + rate.Shape; Wishart samplePost = sample * to_sample; PositiveDefiniteMatrix y = samplePost.GetMean(); PositiveDefiniteMatrix yPlusBr = y + rate.Rate; double result = (shape - c) * y.LogDeterminant() - shape2 * yPlusBr.LogDeterminant() + sample.GetLogProb(y) - samplePost.GetLogProb(y); result += MMath.GammaLn(shape2, dim) - MMath.GammaLn(shape, dim) - MMath.GammaLn(rate.Shape, dim); result += rate.Shape * rate.Rate.LogDeterminant(); return(result); }
public void ValidateDensity(double nu, double density) { const int Order = 1; var matrix = new DenseMatrix(Order); matrix[0, 0] = 1; var x = new DenseMatrix(Order); x[0, 0] = 5; var d = new Wishart(nu, matrix); AssertHelpers.AlmostEqualRelative(density, d.Density(x), 16); }
public void CanCreateWishart(double nu, int order) { var matrix = Matrix <double> .Build.RandomPositiveDefinite(order, 1); var d = new Wishart(nu, matrix); Assert.AreEqual(nu, d.DegreesOfFreedom); for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual(matrix[i, j], d.Scale[i, j]); } } }
public void CanGetS() { const int Order = 2; var matrix = Matrix <double> .Build.RandomPositiveDefinite(Order, 1); var d = new Wishart(1.0, matrix); for (var i = 0; i < Order; i++) { for (var j = 0; j < Order; j++) { Assert.AreEqual(matrix[i, j], d.Scale[i, j]); } } }
public void CanCreateWishart(double nu, int order) { var matrix = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order); var d = new Wishart(nu, matrix); Assert.AreEqual(nu, d.DegreesOfFreedom); for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual(matrix[i, j], d.Scale[i, j]); } } }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ProductWishartOp"]/message_doc[@name="AAverageLogarithm(Wishart, Gamma, Wishart)"]/*'/> public static Wishart AAverageLogarithm([SkipIfUniform] Wishart Product, [Proper] Gamma B, Wishart result) { if (B.IsPointMass) { return(AAverageLogarithm(Product, B.Point, result)); } if (Product.IsPointMass) { return(AAverageLogarithm(Product.Point, B, result)); } // (ab)^(shape-1) exp(-rate*(ab)) result.Shape = Product.Shape; result.Rate.SetToProduct(Product.Rate, B.GetMean()); return(result); }
public void ValidateDensity(double nu, double density) { int order = 1; var matrix = new DenseMatrix(order); matrix[0, 0] = 1; var X = new DenseMatrix(order); X[0, 0] = 5; var d = new Wishart(nu, matrix); AssertHelpers.AlmostEqual(density, d.Density(X), 16); }
public void CanCreateWishart(double nu, int order) { var matrix = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order); var d = new Wishart(nu, matrix); Assert.AreEqual <double>(nu, d.Nu); for (int i = 0; i < d.S.RowCount; i++) { for (int j = 0; j < d.S.ColumnCount; j++) { Assert.AreEqual(matrix[i, j], d.S[i, j]); } } }
public static VectorGaussianWishart Combine(VectorGaussian position, Wishart orientation, VectorGaussianWishart result) { if (orientation.IsUniform()) { result.SetToUniform(); } else if (position.IsUniform()) { result.SetTo(orientation.Shape, orientation.Rate, Vector.Zero(2), 0); } else { PositiveDefiniteMatrix rateTimesPrecision = new PositiveDefiniteMatrix(2, 2); rateTimesPrecision.SetToProduct(orientation.Rate, position.Precision); double trace = MathHelpers.Invert(rateTimesPrecision).Trace(); Vector positionMean = position.MeanTimesPrecision * MathHelpers.Invert(position.Precision); result.SetTo(orientation.Shape, orientation.Rate, positionMean, orientation.Dimension / (orientation.Shape * trace)); } return result; }
public static double LogEvidenceRatio(PositiveDefiniteMatrix product, Wishart a, double b) { throw new NotImplementedException(); }
public static Wishart AAverageConditional([SkipIfUniform] Wishart Product, double B, Wishart result) { result.SetTo(Product); result.Rate.Scale(B); return result; }
public static Wishart ProductAverageConditional(Wishart Product, Wishart A, Gamma B, Wishart result) { }
/// <summary> /// VMP message to 'precision' /// </summary> /// <param name="Sample">Incoming message from 'sample'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param> /// <param name="SampleMean">Buffer 'SampleMean'.</param> /// <param name="SampleVariance">Buffer 'SampleVariance'.</param> /// <param name="Mean">Incoming message from 'mean'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param> /// <param name="MeanMean">Buffer 'MeanMean'.</param> /// <param name="MeanVariance">Buffer 'MeanVariance'.</param> /// <param name="result">Modified to contain the outgoing message</param> /// <returns><paramref name="result"/></returns> /// <remarks><para> /// The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except 'precision'. /// The formula is <c>exp(sum_(sample,mean) p(sample,mean) log(factor(sample,mean,precision)))</c>. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="Sample"/> is not a proper distribution</exception> /// <exception cref="ImproperMessageException"><paramref name="Mean"/> is not a proper distribution</exception> public static Wishart PrecisionAverageLogarithm( [Proper] VectorGaussian Sample, [Fresh] Vector SampleMean, [Fresh] PositiveDefiniteMatrix SampleVariance, [Proper] VectorGaussian Mean, [Fresh] Vector MeanMean, [Fresh] PositiveDefiniteMatrix MeanVariance, Wishart result) { if (Sample.IsPointMass) return PrecisionAverageLogarithm(Sample.Point, Mean, MeanMean, MeanVariance, result); if (Mean.IsPointMass) return PrecisionAverageLogarithm(Sample, SampleMean, SampleVariance, Mean.Point, result); // The formula is exp(int_x int_mean p(x) p(mean) log N(x;mean,1/prec)) = // exp(-0.5 prec E[(x-mean)^2] + 0.5 log(prec)) = // Gamma(prec; 0.5, 0.5*E[(x-mean)^2]) // E[(x-mean)^2] = E[x^2] - 2 E[x] E[mean] + E[mean^2] = var(x) + (E[x]-E[mean])^2 + var(mean) if (result == default(Wishart)) result = new Wishart(Sample.Dimension); result.Shape = 0.5 * (result.Dimension + 2); Vector diff = SampleMean - MeanMean; result.Rate.SetToOuter(diff, diff); result.Rate.SetToSum(result.Rate, SampleVariance); result.Rate.SetToSum(result.Rate, MeanVariance); result.Rate.Scale(0.5); return result; }
public static double AverageLogFactor(Wishart product) { return 0.0; }
/// <summary> /// VMP message to 'precision' /// </summary> /// <param name="Sample">Constant value for 'sample'.</param> /// <param name="Mean">Constant value for 'mean'.</param> /// <param name="result">Modified to contain the outgoing message</param> /// <returns><paramref name="result"/></returns> /// <remarks><para> /// The outgoing message is the factor viewed as a function of 'precision' conditioned on the given values. /// </para></remarks> public static Wishart PrecisionAverageLogarithm(Vector Sample, Vector Mean, Wishart result) { return PrecisionConditional(Sample, Mean, result); }
public void FailSetRandomSourceWithNullReference() { var d = new Wishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)); Assert.Throws<ArgumentNullException>(() => d.RandomSource = null); }
public static double LogEvidenceRatio(Wishart product, Wishart a, double b) { return 0.0; }
public void ValidateMode(double nu, int order) { var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order)); var mode = d.Mode; for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual((nu - d.Scale.RowCount - 1.0) * d.Scale[i, j], mode[i, j]); } } }
public void ValidateVariance(double nu, int order) { var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order)); var variance = d.Variance; for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual(nu * ((d.Scale[i, j] * d.Scale[i, j]) + (d.Scale[i, i] * d.Scale[j, j])), variance[i, j]); } } }
public void CanGetNu(double nu) { var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)); Assert.AreEqual(nu, d.DegreesOfFreedom); }
public void ValidateToString() { var d = new Wishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)); Assert.AreEqual("Wishart(DegreesOfFreedom = 1, Rows = 2, Columns = 2)", d.ToString()); }
/// <summary> /// VMP message to 'a' /// </summary> /// <param name="Product">Constant value for 'product'.</param> /// <param name="B">Constant value for 'b'.</param> /// <returns>The outgoing VMP message to the 'a' argument</returns> /// <remarks><para> /// The outgoing message is the factor viewed as a function of 'a' conditioned on the given values. /// </para></remarks> public static Wishart AAverageLogarithm(PositiveDefiniteMatrix Product, double B, Wishart result) { result.Point = Product; result.Point.Scale(1/B); return result; }
/// <summary> /// Gibbs message to 'precision' /// </summary> /// <param name="Sample">Constant value for 'sample'.</param> /// <param name="Mean">Constant value for 'mean'.</param> /// <param name="result">Modified to contain the outgoing message</param> /// <returns><paramref name="result"/></returns> /// <remarks><para> /// The outgoing message is the factor viewed as a function of 'precision' conditioned on the given values. /// </para></remarks> public static Wishart PrecisionConditional(Vector Sample, Vector Mean, Wishart result) { Vector workspace = Vector.Zero(Sample.Count); return PrecisionConditional(Sample, Mean, result, workspace); }
/// <summary> /// VMP message to 'a' /// </summary> /// <param name="Product">Incoming message from 'product'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <param name="B">Constant value for 'b'.</param> /// <returns>The outgoing VMP message to the 'a' argument</returns> /// <remarks><para> /// The outgoing message is the factor viewed as a function of 'a' with 'product' integrated out. /// The formula is <c>sum_product p(product) factor(product,a,b)</c>. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="Product"/> is not a proper distribution</exception> public static Wishart AAverageLogarithm([SkipIfUniform] Wishart Product, double B, Wishart result) { if (Product.IsPointMass) return AAverageLogarithm(Product.Point, B, result); return AAverageConditional(Product, B, result); }
/// <summary> /// VMP message to 'precision' /// </summary> /// <param name="Sample">Incoming message from 'sample'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param> /// <param name="SampleMean">Buffer 'SampleMean'.</param> /// <param name="SampleVariance">Buffer 'SampleVariance'.</param> /// <param name="Mean">Constant value for 'mean'.</param> /// <param name="result">Modified to contain the outgoing message</param> /// <returns><paramref name="result"/></returns> /// <remarks><para> /// The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except 'precision'. /// The formula is <c>exp(sum_(sample) p(sample) log(factor(sample,mean,precision)))</c>. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="Sample"/> is not a proper distribution</exception> public static Wishart PrecisionAverageLogarithm( [Proper] VectorGaussian Sample, [Fresh] Vector SampleMean, [Fresh] PositiveDefiniteMatrix SampleVariance, Vector Mean, Wishart result) { return PrecisionAverageLogarithm(Mean, Sample, SampleMean, SampleVariance, result); }
public static Wishart AAverageLogarithm(PositiveDefiniteMatrix Product, [Proper] Gamma B, Wishart result) { throw new NotSupportedException(GaussianProductVmpOp.NotSupportedMessage); }
public void HasRandomSource() { var d = new Wishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)); Assert.IsNotNull(d.RandomSource); }
public void CanSample() { var d = new Wishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)); d.Sample(); }
/// <summary> /// VMP message to 'product' /// </summary> /// <param name="B">Constant value for 'a'.</param> /// <param name="A">Incoming message from 'b'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <returns>The outgoing VMP message to the 'product' argument</returns> /// <remarks><para> /// The outgoing message is a distribution matching the moments of 'product' as the random arguments are varied. /// The formula is <c>proj[sum_(b) p(b) factor(product,a,b)]</c>. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="A"/> is not a proper distribution</exception> public static Wishart ProductAverageLogarithm([SkipIfUniform] Wishart A, double B, Wishart result) { return ProductAverageConditional(A, B, result); }
public static Wishart AAverageLogarithm([SkipIfUniform] Wishart Product, [Proper] Gamma B, Wishart result) { if (B.IsPointMass) return AAverageLogarithm(Product, B.Point, result); if (Product.IsPointMass) return AAverageLogarithm(Product.Point, B, result); // (ab)^(shape-1) exp(-rate*(ab)) result.Shape = Product.Shape; result.Rate.SetToProduct(Product.Rate, B.GetMean()); return result; }
/// <summary> /// VMP message to 'product' /// </summary> /// <param name="B">Incoming message from 'a'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <param name="A">Incoming message from 'b'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <returns>The outgoing VMP message to the 'product' argument</returns> /// <remarks><para> /// The outgoing message is a distribution matching the moments of 'product' as the random arguments are varied. /// The formula is <c>proj[sum_(a,b) p(a,b) factor(product,a,b)]</c>. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="B"/> is not a proper distribution</exception> /// <exception cref="ImproperMessageException"><paramref name="A"/> is not a proper distribution</exception> public static Wishart ProductAverageLogarithm([SkipIfUniform] Wishart A, [SkipIfUniform] Gamma B, Wishart result) { if (B.IsPointMass) return ProductAverageLogarithm(A, B.Point, result); // E[x] = E[a]*E[b] // E[log(x)] = E[log(a)]+E[log(b)] PositiveDefiniteMatrix m = new PositiveDefiniteMatrix(A.Dimension, A.Dimension); A.GetMean(m); m.Scale(B.GetMean()); double meanLogDet = A.Dimension*B.GetMeanLog() + A.GetMeanLogDeterminant(); if (m.LogDeterminant() < meanLogDet) throw new MatrixSingularException(m); return Wishart.FromMeanAndMeanLogDeterminant(m, meanLogDet, result); }
/// <summary> /// Gibbs message to 'precision' /// </summary> /// <param name="Sample">Constant value for 'sample'.</param> /// <param name="Mean">Constant value for 'mean'.</param> /// <param name="result">Modified to contain the outgoing message</param> /// <returns><paramref name="result"/></returns> /// <remarks><para> /// The outgoing message is the factor viewed as a function of 'precision' conditioned on the given values. /// </para></remarks> public static Wishart PrecisionConditional(Vector Sample, Vector Mean, Wishart result, Vector diff) { if (result == default(Wishart)) result = new Wishart(Sample.Count); diff.SetToDifference(Sample, Mean); const double SQRT_HALF = 0.70710678118654752440084436210485; diff.Scale(SQRT_HALF); result.Rate.SetToOuter(diff, diff); result.Shape = 0.5 * (result.Dimension + 2); return result; }
/// <summary> /// Evidence message for EP /// </summary> /// <param name="sample">Incoming message from 'sample'.</param> /// <param name="to_sample">Outgoing message to 'sample'.</param> /// <returns>Logarithm of the factor's average value across the given argument distributions</returns> /// <remarks><para> /// The formula for the result is <c>log(sum_(sample) p(sample) factor(sample,shape,scale))</c>. /// </para></remarks> public static double LogAverageFactor(Wishart sample, [Fresh] Wishart to_sample) { return to_sample.GetLogAverageOf(sample); }