本文整理汇总了C#中Bernoulli.GetProbFalse方法的典型用法代码示例。如果您正苦于以下问题:C# Bernoulli.GetProbFalse方法的具体用法?C# Bernoulli.GetProbFalse怎么用?C# Bernoulli.GetProbFalse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bernoulli
的用法示例。
在下文中一共展示了Bernoulli.GetProbFalse方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LogAverageFactor
/// <summary>
/// Evidence message for EP
/// </summary>
/// <param name="isBetween">Incoming message from 'isBetween'.</param>
/// <param name="X">Incoming message from 'x'.</param>
/// <param name="lowerBound">Incoming message from 'lowerBound'.</param>
/// <param name="upperBound">Incoming message from 'upperBound'.</param>
/// <returns>Logarithm of the factor's average value across the given argument distributions</returns>
/// <remarks><para>
/// The formula for the result is <c>log(sum_(isBetween,x,lowerBound,upperBound) p(isBetween,x,lowerBound,upperBound) factor(isBetween,x,lowerBound,upperBound))</c>.
/// </para></remarks>
public static double LogAverageFactor(Bernoulli isBetween, Gaussian X, Gaussian lowerBound, Gaussian upperBound)
{
if (isBetween.LogOdds == 0.0) return -MMath.Ln2;
else
{
#if true
double logitProbBetween = MMath.LogitFromLog(LogProbBetween(X, lowerBound, upperBound));
return Bernoulli.LogProbEqual(isBetween.LogOdds, logitProbBetween);
#else
double d_p = isBetween.GetProbTrue() - isBetween.GetProbFalse();
return Math.Log(d_p * Math.Exp(LogProbBetween()) + isBetween.GetProbFalse());
#endif
}
}
示例2: AverageLogFactor
/// <summary>
/// Evidence message for VMP.
/// </summary>
/// <param name="sample">Incoming message from sample</param>
/// <param name="logOdds">Fixed value for logOdds</param>
/// <returns><c>sum_x marginal(x)*log(factor(x))</c></returns>
/// <remarks><para>
/// The formula for the result is <c>int log(f(x)) q(x) dx</c>
/// where <c>x = (sample,logOdds)</c>.
/// </para></remarks>
public static double AverageLogFactor(Bernoulli sample, double logOdds)
{
if (sample.IsPointMass) return AverageLogFactor(sample.Point, logOdds);
// probTrue*log(sigma(logOdds)) + probFalse*log(sigma(-logOdds))
// = -log(1+exp(-logOdds)) + probFalse*(-logOdds)
// = probTrue*logOdds - log(1+exp(logOdds))
if (logOdds >= 0) {
double probFalse = sample.GetProbFalse();
return -probFalse * logOdds - MMath.Log1PlusExp(-logOdds);
} else {
double probTrue = sample.GetProbTrue();
return probTrue * logOdds - MMath.Log1PlusExp(logOdds);
}
}
示例3: SampleAverageLogarithm
/// <summary>
/// VMP message to 'sample'.
/// </summary>
/// <param name="choice">Incoming message from 'choice'.</param>
/// <param name="probTrue0">Constant value for 'probTrue0'.</param>
/// <param name="probTrue1">Constant value for 'probTrue1'.</param>
/// <returns>The outgoing VMP message to the 'sample' argument.</returns>
/// <remarks><para>
/// The outgoing message is the exponential of the integral of the log-factor times incoming messages, over all arguments except 'sample'.
/// The formula is <c>int log(f(sample,x)) q(x) dx</c> where <c>x = (choice,probTrue0,probTrue1)</c>.
/// </para></remarks>
public static Bernoulli SampleAverageLogarithm(Bernoulli choice, double probTrue0, double probTrue1)
{
Bernoulli result = new Bernoulli();
if(choice.IsPointMass) return SampleConditional(choice.Point,probTrue0,probTrue1);
// log(p(X=true)/p(X=false)) = sum_k p(Y=k) log(ProbTrue[k]/(1-ProbTrue[k]))
result.LogOdds = choice.GetProbFalse() * MMath.Logit(probTrue0) + choice.GetProbTrue() * MMath.Logit(probTrue1);
return result;
}
示例4: ChoiceAverageLogarithm
/// <summary>
/// VMP message to 'choice'.
/// </summary>
/// <param name="sample">Incoming message from 'sample'.</param>
/// <param name="probTrue0">Constant value for 'probTrue0'.</param>
/// <param name="probTrue1">Constant value for 'probTrue1'.</param>
/// <returns>The outgoing VMP message to the 'choice' argument.</returns>
/// <remarks><para>
/// The outgoing message is the exponential of the integral of the log-factor times incoming messages, over all arguments except 'choice'.
/// The formula is <c>int log(f(choice,x)) q(x) dx</c> where <c>x = (sample,probTrue0,probTrue1)</c>.
/// </para></remarks>
public static Bernoulli ChoiceAverageLogarithm(Bernoulli sample, double probTrue0, double probTrue1)
{
Bernoulli result = new Bernoulli();
if(sample.IsPointMass) return ChoiceConditional(sample.Point,probTrue0,probTrue1);
// p(Y=k) =propto ProbTrue[k]^p(X=true) (1-ProbTrue[k])^p(X=false)
// log(p(Y=true)/p(Y=false)) = p(X=true)*log(ProbTrue[1]/ProbTrue[0]) + p(X=false)*log((1-ProbTrue[1])/(1-ProbTrue[0]))
// = p(X=false)*(log(ProbTrue[0]/(1-ProbTrue[0]) - log(ProbTrue[1]/(1-ProbTrue[1]))) + log(ProbTrue[1]/ProbTrue[0])
if (probTrue0 == 0 || probTrue1 == 0) throw new ArgumentException("probTrue is zero");
result.LogOdds = sample.GetProbTrue() * Math.Log(probTrue1 / probTrue0) + sample.GetProbFalse() * Math.Log((1 - probTrue1) / (1 - probTrue0));
return result;
}
示例5: AAverageLogarithm
/// <summary>
/// VMP message to 'a'
/// </summary>
/// <param name="or">Incoming message from 'or'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="B">Incoming message from 'b'.</param>
/// <returns>The outgoing VMP message to the 'a' argument</returns>
/// <remarks><para>
/// The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except 'a'.
/// Because the factor is deterministic, 'or' is integrated out before taking the logarithm.
/// The formula is <c>exp(sum_(b) p(b) log(sum_or p(or) factor(or,a,b)))</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="or"/> is not a proper distribution</exception>
public static Bernoulli AAverageLogarithm([SkipIfUniform] Bernoulli or, Bernoulli B)
{
// when 'or' is marginalized, the factor is proportional to exp((A|B)*or.LogOdds)
return Bernoulli.FromLogOdds(or.LogOdds * B.GetProbFalse());
}
示例6: SampleAverageConditional
/// <summary>
/// EP message to 'sample'.
/// </summary>
/// <param name="choice">Incoming message from 'choice'.</param>
/// <param name="probTrue0">Constant value for 'probTrue0'.</param>
/// <param name="probTrue1">Constant value for 'probTrue1'.</param>
/// <returns>The outgoing EP message to the 'sample' argument.</returns>
/// <remarks><para>
/// The outgoing message is the integral of the factor times incoming messages, over all arguments except 'sample'.
/// The formula is <c>int f(sample,x) q(x) dx</c> where <c>x = (choice,probTrue0,probTrue1)</c>.
/// </para></remarks>
public static Bernoulli SampleAverageConditional(Bernoulli choice, double probTrue0, double probTrue1)
{
Bernoulli result = new Bernoulli();
if(choice.IsPointMass) return SampleConditional(choice.Point,probTrue0,probTrue1);
#if FAST
result.SetProbTrue(choice.GetProbFalse() * probTrue0 + choice.GetProbTrue() * probTrue1);
#else
// This method is more numerically stable but slower.
// let oX = log(p(X)/(1-p(X))
// let oY = log(p(Y)/(1-p(Y))
// oX = log( (TT*sigma(oY) + TF*sigma(-oY))/(FT*sigma(oY) + FF*sigma(-oY)) )
// = log( (TT*exp(oY) + TF)/(FT*exp(oY) + FF) )
// = log( (exp(oY) + TF/TT)/(exp(oY) + FF/FT) ) + log(TT/FT)
// ay = log(TF/TT)
// by = log(FF/FT)
// offset = log(TT/FT)
if (probTrue0 == 0 || probTrue1 == 0) throw new ArgumentException("probTrue is zero");
double ay = Math.Log(probTrue0 / probTrue1);
double by = Math.Log((1 - probTrue0) / (1 - probTrue1));
double offset = MMath.Logit(probTrue1);
result.LogOdds = MMath.DiffLogSumExp(choice.LogOdds, ay, by) + offset;
#endif
return result;
}
示例7: ShapeParamsAverageConditional
private static GaussianGamma ShapeParamsAverageConditional(
double coord, double otherCoord, Bernoulli label, GaussianGamma shapeParamsDistr, GaussianGamma otherShapeParamsDistr, GaussianGamma result)
{
GaussianGamma shapeParamsDistrTimesFactor = DistributionTimesFactor(coord, shapeParamsDistr);
GaussianGamma otherShapeParamsDistrTimesFactor = DistributionTimesFactor(otherCoord, otherShapeParamsDistr);
double labelProbFalse = label.GetProbFalse();
double weight1 = labelProbFalse;
double weight2 = Math.Exp(
shapeParamsDistrTimesFactor.GetLogNormalizer() - shapeParamsDistr.GetLogNormalizer() +
otherShapeParamsDistrTimesFactor.GetLogNormalizer() - otherShapeParamsDistr.GetLogNormalizer()) *
(1 - 2 * labelProbFalse);
var projectionOfSum = new GaussianGamma();
projectionOfSum.SetToSum(weight1, shapeParamsDistr, weight2, shapeParamsDistrTimesFactor);
result.SetToRatio(projectionOfSum, shapeParamsDistr);
return result;
}
示例8: LogAverageFactor
public static double LogAverageFactor(Bernoulli label, Vector point, GaussianGamma shapeParamsX, GaussianGamma shapeParamsY)
{
GaussianGamma shapeParamsXDistrTimesFactor = DistributionTimesFactor(point[0], shapeParamsX);
GaussianGamma shapeParamsYDistrTimesFactor = DistributionTimesFactor(point[1], shapeParamsY);
double labelProbFalse = label.GetProbFalse();
double normalizerProduct = Math.Exp(
shapeParamsXDistrTimesFactor.GetLogNormalizer() - shapeParamsX.GetLogNormalizer() +
shapeParamsYDistrTimesFactor.GetLogNormalizer() - shapeParamsY.GetLogNormalizer());
double averageFactor = labelProbFalse + (1 - 2 * labelProbFalse) * normalizerProduct;
Debug.Assert(averageFactor > 0);
return Math.Log(averageFactor);
}
示例9: ShapeAverageConditional
private static Gaussian ShapeAverageConditional(
Vector point, Bernoulli label, Gaussian shapeX, Gaussian shapeY, PositiveDefiniteMatrix shapeOrientation, bool resultForXCoord)
{
if (shapeX.IsPointMass && shapeY.IsPointMass)
{
double labelProbTrue = label.GetProbTrue();
double labelProbFalse = 1.0 - labelProbTrue;
double probDiff = labelProbTrue - labelProbFalse;
Vector shapeLocation = Vector.FromArray(shapeX.Point, shapeY.Point);
Vector diff = point - shapeLocation;
Vector orientationTimesDiff = shapeOrientation * diff;
Matrix orientationTimesDiffOuter = orientationTimesDiff.Outer(orientationTimesDiff);
double factorValue = Math.Exp(-0.5 * shapeOrientation.QuadraticForm(diff));
double funcValue = factorValue * probDiff + labelProbFalse;
Vector dFunc = probDiff * factorValue * orientationTimesDiff;
Vector dLogFunc = 1.0 / funcValue * dFunc;
Matrix ddLogFunc =
((orientationTimesDiffOuter + shapeOrientation) * factorValue * funcValue - orientationTimesDiffOuter * probDiff * factorValue * factorValue)
* (probDiff / (funcValue * funcValue));
double x = resultForXCoord ? shapeX.Point : shapeY.Point;
double d = resultForXCoord ? dLogFunc[0] : dLogFunc[1];
double dd = resultForXCoord ? ddLogFunc[0, 0] : ddLogFunc[1, 1];
return Gaussian.FromDerivatives(x, d, dd, forceProper: true);
}
else if (!shapeX.IsPointMass && !shapeY.IsPointMass)
{
VectorGaussian shapeLocationTimesFactor = ShapeLocationTimesFactor(point, shapeX, shapeY, shapeOrientation);
double labelProbFalse = label.GetProbFalse();
double shapeLocationWeight = labelProbFalse;
double shapeLocationTimesFactorWeight =
Math.Exp(shapeLocationTimesFactor.GetLogNormalizer() - shapeX.GetLogNormalizer() - shapeY.GetLogNormalizer() - 0.5 * shapeOrientation.QuadraticForm(point)) *
(1 - 2 * labelProbFalse);
var projectionOfSum = new Gaussian();
projectionOfSum.SetToSum(
shapeLocationWeight,
resultForXCoord ? shapeX : shapeY,
shapeLocationTimesFactorWeight,
shapeLocationTimesFactor.GetMarginal(resultForXCoord ? 0 : 1));
Gaussian result = new Gaussian();
result.SetToRatio(projectionOfSum, resultForXCoord ? shapeX : shapeY);
return result;
}
else
{
throw new NotSupportedException();
}
}