本文整理汇总了C#中PositiveDefiniteMatrix.Diagonal方法的典型用法代码示例。如果您正苦于以下问题:C# PositiveDefiniteMatrix.Diagonal方法的具体用法?C# PositiveDefiniteMatrix.Diagonal怎么用?C# PositiveDefiniteMatrix.Diagonal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PositiveDefiniteMatrix
的用法示例。
在下文中一共展示了PositiveDefiniteMatrix.Diagonal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SumAverageLogarithm
/// <summary>
/// VMP message to 'Sum'
/// </summary>
/// <param name="A">Incoming message from 'A'.</param>
/// <param name="B">Incoming message from 'B'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
/// <param name="MeanOfB">Buffer 'MeanOfB'.</param>
/// <param name="CovarianceOfB">Buffer 'CovarianceOfB'.</param>
/// <returns>The outgoing VMP message to the 'Sum' argument</returns>
/// <remarks><para>
/// The outgoing message is a distribution matching the moments of 'Sum' as the random arguments are varied.
/// The formula is <c>proj[sum_(A,B) p(A,B) factor(Sum,A,B)]</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="B"/> is not a proper distribution</exception>
public static Gaussian SumAverageLogarithm(DistributionStructArray<Bernoulli, bool> A, [SkipIfUniform] VectorGaussian B, Vector MeanOfB, PositiveDefiniteMatrix CovarianceOfB)
{
Gaussian result = new Gaussian();
// p(x|a,b) = N(E[a]'*E[b], E[b]'*var(a)*E[b] + E[a]'*var(b)*E[a] + trace(var(a)*var(b)))
Vector ma = Vector.Zero(A.Count);
Vector va = Vector.Zero(A.Count);
for (int i = 0; i < A.Count; i++) {
ma[i] = A[i].GetMean();
va[i] = A[i].GetVariance();
}
// Uses John Winn's rule for deterministic factors.
// Strict variational inference would set the variance to 0.
var MeanOfBSquared = Vector.Zero(MeanOfB.Count);
MeanOfBSquared.SetToFunction(MeanOfB, x => x * x);
result.SetMeanAndVariance(ma.Inner(MeanOfB), va.Inner(MeanOfBSquared) + CovarianceOfB.QuadraticForm(ma) + va.Inner(CovarianceOfB.Diagonal()));
return result;
}
示例2: XAverageLogarithm
/// <summary>
/// VMP message to 'X'
/// </summary>
/// <param name="A">Incoming message from 'A'. Must be a proper distribution. If all elements are uniform, the result will be uniform.</param>
/// <param name="B">Incoming message from 'B'. Must be a proper distribution. If all elements are uniform, the result will be uniform.</param>
/// <param name="MeanOfB">Buffer 'MeanOfB'.</param>
/// <param name="CovarianceOfB">Buffer 'CovarianceOfB'.</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 'X' as the random arguments are varied.
/// The formula is <c>proj[sum_(A,B) p(A,B) factor(X,A,B)]</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="A"/> is not a proper distribution</exception>
/// <exception cref="ImproperMessageException"><paramref name="B"/> is not a proper distribution</exception>
public static Gaussian XAverageLogarithm([SkipIfAllUniform] GaussianArray A, [SkipIfAllUniform] VectorGaussian B, Vector MeanOfB, PositiveDefiniteMatrix CovarianceOfB)
{
int K = MeanOfB.Count;
// p(x|a,b) = N(E[a]'*E[b], E[b]'*var(a)*E[b] + E[a]'*var(b)*E[a] + trace(var(a)*var(b)))
var ma = Vector.Zero(K);
var va = Vector.Zero(K);
for (int k = 0; k < K; k++) {
double m, v;
A[k].GetMeanAndVariance(out m, out v);
ma[k] = m;
va[k] = v;
}
// Uses John Winn's rule for deterministic factors.
// Strict variational inference would set the variance to 0.
var mbj2 = Vector.Zero(K);
mbj2.SetToFunction(MeanOfB, x => x * x);
// slooow
Gaussian result = new Gaussian();
result.SetMeanAndVariance(ma.Inner(MeanOfB), va.Inner(mbj2) + CovarianceOfB.QuadraticForm(ma) + va.Inner(CovarianceOfB.Diagonal()));
if (result.Precision < 0)
throw new ApplicationException("improper message");
return result;
}