/// <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)))); }
/// <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))))); }
/// <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)); }
/// <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)); }
public static Samples <A> Normalize <A>(this Samples <A> self) { return(Importance.Normalize(self)); }
/// <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))); }
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)))); }
/// <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())); }
public static Samples <B> Select <A, B>(this Samples <A> self, Func <ItemProb <A>, ItemProb <B> > f) { return(Samples(self.Weights.Select(f))); }
/// <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))); }
/// <summary> /// Categorical distribution /// </summary> public static Dist <A> Categorical <A>(Samples <A> samples) { return(Primitive(CategoricalF(samples).ToSampleDist())); }
/// <summary> /// Categorical distribution /// Only composable with other finite distributions /// </summary> public static FiniteDist <A> CategoricalF <A>(Samples <A> samples) { return(new FiniteDist <A>(samples)); }
public FiniteDist(Samples <A> samples) { Explicit = samples; }