本文整理汇总了C#中VectorGaussian.GetMeanAndVariance方法的典型用法代码示例。如果您正苦于以下问题:C# VectorGaussian.GetMeanAndVariance方法的具体用法?C# VectorGaussian.GetMeanAndVariance怎么用?C# VectorGaussian.GetMeanAndVariance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VectorGaussian
的用法示例。
在下文中一共展示了VectorGaussian.GetMeanAndVariance方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BAverageConditional
/// <summary>
/// EP message to 'B'
/// </summary>
/// <param name="matrixMultiply">Incoming message from 'matrixMultiply'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
/// <param name="A">Constant value for '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="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 'B' as the random arguments are varied.
/// The formula is <c>proj[p(B) sum_(matrixMultiply) p(matrixMultiply) factor(matrixMultiply,A,B)]/p(B)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="matrixMultiply"/> is not a proper distribution</exception>
/// <exception cref="ImproperMessageException"><paramref name="B"/> is not a proper distribution</exception>
public static GaussianArray2D BAverageConditional([SkipIfUniform] GaussianArray2D matrixMultiply, double[,] A, [SkipIfUniform] GaussianArray2D B, GaussianArray2D result)
{
int rows = matrixMultiply.GetLength(0);
int cols = matrixMultiply.GetLength(1);
int inner = A.GetLength(1);
if (result == null) result = new GaussianArray2D(inner, cols);
var ai = DenseVector.Zero(inner);
var mean = DenseVector.Zero(inner);
PositiveDefiniteMatrix variance = new
PositiveDefiniteMatrix(inner, inner);
var bj = new VectorGaussian(inner);
for (int j = 0; j < cols; j++) {
bj.Precision.SetAllElementsTo(0);
bj.MeanTimesPrecision.SetAllElementsTo(0);
// we are projecting from family of full covariance Gaussians to diagonal
// covariance, so we should include the context
for (int c = 0; c < inner; c++) {
bj.Precision[c, c] = B[c, j].Precision;
bj.MeanTimesPrecision[c] = B[c, j].MeanTimesPrecision;
}
for (int i = 0; i < rows; i++) {
Gaussian xij = matrixMultiply[i, j];
for (int k = 0; k < inner; k++) {
ai[k] = A[i, k];
}
if (xij.IsPointMass) throw new NotImplementedException(LowRankNotSupportedMessage);
bj.Precision.SetToSumWithOuter(bj.Precision, xij.Precision, ai, ai);
bj.MeanTimesPrecision.SetToSum(1.0, bj.MeanTimesPrecision, xij.MeanTimesPrecision, ai);
}
bj.GetMeanAndVariance(mean, variance);
for (int k = 0; k < inner; k++) {
Gaussian rkj = result[k, j];
rkj.SetMeanAndVariance(mean[k], variance[k, k]);
result[k, j] = rkj / B[k, j];
}
}
return result;
}
示例2: AAverageConditional
/// <summary>
/// EP message to 'A'
/// </summary>
/// <param name="matrixMultiply">Incoming message from 'matrixMultiply'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
/// <param name="A">Incoming message from 'A'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
/// <param name="B">Constant value for 'B'.</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 'A' as the random arguments are varied.
/// The formula is <c>proj[p(A) sum_(matrixMultiply) p(matrixMultiply) factor(matrixMultiply,A,B)]/p(A)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="matrixMultiply"/> is not a proper distribution</exception>
/// <exception cref="ImproperMessageException"><paramref name="A"/> is not a proper distribution</exception>
public static GaussianArray2D AAverageConditional([SkipIfUniform] GaussianArray2D matrixMultiply, [SkipIfUniform] GaussianArray2D A, double[,] B, GaussianArray2D result)
{
int rows = matrixMultiply.GetLength(0);
int cols = matrixMultiply.GetLength(1);
int inner = B.GetLength(0);
if (result == null) result = new GaussianArray2D(rows, inner);
// sum_{i,j} (m[i,j] - a[i,:]*b[:,j])^2/v[i,j] =
// sum_{i,j} (m[i,j]^2 - 2m[i,j]a[i,:]*b[:,j] + a[i,:]*(b[:,j] b[:,j]')*a[i,:]')/v[i,j]
// meanTimesPrec(a[i,:]) = sum_j (m[i,j]/v[i,j]) b[:,j]
// prec(a[i,:]) = sum_j b[:,j]*b[:,j]'/v[i,j]
Vector bj = Vector.Zero(inner);
Vector mean = Vector.Zero(inner);
VectorGaussian ai = new VectorGaussian(inner);
PositiveDefiniteMatrix variance = new PositiveDefiniteMatrix(inner, inner);
for (int i = 0; i < rows; i++) {
ai.Precision.SetAllElementsTo(0.0);
ai.MeanTimesPrecision.SetAllElementsTo(0.0);
// we are projecting from family of full covariance Gaussians to diagonal
// covariance, so we should include the context
for (int c = 0; c < inner; c++) {
ai.Precision[c, c] = A[i, c].Precision;
ai.MeanTimesPrecision[c] = A[i, c].MeanTimesPrecision;
}
for (int j = 0; j < cols; j++) {
Gaussian xij = matrixMultiply[i, j];
for (int k = 0; k < inner; k++) {
bj[k] = B[k, j];
}
if (xij.IsPointMass) throw new NotImplementedException(LowRankNotSupportedMessage);
ai.Precision.SetToSumWithOuter(ai.Precision, xij.Precision, bj, bj);
ai.MeanTimesPrecision.SetToSum(1.0, ai.MeanTimesPrecision, xij.MeanTimesPrecision, bj);
}
ai.GetMeanAndVariance(mean, variance);
for (int k = 0; k < inner; k++) {
Gaussian rik = result[i, k];
rik.SetMeanAndVariance(mean[k], variance[k, k]);
result[i, k] = rik / A[i, k];
}
}
return result;
}