1. Devin Martin
  2. FlipSideOut.Linqpad

Commits

Devin Martin  committed 079e0c5

Add double to int. Add int based histogram. Move quantile into root class. Add statistical extensions.

  • Participants
  • Parent commits e011e55
  • Branches master

Comments (0)

Files changed (7)

File FlipSideOut.Linqpad/Conversion/ConversionExtensions.cs

  • Ignore whitespace
-namespace FlipSideOut.Linqpad.Conversion
-{
-    using System;
-    using System.IO;
-
-    public static class ConversionExtensions
-    {
-        public static byte[] FromHex(this string hex)
-        {
-            if (hex == null)
-                throw new ArgumentNullException("hex");
-
-            hex = hex.Replace("-", string.Empty);
-
-            if (hex.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
-                hex = hex.Substring(2);
-
-            if (hex.Length % 2 != 0)
-                throw new ArgumentException("hex");
-
-            int NumberChars = (hex.Length / 2);
-
-            byte[] bytes = new byte[NumberChars];
-            StringReader sr = new StringReader(hex);
-            for (int i = 0; i < NumberChars; i++)
-                bytes[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
-            sr.Dispose();
-            return bytes;
-        }
-
-        public static string ToHex(this byte[] ba)
-        {
-            string hex = BitConverter.ToString(ba);
-            return hex.Replace("-", "");
-        }
-
-        public static string ToBinaryString(this byte n)
-        {
-            int pos = 7;
-            int i = 0;
-            char[] output = new char[8];
-            while (i < 8)
-            {
-                if ((n & (1 << i)) != 0)
-                {
-                    output[pos] = '1';
-                }
-                else
-                {
-                    output[pos] = '0';
-                }
-                pos--;
-                i++;
-            }
-            return new string(output);
-        }
-    }
-}

File FlipSideOut.Linqpad/ConversionExtensions.cs

View file
  • Ignore whitespace
+namespace FlipSideOut.Linqpad
+{
+    using System;
+    using System.Collections.Generic;
+    using System.IO;
+
+    public static class ConversionExtensions
+    {
+        public static IEnumerable<int> ToInt(this IEnumerable<double> input)
+        {
+            return input.ToInt(v => (int)Math.Round(v, 0, MidpointRounding.AwayFromZero));
+        }
+
+        /// <summary>
+        /// Takes a number intended to be from 0.00 to 1.00 and casts it as a percent. There is no validation
+        /// </summary>
+        /// <param name="input"></param>
+        /// <returns></returns>
+        public static IEnumerable<int> ToIntAsPercent(this IEnumerable<double> input)
+        {
+            return input.ToInt(v => (int)Math.Round(v * 100, 0, MidpointRounding.AwayFromZero));
+        }
+
+        public static IEnumerable<int> ToInt(this IEnumerable<double> input, Func<double, int> convert)
+        {
+            foreach (var value in input)
+                yield return convert(value);
+        }
+
+        public static byte[] FromHex(this string hex)
+        {
+            if (hex == null)
+                throw new ArgumentNullException("hex");
+
+            hex = hex.Replace("-", string.Empty);
+
+            if (hex.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
+                hex = hex.Substring(2);
+
+            if (hex.Length % 2 != 0)
+                throw new ArgumentException("hex");
+
+            int NumberChars = (hex.Length / 2);
+
+            byte[] bytes = new byte[NumberChars];
+            StringReader sr = new StringReader(hex);
+            for (int i = 0; i < NumberChars; i++)
+                bytes[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
+            sr.Dispose();
+            return bytes;
+        }
+
+        public static string ToHex(this byte[] ba)
+        {
+            string hex = BitConverter.ToString(ba);
+            return hex.Replace("-", "");
+        }
+
+        public static string ToBinaryString(this byte n)
+        {
+            int pos = 7;
+            int i = 0;
+            char[] output = new char[8];
+            while (i < 8)
+            {
+                if ((n & (1 << i)) != 0)
+                {
+                    output[pos] = '1';
+                }
+                else
+                {
+                    output[pos] = '0';
+                }
+                pos--;
+                i++;
+            }
+            return new string(output);
+        }
+    }
+}

File FlipSideOut.Linqpad/FlipSideOut.Linqpad.csproj

View file
  • Ignore whitespace
     <Reference Include="System.Windows.Forms.DataVisualization" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Conversion\ConversionExtensions.cs" />
+    <Compile Include="ConversionExtensions.cs" />
     <Compile Include="Histogram.cs" />
+    <Compile Include="Quantile.cs" />
     <Compile Include="RandomEnumerables.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="StatisticalExtensions.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="FlipSideOut.Linqpad.nuspec" />

File FlipSideOut.Linqpad/Histogram.cs

View file
  • Ignore whitespace
     /// </remarks>
     public class Histogram
     {
-        public static class Quantile
+        public static byte[] CreateHistogram(IEnumerable<int> data, int size)
         {
-            public static double Cauchy(double p)
-            {
-                return Math.Tan(Math.PI * (p - 0.5));
-            }
+            var histogramData = CreateHistogramData(data);
+            return CreateHistogramGraph(size, histogramData);
         }
+
         public static byte[] CreateHistogram(IEnumerable<double> data, int buckets, double min, double max, int size)
         {
             var histogramData = CreateHistogramData(data, buckets, min, max);
+            return CreateHistogramGraph(size, histogramData);
+        }
+
+        private static byte[] CreateHistogramGraph(int size, int[] histogramData)
+        {
             using (MemoryStream stream = new MemoryStream())
             {
                 using (var ch = new Chart())
                 {
                     using (var s = new Series())
                     {
-                        foreach (var pnt in histogramData) s.Points.Add(pnt);
+                        foreach (var pnt in histogramData)
+                        {
+                            s.Points.Add(pnt);
+                        }
 
                         ch.Series.Add(s);
 
             }
             return results;
         }
+
+        private static int[] CreateHistogramData(IEnumerable<int> data)
+        {
+            var query = from datum in data
+                        group datum by datum into aggregateData
+                        select new { Key = aggregateData.Key, Count = aggregateData.Count() };
+
+            var arrayProjection = from g in query.OrderBy(a => a.Key)
+                                  select g.Count;
+            return arrayProjection.ToArray();
+        }
     }
 }

File FlipSideOut.Linqpad/Quantile.cs

View file
  • Ignore whitespace
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlipSideOut.Linqpad
+{
+    public static class Quantile
+    {
+        public static double Cauchy(double p)
+        {
+            return Math.Tan(Math.PI * (p - 0.5));
+        }
+    }
+}

File FlipSideOut.Linqpad/StatisticalExtensions.cs

View file
  • Ignore whitespace
+namespace FlipSideOut.Linqpad
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Linq;
+    using System.Text;
+    using System.Threading.Tasks;
+
+    public static class StatisticalExtensions
+    {
+        /// <summary>
+        /// Calculates the standard deviation
+        /// </summary>
+        /// <param name="set"></param>
+        /// <returns></returns>
+        public static double StandardDeviation(IEnumerable<double> set)
+        {
+            var data = set.ToArray();
+            double average = data.Average();
+            double sumOfDerivation = 0;
+            foreach (double value in data)
+            {
+                sumOfDerivation += (value) * (value);
+            }
+            double sumOfDerivationAverage = sumOfDerivation / (data.Count() - 1);
+            return Math.Sqrt(sumOfDerivationAverage - (average * average));  
+        }
+    }
+}

File FlipSideOut.Linqpad/linqpad-samples/Histograms/NonUniformDistributionHistograms.linq

View file
  • Ignore whitespace
 	// http://ericlippert.com/2012/02/21/generating-random-non-uniform-data/
 
 	var distribution = from x in RandomEnumerables.Double()
-					   select Histogram.Quantile.Cauchy(x);
+					   select Quantile.Cauchy(x);
 
 	var imageData = Histogram.CreateHistogram(distribution.Take(100000), 81, -6.0, 6.0, 500);
 	Util.Image(imageData).Dump();