Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }
        // 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);
        }
Example #4
0
        /// <summary>
        /// Returns the probability that A>B
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        private static Bernoulli ProbGreater(Gaussian A, Gaussian B)
        {
            Gaussian diff = DoublePlusOp.AAverageConditional(Sum: A, b: B);

            return(IsPositiveOp.IsPositiveAverageConditional(diff));
        }
Example #5
0
 /// <summary>EP message to <c>x</c>.</summary>
 /// <param name="isPositive">Incoming message from <c>isPositive</c>. Must be a proper distribution. If uniform, the result will be uniform.</param>
 /// <param name="x">Incoming message from <c>x</c>. Must be a proper distribution. If uniform, the result will be uniform.</param>
 /// <returns>The outgoing EP message to the <c>x</c> argument.</returns>
 /// <remarks>
 ///   <para>The outgoing message is a distribution matching the moments of <c>x</c> as the random arguments are varied. The formula is <c>proj[p(x) sum_(isPositive) p(isPositive) factor(isPositive,x)]/p(x)</c>.</para>
 /// </remarks>
 /// <exception cref="ImproperMessageException">
 ///   <paramref name="isPositive" /> is not a proper distribution.</exception>
 /// <exception cref="ImproperMessageException">
 ///   <paramref name="x" /> is not a proper distribution.</exception>
 public static Gaussian XAverageConditional([SkipIfUniform] Bernoulli isPositive, [SkipIfUniform, Proper] Gaussian x)
 {
     return(IsPositiveOp.XAverageConditional_Helper(isPositive, x, forceProper: true));
 }