Example #1
0
        /// <summary>EP message to <c>characters</c>.</summary>
        /// <param name="str">Incoming message from <c>str</c>.</param>
        /// <param name="characters">Incoming message from <c>characters</c>.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>characters</c> as the random arguments are varied. The formula is <c>proj[p(characters) sum_(str) p(str) factor(str,characters)]/p(characters)</c>.</para>
        /// </remarks>
        /// <typeparam name="TDiscreteCharList">The type of an outgoing message to <c>chars</c>.</typeparam>
        public static TDiscreteCharList CharactersAverageConditional <TDiscreteCharList>(
            StringDistribution str, IList <DiscreteChar> characters, TDiscreteCharList result)
            where TDiscreteCharList : IList <DiscreteChar>
        {
            for (int i = 0; i < characters.Count; ++i)
            {
                // TODO: perhaps there is a faster way to extract the distribution of interest
                var reweightedStr        = str.Product(GetCharWeighter(characters, i));
                var outgoingMessageAsStr = SubstringOp.SubAverageConditional(reweightedStr, i, 1);
                if (outgoingMessageAsStr.IsZero())
                {
                    throw new AllZeroException("Impossible model detected in StringFromCharsOp.");
                }

                result[i] = SingleOp.CharacterAverageConditional(outgoingMessageAsStr);
            }

            return(result);
        }
Example #2
0
        /// <summary>EP message to <c>ch</c>.</summary>
        /// <param name="concat">Incoming message from <c>concat</c>.</param>
        /// <param name="str">Incoming message from <c>str</c>.</param>
        /// <returns>The outgoing EP message to the <c>ch</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>ch</c> as the random arguments are varied. The formula is <c>proj[p(ch) sum_(concat,str) p(concat,str) factor(concat,ch,str)]/p(ch)</c>.</para>
        /// </remarks>
        public static DiscreteChar ChAverageConditional(StringDistribution concat, StringDistribution str)
        {
            var result = StringConcatOp.Str1AverageConditional(concat, str);

            return(SingleOp.CharacterAverageConditional(result));
        }