/// <summary>EP message to <c>list</c>.</summary> /// <param name="Buffer">Buffer <c>Buffer</c>.</param> /// <param name="to_list">Previous outgoing message to <c>list</c>.</param> /// <param name="IndexOfMaximumDouble">Constant value for <c>indexOfMaximumDouble</c>.</param> /// <returns>The outgoing EP message to the <c>list</c> argument.</returns> /// <remarks> /// <para>The outgoing message is the factor viewed as a function of <c>list</c> conditioned on the given values.</para> /// </remarks> /// <typeparam name="GaussianList">The type of an incoming message from <c>list</c>.</typeparam> public static GaussianList listAverageConditional <GaussianList>( IndexOfMaximumBuffer Buffer, GaussianList to_list, int IndexOfMaximumDouble) where GaussianList : IList <Gaussian> { to_list.SetTo(Buffer.to_list); return(to_list); }
/// <summary>Update the buffer <c>Buffer</c>.</summary> /// <param name="Buffer">Buffer <c>Buffer</c>.</param> /// <param name="list">Incoming message from <c>list</c>.</param> /// <param name="IndexOfMaximumDouble">Constant value for <c>indexOfMaximumDouble</c>.</param> /// <returns>New value of buffer <c>Buffer</c>.</returns> /// <remarks> /// <para /> /// </remarks> /// <typeparam name="GaussianList">The type of an incoming message from <c>list</c>.</typeparam> public static IndexOfMaximumBuffer Buffer <GaussianList>( IndexOfMaximumBuffer Buffer, GaussianList list, int IndexOfMaximumDouble) // redundant parameters required for correct dependency graph where GaussianList : IList <Gaussian> { var max_marginal = Buffer.to_list[IndexOfMaximumDouble] * list[IndexOfMaximumDouble]; Gaussian product = Gaussian.Uniform(); //var order = Rand.Perm(list.Count); for (int i = 0; i < list.Count; i++) { //int c = order[i]; int c = i; if (c != IndexOfMaximumDouble) { var msg_to_sum = max_marginal / Buffer.MessagesToMax[c]; var msg_to_positiveop = DoublePlusOp.AAverageConditional(Sum: msg_to_sum, b: list[c]); var msgFromPositiveOp = IsPositiveOp.XAverageConditional(true, msg_to_positiveop); Buffer.MessagesToMax[c] = DoublePlusOp.SumAverageConditional(list[c], msgFromPositiveOp); Buffer.to_list[c] = DoublePlusOp.AAverageConditional(Sum: msg_to_sum, b: msgFromPositiveOp); max_marginal = msg_to_sum * Buffer.MessagesToMax[c]; product.SetToProduct(product, Buffer.MessagesToMax[c]); } } //Buffer.to_list[IndexOfMaximumDouble] = max_marginal / list[IndexOfMaximumDouble]; Buffer.to_list[IndexOfMaximumDouble] = product; return(Buffer); }
/// <summary>Evidence message for EP.</summary> /// <param name="Buffer">Buffer <c>Buffer</c>.</param> /// <param name="list">Incoming message from <c>list</c>.</param> /// <param name="IndexOfMaximumDouble">Constant value for <c>indexOfMaximumDouble</c>.</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_(list) p(list) factor(indexOfMaximumDouble,list))</c>.</para> /// </remarks> /// <typeparam name="GaussianList">The type of an incoming message from <c>list</c>.</typeparam> public static double LogAverageFactor <GaussianList>(IndexOfMaximumBuffer Buffer, GaussianList list, int IndexOfMaximumDouble) where GaussianList : IList <Gaussian> { double evidence = 0; var max_marginal = list[IndexOfMaximumDouble] * Buffer.to_list[IndexOfMaximumDouble]; for (int c = 0; c < list.Count; c++) { if (c != IndexOfMaximumDouble) { var msg_to_sum = max_marginal / Buffer.MessagesToMax[c]; var msg_to_positiveop = DoublePlusOp.AAverageConditional(Sum: msg_to_sum, b: list[c]); evidence += IsPositiveOp.LogEvidenceRatio(true, msg_to_positiveop); // sum operator does not contribute because no projection is involved // the x[index]-x[c] variable does not contribute because it only connects to two factors evidence -= msg_to_sum.GetLogAverageOf(Buffer.MessagesToMax[c]); if (max_marginal.IsPointMass) { evidence += Buffer.MessagesToMax[c].GetLogAverageOf(max_marginal); } else { evidence -= Buffer.MessagesToMax[c].GetLogNormalizer(); } } } //evidence += ReplicateOp.LogEvidenceRatio<Gaussian>(MessagesToMax, list[IndexOfMaximumDouble], MessagesToMax.Select(o => max_marginal / o).ToArray()); if (!max_marginal.IsPointMass) { evidence += max_marginal.GetLogNormalizer() - list[IndexOfMaximumDouble].GetLogNormalizer(); } //evidence -= Buffer.MessagesToMax.Sum(o => o.GetLogNormalizer()); //evidence -= Buffer.MessagesToMax.Sum(o => (max_marginal / o).GetLogAverageOf(o)); return(evidence); }
public static double LogAverageFactor <GaussianList>([SkipIfUniform] GaussianList list, GaussianList to_list, IndexOfMaximumBuffer[] Buffers, Discrete IndexOfMaximumDouble) where GaussianList : DistributionStructArray <Gaussian, double> { var evidences = new double[list.Count]; var tempBuffer = new IndexOfMaximumBuffer(); for (int i = 0; i < list.Count; i++) { tempBuffer.to_list = new DistributionStructArray <Gaussian, double>(list.Select(o => Gaussian.Uniform()).ToArray()); tempBuffer.to_list[i] = Buffers[i].MessagesToMax.Aggregate((p, q) => p * q); tempBuffer.MessagesToMax = new DistributionStructArray <Gaussian, double>(Buffers[i].MessagesToMax.Select(o => (Gaussian)o.Clone()).ToArray()); evidences[i] = IndexOfMaximumOp.LogAverageFactor(tempBuffer, list, i) + IndexOfMaximumDouble.GetLogProb(i);; } return(MMath.LogSumExp(evidences)); }
// redundant parameters required for correct dependency graph public static IndexOfMaximumBuffer Buffer <GaussianList>(IndexOfMaximumBuffer Buffer, GaussianList list, int IndexOfMaximumDouble) where GaussianList : IList <Gaussian> { var max_marginal = Buffer.to_list[IndexOfMaximumDouble] * list[IndexOfMaximumDouble]; //var order = Rand.Perm(list.Count); for (int i = 0; i < list.Count; i++) { //int c = order[i]; int c = i; if (c != IndexOfMaximumDouble) { var msg_to_sum = max_marginal / Buffer.MessagesToMax[c]; var msg_to_positiveop = DoublePlusOp.AAverageConditional(Sum: msg_to_sum, b: list[c]); var msgFromPositiveOp = IsPositiveOp.XAverageConditional(true, msg_to_positiveop); Buffer.MessagesToMax[c] = DoublePlusOp.SumAverageConditional(list[c], msgFromPositiveOp); Buffer.to_list[c] = DoublePlusOp.AAverageConditional(Sum: msg_to_sum, b: msgFromPositiveOp); max_marginal = msg_to_sum * Buffer.MessagesToMax[c]; } } Buffer.to_list[IndexOfMaximumDouble] = max_marginal / list[IndexOfMaximumDouble]; return(Buffer); }
/// <summary>Evidence message for EP.</summary> /// <param name="Buffer">Buffer <c>Buffer</c>.</param> /// <param name="list">Incoming message from <c>list</c>.</param> /// <param name="IndexOfMaximumDouble">Constant value for <c>indexOfMaximumDouble</c>.</param> /// <returns>Logarithm of the factor's contribution the EP model evidence.</returns> /// <remarks> /// <para>The formula for the result is <c>log(sum_(list) p(list) factor(indexOfMaximumDouble,list))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for EP.</para> /// </remarks> /// <typeparam name="GaussianList">The type of an incoming message from <c>list</c>.</typeparam> public static double LogEvidenceRatio <GaussianList>(IndexOfMaximumBuffer Buffer, GaussianList list, int IndexOfMaximumDouble) where GaussianList : IList <Gaussian> { return(LogAverageFactor(Buffer, list, IndexOfMaximumDouble)); }