コード例 #1
0
 /// <summary>
 /// Flattens nested samples
 /// </summary>
 public static Samples <A> Flatten <A>(Samples <Samples <A> > samples)
 {
     return(Samples(from outer in Normalize(samples).Weights
                    from inner in outer.Item.Weights
                    select ItemProb(inner.Item, inner.Prob.Mult(outer.Prob))));
 }
コード例 #2
0
        /// <summary>
        /// Normalizes a list of samples
        /// </summary>
        public static Samples <A> Normalize <A>(Samples <A> samples)
        {
            var normConst = samples.SumProbs();

            return(Samples(samples.Weights.Select(s => ItemProb(s.Item, s.Prob.Div(normConst)))));
        }
コード例 #3
0
 /// <summary>
 /// Generates a weighted histogram from some samples
 /// </summary>
 /// <param name="scale">Scale factor. Defaults to 40</param>
 public static string Weighted(Samples <double> nums, int numBuckets = 10, double scale = DEFAULT_SCALE)
 {
     return(Weighted(nums.Weights, numBuckets, scale));
 }
コード例 #4
0
        /// <summary>
        /// Calculates KL divergence from list of samples and a finite distribution
        /// </summary>
        /// <param name="keyFunc">Groups identical samples</param>
        public static double KLDivergence <A, Key>(Samples <A> samples, FiniteDist <A> dist, Func <A, Key> keyFunc) where A : IComparable <A>
        {
            var sampleDist = CategoricalF(samples);

            return(KLDivergenceF(sampleDist, dist, keyFunc));
        }
コード例 #5
0
 public static Samples <A> Normalize <A>(this Samples <A> self)
 {
     return(Importance.Normalize(self));
 }
コード例 #6
0
 /// <summary>
 /// Unzip samples into items and weights
 /// </summary>
 public static Tuple <IEnumerable <A>, IEnumerable <Prob> > Unzip <A>(this Samples <A> samples)
 {
     return(new Tuple <IEnumerable <A>, IEnumerable <Prob> >(samples.Weights.Select(ip => ip.Item), samples.Weights.Select(ip => ip.Prob)));
 }
コード例 #7
0
 public static Samples <B> MapSample <A, B>(this Samples <A> self, Func <A, B> f)
 {
     return(Samples(self.Weights.Select(ip => ItemProb(f(ip.Item), ip.Prob))));
 }
コード例 #8
0
 /// <summary>
 /// Sum all probabilities in samples
 /// </summary>
 public static Prob SumProbs <A>(this Samples <A> self)
 {
     return(Prob(self.Weights.Select(ip => ip.Prob.Value).Sum()));
 }
コード例 #9
0
 public static Samples <B> Select <A, B>(this Samples <A> self, Func <ItemProb <A>, ItemProb <B> > f)
 {
     return(Samples(self.Weights.Select(f)));
 }
コード例 #10
0
 /// <summary>
 /// Aggregates probabilities of samples with identical values
 /// The samples are arranged in ascending order
 /// </summary>
 /// <param name="keyFunc">Used to identify identical values</param>
 public static Samples <A> Compact <A, Key>(Samples <A> samples, Func <A, Key> keyFunc) where A : IComparable <A>
 {
     return(Samples(CompactUnordered(samples, keyFunc).Weights.OrderBy(w => w.Item)));
 }
コード例 #11
0
 /// <summary>
 /// Categorical distribution
 /// </summary>
 public static Dist <A> Categorical <A>(Samples <A> samples)
 {
     return(Primitive(CategoricalF(samples).ToSampleDist()));
 }
コード例 #12
0
 /// <summary>
 /// Categorical distribution
 /// Only composable with other finite distributions
 /// </summary>
 public static FiniteDist <A> CategoricalF <A>(Samples <A> samples)
 {
     return(new FiniteDist <A>(samples));
 }
コード例 #13
0
 public FiniteDist(Samples <A> samples)
 {
     Explicit = samples;
 }