本文整理汇总了C#中Dirichlet.SetMeanAndMeanSquare方法的典型用法代码示例。如果您正苦于以下问题:C# Dirichlet.SetMeanAndMeanSquare方法的具体用法?C# Dirichlet.SetMeanAndMeanSquare怎么用?C# Dirichlet.SetMeanAndMeanSquare使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dirichlet
的用法示例。
在下文中一共展示了Dirichlet.SetMeanAndMeanSquare方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProbsAverageConditional
/// <summary>
/// EP message to 'probs'
/// </summary>
/// <param name="sample">Incoming message from 'sample'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="probs">Incoming message from 'probs'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is a distribution matching the moments of 'probs' as the random arguments are varied.
/// The formula is <c>proj[p(probs) sum_(sample) p(sample) factor(sample,probs)]/p(probs)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="sample"/> is not a proper distribution</exception>
/// <exception cref="ImproperMessageException"><paramref name="probs"/> is not a proper distribution</exception>
public static Dirichlet ProbsAverageConditional([SkipIfUniform] Discrete sample, Dirichlet probs, Dirichlet result)
{
if (probs.IsPointMass) return ProbsAverageConditional(sample, probs.Point, result);
if (sample.IsPointMass) return ProbsConditional(sample.Point, result);
// Z = sum_x q(x) int_p f(x,p)*q(p) = sum_x q(x) E[p[x]]
Vector sampleProbs = Vector.Zero(sample.Dimension);
sampleProbs = sample.GetProbs(sampleProbs);
double Z = sampleProbs.Inner(probs.PseudoCount);
double invZ = 1.0 / Z;
// the posterior is a mixture of Dirichlets having the following form:
// sum_x q(x) (alpha(x)/sum_i alpha(i)) Dirichlet(p; alpha(x)+1, alpha(not x)+0)
// where the mixture weights are w(x) =propto q(x) alpha(x)
// w[i] = sample[i]*probs.PseudoCount[i]/Z
// The posterior mean of probs(x) = (w(x) + alpha(x))/(1 + sum_x alpha(x))
double invTotalCountPlus1 = 1.0 / (probs.TotalCount + 1);
Vector m = Vector.Zero(sample.Dimension);
m.SetToFunction(sampleProbs, probs.PseudoCount, (x, y) => (x * invZ + 1.0) * y * invTotalCountPlus1);
if (!Dirichlet.AllowImproperSum)
{
// To get the correct mean, we need (probs.PseudoCount[i] + delta[i]) to be proportional to m[i].
// If we set delta[argmin] = 0, then we just solve the equation
// (probs.PseudoCount[i] + delta[i])/probs.PseudoCount[argmin] = m[i]/m[argmin]
// for delta[i].
int argmin = sampleProbs.IndexOfMinimum();
double newTotalCount = probs.PseudoCount[argmin] / m[argmin];
double argMinValue = sampleProbs[argmin];
result.PseudoCount.SetToFunction(m, probs.PseudoCount, (x, y) => 1.0 + (x * newTotalCount) - y);
result.PseudoCount.SetToFunction(result.PseudoCount, sampleProbs, (x, y) => (y==argMinValue) ? 1.0 : x);
result.TotalCount = result.PseudoCount.Sum(); // result.Dimension + newTotalCount - probs.TotalCount;
return result;
}
else
{
// The posterior meanSquare of probs(x) = (2 w(x) + alpha(x))/(2 + sum_x alpha(x)) * (1 + alpha(x))/(1 + sum_x alpha(x))
double invTotalCountPlus2 = 1.0 / (2 + probs.TotalCount);
Vector m2 = Vector.Zero(sample.Dimension);
m2.SetToFunction(sampleProbs, probs.PseudoCount, (x, y) => (2.0 * x * invZ + 1.0) * y * invTotalCountPlus2 * (1.0 + y) * invTotalCountPlus1);
result.SetMeanAndMeanSquare(m, m2);
result.SetToRatio(result, probs);
return result;
}
}