00001 using System;
00002 using System.Collections;
00003 using System.Collections.Generic;
00004
00005
00006 namespace StephenAshley.Biostatistics
00007 {
00008 using NUMBER = Decimal;
00015 public class PairedT_TestGroupsCollection : T_TestGroupsCollection, IEnumerable
00016 {
00017 #region *** fields ***
00018 private Group grpDifferences;
00019 #endregion
00020 #region *** constructors ***
00025 public PairedT_TestGroupsCollection()
00026 : base()
00027 {
00028 grpDifferences = new Group();
00029 }
00030
00043 public PairedT_TestGroupsCollection(List<Group> groups)
00044 : base(groups)
00045 {
00046 if (groupsArray[0].n() != groupsArray[1].n())
00047 throw new BiostatisticsException("Groups have unequal sizes.");
00048
00049 if (k() == 2)
00050 {
00051 grpDifferences = new Group((Int16)groupsArray[0].n());
00052 for (Int32 i = 0; i < groupsArray[0].n(); i++)
00053 grpDifferences.Add(groupsArray[0][i] - groupsArray[1][i]);
00054 }
00055 }
00056 #endregion
00057 #region ***** methods ******
00067 public override void Add(Group group)
00068 {
00069 if (group == null)
00070 throw new BiostatisticsException("group is a null reference.");
00071
00072
00073 if (groupsArray == null || (k() == 0))
00074 {
00075 base.Add(group);
00076 return;
00077 }
00078
00079
00080
00081 if (k() == 1 && groupsArray[0].n() == group.n())
00082 {
00083 base.Add(group);
00084 grpDifferences = new Group((Int16)groupsArray[0].n());
00085 for (Int32 i = 0; i < groupsArray[0].n(); i++)
00086 grpDifferences.Add(groupsArray[0][i] - groupsArray[1][i]);
00087
00088 return;
00089 }
00090
00091
00092
00093 if (k() == 1 && groupsArray[0].n() != group.n())
00094 {
00095 throw new BiostatisticsException(
00096 "Groups in a PairedT_TestGroupsCollection must have an equal number of elements.");
00097 }
00098
00099 if (k() > 1)
00100 throw new BiostatisticsException(
00101 "Addition of a Group would result in a PairedT_TestGroupsCollection with more than 2 Groups.");
00102 }
00103
00117 public override void AddRange(List<Group> lstGroups)
00118 {
00119 if (lstGroups == null)
00120 throw new BiostatisticsException("lstGroups is a null reference.");
00121
00122 if (lstGroups.Count == 0)
00123 throw new BiostatisticsException("lstGroups contains no Groups.");
00124
00125 if (lstGroups.Count + k() > 2)
00126 throw new BiostatisticsException(
00127 "Addition of Groups would result in a PairedT-TestGroupsList with more than 2 Groups.");
00128
00129 if (lstGroups.Count == 1 && k() == 1)
00130 {
00131 this.Add(lstGroups[0]);
00132 return;
00133 }
00134
00135 if (groupsArray == null || k() == 0)
00136 {
00137 if (lstGroups[0].n() == lstGroups[1].n())
00138 {
00139 base.AddRange(lstGroups);
00140 grpDifferences = new Group((Int16)groupsArray[0].n());
00141 for (Int32 i = 0; i < groupsArray[0].n(); i++)
00142 grpDifferences.Add(groupsArray[0][i] - groupsArray[1][i]);
00143
00144 return;
00145 }
00146 else
00147 throw new BiostatisticsException(
00148 "Groups in PairedT-TestGroups Collection must have the same number of elements.");
00149 }
00150 }
00151
00157 public new Int32 DegreesOfFreedom()
00158 { return n() - 1; }
00159
00166 public new NUMBER EstimatedPopulationVariance()
00167 {
00168 NUMBER decEstimatedPopulationVariance;
00169 try
00170 {
00171 decEstimatedPopulationVariance = SumOfSquaredDeviatesOfDifferences() / (n() - 1);
00172 }
00173 catch (Exception e)
00174 {
00175 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00176 }
00177 return decEstimatedPopulationVariance;
00178 }
00179
00187 public NUMBER EstimatedStandardErrorOfDifferencesOfMeans()
00188 {
00189 NUMBER decEstimatedStandardErrorOfDifferencesOfMeans;
00190 try
00191 {
00192 decEstimatedStandardErrorOfDifferencesOfMeans =
00193 DecMath.Sqrt(EstimatedPopulationVariance() / n());
00194 }
00195 catch (Exception e)
00196 {
00197 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00198 }
00199 return decEstimatedStandardErrorOfDifferencesOfMeans;
00200 }
00201
00209 public NUMBER MeanOfDifferences()
00210 {
00211 NUMBER decMeanOfDifferences;
00212 try
00213 {
00214 decMeanOfDifferences = SumOfDifferences() / grpDifferences.n();
00215 }
00216 catch (Exception e)
00217 {
00218 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00219 }
00220 return decMeanOfDifferences;
00221 }
00222
00227 public Int32 n()
00228 { return grpDifferences.n(); }
00229
00234 public override NUMBER p()
00235 { return Distributions.ProbabilityT(t(), DegreesOfFreedom()); }
00236
00242 public NUMBER SumOfDifferences()
00243 {
00244 NUMBER decSumOfDifferences = 0.0m;
00245
00246 foreach (NUMBER value in grpDifferences)
00247 decSumOfDifferences += value;
00248
00249 return decSumOfDifferences;
00250 }
00251
00257 public NUMBER SumOfSquaredDeviatesOfDifferences()
00258 {
00259 return grpDifferences.SumOfSquaredDeviates();
00260 }
00261
00268 public override NUMBER t()
00269 {
00270 NUMBER decT;
00271 try
00272 {
00273 decT = MeanOfDifferences() / EstimatedStandardErrorOfDifferencesOfMeans();
00274 }
00275 catch (Exception e)
00276 {
00277 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00278 }
00279 return decT;
00280 }
00281 #endregion
00282 }
00283 }