Exemplo n.º 1
0
 /// <summary>Update the buffer <c>Buffers</c>.</summary>
 /// <param name="Buffers">Buffer <c>Buffers</c>.</param>
 /// <param name="list">Incoming message from <c>list</c>. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
 /// <param name="IndexOfMaximumDouble">Incoming message from <c>indexOfMaximumDouble</c>.</param>
 /// <returns>New value of buffer <c>Buffers</c>.</returns>
 /// <remarks>
 ///   <para />
 /// </remarks>
 /// <exception cref="ImproperMessageException">
 ///   <paramref name="list" /> is not a proper distribution.</exception>
 /// <typeparam name="GaussianList">The type of an incoming message from <c>list</c>.</typeparam>
 public static IndexOfMaximumBuffer[] Buffers <GaussianList>(IndexOfMaximumBuffer[] Buffers, [SkipIfUniform] GaussianList list, Discrete IndexOfMaximumDouble)
     where GaussianList : IList <Gaussian>
 {
     for (int i = 0; i < list.Count; i++)
     {
         Buffers[i].to_list    = new DistributionStructArray <Gaussian, double>(list.Select(o => Gaussian.Uniform()).ToArray());
         Buffers[i].to_list[i] = Buffers[i].MessagesToMax.Aggregate((p, q) => p * q);
         Buffers[i]            = IndexOfMaximumOp.Buffer(Buffers[i], list, i);
     }
     return(Buffers);
 }
Exemplo n.º 2
0
        /// <summary>EP message to <c>indexOfMaximumDouble</c>.</summary>
        /// <param name="list">Incoming message from <c>list</c>.</param>
        /// <param name="Buffers">Buffer <c>Buffers</c>.</param>
        /// <returns>The outgoing EP message to the <c>indexOfMaximumDouble</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>indexOfMaximumDouble</c> as the random arguments are varied. The formula is <c>proj[p(indexOfMaximumDouble) sum_(list) p(list) factor(indexOfMaximumDouble,list)]/p(indexOfMaximumDouble)</c>.</para>
        /// </remarks>
        /// <typeparam name="GaussianList">The type of an incoming message from <c>list</c>.</typeparam>
        public static Discrete IndexOfMaximumDoubleAverageConditional <GaussianList>(GaussianList list, IndexOfMaximumBuffer[] Buffers)
            where GaussianList : DistributionStructArray <Gaussian, double>
        {
            // var results = list.Select(o => list.Select(p => Gaussian.Uniform()).ToList()).ToArray();
            // TODO: if IndexOfMaximumDouble is uniform we will never call this routine so buffers will not get set, so messages to IndexOfMaximumDouble will be incorrect
            var evidences = new double[list.Count];

            for (int i = 0; i < list.Count; i++)
            {
                //var res = new DistributionStructArray<Gaussian, double>(list.Select(o => Gaussian.Uniform()).ToArray());
                //res[i] = Buffer[i].MessagesToMax.Aggregate((p, q) => p * q);
                evidences[i] = IndexOfMaximumOp.LogAverageFactor(Buffers[i], list, i);
            }
            return(new Discrete(MMath.Softmax(evidences)));
        }
        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));
        }
Exemplo n.º 4
0
 /// <summary>Initialize the buffer <c>Buffers</c>.</summary>
 /// <param name="list">Incoming message from <c>list</c>.</param>
 /// <param name="IndexOfMaximumDouble">Incoming message from <c>indexOfMaximumDouble</c>.</param>
 /// <returns>Initial value of buffer <c>Buffers</c>.</returns>
 /// <remarks>
 ///   <para />
 /// </remarks>
 /// <typeparam name="GaussianList">The type of an incoming message from <c>list</c>.</typeparam>
 public static IndexOfMaximumBuffer[] BuffersInit <GaussianList>([IgnoreDependency] GaussianList list, [IgnoreDependency] Discrete IndexOfMaximumDouble)
     where GaussianList : IList <Gaussian>
 {
     return(list.Select(o => IndexOfMaximumOp.BufferInit(list)).ToArray());
 }