Example #1
0
 /// <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();
     }
 }
Example #2
0
        /// <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");
        }
Example #3
0
        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);
        }
Example #4
0
 public Prob Div(Prob other)
 {
     return(new LogProb(logProb - other.LogValue));
 }
Example #5
0
 public bool Equals(Prob other)
 {
     return(Value.Equals(other.Value));
 }
Example #6
0
 /// <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();
 }
Example #7
0
 public Prob Div(Prob other)
 {
     return(new DoubleProb(Value / other.Value));
 }
Example #8
0
 public int CompareTo(Prob other)
 {
     return(Value.CompareTo(other.Value));
 }
Example #9
0
 public Prob Mult(Prob other)
 {
     return(new LogProb(logProb + other.LogValue));
 }
Example #10
0
 public Prob Mult(Prob other)
 {
     return(new DoubleProb(Value * other.Value));
 }
Example #11
0
 /// <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)));
 }
Example #12
0
 public bool Equals(Prob other)
 {
     return(other.LogValue == logProb);
 }
Example #13
0
 public int CompareTo(Prob other)
 {
     return(logProb.CompareTo(other.LogValue));
 }
 public ItemProb(A item, Prob prob)
 {
     Item = item;
     Prob = prob;
 }
Example #15
0
 /// <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))));
 }
Example #16
0
 /// <summary>
 /// ItemProb constructor
 /// </summary>
 public static ItemProb <A> ItemProb <A>(A item, Prob prob)
 {
     return(new ItemProb <A>(item, prob));
 }
Example #17
0
 /// <summary>
 /// Bernoulli distribution constructed from success probability
 /// </summary>
 public static Dist <bool> Bernoulli(Prob prob)
 {
     return(Primitive(BernoulliF(prob)));
 }