本文整理汇总了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);
}
示例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);
}
}
示例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();
}
}
}