本文整理汇总了C#中Gaussian.SetToProduct方法的典型用法代码示例。如果您正苦于以下问题:C# Gaussian.SetToProduct方法的具体用法?C# Gaussian.SetToProduct怎么用?C# Gaussian.SetToProduct使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Gaussian
的用法示例。
在下文中一共展示了Gaussian.SetToProduct方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UsesAverageLogarithm
public static Gaussian UsesAverageLogarithm(NonconjugateGaussian[] Uses, Gaussian Def, Gaussian result)
{
NonconjugateGaussian prod = Uses[0];
for (int i = 1; i < Uses.Length; i++)
prod.SetToProduct(prod, Uses[i]);
result = prod.GetGaussian(true);
result.SetToProduct(result, Def);
return result;
}
示例2: LogisticProposalDistribution
/// <summary>
/// Find the Laplace approximation for Beta(Logistic(x)) * Gaussian(x))
/// </summary>
/// <param name="beta">Beta distribution</param>
/// <param name="gauss">Gaussian distribution</param>
/// <returns>A proposal distribution</returns>
public static Gaussian LogisticProposalDistribution(Beta beta, Gaussian gauss)
{
if (beta.IsUniform())
return new Gaussian(gauss);
// if gauss is uniform, m,p = 0 below, and the following code will just ignore the Gaussian
// and do a Laplace approximation for Beta(Logistic(x))
double c = beta.TrueCount-1;
double d = beta.FalseCount-1;
double m = gauss.GetMean();
double p = gauss.Precision;
// We want to find the mode of
// ln(g(x)) = c.ln(f(x)) + d.ln(1 - f(x)) - 0.5p((x - m)^2) + constant
// First deriv:
// h(x) = (ln(g(x))' = c.(1 - f(x)) - d.f(x) - p(x-m)
// Second deriv:
// h'(x) = (ln(g(x))' = -(c+d).f'(x) - p
// Use Newton-Raphson to find unique root of h(x).
// g(x) is log-concave so Newton-Raphson should converge quickly.
// Set the initial point by projecting beta
// to a Gaussian and taking the mean of the product:
double bMean, bVar;
beta.GetMeanAndVariance(out bMean, out bVar);
Gaussian prod = new Gaussian();
double invLogisticMean = Math.Log(bMean) - Math.Log(1.0-bMean);
prod.SetToProduct(Gaussian.FromMeanAndVariance(invLogisticMean, bVar), gauss);
double xnew = prod.GetMean();
double x=0, fx, dfx, hx, dhx=0;
int maxIters = 100; // Should only need a handful of iters
int cnt = 0;
do {
x = xnew;
fx = MMath.Logistic(x);
dfx = fx * (1.0-fx);
// Find the root of h(x)
hx = c * (1.0 - fx) - d * fx - p*(x-m);
dhx = -(c+d)*dfx - p;
xnew = x - (hx / dhx); // The Newton step
if (Math.Abs(x - xnew) < 0.00001)
break;
} while (++cnt < maxIters);
if (cnt >= maxIters)
throw new ApplicationException("Unable to find proposal distribution mode");
return Gaussian.FromMeanAndPrecision(x, -dhx);
}