/// <summary> /// A Bayes sampling function of a generic domain A. A probability function /// is required in order to define the probability of each element. /// </summary> /// <typeparam name="A">The domain of values.</typeparam> /// <param name="p">The probability function over A</param> /// <param name="c">Normalization factor (so that p can map over R+)</param> /// <param name="q">The elements to select from.</param> /// <returns>A sampling function over the given domain.</returns> public static IEnumerator <A> BayesRejectionFunction <A>(Prob <A> p, double c, IEnumerator <A> q) { foreach (var v in unit) { if (v < (p(q.Current) / c)) { yield return(q.Current); } q.MoveNext(); } }
/// <summary> /// Pick a value from a distribution using a probability /// </summary> public static A Pick <A>(this FiniteDist <A> distribution, Prob pickProb) { var probVal = pickProb.Value; foreach (var prob in distribution.Explicit.Weights) { if (probVal < prob.Prob.Value) { return(prob.Item); } probVal -= prob.Prob.Value; } throw new ArgumentException("Sampling failed"); }
public static double IncreaseProb(Prob prob, double change) { double result = prob.Value + change; if (result >= 1) { return(0.96); } if (result <= 0) { return(0.01); } return(result); }
public Prob Div(Prob other) { return(new LogProb(logProb - other.LogValue)); }
public bool Equals(Prob other) { return(Value.Equals(other.Value)); }
/// <summary> /// It builds a distribution given a sampling function. /// </summary> /// <param name="samplingFunction">Sampling function that defines the distribution.</param> /// <param name="d"> /// Probability function associated with the elements (if known). /// If null the identity distribution is assumed. /// </param> public Distribution(IEnumerator <T> samplingFunction, Prob <T> d) { _density = d; _samplingFunction = samplingFunction; _samplingFunction.MoveNext(); }
public Prob Div(Prob other) { return(new DoubleProb(Value / other.Value)); }
public int CompareTo(Prob other) { return(Value.CompareTo(other.Value)); }
public Prob Mult(Prob other) { return(new LogProb(logProb + other.LogValue)); }
public Prob Mult(Prob other) { return(new DoubleProb(Value * other.Value)); }
/// <summary> /// Bayes distribution based on the rejection method. /// </summary> /// <typeparam name="A">The domain of values.</typeparam> /// <param name="p">The probability function over A</param> /// <param name="c">Normalization factor (so that p can map over R+)</param> /// <param name="q">The elements to select from.</param> /// <returns>A distribution object that represents the Bayes distribution over a domain A.</returns> public static Distribution <A> BayesRejection <A>(Prob <A> p, double c, IEnumerator <A> q) { return(new Distribution <A>(BayesRejectionFunction <A>(p, c, q))); }
public bool Equals(Prob other) { return(other.LogValue == logProb); }
public int CompareTo(Prob other) { return(logProb.CompareTo(other.LogValue)); }
public ItemProb(A item, Prob prob) { Item = item; Prob = prob; }
/// <summary> /// Bernoulli distribution constructed from success probability /// Only composable with other finite distributions. /// </summary> public static FiniteDist <bool> BernoulliF(Prob prob) { return(new FiniteDist <bool>(ItemProb(true, prob), ItemProb(false, Prob(1 - prob.Value)))); }
/// <summary> /// ItemProb constructor /// </summary> public static ItemProb <A> ItemProb <A>(A item, Prob prob) { return(new ItemProb <A>(item, prob)); }
/// <summary> /// Bernoulli distribution constructed from success probability /// </summary> public static Dist <bool> Bernoulli(Prob prob) { return(Primitive(BernoulliF(prob))); }