Ejemplo n.º 1
0
        /// <summary>Gibbs message to <c>choice</c>.</summary>
        /// <param name="sample">Constant value for <c>sample</c>.</param>
        /// <param name="probTrue0">Constant value for <c>probTrue0</c>.</param>
        /// <param name="probTrue1">Constant value for <c>probTrue1</c>.</param>
        /// <returns>The outgoing Gibbs message to the <c>choice</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is the factor viewed as a function of <c>choice</c> conditioned on the given values.</para>
        /// </remarks>
        public static Bernoulli ChoiceConditional(bool sample, double probTrue0, double probTrue1)
        {
            Bernoulli result = new Bernoulli();

            if (probTrue0 == 0 || probTrue1 == 0)
            {
                throw new ArgumentException("probTrue is zero");
            }
            if (sample)
            {
                double sum = probTrue0 + probTrue1;
                if (sum == 0.0)
                {
                    throw new AllZeroException();
                }
                else
                {
                    result.SetProbTrue(probTrue1 / sum);
                }
            }
            else
            {
                double sum = 2 - probTrue1 - probTrue0;
                if (sum == 0.0)
                {
                    throw new AllZeroException();
                }
                else
                {
                    result.SetProbTrue((1 - probTrue1) / sum);
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gibbs message to 'sample'.
        /// </summary>
        /// <param name="index">Constant value for 'index'.</param>
        /// <param name="ProbTrue">Constant value for 'probTrue'.</param>
        /// <returns>The outgoing Gibbs message to the 'sample' argument.</returns>
        /// <remarks><para>
        /// The outgoing message is the factor viewed as a function of 'sample' conditioned on the given values.
        /// </para></remarks>
        public static Bernoulli SampleConditional(int index, double[] ProbTrue)
        {
            Bernoulli result = new Bernoulli();

            result.SetProbTrue(ProbTrue[index]);
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>Gibbs message to <c>sample</c>.</summary>
        /// <param name="choice">Constant value for <c>choice</c>.</param>
        /// <param name="probTrue0">Constant value for <c>probTrue0</c>.</param>
        /// <param name="probTrue1">Constant value for <c>probTrue1</c>.</param>
        /// <returns>The outgoing Gibbs message to the <c>sample</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is the factor viewed as a function of <c>sample</c> conditioned on the given values.</para>
        /// </remarks>
        public static Bernoulli SampleConditional(bool choice, double probTrue0, double probTrue1)
        {
            Bernoulli result = new Bernoulli();

            result.SetProbTrue(choice ? probTrue1 : probTrue0);
            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>EP message to <c>sample</c>.</summary>
        /// <param name="choice">Incoming message from <c>choice</c>.</param>
        /// <param name="probTrue0">Constant value for <c>probTrue0</c>.</param>
        /// <param name="probTrue1">Constant value for <c>probTrue1</c>.</param>
        /// <returns>The outgoing EP message to the <c>sample</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>sample</c> as the random arguments are varied. The formula is <c>proj[p(sample) sum_(choice) p(choice) factor(sample,choice,probTrue0,probTrue1)]/p(sample)</c>.</para>
        /// </remarks>
        public static Bernoulli SampleAverageConditional(Bernoulli choice, double probTrue0, double probTrue1)
        {
            Bernoulli result = new Bernoulli();

            if (choice.IsPointMass)
            {
                return(SampleConditional(choice.Point, probTrue0, probTrue1));
            }
#if FAST
            result.SetProbTrue(choice.GetProbFalse() * probTrue0 + choice.GetProbTrue() * probTrue1);
#else
            // This method is more numerically stable but slower.
            // let oX = log(p(X)/(1-p(X))
            // let oY = log(p(Y)/(1-p(Y))
            // oX = log( (TT*sigma(oY) + TF*sigma(-oY))/(FT*sigma(oY) + FF*sigma(-oY)) )
            //    = log( (TT*exp(oY) + TF)/(FT*exp(oY) + FF) )
            //    = log( (exp(oY) + TF/TT)/(exp(oY) + FF/FT) ) + log(TT/FT)
            // ay = log(TF/TT)
            // by = log(FF/FT)
            // offset = log(TT/FT)
            if (probTrue0 == 0 || probTrue1 == 0)
            {
                throw new ArgumentException("probTrue is zero");
            }
            double ay     = Math.Log(probTrue0 / probTrue1);
            double by     = Math.Log((1 - probTrue0) / (1 - probTrue1));
            double offset = MMath.Logit(probTrue1);
            result.LogOdds = MMath.DiffLogSumExp(choice.LogOdds, ay, by) + offset;
#endif
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// EP message to 'sample'.
        /// </summary>
        /// <param name="index">Incoming message from 'index'.</param>
        /// <param name="ProbTrue">Constant value for 'probTrue'.</param>
        /// <returns>The outgoing EP message to the 'sample' argument.</returns>
        /// <remarks><para>
        /// The outgoing message is the integral of the factor times incoming messages, over all arguments except 'sample'.
        /// The formula is <c>int f(sample,x) q(x) dx</c> where <c>x = (index,probTrue)</c>.
        /// </para></remarks>
        public static Bernoulli SampleAverageConditional(Discrete index, double[] ProbTrue)
        {
            Bernoulli result = new Bernoulli();
            // E[X] = sum_Y p(Y) ProbTrue[Y]
            double p = 0;

            for (int i = 0; i < index.Dimension; i++)
            {
                p += ProbTrue[i] * index[i];
            }
            result.SetProbTrue(p);
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gibbs message to 'choice'.
        /// </summary>
        /// <param name="sample">Constant value for 'sample'.</param>
        /// <param name="probTrue0">Constant value for 'probTrue0'.</param>
        /// <param name="probTrue1">Constant value for 'probTrue1'.</param>
        /// <returns>The outgoing Gibbs message to the 'choice' argument.</returns>
        /// <remarks><para>
        /// The outgoing message is the factor viewed as a function of 'choice' conditioned on the given values.
        /// </para></remarks>
		public static Bernoulli ChoiceConditional(bool sample, double probTrue0, double probTrue1)
		{
            Bernoulli result = new Bernoulli();
			if (probTrue0 == 0 || probTrue1 == 0) throw new ArgumentException("probTrue is zero");
			if (sample) {
				double sum = probTrue0 + probTrue1;
				if(sum == 0.0) throw new AllZeroException();
				else result.SetProbTrue(probTrue1 / sum);
			} else {
				double sum = 2 - probTrue1 - probTrue0;
				if(sum == 0.0) throw new AllZeroException();
				else result.SetProbTrue((1 - probTrue1) / sum);
			}
			return result;
		}
Ejemplo n.º 7
0
        /// <summary>EP message to <c>choice</c>.</summary>
        /// <param name="sample">Incoming message from <c>sample</c>. Must be a proper distribution. If uniform, the result will be uniform.</param>
        /// <param name="probTrue0">Constant value for <c>probTrue0</c>.</param>
        /// <param name="probTrue1">Constant value for <c>probTrue1</c>.</param>
        /// <returns>The outgoing EP message to the <c>choice</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>choice</c> as the random arguments are varied. The formula is <c>proj[p(choice) sum_(sample) p(sample) factor(sample,choice,probTrue0,probTrue1)]/p(choice)</c>.</para>
        /// </remarks>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="sample" /> is not a proper distribution.</exception>
        public static Bernoulli ChoiceAverageConditional([SkipIfUniform] Bernoulli sample, double probTrue0, double probTrue1)
        {
            Bernoulli result = new Bernoulli();

            if (sample.IsPointMass)
            {
                return(ChoiceConditional(sample.Point, probTrue0, probTrue1));
            }
#if FAST
            double p1  = sample.GetProbFalse() * (1 - probTrue1) + sample.GetProbTrue() * probTrue1;
            double p0  = sample.GetProbFalse() * (1 - probTrue0) + sample.GetProbTrue() * probTrue0;
            double sum = p0 + p1;
            if (sum == 0.0)
            {
                throw new AllZeroException();
            }
            else
            {
                result.SetProbTrue(p1 / sum);
            }
#else
            // This method is more numerically stable but slower.
            // ax = log(FT/TT)
            // bx = log(FF/TF)
            // offset = log(TT/TF)
            if (probTrue0 == 0 || probTrue1 == 0)
            {
                throw new ArgumentException("probTrue is zero");
            }
            double ax     = -MMath.Logit(probTrue1);
            double bx     = -MMath.Logit(probTrue0);
            double offset = Math.Log(probTrue1 / probTrue0);
            result.LogOdds = MMath.DiffLogSumExp(sample.LogOdds, ax, bx) + offset;
#endif
            return(result);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gibbs message to 'sample'.
        /// </summary>
        /// <param name="choice">Constant value for 'choice'.</param>
        /// <param name="probTrue0">Constant value for 'probTrue0'.</param>
        /// <param name="probTrue1">Constant value for 'probTrue1'.</param>
        /// <returns>The outgoing Gibbs message to the 'sample' argument.</returns>
        /// <remarks><para>
        /// The outgoing message is the factor viewed as a function of 'sample' conditioned on the given values.
        /// </para></remarks>
        public static Bernoulli SampleConditional(bool choice, double probTrue0, double probTrue1)
		{
            Bernoulli result = new Bernoulli();
			result.SetProbTrue(choice ? probTrue1 : probTrue0);
			return result;
		}
Ejemplo n.º 9
0
        /// <summary>
        /// EP message to 'choice'.
        /// </summary>
        /// <param name="sample">Incoming message from 'sample'. Must be a proper distribution.  If uniform, the result will be uniform.</param>
        /// <param name="probTrue0">Constant value for 'probTrue0'.</param>
        /// <param name="probTrue1">Constant value for 'probTrue1'.</param>
        /// <returns>The outgoing EP message to the 'choice' argument.</returns>
        /// <remarks><para>
        /// The outgoing message is the integral of the factor times incoming messages, over all arguments except 'choice'.
        /// The formula is <c>int f(choice,x) q(x) dx</c> where <c>x = (sample,probTrue0,probTrue1)</c>.
        /// </para></remarks>
        /// <exception cref="ImproperMessageException"><paramref name="sample"/> is not a proper distribution</exception>
		public static Bernoulli ChoiceAverageConditional([SkipIfUniform] Bernoulli sample, double probTrue0, double probTrue1)
		{
            Bernoulli result = new Bernoulli();
			if(sample.IsPointMass) return ChoiceConditional(sample.Point,probTrue0,probTrue1);
#if FAST
			double p1 = sample.GetProbFalse() * (1 - probTrue1) + sample.GetProbTrue() * probTrue1;
			double p0 = sample.GetProbFalse() * (1 - probTrue0) + sample.GetProbTrue() * probTrue0;
			double sum = p0+p1;
			if(sum == 0.0) throw new AllZeroException();
			else result.SetProbTrue(p1 / sum);
#else
			// This method is more numerically stable but slower.
			// ax = log(FT/TT)
			// bx = log(FF/TF)
			// offset = log(TT/TF)
			if (probTrue0 == 0 || probTrue1 == 0) throw new ArgumentException("probTrue is zero");
			double ax = -MMath.Logit(probTrue1);
			double bx = -MMath.Logit(probTrue0);
			double offset = Math.Log(probTrue1 / probTrue0);
			result.LogOdds = MMath.DiffLogSumExp(sample.LogOdds, ax, bx) + offset;
#endif
			return result;
		}
Ejemplo n.º 10
0
        /// <summary>
        /// EP message to 'sample'.
        /// </summary>
        /// <param name="choice">Incoming message from 'choice'.</param>
        /// <param name="probTrue0">Constant value for 'probTrue0'.</param>
        /// <param name="probTrue1">Constant value for 'probTrue1'.</param>
        /// <returns>The outgoing EP message to the 'sample' argument.</returns>
        /// <remarks><para>
        /// The outgoing message is the integral of the factor times incoming messages, over all arguments except 'sample'.
        /// The formula is <c>int f(sample,x) q(x) dx</c> where <c>x = (choice,probTrue0,probTrue1)</c>.
        /// </para></remarks>
        public static Bernoulli SampleAverageConditional(Bernoulli choice, double probTrue0, double probTrue1)
		{
            Bernoulli result = new Bernoulli();
			if(choice.IsPointMass) return SampleConditional(choice.Point,probTrue0,probTrue1);
#if FAST
			result.SetProbTrue(choice.GetProbFalse() * probTrue0 + choice.GetProbTrue() * probTrue1);
#else
			// This method is more numerically stable but slower.
			// let oX = log(p(X)/(1-p(X))
			// let oY = log(p(Y)/(1-p(Y))
			// oX = log( (TT*sigma(oY) + TF*sigma(-oY))/(FT*sigma(oY) + FF*sigma(-oY)) )
			//    = log( (TT*exp(oY) + TF)/(FT*exp(oY) + FF) )
			//    = log( (exp(oY) + TF/TT)/(exp(oY) + FF/FT) ) + log(TT/FT)
			// ay = log(TF/TT)
			// by = log(FF/FT)
			// offset = log(TT/FT)
			if (probTrue0 == 0 || probTrue1 == 0) throw new ArgumentException("probTrue is zero");
			double ay = Math.Log(probTrue0 / probTrue1);
			double by = Math.Log((1 - probTrue0) / (1 - probTrue1));
			double offset = MMath.Logit(probTrue1);
			result.LogOdds = MMath.DiffLogSumExp(choice.LogOdds, ay, by) + offset;
#endif
			return result;
		}
Ejemplo n.º 11
0
		/// <summary>
		/// Gibbs message to 'sample'.
		/// </summary>
		/// <param name="index">Constant value for 'index'.</param>
		/// <param name="ProbTrue">Constant value for 'probTrue'.</param>
		/// <returns>The outgoing Gibbs message to the 'sample' argument.</returns>
		/// <remarks><para>
		/// The outgoing message is the factor viewed as a function of 'sample' conditioned on the given values.
		/// </para></remarks>
		public static Bernoulli SampleConditional(int index, double[] ProbTrue)
		{
			Bernoulli result = new Bernoulli();
			result.SetProbTrue(ProbTrue[index]);
			return result;
		}
Ejemplo n.º 12
0
		/// <summary>
		/// EP message to 'sample'.
		/// </summary>
		/// <param name="index">Incoming message from 'index'.</param>
		/// <param name="ProbTrue">Constant value for 'probTrue'.</param>
		/// <returns>The outgoing EP message to the 'sample' argument.</returns>
		/// <remarks><para>
		/// The outgoing message is the integral of the factor times incoming messages, over all arguments except 'sample'.
		/// The formula is <c>int f(sample,x) q(x) dx</c> where <c>x = (index,probTrue)</c>.
		/// </para></remarks>
		public static Bernoulli SampleAverageConditional(Discrete index, double[] ProbTrue)
		{
			Bernoulli result = new Bernoulli();
			// E[X] = sum_Y p(Y) ProbTrue[Y]
			double p = 0;
			for (int i = 0; i < index.Dimension; i++)
			{
				p += ProbTrue[i] * index[i];
			}
			result.SetProbTrue(p);
			return result;
		}