00001 using System;
00002 using System.Collections;
00003 using System.Collections.Generic;
00004
00005 namespace StephenAshley.Biostatistics
00006 {
00007 using NUMBER = Decimal;
00008
00015 public class T_TestGroupsCollection : GroupsCollection, IEnumerable
00016 {
00017 #region *** constructors ***
00022 public T_TestGroupsCollection()
00023 : base()
00024 {
00025 }
00026
00037 public T_TestGroupsCollection(List<Group> lstGroups)
00038 : base(lstGroups)
00039 {
00040 if (k() > 2)
00041 {
00042 throw new BiostatisticsException(
00043 "T_TestGroupsCollection contains more than 2 Groups.");
00044 }
00045 }
00046 #endregion
00047 #region ***** methods ******
00058 public override void Add(Group group)
00059 {
00060 if (group == null)
00061 {
00062 throw new BiostatisticsException(
00063 "group parameter in Add method of T_TestGroupsCollection is null.");
00064 }
00065
00066 if (groupsArray == null || (k() == 1))
00067 base.Add(group);
00068 else
00069 throw new BiostatisticsException(
00070 "Addition of Group would result in T_TestGroupsCollection with more than 2 Groups.");
00071 }
00072
00084 public override void AddRange(List<Group> lstGroups)
00085 {
00086 if (lstGroups == null)
00087 {
00088 throw new BiostatisticsException(
00089 "groups parameter in AddRange method of T_TestGroupsCollection is null.");
00090 }
00091
00092 if (groupsArray == null || (k() + lstGroups.Count <= 2))
00093 base.AddRange(lstGroups);
00094 else
00095 throw new BiostatisticsException(
00096 "Addition of a Group would result in a T_TestGroupsCollection with more than 2 Groups.");
00097 }
00098
00107 public NUMBER EstimatedPopulationVariance()
00108 {
00109 if (k() != 2)
00110 throw new BiostatisticsException(
00111 "T-TestGroupsList does not contain 2 Groups.");
00112 Group groupA = groupsArray[0];
00113 Group groupB = groupsArray[1];
00114 NUMBER decEstimatedPopulationVariance;
00115 try
00116 {
00117 decEstimatedPopulationVariance = (groupA.SumOfSquaredDeviates()
00118 + groupB.SumOfSquaredDeviates()) / (groupA.n() + groupB.n() - 2);
00119 }
00120 catch (Exception e)
00121 {
00122 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00123 }
00124 return decEstimatedPopulationVariance;
00125 }
00126
00136 public NUMBER EstimatedStandardErrorOfDifferenceOfMeans()
00137 {
00138 if (k() != 2)
00139 throw new BiostatisticsException(
00140 "T-TestGroupsList does not contain 2 Groups.");
00141 NUMBER decEstimatedStandardErrorOfDifferenceOfMeans;
00142 try
00143 {
00144 decEstimatedStandardErrorOfDifferenceOfMeans =
00145 DecMath.Sqrt(EstimatedPopulationVariance() / groupsArray[0].n() +
00146 EstimatedPopulationVariance() / groupsArray[1].n());
00147 }
00148 catch (Exception e)
00149 {
00150 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00151 }
00152 return decEstimatedStandardErrorOfDifferenceOfMeans;
00153 }
00154
00163 public virtual NUMBER t()
00164 {
00165 if (k() != 2)
00166 throw new BiostatisticsException(
00167 "T-TestGroupsList does not contain 2 Groups.");
00168 NUMBER decT;
00169 try
00170 {
00171 decT = (groupsArray[0].Mean() - groupsArray[1].Mean()) /
00172 EstimatedStandardErrorOfDifferenceOfMeans();
00173 }
00174 catch (Exception e)
00175 {
00176 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00177 }
00178 return decT;
00179 }
00180
00188 public Int32 DegreesOfFreedom()
00189 {
00190 if (k() != 2)
00191 throw new BiostatisticsException(
00192 "T-TestGroupsList does not contain 2 Groups.");
00193 return groupsArray[0].n() + groupsArray[1].n() - 2;
00194 }
00195
00200 public virtual NUMBER p()
00201 { return Distributions.ProbabilityT(t(), DegreesOfFreedom()); }
00202
00203 #endregion
00204
00205 }
00206 }