Skip to content

Instantly share code, notes, and snippets.

@jvangael
Created April 12, 2011 21:33
Show Gist options
  • Save jvangael/916469 to your computer and use it in GitHub Desktop.
Save jvangael/916469 to your computer and use it in GitHub Desktop.
Math.Net Numerics - Online computation of statistics
using System;
using System.IO;
using System.Linq;
using MathNet.Numerics.Random;
using MathNet.Numerics.Statistics;
using MathNet.Numerics.Distributions;
using System.Collections.Generic;
using System.Diagnostics;
namespace RunningVariance
{
class Program
{
/// <summary>
/// Creates a file with N double entries.
/// </summary>
static string CreateData(int N)
{
var rnd = new MersenneTwister();
var path = Path.GetTempFileName();
using (var sw = new StreamWriter(path))
{
for (int i = 0; i < N; i++)
{
sw.WriteLine(Normal.Sample(rnd, 3.5, 1.0));
}
}
Console.WriteLine("Done creating data, press any key to start computation ...");
Console.ReadLine();
return path;
}
static IEnumerable<double> ReadFile(StreamReader sr)
{
while (!sr.EndOfStream)
{
yield return Double.Parse(sr.ReadLine());
}
}
static void Main(string[] args)
{
var path = CreateData(100000000);
using (var sr = new StreamReader(path))
{
var sw = new Stopwatch();
sw.Start();
var sdev = Statistics.StandardDeviation(ReadFile(sr));
sw.Stop();
Console.WriteLine("From file: standard Deviation = {0} in {1} millis.", sdev, sw.ElapsedMilliseconds);
}
using (var sr = new StreamReader(path))
{
var data = new double[100000000];
var e = ReadFile(sr).GetEnumerator();
for (int i = 0; e.MoveNext(); i++)
{
data[i] = e.Current;
}
var sw = new Stopwatch();
sw.Start();
var sdev = Statistics.StandardDeviation(data);
sw.Stop();
Console.WriteLine("From memory: standard Deviation = {0} in {1} millis.", sdev, sw.ElapsedMilliseconds);
}
File.Delete(path);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment