[TOC]
using System;
using CenterSpace.NMath.Core;
using CenterSpace.NMath.Stats;
namespace HypothesisTestExample
{
/// <summary>
/// A .NET example in C# showing how to use the hypothesis test classes to test statistical
/// hypotheses.
/// </summary>
/// <remarks>
/// Hypothesis tests use statistics to determine the probability that a
/// given hypothesis is true. For example, could the differences between two
/// sample means be explained away as sampling error? NMath Stats provides
/// classes for many common hypothesis tests, such as the z-test, t-test,
/// F-test, and Kolmogorov-Smirnov test, with calculation of p-values,
/// critical values, and confidence intervals. All hypothesis test classes
/// share substantially the same interface. Once you learn how to use one
/// test, its easy to use any of the others.
/// </remarks>
public class HypothesisTestExample
{
static void Main(string[] args)
{
// Suppose we measure the thickness of plaque (mm) in the carotid
// artery of 10 randomly selected patients with mild atherosclerotic
// disease. Two measurements are taken: before treatment with
// Vitamin E (baseline), and after two years of taking Vitamin E daily.
DataFrame df = new DataFrame();
DFNumericColumn col1 = new DFNumericColumn("Baseline");
col1.NumericFormat = "F2";
df.AddColumn(col1);
DFNumericColumn col2 = new DFNumericColumn("Vit E");
col2.NumericFormat = "F2";
df.AddColumn(col2);
df.AddRow("Subject1", 0.66, 0.60);
df.AddRow("Subject2", 0.72, 0.65);
df.AddRow("Subject3", 0.85, 0.79);
df.AddRow("Subject4", 0.62, 0.63);
df.AddRow("Subject5", 0.59, 0.54);
df.AddRow("Subject6", 0.63, 0.55);
df.AddRow("Subject7", 0.64, 0.62);
df.AddRow("Subject8", 0.70, 0.67);
df.AddRow("Subject9", 0.73, 0.68);
df.AddRow("Subject10", 0.68, 0.64);
// Display the data
Console.WriteLine();
Console.WriteLine(df);
Console.WriteLine();
// Construct a TwoSamplePairedTTest from the two columns of data.
TwoSamplePairedTTest test = new TwoSamplePairedTTest(df["Baseline"], df["Vit E"]);
// Display the results
Console.WriteLine(test);
Console.WriteLine();
// Change test type and alpha level, and display new results.
test.Type = HypothesisType.Right;
test.Alpha = 0.05;
Console.WriteLine(test);
Console.WriteLine();
// Modify the data.
df[5, 1] = 0.56;
df.AddRow("Subject11", 0.65, 0.64);
Console.WriteLine(df);
Console.WriteLine();
// Update the test, and display new results.
test.Update(df["Baseline"], df["Vit E"]);
test.Type = HypothesisType.TwoSided;
Console.WriteLine(test);
Console.WriteLine();
// Properties provide programmatic access to individual elements in the test.
Console.WriteLine("Properties");
Console.WriteLine("number of pairs = " + test.N);
Console.WriteLine("mean difference between pairs = " + test.Xbar);
Console.WriteLine("standard deviation of differences between pairs = " + test.S);
Console.WriteLine("deg of freedom = " + test.DegreesOfFreedom);
Console.WriteLine("t-statistic = " + test.Statistic);
Console.WriteLine("test type = " + test.Type);
Console.WriteLine("alpha level = " + test.Alpha);
Console.WriteLine("p-value = " + test.P);
Console.WriteLine("critical values: " + test.LeftCriticalValue + " " +
test.RightCriticalValue);
Console.WriteLine("reject the null hypothesis? " + test.Reject);
Console.WriteLine("confidence interval: " + test.LowerConfidenceLimit + " " +
test.UpperConfidenceLimit);
Console.WriteLine();
Console.WriteLine("Press Enter Key");
Console.Read();
} // Main
} // class
} // namespace
[TOC]