using System; using CenterSpace.NMath.Core; namespace CenterSpace.NMath.Examples.CSharp { /// <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 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"; df.AddColumn( col1 ); var col2 = new DFNumericColumn( "Vit E" ); col2.NumericFormat = "F7"; df.AddColumn( col2 ); df.AddRow( "Subj1", 0.66, 0.60 ); df.AddRow( "Subj2", 0.72, 0.65 ); df.AddRow( "Subj3", 0.85, 0.79 ); df.AddRow( "Subj4", 0.62, 0.63 ); df.AddRow( "Subj5", 0.59, 0.54 ); df.AddRow( "Subj6", 0.63, 0.55 ); df.AddRow( "Subj7", 0.64, 0.62 ); df.AddRow( "Subj8", 0.70, 0.67 ); df.AddRow( "Subj9", 0.73, 0.68 ); df.AddRow( "Subj10", 0.68, 0.64 ); // 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; df.AddRow( "Subj11", 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← All NMath Code Examples