Using statistical routines.
using System;
using System.Collections.Generic;
using matrix_science.msparser;
namespace MsParserExamples
{
class tools_stats
{
public static void Main(string[] args)
{
Console.WriteLine("Binomial coefficients for sample size 6:");
for (int i = 0; i <= 6; i++)
{
Console.WriteLine("\t6 choose {0} = {1}", i, ms_quant_stats.binomialCoefficient(6, (uint) i));
}
Console.WriteLine("First 10 values of the Poisson (lambda = 1) distribution");
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("\tPoisson(lambda 1, k {0}) = {1}", i, ms_quant_stats.poissonDensity(1, (uint) i));
}
Console.WriteLine("Common values of the standard normal distribution");
foreach (double t in (new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.5, 0.9, 0.95, 0.975, 0.99, 0.999})) {
double critval;
if (t < 0.5)
{
critval = -ms_quant_stats.normalCriticalValue(1d - t);
}
else
{
critval = ms_quant_stats.normalCriticalValue(t);
}
Console.WriteLine("\tNormal-1({0}) = {1}", t, critval);
}
foreach (double t in (new double[] {-3.09, -2.32, -1.96, -1.64, -1.28, 0, 1.28, 1.64, 1.96, 2.32, 3.09}))
{
double p;
if (t < 0)
{
p = 0.5 - ms_quant_stats.normalCumulativeProbability(-t);
}
else
{
p = 0.5 + ms_quant_stats.normalCumulativeProbability(t);
}
Console.WriteLine("\tNormal(x <= {0}) = {1:0.######}", t, p);
}
Console.WriteLine("Common values of the chi-squared distribution at 10 degrees of freedom:");
foreach (double t in (new double[] {0.001, 0.01, 0.025, 0.05, 0.1})) {
double lowcrit = ms_quant_stats.chisqLowerCriticalValue(10, t);
double upcrit = ms_quant_stats.chisqUpperCriticalValue(10, t);
Console.WriteLine("\tchi-sq(df 10, sigLevel {0}) = <{1}, {2}>", t, lowcrit, upcrit);
}
Console.WriteLine("Common values of the Student's t distribution at 10 degrees of freedom:");
foreach (double t in (new double[] {0.001, 0.005, 0.01, 0.025, 0.05, 0.1}))
{
Console.WriteLine("\tt(df 10, sigLevel {0}) = {1}", t, ms_quant_stats.studentsCriticalValue(10, t));
}
Console.WriteLine("Common values of the F distribution at 10 and 1 degrees of freedom:");
foreach(double t in (new double[] {0.01, 0.05, 0.1}))
{
Console.WriteLine("\tF(df1 10, df2 1, sifLevel {0}) = {1}", t, ms_quant_stats.snedecorsCriticalValue(10, 1, t));
}
List<Sample> samples = new List<Sample>();
samples.Add(new Sample("uniform(0,1)",
new double[] { 0.400313346021907, 0.635154745166318, 0.795328049493161, 0.465079196585926, 0.709667388012424 ,
0.76161797692474, 0.617230566355229, 0.282583560369936, 0.251706165020014, 0.2962014901575},
0.5));
samples.Add(new Sample("normal(5, 2.5)",
new double[] { 3.1476833, 9.2061900, 2.6079399, 4.4361054, 6.4133568, 4.3745899, 3.5588195, 3.0542840, 0.6643241,4.3697818},
5));
samples.Add(new Sample("beta(0.5, 0.5)",
new double[] { 0.754152463, 0.008992998, 0.221637853, 0.426146895, 0.656421042, 0.224818243, 0.022050868, 0.539143737 ,
0.877643550 ,0.128030530 },
0.5));
Console.WriteLine();
foreach (Sample sample in samples)
{
vectord vec = sample.Data;
Console.WriteLine("Sample '{0}' = [{1}]", sample.SampleName, string.Join(", ", vec.ToArray()));
vectord vecSorted = sample.SortedData;
Console.WriteLine("\tSum = {0:#.#####}", ms_quant_stats.sum(vec));
Console.WriteLine("\tSum of squares = {0:#.#####}", ms_quant_stats.sumsq(vec));
Console.WriteLine("\tLog transformed = [{0}]", string.Join(", ", ms_quant_stats.logTransform(vec).ToArray()));
Console.WriteLine("\tExp transformed = [{0}]", string.Join(", ", ms_quant_stats.expTransform(vec).ToArray()));
double xbar = ms_quant_stats.arithmeticMean(vec);
double stdev = ms_quant_stats.arithmeticStandardDeviation(vec, xbar);
double median = ms_quant_stats.sortedMedian(vecSorted);
double geombar = ms_quant_stats.geometricMean(vec);
double geomdev = ms_quant_stats.geometricStandardDeviation(vec, xbar);
Console.WriteLine("\tArithmetic stdev = {0}", stdev);
Console.WriteLine("\tArithmetic mean = {0}", xbar);
Console.WriteLine("\tStandard error of mean = {0}", ms_quant_stats.arithmeticStandardErrorOfMean(stdev, (uint)vec.Count));
Console.WriteLine("\tMedian = {0}", median);
Console.WriteLine("\tStandard error of median = {0}", ms_quant_stats.arithmeticStandardErrorOfMedian(stdev, (uint)vec.Count));
Console.WriteLine("\tGeometric mean = {0}", geombar);
Console.WriteLine("\tGeometric stdev = {0}", geomdev);
Console.WriteLine("\tHypothesis: distribution is N({0}, sigma^2) (mean test). p-value = {1:0.#####}", sample.PopulationMean,
ms_quant_stats.calculateNormalMeanPvalue(sample.PopulationMean, xbar, stdev, (uint)vec.Count));
Console.WriteLine("\tHypothesis: distribution is N({0}, sigma^2) (median test). p-value = {1:0.#####}", sample.PopulationMean,
ms_quant_stats.calculateNormalMedianPvalue(sample.PopulationMean, median, stdev, (uint)vec.Count));
{
double W, p;
bool ok = ms_quant_stats.testShapiroWilkW(vecSorted, out W, out p);
Console.WriteLine("\tShapiro-Wilk ok = {0}, W = {1}, p-value = {2}", ok, W != 0 ? W.ToString() : "(none)",
p != 0 ? p.ToString() : "(none)");
}
{
uint numL, numH;
ms_quant_stats.detectOutliers(vecSorted, "auto", 0.05, out numL, out numH);
Console.WriteLine("\tOutlier detection('auto',0.05) = <{0}, {1}>", numL,
numH);
}
Console.WriteLine();
}
}
}
public static class VectordExtensionMethods
{
public static double[] ToArray(this vectord vector)
{
double[] data = new double[vector.Count];
vector.CopyTo(data);
return data;
}
}
struct Sample
{
private string Name;
private double[] data;
private double PopMean;
public Sample(string name, double[] data, double popmean)
{
this.Name = name;
this.data = data;
this.PopMean = popmean;
}
public string SampleName
{
get
{
return Name;
}
}
public double PopulationMean
{
get
{
return PopMean;
}
}
public vectord Data
{
get
{
return new vectord(data);
}
}
public vectord SortedData
{
get
{
double[] dataCopy = new double[data.Length];
this.Data.CopyTo(dataCopy);
Array.Sort(dataCopy);
return new vectord(dataCopy);
}
}
}
}