當前位置: 首頁>>代碼示例>>C#>>正文


C# Beta.GetLogNormalizer方法代碼示例

本文整理匯總了C#中Beta.GetLogNormalizer方法的典型用法代碼示例。如果您正苦於以下問題:C# Beta.GetLogNormalizer方法的具體用法?C# Beta.GetLogNormalizer怎麽用?C# Beta.GetLogNormalizer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Beta的用法示例。


在下文中一共展示了Beta.GetLogNormalizer方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: AverageLogFactor

		/// <summary>
		/// Evidence message for VMP.
		/// </summary>
		/// <param name="sample">Incoming message from sample</param>
		/// <param name="logOdds">Incoming message from logOdds</param>
		/// <returns><c>sum_x marginal(x)*log(factor(x))</c></returns>
		/// <remarks><para>
		/// The formula for the result is <c>int log(f(x)) q(x) dx</c>
		/// where <c>x = (sample,logOdds)</c>.
		/// </para></remarks>
		public static double AverageLogFactor(Beta logistic, [Proper, SkipIfUniform] Gaussian x, Beta to_logistic, double a)
		{
			double b = logistic.FalseCount;
			double scale = logistic.TrueCount + b - 2;
			double shift = -(b - 1);
			double m, v;
			x.GetMeanAndVariance(out m, out v);
			double boundOnLog1PlusExp = a*a*v/2.0 + MMath.Log1PlusExp(m+(1.0-2.0*a)*v/2.0);
			double boundOnLogSigma = m - boundOnLog1PlusExp;
			return scale * boundOnLogSigma + shift * m - logistic.GetLogNormalizer() - to_logistic.GetAverageLog(logistic);
		}
開發者ID:xornand,項目名稱:Infer.Net,代碼行數:21,代碼來源:Logistic.cs

示例2: LogisticAverageConditional

		/// <summary>
		/// EP message to 'logistic'
		/// </summary>
		/// <param name="logistic">Incoming message from 'logistic'.</param>
		/// <param name="x">Incoming message from 'x'. Must be a proper distribution.  If uniform, the result will be uniform.</param>
		/// <param name="falseMsg">Buffer 'falseMsg'.</param>
		/// <returns>The outgoing EP message to the 'logistic' argument</returns>
		/// <remarks><para>
		/// The outgoing message is a distribution matching the moments of 'logistic' as the random arguments are varied.
		/// The formula is <c>proj[p(logistic) sum_(x) p(x) factor(logistic,x)]/p(logistic)</c>.
		/// </para></remarks>
		/// <exception cref="ImproperMessageException"><paramref name="x"/> is not a proper distribution</exception>
		public static Beta LogisticAverageConditional(Beta logistic, [Proper] Gaussian x, Gaussian falseMsg)
		{
			if (x.IsPointMass)
				return Beta.PointMass(MMath.Logistic(x.Point));

			if (logistic.IsPointMass || x.IsUniform())
				return Beta.Uniform();

			double m,v;
			x.GetMeanAndVariance(out m, out v);
			if ((logistic.TrueCount == 2 && logistic.FalseCount == 1) ||
				  (logistic.TrueCount == 1 && logistic.FalseCount == 2) ||
				   logistic.IsUniform()) {
				// shortcut for the common case
				// result is a Beta distribution satisfying:
				// int_p to_p(p) p dp = int_x sigma(x) qnoti(x) dx
				// int_p to_p(p) p^2 dp = int_x sigma(x)^2 qnoti(x) dx
				// the second constraint can be rewritten as:
				// int_p to_p(p) p (1-p) dp = int_x sigma(x) (1 - sigma(x)) qnoti(x) dx
				// the constraints are the same if we replace p with (1-p)
				double mean = MMath.LogisticGaussian(m, v);
				// meanTF = E[p] - E[p^2]
				double meanTF = MMath.LogisticGaussianDerivative(m, v);
				double meanSquare = mean - meanTF;
				return Beta.FromMeanAndVariance(mean, meanSquare - mean*mean);
			} else {
				// stabilized EP message
				// choose a normalized distribution to_p such that:
				// int_p to_p(p) qnoti(p) dp = int_x qnoti(sigma(x)) qnoti(x) dx
				// int_p to_p(p) p qnoti(p) dp = int_x qnoti(sigma(x)) sigma(x) qnoti(x) dx
				double logZ = LogAverageFactor(logistic, x, falseMsg) + logistic.GetLogNormalizer(); // log int_x logistic(sigma(x)) N(x;m,v) dx
				Gaussian post = XAverageConditional(logistic, falseMsg) * x;
				double mp,vp;
				post.GetMeanAndVariance(out mp, out vp);
				double tc1 = logistic.TrueCount-1;
				double fc1 = logistic.FalseCount-1;
				double Ep;
				if (tc1+fc1 == 0) {
					Beta logistic1 = new Beta(logistic.TrueCount+1, logistic.FalseCount);
					double logZp = LogAverageFactor(logistic1, x, falseMsg) + logistic1.GetLogNormalizer();
					Ep = Math.Exp(logZp - logZ);
				} else {
					// Ep = int_p to_p(p) p qnoti(p) dp / int_p to_p(p) qnoti(p) dp
					// mp = m + v (a - (a+b) Ep)
					Ep = (tc1 - (mp - m)/v)/(tc1+fc1);
				}
				return BetaFromMeanAndIntegral(Ep, logZ, tc1, fc1);
			}
		}
開發者ID:xornand,項目名稱:Infer.Net,代碼行數:61,代碼來源:Logistic.cs

示例3: LogAverageFactor

		/// <summary>
		/// Evidence message for EP
		/// </summary>
		/// <param name="logistic">Incoming message from 'logistic'.</param>
		/// <param name="x">Incoming message from 'x'.</param>
		/// <param name="falseMsg">Buffer 'falseMsg'.</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_(logistic,x) p(logistic,x) factor(logistic,x))</c>.
		/// </para></remarks>
		public static double LogAverageFactor(Beta logistic, Gaussian x, Gaussian falseMsg)
		{
			// return log(int_y int_x delta(y - Logistic(x)) Beta(y) Gaussian(x) dx dy)
			double m,v;
			x.GetMeanAndVariance(out m, out v);
			if (logistic.TrueCount == 2 && logistic.FalseCount == 1) {
				// shortcut for common case
				return Math.Log(2*MMath.LogisticGaussian(m, v));
			} else if (logistic.TrueCount == 1 && logistic.FalseCount == 2) {
				return Math.Log(2*MMath.LogisticGaussian(-m, v));
			} else {
				// logistic(sigma(x)) N(x;m,v)
				// = sigma(x)^(a-1) sigma(-x)^(b-1) N(x;m,v) gamma(a+b)/gamma(a)/gamma(b)
				// = e^((a-1)x) sigma(-x)^(a+b-2) N(x;m,v)
				// = sigma(-x)^(a+b-2) N(x;m+(a-1)v,v) exp((a-1)m + (a-1)^2 v/2)
				// int_x logistic(sigma(x)) N(x;m,v) dx 
				// =approx (int_x sigma(-x)/falseMsg(x) falseMsg(x)^(a+b-2) N(x;m+(a-1)v,v))^(a+b-2) 
				//       * (int_x falseMsg(x)^(a+b-2) N(x;m+(a-1)v,v))^(1 - (a+b-2))
				//       *  exp((a-1)m + (a-1)^2 v/2) gamma(a+b)/gamma(a)/gamma(b)
				// This formula comes from (66) in Minka (2005)
				// Alternatively,
				// =approx (int_x falseMsg(x)/sigma(-x) falseMsg(x)^(a+b-2) N(x;m+(a-1)v,v))^(-(a+b-2))
				//       * (int_x falseMsg(x)^(a+b-2) N(x;m+(a-1)v,v))^(1 + (a+b-2))
				//       *  exp((a-1)m + (a-1)^2 v/2) gamma(a+b)/gamma(a)/gamma(b)
				double tc1 = logistic.TrueCount-1;
				double fc1 = logistic.FalseCount-1;
				Gaussian prior = new Gaussian(m + tc1*v, v);
				if (tc1+fc1 < 0) {
					// numerator2 = int_x falseMsg(x)^(a+b-1) N(x;m+(a-1)v,v) dx
					double numerator2 = prior.GetLogAverageOfPower(falseMsg, tc1+fc1+1);
					Gaussian prior2 = prior*(falseMsg^(tc1+fc1+1));
					double mp,vp;
					prior2.GetMeanAndVariance(out mp, out vp);
					// numerator = int_x (1+exp(x)) falseMsg(x)^(a+b-1) N(x;m+(a-1)v,v) dx / int_x falseMsg(x)^(a+b-1) N(x;m+(a-1)v,v) dx
					double numerator = Math.Log(1 + Math.Exp(mp + 0.5*vp));
					// denominator = int_x falseMsg(x)^(a+b-2) N(x;m+(a-1)v,v) dx
					double denominator = prior.GetLogAverageOfPower(falseMsg, tc1+fc1);
					return -(tc1+fc1)*(numerator+numerator2-denominator) + denominator + (tc1*m + tc1*tc1*v*0.5) - logistic.GetLogNormalizer();
				} else {
					// numerator2 = int_x falseMsg(x)^(a+b-3) N(x;m+(a-1)v,v) dx
					double numerator2 = prior.GetLogAverageOfPower(falseMsg, tc1+fc1-1);
					Gaussian prior2 = prior*(falseMsg^(tc1+fc1-1));
					double mp,vp;
					prior2.GetMeanAndVariance(out mp, out vp);
					// numerator = int_x sigma(-x) falseMsg(x)^(a+b-3) N(x;m+(a-1)v,v) dx / int_x falseMsg(x)^(a+b-3) N(x;m+(a-1)v,v) dx
					double numerator = Math.Log(MMath.LogisticGaussian(-mp, vp));
					// denominator = int_x falseMsg(x)^(a+b-2) N(x;m+(a-1)v,v) dx
					double denominator = prior.GetLogAverageOfPower(falseMsg, tc1+fc1);
					return (tc1+fc1)*(numerator+numerator2-denominator) + denominator + (tc1*m + tc1*tc1*v*0.5) - logistic.GetLogNormalizer();
				}
			}
		}
開發者ID:xornand,項目名稱:Infer.Net,代碼行數:62,代碼來源:Logistic.cs


注:本文中的Beta.GetLogNormalizer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。