本文整理汇总了C#中Gaussian.GetMeanAndVarianceImproper方法的典型用法代码示例。如果您正苦于以下问题:C# Gaussian.GetMeanAndVarianceImproper方法的具体用法?C# Gaussian.GetMeanAndVarianceImproper怎么用?C# Gaussian.GetMeanAndVarianceImproper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Gaussian
的用法示例。
在下文中一共展示了Gaussian.GetMeanAndVarianceImproper方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SampleAverageConditional
/// <summary>
/// EP message to 'sample'
/// </summary>
/// <param name="sample">Incoming message from 'sample'.</param>
/// <param name="mean">Incoming message from 'mean'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="precision">Incoming message from 'precision'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <returns>The outgoing EP message to the 'sample' argument</returns>
/// <remarks><para>
/// The outgoing message is a distribution matching the moments of 'sample' as the random arguments are varied.
/// The formula is <c>proj[p(sample) sum_(mean,precision) p(mean,precision) factor(sample,mean,precision)]/p(sample)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="mean"/> is not a proper distribution</exception>
/// <exception cref="ImproperMessageException"><paramref name="precision"/> is not a proper distribution</exception>
public static Gaussian SampleAverageConditional(Gaussian sample, [SkipIfUniform] Gaussian mean, [SkipIfUniform] Gamma precision)
{
Gaussian result = new Gaussian();
if (precision.IsPointMass) {
return SampleAverageConditional(mean, precision.Point);
} else if (sample.IsUniform()) {
// for large vx, Z =approx N(mx; mm, vx+vm+E[1/prec])
double mm,mv;
mean.GetMeanAndVariance(out mm, out mv);
// NOTE: this error may happen because sample didn't receive any message yet under the schedule.
// Need to make the scheduler smarter to avoid this.
if(precision.Shape <= 1.0) throw new ArgumentException("The posterior has infinite variance due to precision distributed as "+precision+" (shape <= 1). Try using a different prior for the precision, with shape > 1.");
return Gaussian.FromMeanAndVariance(mm, mv + precision.GetMeanInverse());
} else if (mean.IsUniform() || precision.IsUniform()) {
result.SetToUniform();
} else if (sample.IsPointMass) {
// The correct answer here is not uniform, but rather a limit.
// However it doesn't really matter what we return since multiplication by a point mass
// always yields a point mass.
result.SetToUniform();
} else if (!precision.IsProper()) {
throw new ImproperMessageException(precision);
} else {
// The formula is int_prec int_mean N(x;mean,1/prec) p(x) p(mean) p(prec) =
// int_prec N(x; mm, mv + 1/prec) p(x) p(prec) =
// int_prec N(x; new xm, new xv) N(xm; mm, mv + xv + 1/prec) p(prec)
// Let R = Prec/(Prec + mean.Prec)
// new xv = inv(R*mean.Prec + sample.Prec)
// new xm = xv*(R*mean.PM + sample.PM)
// In the case where sample and mean are improper distributions,
// we must only consider values of prec for which (new xv > 0).
// This happens when R*mean.Prec > -sample.Prec
// As a function of Prec, R*mean.Prec has a singularity at Prec=-mean.Prec
// This function is greater than a threshold when Prec is sufficiently small or sufficiently large.
// Therefore we construct an interval of Precs to exclude from the integration.
double xm, xv, mm, mv;
sample.GetMeanAndVarianceImproper(out xm, out xv);
mean.GetMeanAndVarianceImproper(out mm, out mv);
double lowerBound = 0;
double upperBound = Double.PositiveInfinity;
bool precisionIsBetween = true;
if (mean.Precision >= 0) {
if (sample.Precision < -mean.Precision) throw new ImproperMessageException(sample);
//lowerBound = -mean.Precision * sample.Precision / (mean.Precision + sample.Precision);
lowerBound = -1.0 / (xv + mv);
} else { // mean.Precision < 0
if (sample.Precision < 0) {
precisionIsBetween = true;
lowerBound = -1.0 / (xv + mv);
upperBound = -mean.Precision;
} else if (sample.Precision < -mean.Precision) {
precisionIsBetween = true;
lowerBound = 0;
upperBound = -mean.Precision;
} else {
// in this case, the precision should NOT be in this interval.
precisionIsBetween = false;
lowerBound = -mean.Precision;
lowerBound = -1.0 / (xv + mv);
}
}
double[] nodes = new double[QuadratureNodeCount];
double[] weights = new double[nodes.Length];
QuadratureNodesAndWeights(precision, nodes, weights);
double Z = 0, rmean = 0, rvariance = 0;
for (int i = 0; i < nodes.Length; i++) {
double newVar, newMean;
Assert.IsTrue(nodes[i] > 0);
if ((nodes[i] > lowerBound && nodes[i] < upperBound) != precisionIsBetween) continue;
// the following works even if sample is uniform. (sample.Precision == 0)
if (mean.IsPointMass) {
// take limit mean.Precision -> Inf
newVar = 1.0 / (nodes[i] + sample.Precision);
newMean = newVar * (nodes[i] * mean.Point + sample.MeanTimesPrecision);
} else {
// mean.Precision < Inf
double R = nodes[i] / (nodes[i] + mean.Precision);
newVar = 1.0 / (R * mean.Precision + sample.Precision);
newMean = newVar * (R * mean.MeanTimesPrecision + sample.MeanTimesPrecision);
}
double f;
// If p(x) is uniform, xv=Inf and the term N(xm; mm, mv + xv + 1/prec) goes away
if (sample.IsUniform())
f = weights[i];
else
//.........这里部分代码省略.........
示例2: SampleAverageConditional
/// <summary>
/// EP message to 'sample'
/// </summary>
/// <param name="sample">Incoming message from 'sample'.</param>
/// <param name="mean">Incoming message from 'mean'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="precision">Incoming message from 'precision'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <returns>The outgoing EP message to the 'sample' argument</returns>
/// <remarks><para>
/// The outgoing message is a distribution matching the moments of 'sample' as the random arguments are varied.
/// The formula is <c>proj[p(sample) sum_(mean,precision) p(mean,precision) factor(sample,mean,precision)]/p(sample)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="mean"/> is not a proper distribution</exception>
/// <exception cref="ImproperMessageException"><paramref name="precision"/> is not a proper distribution</exception>
public static Gaussian SampleAverageConditional(Gaussian sample, [SkipIfUniform] Gaussian mean, [SkipIfUniform] Gamma precision, Gamma to_precision)
{
if (sample.IsUniform() && precision.Shape <= 1.0) sample = Gaussian.FromNatural(1e-20, 1e-20);
if (precision.IsPointMass) {
return SampleAverageConditional(mean, precision.Point);
} else if (sample.IsUniform()) {
// for large vx, Z =approx N(mx; mm, vx+vm+E[1/prec])
double mm,mv;
mean.GetMeanAndVariance(out mm, out mv);
// NOTE: this error may happen because sample didn't receive any message yet under the schedule.
// Need to make the scheduler smarter to avoid this.
if (precision.Shape <= 1.0) throw new ArgumentException("The posterior has infinite variance due to precision distributed as "+precision+" (shape <= 1). Try using a different prior for the precision, with shape > 1.");
return Gaussian.FromMeanAndVariance(mm, mv + precision.GetMeanInverse());
} else if (mean.IsUniform() || precision.IsUniform()) {
return Gaussian.Uniform();
} else if (sample.IsPointMass) {
// The correct answer here is not uniform, but rather a limit.
// However it doesn't really matter what we return since multiplication by a point mass
// always yields a point mass.
return Gaussian.Uniform();
} else if (!precision.IsProper()) {
throw new ImproperMessageException(precision);
} else {
// The formula is int_prec int_mean N(x;mean,1/prec) p(x) p(mean) p(prec) =
// int_prec N(x; mm, mv + 1/prec) p(x) p(prec) =
// int_prec N(x; new xm, new xv) N(xm; mm, mv + xv + 1/prec) p(prec)
// Let R = Prec/(Prec + mean.Prec)
// new xv = inv(R*mean.Prec + sample.Prec)
// new xm = xv*(R*mean.PM + sample.PM)
// In the case where sample and mean are improper distributions,
// we must only consider values of prec for which (new xv > 0).
// This happens when R*mean.Prec > -sample.Prec
// As a function of Prec, R*mean.Prec has a singularity at Prec=-mean.Prec
// This function is greater than a threshold when Prec is sufficiently small or sufficiently large.
// Therefore we construct an interval of Precs to exclude from the integration.
double xm, xv, mm, mv;
sample.GetMeanAndVarianceImproper(out xm, out xv);
mean.GetMeanAndVarianceImproper(out mm, out mv);
double lowerBound = 0;
double upperBound = Double.PositiveInfinity;
bool precisionIsBetween = true;
if (mean.Precision >= 0) {
if (sample.Precision < -mean.Precision) throw new ImproperMessageException(sample);
//lowerBound = -mean.Precision * sample.Precision / (mean.Precision + sample.Precision);
lowerBound = -1.0 / (xv + mv);
} else { // mean.Precision < 0
if (sample.Precision < 0) {
precisionIsBetween = true;
lowerBound = -1.0 / (xv + mv);
upperBound = -mean.Precision;
} else if (sample.Precision < -mean.Precision) {
precisionIsBetween = true;
lowerBound = 0;
upperBound = -mean.Precision;
} else {
// in this case, the precision should NOT be in this interval.
precisionIsBetween = false;
lowerBound = -mean.Precision;
lowerBound = -1.0 / (xv + mv);
}
}
double[] nodes = new double[QuadratureNodeCount];
double[] logWeights = new double[nodes.Length];
Gamma precMarginal = precision*to_precision;
QuadratureNodesAndWeights(precMarginal, nodes, logWeights);
if (!to_precision.IsUniform()) {
// modify the weights
for (int i = 0; i < logWeights.Length; i++) {
logWeights[i] += precision.GetLogProb(nodes[i]) - precMarginal.GetLogProb(nodes[i]);
}
}
GaussianEstimator est = new GaussianEstimator();
double shift = 0;
for (int i = 0; i < nodes.Length; i++) {
double newVar, newMean;
Assert.IsTrue(nodes[i] > 0);
if ((nodes[i] > lowerBound && nodes[i] < upperBound) != precisionIsBetween) continue;
// the following works even if sample is uniform. (sample.Precision == 0)
if (mean.IsPointMass) {
// take limit mean.Precision -> Inf
newVar = 1.0 / (nodes[i] + sample.Precision);
newMean = newVar * (nodes[i] * mean.Point + sample.MeanTimesPrecision);
} else {
// mean.Precision < Inf
double R = nodes[i] / (nodes[i] + mean.Precision);
newVar = 1.0 / (R * mean.Precision + sample.Precision);
//.........这里部分代码省略.........