Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


One Sample T-Test Demo Example (C#)

[C#]
/* COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
 * All rights reserved.
 *
 * Use of this copyright notice is precautionary only, and does not imply
 * publication or disclosure. The content of this work contains confidential
 * and proprietary information of Simplexar Software Limited. Any duplication,
 * modification, distribution, or disclosure in any form, in whole, or in part,
 * is strictly prohibited without express prior written permission.
 */

// Get namespaces.
using System;
using Simplexar.Statsar;
using Simplexar.Statsar.HypothesisTests;

// Start namespace.
namespace Simplexar.Examples.Statsar
{
    /// <summary>
    /// This example shows how to perform a statistical hypothesis test
    /// by using the one sample T-test class. This tests if a sample
    /// comes from a particular population, by comparing means.
    /// </summary>
    public static class Example
    {
        /// <summary>
        /// The application entry point.
        /// </summary>
        public static void Main()
        {
            try
            {
                RunExample();
            }
            catch(Exception exception)
            {
                Console.WriteLine(exception);
            }
            Console.WriteLine("Press ENTER to terminate.");
            Console.ReadLine();
        }

        private static void RunExample()
        {
            // Define test parameters.
            double xbar = 5; // The sample mean.
            double sampleStandardDeviation = 1.5;
            int n = 53;      // The sample size.
            double populationMean = 5.4;
            double alpha = 0.05;

            // Create a new one sample T-test. In this example
            // we create the test using explict test parameters.
            // A second constructor is also provided which
            // constructs a test from sample data.
            OneSampleTTest test = new OneSampleTTest(
                xbar, sampleStandardDeviation, n,
                populationMean, alpha, HypothesisType.TwoSided);

            // Display test statistics.
            Console.WriteLine(
                "P-value: {0}", test.P);

            Console.WriteLine(
                "Reject: {0}", test.Reject);

            Console.WriteLine(
                "Statistic: {0}", test.Statistic);

            Console.WriteLine(
                "Left critical value: {0}", test.LeftCriticalValue);

            Console.WriteLine(
                "Right critical value: {0}", test.RightCriticalValue);

            Console.WriteLine(
                "Left probability: {0}", test.LeftProbability);

            Console.WriteLine(
                "Right probability: {0}", test.RightProbability);

            Console.WriteLine(
                "Lower confidence limit: {0}", test.LowerConfidenceLimit);

            Console.WriteLine(
                "Upper confidence limit: {0}", test.UpperConfidenceLimit);

            Console.WriteLine(
                "Standard error of the mean: {0}", test.SEM);

            Console.WriteLine(
                "Distribution degrees of freedom: {0}", test.DegreesOfFreedom);
        }
    }
}