本文整理汇总了C#中Discrete.GetWorkspace方法的典型用法代码示例。如果您正苦于以下问题:C# Discrete.GetWorkspace方法的具体用法?C# Discrete.GetWorkspace怎么用?C# Discrete.GetWorkspace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Discrete
的用法示例。
在下文中一共展示了Discrete.GetWorkspace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SampleAverageConditional
/// <summary>
/// EP message to 'sample'.
/// </summary>
/// <param name="selector">Incoming message from 'selector'.</param>
/// <param name="probs">Constant value for 'probs'.</param>
/// <param name="result">Modified to contain the outgoing message.</param>
/// <returns><paramref name="result"/></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 = (selector,probs)</c>.
/// </para></remarks>
public static Discrete SampleAverageConditional(Discrete selector, Matrix probs, Discrete result)
{
Vector v = result.GetWorkspace();
v.SetToProduct(selector.GetProbs(), probs);
result.SetProbs(v);
return result;
}
示例2: SelectorAverageConditional
/// <summary>
/// EP message to 'selector'.
/// </summary>
/// <param name="sample">Incoming message from 'sample'.</param>
/// <param name="probs">Constant value for 'probs'.</param>
/// <param name="result">Modified to contain the outgoing message.</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is the integral of the factor times incoming messages, over all arguments except 'selector'.
/// The formula is <c>int f(selector,x) q(x) dx</c> where <c>x = (sample,probs)</c>.
/// </para></remarks>
public static Discrete SelectorAverageConditional(Discrete sample, Matrix probs, Discrete result)
{
Vector v = result.GetWorkspace();
v.SetToProduct(probs, sample.GetProbs());
result.SetProbs(v);
return result;
}
示例3: IndexAverageConditional
/// <summary>
/// EP message to 'index'.
/// </summary>
/// <param name="sample">Incoming message from 'sample'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="ProbTrue">Constant value for 'probTrue'.</param>
/// <param name="result">Modified to contain the outgoing message.</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is the integral of the factor times incoming messages, over all arguments except 'index'.
/// The formula is <c>int f(index,x) q(x) dx</c> where <c>x = (sample,probTrue)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="sample"/> is not a proper distribution</exception>
public static Discrete IndexAverageConditional([SkipIfUniform] Bernoulli sample, double[] ProbTrue, Discrete result)
{
if (result == default(Discrete)) result = Discrete.Uniform(ProbTrue.Length);
// p(Y) = ProbTrue[Y]*p(X=true) + (1-ProbTrue[Y])*p(X=false)
Vector probs = result.GetWorkspace();
double p = sample.GetProbTrue();
probs.SetTo(ProbTrue);
probs.SetToProduct(probs, 2.0 * p - 1.0);
probs.SetToSum(probs, 1.0 - p);
result.SetProbs(probs);
return result;
}
示例4: AAverageLogarithm
/// <summary>
/// VMP message to 'a'
/// </summary>
/// <param name="isGreaterThan">Incoming message from 'isGreaterThan'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="b">Incoming message from 'b'.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></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, 'isGreaterThan' is integrated out before taking the logarithm.
/// The formula is <c>exp(sum_(b) p(b) log(sum_isGreaterThan p(isGreaterThan) factor(isGreaterThan,a,b)))</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="isGreaterThan"/> is not a proper distribution</exception>
public static Discrete AAverageLogarithm([SkipIfUniform] Bernoulli isGreaterThan, Discrete b, Discrete result)
{
if (b.IsPointMass) return AAverageLogarithm(isGreaterThan, b.Point, result);
if (isGreaterThan.IsPointMass) return AAverageLogarithm(isGreaterThan.Point, b, result);
// f(a,b) = p(c=1) I(a > b) + p(c=0) I(a <= b)
// message to a = exp(sum_b q(b) log f(a,b))
Vector aProbs = result.GetWorkspace();
double logProbTrue = isGreaterThan.GetLogProbTrue();
double logProbFalse = isGreaterThan.GetLogProbFalse();
for (int i = 0; i < aProbs.Count; i++) {
double sum = 0.0;
int j = 0;
for (; (j < i) && (j < b.Dimension); j++) {
sum += logProbTrue*b[j];
}
for (; j < b.Dimension; j++) {
sum += logProbFalse*b[j];
}
aProbs[i] = Math.Exp(sum);
}
result.SetProbs(aProbs);
return result;
}
示例5: BAverageConditional
/// <summary>
/// EP message to 'b'
/// </summary>
/// <param name="isGreaterThan">Incoming message from 'isGreaterThan'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="a">Incoming message from 'a'.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is a distribution matching the moments of 'b' as the random arguments are varied.
/// The formula is <c>proj[p(b) sum_(isGreaterThan,a) p(isGreaterThan,a) factor(isGreaterThan,a,b)]/p(b)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="isGreaterThan"/> is not a proper distribution</exception>
static public Discrete BAverageConditional([SkipIfUniform] Bernoulli isGreaterThan, Discrete a, Discrete result)
{
if (a.IsPointMass) return BAverageConditional(isGreaterThan, a.Point, result);
Vector bProbs = result.GetWorkspace();
double probTrue = isGreaterThan.GetProbTrue();
double probFalse = 1 - probTrue;
for (int j = 0; j < bProbs.Count; j++) {
double sum0 = 0.0;
int i = 0;
for (; (i <= j) && (i < a.Dimension); i++) {
sum0 += a[i];
}
double sum1 = 0.0;
for (; i < a.Dimension; i++) {
sum1 += a[i];
}
bProbs[j] = probTrue*sum1 + probFalse*sum0;
}
result.SetProbs(bProbs);
return result;
}
示例6: AAverageConditional
/// <summary>
/// EP message to 'a'
/// </summary>
/// <param name="isGreaterThan">Incoming message from 'isGreaterThan'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="b">Incoming message from 'b'.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is a distribution matching the moments of 'a' as the random arguments are varied.
/// The formula is <c>proj[p(a) sum_(isGreaterThan,b) p(isGreaterThan,b) factor(isGreaterThan,a,b)]/p(a)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="isGreaterThan"/> is not a proper distribution</exception>
static public Discrete AAverageConditional([SkipIfUniform] Bernoulli isGreaterThan, Discrete b, Discrete result)
{
if (b.IsPointMass) return AAverageConditional(isGreaterThan, b.Point, result);
Vector aProbs = result.GetWorkspace();
double probTrue = isGreaterThan.GetProbTrue();
double probFalse = 1 - probTrue;
for (int i = 0; i < aProbs.Count; i++) {
double sum1 = 0.0;
int j = 0;
for (; (j < i) && (j < b.Dimension); j++) {
sum1 += b[j];
}
double sum0 = 0.0;
for (; j < b.Dimension; j++) {
sum0 += b[j];
}
aProbs[i] = probTrue*sum1 + probFalse*sum0;
}
result.SetProbs(aProbs);
return result;
}
示例7: SizeAverageConditional
/// <summary>
/// EP message to 'size'
/// </summary>
/// <param name="sample">Constant value for 'sample'.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is the factor viewed as a function of 'size' conditioned on the given values.
/// </para></remarks>
public static Discrete SizeAverageConditional(int sample, Discrete result)
{
Vector probs = result.GetWorkspace();
for (int size = 0; size <= sample; size++)
{
probs[size] = 0.0;
}
for (int size = sample+1; size < probs.Count; size++)
{
probs[size] = 1.0/size;
}
result.SetProbs(probs);
return result;
}
示例8: SampleAverageConditional
/// <summary>
/// EP message to 'sample'
/// </summary>
/// <param name="size">Constant value for 'size'.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is the factor viewed as a function of 'sample' conditioned on the given values.
/// </para></remarks>
public static Discrete SampleAverageConditional(int size, Discrete result)
{
if (size == 0) return result; //throw new AllZeroException();
if (result.Dimension < size) throw new ArgumentException("result.Dimension ("+result.Dimension+") < size ("+size+")");
Vector probs = result.GetWorkspace();
double invSize = 1.0/size;
for (int i = 0; i < size; i++)
{
probs[i] = invSize;
}
for (int i = size; i < probs.Count; i++)
{
probs[i] = 0.0;
}
result.SetProbs(probs);
return result;
}
示例9: AAverageConditional
/// <summary>
/// EP message to 'a'.
/// </summary>
/// <param name="areEqual">Constant value for 'areEqual'.</param>
/// <param name="B">Incoming message from 'b'.</param>
/// <param name="result">Modified to contain the outgoing message.</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is the integral of the factor times incoming messages, over all arguments except 'a'.
/// The formula is <c>int f(a,x) q(x) dx</c> where <c>x = (areEqual,b)</c>.
/// </para></remarks>
public static Discrete AAverageConditional(bool areEqual, Discrete B, Discrete result)
{
if (B.IsPointMass) return AAverageConditional(areEqual, B.Point, result);
if (result == default(Discrete)) result = Distributions.Discrete.Uniform(B.Dimension, B.Sparsity);
if (areEqual) result.SetTo(B);
else {
Vector probs = result.GetWorkspace();
probs = B.GetProbs(probs);
probs.SetToDifference(1.0, probs);
result.SetProbs(probs);
}
return result;
}
示例10: SampleAverageConditional
/// <summary>
/// EP message to 'sample'
/// </summary>
/// <param name="p">Incoming message from 'p'.</param>
/// <param name="trialCount">Constant value for 'trialCount'.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></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_(p) p(p) factor(sample,trialCount,p)]/p(sample)</c>.
/// </para></remarks>
public static Discrete SampleAverageConditional(Beta p, int trialCount, Discrete result)
{
if (p.IsPointMass) return SampleAverageConditional(p.Point, trialCount, result);
// result must range from 0 to n
if (result.Dimension < trialCount+1) throw new ArgumentException("result.Dimension ("+result.Dimension+") < n+1 ("+trialCount+"+1)");
Vector probs = result.GetWorkspace();
double a = p.TrueCount;
double b = p.FalseCount;
probs.SetAllElementsTo(0.0);
double max = double.NegativeInfinity;
for (int k = 0; k <= trialCount; k++) {
double logProb = MMath.GammaLn(a+k)-MMath.GammaLn(1+k) + MMath.GammaLn(b+trialCount-k)-MMath.GammaLn(1+trialCount-k);
probs[k] = logProb;
if (logProb > max) max = logProb;
}
for (int k = 0; k <= trialCount; k++) {
probs[k] = Math.Exp(probs[k] - max);
}
result.SetProbs(probs);
return result;
}
示例11: SampleAverageLogarithm
/// <summary>
/// VMP message to 'sample'
/// </summary>
/// <param name="probs">Incoming message from 'probs'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except 'sample'.
/// The formula is <c>exp(sum_(probs) p(probs) log(factor(sample,probs)))</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="probs"/> is not a proper distribution</exception>
public static Discrete SampleAverageLogarithm([SkipIfUniform] Dirichlet probs, Discrete result)
{
// E[sum_k I(X=k) log(P[k])] = sum_k I(X=k) E[log(P[k])]
Vector p = probs.GetMeanLog(result.GetWorkspace());
double max = p.Max();
p.SetToFunction(p, x => Math.Exp(x - max));
result.SetProbs(p);
return result;
}
示例12: SampleAverageConditional
/// <summary>
/// EP message to 'sample'
/// </summary>
/// <param name="probs">Incoming message from 'probs'. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></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_(probs) p(probs) factor(sample,probs)]/p(sample)</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="probs"/> is not a proper distribution</exception>
public static Discrete SampleAverageConditional([SkipIfUniform] Dirichlet probs, Discrete result)
{
result.SetProbs(probs.GetMean(result.GetWorkspace()));
return result;
}
示例13: IndexAverageLogarithm
/// <summary>
/// VMP message to 'index'.
/// </summary>
/// <param name="sample">Incoming message from 'sample'.</param>
/// <param name="ProbTrue">Constant value for 'probTrue'.</param>
/// <param name="result">Modified to contain the outgoing message.</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is the exponential of the integral of the log-factor times incoming messages, over all arguments except 'index'.
/// The formula is <c>int log(f(index,x)) q(x) dx</c> where <c>x = (sample,probTrue)</c>.
/// </para></remarks>
public static Discrete IndexAverageLogarithm(Bernoulli sample, double[] ProbTrue, Discrete result)
{
if (result == default(Discrete)) result = Discrete.Uniform(ProbTrue.Length);
// E[sum_k I(Y=k) (X*log(ProbTrue[k]) + (1-X)*log(1-ProbTrue[k]))]
// = sum_k I(Y=k) (p(X=true)*log(ProbTrue[k]) + p(X=false)*log(1-ProbTrue[k]))
// p(Y=k) =propto ProbTrue[k]^p(X=true) (1-ProbTrue[k])^p(X=false)
Vector probs = result.GetWorkspace();
double p = sample.GetProbTrue();
probs.SetTo(ProbTrue);
probs.SetToFunction(probs, x => Math.Pow(x, p) * Math.Pow(1.0 - x, 1.0 - p));
result.SetProbs(probs);
return result;
}
示例14: TrialCountAverageConditional
/// <summary>
/// EP message to 'trialCount'
/// </summary>
/// <param name="sample">Incoming message from 'sample'.</param>
/// <param name="p">Constant value for 'p'.</param>
/// <param name="result">Modified to contain the outgoing message</param>
/// <returns><paramref name="result"/></returns>
/// <remarks><para>
/// The outgoing message is a distribution matching the moments of 'trialCount' as the random arguments are varied.
/// The formula is <c>proj[p(trialCount) sum_(sample) p(sample) factor(sample,trialCount,p)]/p(trialCount)</c>.
/// </para></remarks>
public static Discrete TrialCountAverageConditional(Discrete sample, double p, Discrete result)
{
if (sample.IsPointMass) return TrialCountAverageConditional(sample.Point, p, result);
// n must range from 0 to sampleMax
if (result.Dimension < sample.Dimension) throw new ArgumentException("result.Dimension ("+result.Dimension+") < sample.Dimension ("+sample.Dimension+")");
Vector probs = result.GetWorkspace();
double logp = Math.Log(p);
double log1minusp = Math.Log(1-p);
// p(n) = sum_(k<=n) p(k) nchoosek(n,k) p^k (1-p)^(n-k)
for (int n = 0; n < result.Dimension; n++)
{
double s = 0.0;
for (int k = 0; k <= n; k++)
{
s += sample[k] * Math.Exp(MMath.ChooseLn(n, k) + k*(logp - log1minusp));
}
probs[n] = Math.Exp(n*log1minusp)*s;
}
result.SetProbs(probs);
return result;
}
示例15: AAverageLogarithm
/// <summary>
/// VMP message to 'a'.
/// </summary>
/// <param name="areEqual">Incoming message from 'areEqual'. Must be a proper distribution. If uniform, the result will be uniform.</param>
/// <param name="B">Incoming message from 'b'.</param>
/// <param name="result">Modified to contain the outgoing message.</param>
/// <returns><paramref name="result"/></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, 'areEqual' is integrated out before taking the logarithm.
/// The formula is <c>exp(sum_(b) p(b) log(sum_areEqual p(areEqual) factor(areEqual,a,b)))</c>.
/// </para></remarks>
/// <exception cref="ImproperMessageException"><paramref name="areEqual"/> is not a proper distribution</exception>
public static Discrete AAverageLogarithm([SkipIfUniform] Bernoulli areEqual, Discrete B, Discrete result)
{
if (areEqual.IsPointMass) return AAverageLogarithm(areEqual.Point, B, result);
if (result == default(Discrete)) result = Discrete.Uniform(B.Dimension, B.Sparsity);
// when AreEqual is marginalized, the factor is proportional to exp((A==B)*areEqual.LogOdds)
Vector probs = result.GetWorkspace();
probs = B.GetProbs(probs);
probs.SetToFunction(probs, x => Math.Exp(x * areEqual.LogOdds));
result.SetProbs(probs);
return result;
}