# C# Hypothesis Test Example

← All NMath Stats Code Examples

```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, it’s 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 artherosclerotic
// disease. Two measurements are taken: before treatment with
// Vitamin E (baseline), and after two years of taking Vitamin E daily.
var df = new DataFrame();
var col1 = new DFNumericColumn( "Baseline" );
col1.NumericFormat = "F7";
var col2 = new DFNumericColumn( "Vit E" );
col2.NumericFormat = "F7";

// Display the data
Console.WriteLine();
Console.WriteLine( df );
Console.WriteLine();

// Construct a TwoSamplePairedTTest from the two columns of data.
var 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;
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();

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" );