示例#1
0
文件: SparseGP.cs 项目: 0xCM/arrows
 /// <summary>
 /// Sets this instance to the ratio of two sparse GPs.
 /// </summary>
 /// <param name="numerator">Sparse GP</param>
 /// <param name="denominator">Sparse GP</param>
 /// <param name="forceProper"></param>
 public void SetToRatio(SparseGP numerator, SparseGP denominator, bool forceProper = false)
 {
     if (numerator.FixedParameters != denominator.FixedParameters)
     {
         throw new ArgumentException("SparseGPs do not have the same FixedParameters.  numerator.FixedParameters = " + numerator.FixedParameters +
                                     ", denominator.FixedParameters = " + denominator.FixedParameters);
     }
     FixedParameters = numerator.FixedParameters;
     if (numerator.IncludePrior)
     {
         IncludePrior = !denominator.IncludePrior;
     }
     else if (denominator.IncludePrior)
     {
         throw new ArgumentException("Only the denominator includes the prior.  Cannot divide.");
     }
     else
     {
         IncludePrior = false;  // neither include the prior
     }
     if (numerator.IsPointMass)
     {
         if (denominator.IsPointMass)
         {
             if (numerator.Point.Equals(denominator.Point))
             {
                 SetToUniform();
             }
             else
             {
                 throw new DivideByZeroException();
             }
         }
         else
         {
             Point = numerator.Point;
         }
     }
     else if (denominator.IsPointMass)
     {
         throw new DivideByZeroException();
     }
     else
     {
         // neither is point mass
         InducingDist.SetToRatio(numerator.InducingDist, denominator.InducingDist, forceProper);
         pointFunc = null;
         ClearCachedValues();
     }
 }
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SumVectorGaussianOp"]/message_doc[@name="ArrayAverageLogarithm1{TVectorGaussianList}(VectorGaussian, IList{VectorGaussian}, TVectorGaussianList)"]/*'/>
        /// <typeparam name="TVectorGaussianList">A list of <see cref="VectorGaussian"/> distributions.</typeparam>
        public static TVectorGaussianList ArrayAverageLogarithm1 <TVectorGaussianList>(
            [SkipIfUniform] VectorGaussian sum,
            [Stochastic, Proper] IList <VectorGaussian> array,
            TVectorGaussianList to_array)
            where TVectorGaussianList : IList <VectorGaussian>
        {
            // Check inputs for consistency
            int dimension = CheckArgumentConsistency(sum, sum, array, to_array);

            TVectorGaussianList result = to_array;

            var sumMean     = Vector.Zero(dimension);
            var sumVariance = PositiveDefiniteMatrix.Identity(dimension);

            sum.GetMeanAndVariance(sumMean, sumVariance);

            // This version does one update of q(array[i]) for each array element in turn.
            Vector arraySumOfMean = Vector.Zero(dimension);

            foreach (VectorGaussian element in array)
            {
                arraySumOfMean.SetToSum(arraySumOfMean, element.GetMean());
            }

            for (int i = 0; i < result.Count; i++)
            {
                arraySumOfMean.SetToDifference(arraySumOfMean, array[i].GetMean());

                VectorGaussian oldResult = result[i];
                result[i] = new VectorGaussian(sumMean - arraySumOfMean, sumVariance);

                oldResult.SetToRatio(result[i], oldResult);
                oldResult.SetToProduct(array[i], oldResult);

                arraySumOfMean.SetToSum(arraySumOfMean, oldResult.GetMean());
            }

            return(result);
        }