public static MP Expected <M, R, MP, RF>(this IDensity <IModuleWithExtension <M, R, MP, RF>, M, RF> d) { var g = d.BaseStructure; var embeddedDensity = d.EmbedTo <M, R, MP, RF>(); return(embeddedDensity.Expected()); }
public static IList <M> SortedKeys <G, M, RF>(this IDensity <G, M, RF> d) where G : IBaseStructure <M>, IComparer <M> { return(d.GetKeys().OrderBy(k => k, d.BaseStructure).ToList()); }
public static IDensity <S, N, RF> Op <G, M, S, N, RF>(this IDensity <G, M, RF> d, S newBaseStructure, Func <M, N> op, Func <string, string> opStrFunc = null) { return(Op(d, newBaseStructure, op, opStrFunc)); }
public static PlotModel OxyPlot <G, M, RF>(this IDensity <G, M, RF> d, string title = null) where G : IBaseStructure <M>, IRealEmbedding <M, RF> where M : struct where RF : struct { var oxyplotter = new OxyPlotter <G, M, RealFieldType <RF>, RF>(d.BaseStructure, new RealFieldType <RF>()); var rf = d.RealField; title = title ?? d.GetTrimmedName(); // TODO: For Expected() and Stdev() much more assumptions are necessary! // var subtitle = string.Format( // CultureInfo.InvariantCulture, // "Expected = {0:0.0000}, Stdev = {1:0.00}", // d.Expected(), // d.Stdev() // ); var subtitle = ""; var plotModel = oxyplotter.GetPlot( k => d[k], d.SortedKeys(), title: title, subtitle: subtitle, yMin: rf.EmbedFromReal(0.0), valueLabelFormatter: p => rf.EmbedToReal(p).ToString("P", CultureInfo.InvariantCulture)); return(plotModel); }
public static RF Prob(IDensity <G, M, RF> d, Func <M, bool> cond) { var rf = d.RealField; var resSum = rf.Sum(d.GetKeys().Where(cond).Select(k => d[k])); return(resSum); }
public static IList <RF> SortedValues <G, M, RF>(this IDensity <G, M, RF> d) where G : IBaseStructure <M>, IComparer <M> { return(d.SortedKeys().Select(k => d[k]).ToList()); }
public bool Equals(IDensity <G, M, RF> other) { var isNull = object.ReferenceEquals(other, null); if (isNull) { return(false); } var countsDiffer = Dictionary.Count != other.Dictionary.Count; if (countsDiffer) { return(false); } var keysDiffer = Dictionary.Keys.Except(other.Dictionary.Keys).Any(); if (keysDiffer) { return(false); } var valuesDiffer = Dictionary.Values.Except(other.Dictionary.Values).Any(); if (valuesDiffer) { return(false); } return(true); }
public static IDensity <G, M, RF> Negate <G, M, RF>(this IDensity <G, M, RF> d) where G : IAdditiveGroup <M> { var g = d.BaseStructure; return(d.Op(g, a => g.Negate(a), s => $"(-{s})")); }
public static IDensity <G, M, RF> Subtract <G, M, RF>(this IDensity <G, M, RF> d, IDensity <G, M, RF> d2) where G : IAdditiveGroup <M> { var g = d.BaseStructure; return(Density <G, M, RF> .BinaryOp <G, M>(g, d, d2, (a, b) => g.Subtract(a, b), (s, t) => $"({s}-{t})")); }
public static IDensity <G, M, RF> Divide <G, M, RF>(this IDensity <G, M, RF> d, IDensity <G, M, RF> d2) where G : IMultiplicativeGroup <M> { var g = d.BaseStructure; return(Density <G, M, RF> .BinaryOp <G, M>(g, d, d2, (a, b) => g.Divide(a, b), (s, t) => $"({s}/{t})")); }
public static RF EqProb <G, M, RF>(this IDensity <G, M, RF> d, IDensity <G, M, RF> d2) where G : IBaseStructure <M> { var g = d.BaseStructure; return(Density <G, M, RF> .BinaryProb(d, d2, (a, b) => g.Equals(a, b))); }
public static IDensity <G, M, RF> Add <G, M, RF>(this IDensity <G, M, RF> d, IDensity <G, M, RF> d2) where G : IAdditiveMonoid <M> { var g = d.BaseStructure; return(Density <G, M, RF> .BinaryOp <G, M>(g, d, d2, (a, b) => g.Add(a, b), (s, t) => $"({s}+{t})")); }
public static MP Expected <MP, RF>(this IDensity <IRealVectorspace <MP, RF>, MP, RF> d) { var gp = d.BaseStructure; var grp = gp.BaseRealField; var expected = gp.Sum(d.Dictionary.Select(p => gp.ScalarMult(p.Value, p.Key))); return(expected); }
public static IDensity <G, M, RF> Multiply <G, M, RF>(this IDensity <G, M, RF> d, IDensity <G, M, RF> d2) where G : IMultiplicativeMonoid <M> { var g = d.BaseStructure; return(Density <G, M, RF> .BinaryOp <G, M>(g, d, d2, (a, b) => g.Multiply(a, b), (s, t) => $"({s}*{t})")); }
public static MultiDensity <G, M, RF> AsMultiDensity <G, M, RF>(this IDensity <G, M, RF> d, int n) where G : IAdditiveMonoid <M> { var dList = Enumerable.Repeat(d, n).ToList(); var multiDensity = new MultiDensity <G, M, RF>(dList); return(multiDensity); }
public static RF GeqProb <G, M, RF>(this IDensity <G, M, RF> d, IDensity <G, M, RF> d2) where G : IBaseStructure <M>, IComparer <M> { var g = d.BaseStructure; return(Density <G, M, RF> .BinaryProb(d, d2, (a, b) => g.Compare(a, b) >= 0)); }
public static string GetTrimmedName <G, M, RF>(this IDensity <G, M, RF> d) { var trimmedName = d.Name; while (trimmedName.StartsWith("(") && trimmedName.EndsWith(")")) { trimmedName = trimmedName.Substring(1, Math.Max(1, trimmedName.Length - 2)); } return(trimmedName); }
public static RF Cdf <G, M, RF>(this IDensity <G, M, RF> d, RF x) where G : IBaseStructure <M>, IComparer <M>, IRealEmbedding <M, RF> { var g = d.BaseStructure; var rf = d.RealField; return(d.Prob(a => rf.Compare(g.EmbedToReal(a), x) <= 0)); }
public static IDensity <S, N, RF> Op <S, N>( IDensity <G, M, RF> d, S newBaseStructure, Func <M, N> op, Func <string, string> opStrFunc = null) { var resDensity = GetOpDictionary(d, op); var name = opStrFunc == null ? DefaultName : opStrFunc(d.Name); return(new Density <S, N, RF>(resDensity, newBaseStructure, d.RealField, name)); }
public static RF Variance <MP, RF>(this IDensity <IRealVectorspace <MP, RF>, MP, RF> d) { var gp = d.BaseStructure; var grp = gp.BaseRealField; var expected = d.Expected <MP, RF>(); Func <MP, RF> normFunc = mp => grp.ScalarPow(gp.Norm(gp.Subtract(expected, mp)), 2); var squaredDistances = d.Dictionary .Select(p => grp.Multiply(p.Value, normFunc(p.Key))) .ToList(); var variance = grp.Sum(squaredDistances); return(variance); }
public static IDensity <G, M, RF> ConditionalDensity( IDensity <G, M, RF> d, Func <M, bool> cond, Func <string, string> condStrFunc = null) { var resDict = GetConditionalDensityDictionary(d, cond); var name = condStrFunc != null ? condStrFunc(d.Name) : DefaultName; var newDensity = new Density <G, M, RF>(resDict, d.BaseStructure, d.RealField, name); return(newDensity); }
public static IDensity <S, N, RF> BinaryOp <S, N>( S newBaseStructure, IDensity <G, M, RF> d1, IDensity <G, M, RF> d2, Func <M, M, N> op, Func <string, string, string> binOpStrFunc = null) where S : IBaseStructure <N> { var resDensity = GetBinaryOpDictionary(d1, d2, op); var name = binOpStrFunc == null ? DefaultName : binOpStrFunc(d1.Name, d2.Name); return(new Density <S, N, RF>(resDensity, newBaseStructure, d1.RealField, name)); }
public static RF BinaryProb(IDensity <G, M, RF> d1, IDensity <G, M, RF> d2, Func <M, M, bool> cond) { var resSum = default(RF); var rf = d1.RealField; foreach (var key1 in d1.GetKeys()) { foreach (var key2 in d2.GetKeys()) { if (cond(key1, key2)) { resSum = rf.Add(resSum, rf.Multiply(d1[key1], d2[key2])); } } } return(resSum); }
public static string GetOxyPlotSvg <G, M, RF>(this IDensity <G, M, RF> d, string title) where G : IBaseStructure <M>, IRealEmbedding <M, RF> where M : struct where RF : struct { var oxyplot = d.OxyPlot(title); var svgExporter = new SvgExporter { Width = 600, Height = 400 }; var res = svgExporter.ExportToString(oxyplot); return(res); }
protected static IDictionary <N, RF> GetOpDictionary <N>( IDensity <G, M, RF> d, Func <M, N> op) { var resDensity = new Dictionary <N, RF>(); var rf = d.RealField; foreach (var key in d.GetKeys()) { var resKey = op(key); if (!resDensity.ContainsKey(resKey)) { resDensity[resKey] = default(RF); } resDensity[resKey] = rf.Add(resDensity[resKey], d[key]); } return(resDensity); }
protected static IDictionary <M, RF> GetConditionalDensityDictionary( IDensity <G, M, RF> d, Func <M, bool> cond) { var resDict = new Dictionary <M, RF>(); var rf = d.RealField; foreach (var key in d.GetKeys().Where(cond)) { resDict[key] = d[key]; } var conditionalProbability = rf.Sum(resDict.Values); foreach (var key in resDict.Keys) { resDict[key] = rf.Divide(resDict[key], conditionalProbability); } return(resDict); }
public int CompareTo(IDensity other) { if (ReferenceEquals(other, null)) { return(1); } else if (this < other) { return(-1); } else if (this > other) { return(1); } else { return(0); } }
public static string Plot <G, M, RF>(this IDensity <G, M, RF> d, int plotWidth = 70) where G : IBaseStructure <M> where RF : struct { var rf = d.RealField; var maxPercentage = rf.Max(d.GetValues()); var asciiPlotter = new AsciiPlotter <M, RealFieldType <RF>, RF>(new RealFieldType <RF>()); var plotStr = asciiPlotter.GetPlot( k => d[k], d.SortedKeys(), plotWidth: plotWidth, minP: rf.Zero(), maxP: maxPercentage, asPercentage: true); return(plotStr); }
public static void SaveOxyPlotPdf <G, M, RF>(this IDensity <G, M, RF> d, string filename = null) where G : IBaseStructure <M>, IRealEmbedding <M, RF> where M : struct where RF : struct { filename = filename ?? d.GetTrimmedName() + ".pdf"; var oxyplot = d.OxyPlot(Path.GetFileName(filename)); using (var stream = File.Create(filename)) { var pdfExporter = new PdfExporter { Width = 600, Height = 400 }; pdfExporter.Export(oxyplot, stream); } }
public static IDensity <G, M, RF> ArithMult <G, M, RF>(this IDensity <G, M, RF> d, int n) where G : IAdditiveMonoid <M> { if (n < 0) { throw new ArgumentException(); } if (n == 0) { return(new Zero <G, M, RF>(d.BaseStructure, d.RealField)); } var newDensity = d; foreach (var step in Enumerable.Range(1, n - 1)) { newDensity = newDensity.Add(d); } newDensity.Name = $"({d.GetTrimmedName()}).ArithMult({n})"; return(newDensity); }