/// <summary> /// Generates a histogram from some samples, grouping by a show function. /// </summary> /// <param name="scale">Scale factor. Defaults to 40</param> public static string Finite <A>(Samples <A> itemProbs, Func <A, string> showFunc = null, double scale = DEFAULT_SCALE) { if (!itemProbs.Weights.Any()) { return("No data to graph."); } if (showFunc == null) { showFunc = a => a.ToString(); } var normalized = Importance.Normalize(CompactUnordered(itemProbs, showFunc)); var sb = new StringBuilder(); var display = normalized.Weights.Select(ip => new Tuple <string, int, Prob>(showFunc(ip.Item), (int)Math.Floor(ip.Prob.Value * scale), ip.Prob)); var maxWidth = display.Select(d => d.Item1.Length).Max(); var barScale = BarScale(display.Select(d => d.Item2), scale); foreach (var line in display) { var item = line.Item1; var barLength = (int)(line.Item2 * barScale); sb.AppendLine($"{item.PadLeft(maxWidth)} {line.Item3,6} {Bar(barLength)}"); } return(sb.ToString()); }
/// <summary> /// Finite uniform distribution over list of items. /// Only composable with other finite distributions. /// </summary> public static FiniteDist <A> EnumUniformF <A>(IEnumerable <A> items) { var uniform = Samples(items.Select(i => new ItemProb <A>(i, Prob(1)))); return(new FiniteDist <A>(Importance.Normalize(uniform))); }
public static Samples <A> Normalize <A>(this Samples <A> self) { return(Importance.Normalize(self)); }
/// <summary> /// Aggregate & normalize samples /// The samples are arranged in ascending order /// </summary> public static Samples <A> Enumerate <A, Key>(FiniteDist <A> dist, Func <A, Key> keyFunc) where A : IComparable <A> { return(Importance.Normalize(Compact(dist.Explicit, keyFunc))); }