本文整理汇总了C#中Gamma.GetShapeAndScale方法的典型用法代码示例。如果您正苦于以下问题:C# Gamma.GetShapeAndScale方法的具体用法?C# Gamma.GetShapeAndScale怎么用?C# Gamma.GetShapeAndScale使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Gamma
的用法示例。
在下文中一共展示了Gamma.GetShapeAndScale方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EvidenceMessageExpectations
/// <summary>
/// Perform the quadrature required for the VMP evidence message
/// </summary>
/// <param name="meanQ">Incoming message from m='mean'.</param>
/// <param name="totalCountQ">Incoming message from s='totalCount'.</param>
/// <returns>Vector of E[ LogGamma(s*m_k)].</returns>
/// <remarks><para>
/// The quadrature over 'totalCount' (which is Gamma-distributed) is
/// peformed by a change of variable x=log(s) followed by Gauss-Hermite
/// quadrature. The quadrature over m is performed using Gauss-Legendre.
/// </para></remarks>
public static Vector EvidenceMessageExpectations(
Dirichlet meanQ,
Gamma totalCountQ)
{
// Get shape and scale of the distribution
double at, bt;
totalCountQ.GetShapeAndScale(out at, out bt);
bt = 1 / bt; // want rate not scale
// Mean in the transformed domain
double proposalMean = totalCountQ.GetMeanLog();
// Laplace approximation of variance in transformed domain
double proposalVariance = 1 / at;
// Quadrature coefficient
int nt = 32;
Vector nodes = Vector.Zero(nt);
Vector weights = Vector.Zero(nt);
Vector expx = Vector.Zero(nt);
if (!totalCountQ.IsPointMass) {
Quadrature.GaussianNodesAndWeights(proposalMean, proposalVariance, nodes, weights);
// Precompute weights for each m slice
for (int i = 0; i < nt; i++) {
double x = nodes[i];
expx[i] = Math.Exp(x);
double p = at * x - bt * expx[i] - Gaussian.GetLogProb(x, proposalMean, proposalVariance);
weights[i] *= Math.Exp(p);
}
}
int nm = 20;
Vector mnodes = Vector.Zero(nm);
Vector mweight = Vector.Zero(nm);
Quadrature.UniformNodesAndWeights(0, 1, mnodes, mweight);
int K = meanQ.Dimension;
Vector[] mweights = new Vector[K];
Beta[] mkDist = new Beta[K];
double[] EELogGamma = new double[K];
for (int i = 0; i < K; i++) {
mweights[i] = Vector.Copy(mweight);
mkDist[i] = new Beta(meanQ.PseudoCount[i], meanQ.TotalCount - meanQ.PseudoCount[i]);
EELogGamma[i] = 0;
}
for (int j = 0; j < nm; j++) {
double m = mnodes[j];
double ELogGamma = 0;
if (totalCountQ.IsPointMass)
ELogGamma = MMath.GammaLn(m * totalCountQ.Point);
else {
// Calculate expectations in x=log(s) space using Gauss-Hermite quadrature
for (int i = 0; i < nt; i++) {
double x = nodes[i];
ELogGamma += weights[i] * (MMath.GammaLn(m * expx[i]) + x);
}
// Normalise and add removed components
double normalisation = Math.Pow(bt, at) / MMath.Gamma(at);
ELogGamma = normalisation * ELogGamma - proposalMean;
}
for (int i = 0; i < K; i++) {
mweights[i][j] *= Math.Exp(mkDist[i].GetLogProb(m));
EELogGamma[i] += mweights[i][j] * ELogGamma;
}
}
return Vector.FromArray(EELogGamma);
}