本文整理汇总了C#中Beta.GetMean方法的典型用法代码示例。如果您正苦于以下问题:C# Beta.GetMean方法的具体用法?C# Beta.GetMean怎么用?C# Beta.GetMean使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Beta
的用法示例。
在下文中一共展示了Beta.GetMean方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: drawBetaDistribution
private static void drawBetaDistribution(Rectangle rect, Beta dist)
{
GradientStopCollection gsc = new GradientStopCollection();
int numStops = 21;
double mean = dist.GetMean();
double meanDensity = Math.Exp(dist.GetLogProb(mean));
double inc = 1.0 / (numStops-1);
double curr = 0.0;
double maxLogProb = Double.MinValue;
double minLogProb = -5.0;
for (int i=0; i < numStops; i++)
{
double logProb = dist.GetLogProb(curr);
if (logProb > maxLogProb) maxLogProb = logProb;
curr += inc;
}
if (maxLogProb <= minLogProb)
maxLogProb = minLogProb + 1.0;
double diff = maxLogProb - minLogProb;
double mult = 1.0 / (maxLogProb - minLogProb);
curr = 0.0;
double blueLeft = 0; double blueRight = 0;
double redLeft = 255; double redRight = 255;
double greenLeft = 255; double greenRight = 255;
for (int i=0; i < numStops; i++)
{
double red, green, blue;
double logProb = dist.GetLogProb(curr);
if (logProb < minLogProb) logProb = minLogProb;
double level = mult * (logProb - minLogProb);
red = level * (mean * redRight + (1.0 - mean) * redLeft);
green = level * (mean * greenRight + (1.0 - mean) * greenLeft);
blue =level * (mean * blueRight + (1.0 - mean) * blueLeft);
byte redb = red < 0.0 ? (byte)0 : red > 255.0 ? (byte)255 : (byte)red;
byte greenb = green < 0.0 ? (byte)0 : green > 255.0 ? (byte)255 : (byte)green;
byte blueb = blue < 0.0 ? (byte)0 : blue > 255.0 ? (byte)255 : (byte)blue;
gsc.Add(new GradientStop { Color =new Color() { A = 255, R = redb, G = greenb, B = blueb } , Offset = curr});
curr += inc;
}
LinearGradientBrush brush = rect.Fill as LinearGradientBrush;
brush.GradientStops = gsc;
}
示例2: ProbTrueAverageConditional
/// <summary>
/// EP message to 'probTrue'
/// </summary>
/// <param name="sample">Incoming message from 'sample'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="probTrue">Incoming message from 'probTrue'.</param>
/// <returns>The outgoing EP message to the 'probTrue' argument</returns>
/// <remarks><para>
/// The outgoing message is a distribution matching the moments of 'probTrue' as the random arguments are varied.
/// The formula is <c>proj[p(probTrue) sum_(sample) p(sample) factor(sample,probTrue)]/p(probTrue)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="sample"/> is not a proper distribution</exception>
public static Beta ProbTrueAverageConditional([SkipIfUniform] Bernoulli sample, Beta probTrue)
{
// this code is similar to DiscreteFromDirichletOp.PAverageConditional()
if (probTrue.IsPointMass) {
return Beta.Uniform();
}
if (sample.IsPointMass) {
// shortcut
return ProbTrueConditional(sample.Point);
}
if (!probTrue.IsProper()) throw new ImproperMessageException(probTrue);
// q(x) is the distribution stored in this.X.
// q(p) is the distribution stored in this.P.
// f(x,p) is the factor.
// Z = sum_x q(x) int_p f(x,p)*q(p) = q(false)*E[1-p] + q(true)*E[p]
// Ef[p] = 1/Z sum_x q(x) int_p p*f(x,p)*q(p) = 1/Z (q(false)*E[p(1-p)] + q(true)*E[p^2])
// Ef[p^2] = 1/Z sum_x q(x) int_p p^2*f(x,p)*q(p) = 1/Z (q(false)*E[p^2(1-p)] + q(true)*E[p^3])
// var_f(p) = Ef[p^2] - Ef[p]^2
double mo = probTrue.GetMean();
double m2o = probTrue.GetMeanSquare();
double pT = sample.GetProbTrue();
double pF = sample.GetProbFalse();
double Z = pF * (1 - mo) + pT * mo;
double m = pF * (mo - m2o) + pT * m2o;
m = m / Z;
if (!Beta.AllowImproperSum) {
if (pT < 0.5) {
double inc = probTrue.TotalCount * (mo / m - 1);
return new Beta(1, 1 + inc);
} else {
double inc = probTrue.TotalCount * ((1 - mo) / (1 - m) - 1);
return new Beta(1 + inc, 1);
}
} else {
double m3o = probTrue.GetMeanCube();
double m2 = pF * (m2o - m3o) + pT * m3o;
m2 = m2 / Z;
Beta result = Beta.FromMeanAndVariance(m, m2 - m * m);
result.SetToRatio(result, probTrue);
return result;
}
}
示例3: AddAnalyticComponent
// Helper function to add the removed parts back (see note)
private static void AddAnalyticComponent(
Beta meanQ,
double ELogS,
double ES,
double ESLogS,
ref double EELogMLogGamma,
ref double EELogOneMinusMLogGamma)
{
double ELogM, ELogOneMinusM;
meanQ.GetMeanLogs(out ELogM, out ELogOneMinusM);
double ELogMSquared = ELogM * ELogM
+ MMath.Trigamma(meanQ.TrueCount) - MMath.Trigamma(meanQ.TotalCount);
EELogMLogGamma -= ELogS * ELogM + ELogMSquared;
double Em = meanQ.GetMean();
double am = meanQ.TrueCount;
double bm = meanQ.FalseCount;
double EmlogOneMinusM = Em * (MMath.Digamma(bm) - MMath.Digamma(am + bm + 1));
double EmlogmlogOneMinusM = Em * ((MMath.Digamma(bm) - MMath.Digamma(am + bm + 1)) * (MMath.Digamma(am + 1)
- MMath.Digamma(am + bm + 1)) - MMath.Trigamma(am + bm + 1));
double ELogMLogOneMinusM = ELogM * ELogOneMinusM - MMath.Trigamma(meanQ.TotalCount);
EELogOneMinusMLogGamma += .5 * Math.Log(2 * Math.PI) * ELogOneMinusM - .5 * ELogS * ELogOneMinusM
- .5 * ELogMLogOneMinusM + EmlogOneMinusM * ESLogS
+ ES * EmlogmlogOneMinusM - ES * EmlogOneMinusM;
}