using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Science.Statistics.BasicStatistics { public class OneSampleTTest { private double ov; private double df; private bool tt; public OneSampleTTest(StandardErrorAndExpectedValue input, ObservedValue observedValue, int degreesOfFreedom) { df = (double)degreesOfFreedom; if (observedValue.SumQ == true) { ov = observedValue.Sum; tv = (ov - input.ExpectedValueForSum) / input.StandardErrorForSum; } else if (observedValue.AverageQ == true) { ov = observedValue.Average; tv = (ov - input.ExpectedValueForAverage) / input.StandardErrorForAverage; } else { ov = observedValue.Percentage; tv = (ov - input.ExpectedValueForPercentage) / input.StandardErrorForPercentage; } tt = observedValue.TwoTailedQ; FindpValue(); } private void FindpValue() { pv = (1.0 - AreaBetweenMinustAndPlust()) / 2.0; if (tt) pv *= 2.0; pv *= 100.0; if (pv < 0.01) c = "Reject null."; else if (pv >= 0.01 && pv < 1.0) c = " Reject null at 1% level."; else if (pv >= 1.0 && pv < 5.0) c = " Reject null at 5% level."; else c = "Accept null."; } private double tv; public double tValue { get { return tv; } set { tv = value; } } private double pv; public double PValue { get { return pv; } set { pv = value; } } private string c; public string Conclusion { get { return c; } } private double func(double x) { return Math.Pow(1.0 + (x*x/df),-(df+1.0)/2.0); } private double AreaBetweenMinustAndPlust() { Function.ToLastType del = new Function.ToLastType(func); IntegrationMidpoint obj = new IntegrationMidpoint(del, -tv, tv); obj.Compute(); return obj.Result / Math.Sqrt(df) / Beta(0.5,df/2.0); } private double LogGamma(double xx) { double x, y, tmp, ser; double[] cof = new Double[6]; cof[0] = 76.18009172947146; cof[1] = -86.50532032941677; cof[2] = 24.01409824083091; cof[3] = -1.231739572450155; cof[4] = 0.1208650973866179e-2; cof[5] = -0.5395239384953e-5; int j; y = x = xx; tmp = x + 5.5; tmp -= (x + 0.5) * Math.Log(tmp); ser = 1.000000000190015; for (j = 0; j <= 5; j++) ser += cof[j] / ++y; return -tmp + Math.Log(2.5066282746310005 * ser / x); } private double Beta(double z, double w) { return Math.Exp(LogGamma(z) + LogGamma(w) - LogGamma(z + w)); } } }