Created
February 20, 2017 18:22
-
-
Save JCGrant/8d531bcefe9f277e6ad8cd9bb7c99f29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
namespace SemanticEvolution { | |
public class Program { | |
private static List<double> GetValuesFromCsv(string filePath, char delimiter=',') { | |
List<double> values = new List<double>(); | |
using(FileStream file = File.OpenRead(filePath)) | |
using(StreamReader reader = new StreamReader(file)) { | |
string[] valueStrs = reader.ReadLine().Split(delimiter); | |
foreach (string valueStr in valueStrs) { | |
values.Add(Double.Parse(valueStr)); | |
} | |
} | |
return values; | |
} | |
public static void Main(string[] args) { | |
List<double> values = GetValuesFromCsv(@"SampleData.csv"); | |
int BIN_SIZE = 10; | |
int n = 0; | |
double mean = 0; | |
double S = 0; | |
SortedDictionary<int, int> histogram = new SortedDictionary<int, int>(); | |
foreach (double value in values) { | |
n++; | |
double delta = value - mean; | |
mean += delta / n; | |
double delta2 = value - mean; | |
S += delta * delta2; | |
int bin = (int) Math.Floor(value / BIN_SIZE); | |
if (histogram.ContainsKey(bin)) { | |
histogram[bin] += 1; | |
} else { | |
histogram[bin] = 1; | |
} | |
} | |
double sd = Math.Sqrt(S / n); | |
Console.WriteLine("The arithmetic mean is: " + mean); | |
Console.WriteLine("The standard deviation is: " + sd); | |
foreach (KeyValuePair<int, int> pair in histogram) { | |
int binMin = pair.Key * BIN_SIZE; | |
int binMax = (pair.Key + 1) * BIN_SIZE; | |
int frequency = pair.Value; | |
Console.WriteLine("The frequency of values in bin {0} - {1} is: {2}", | |
binMin, binMax, frequency); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment