00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005
00006 namespace StephenAshley.Biostatistics
00007 {
00008 using NUMBER = Decimal;
00009
00014 public class CochranQGroupsCollection : GroupsCollection
00015 {
00019 public CochranQGroupsCollection() : base() { }
00020
00026 public CochranQGroupsCollection(List<Group> groups) : base(groups) { }
00027
00039 public override void Add(Group group)
00040 {
00041 if (group == null)
00042 throw new BiostatisticsException(
00043 "group is a null reference.");
00044
00045 if (group.n() == 0)
00046 throw new BiostatisticsException(
00047 "group has no members.");
00048
00049 if (groupsArray != null && k() > 0)
00050 {
00051 for (int i = 0; i < k(); i++)
00052 {
00053 if (group.n() != groupsArray[i].n())
00054 throw new BiostatisticsException(
00055 "Group sizes do not match.");
00056 }
00057 }
00058
00059 base.Add(group);
00060 return;
00061 }
00062
00075 public Int32[] B()
00076 {
00077 if (groupsArray == null)
00078 throw new BiostatisticsException("groupsArray is a null reference.");
00079 if (k() < 2)
00080 throw new BiostatisticsException(
00081 "CochranQGroupsCollection contains fewer than two Groups.");
00082 if (N() == 0)
00083 throw new BiostatisticsException(
00084 "CochranQGroupsCollection contains no data.");
00085
00086 Int32[] arrayB = new Int32[N()];
00087
00088 for (int j = 0; j < N(); j++)
00089 {
00090 arrayB[j] = 0;
00091 for (int i = 0; i < k(); i++)
00092 {
00093 arrayB[j] += (Int32)groupsArray[i][j];
00094 }
00095 }
00096 return arrayB;
00097 }
00098
00110 public Int32 DegreesOfFreedom()
00111 {
00112 if (groupsArray == null)
00113 throw new BiostatisticsException("groupsArray is a null reference.");
00114 if (k() < 2)
00115 throw new BiostatisticsException(
00116 "CochranQGroupsCollection contains fewer than two Groups.");
00117 if (N() == 0)
00118 throw new BiostatisticsException(
00119 "CochranQGroupsCollection contains no data.");
00120 return k() - 1;
00121 }
00122
00135 public Int32[] G()
00136 {
00137 if (groupsArray == null)
00138 throw new BiostatisticsException("groupsArray is a null reference.");
00139 if (k() < 2)
00140 throw new BiostatisticsException(
00141 "CochranQGroupsCollection contains fewer than two Groups.");
00142 if (N() == 0)
00143 throw new BiostatisticsException(
00144 "CochranQGroupsCollection contains no data.");
00145
00146 Int32[] arrayG = new Int32[k()];
00147
00148 for (int i = 0; i < k(); i++)
00149 {
00150 arrayG[i] = 0;
00151 for (int j = 0; j < N(); j++)
00152 {
00153 arrayG[i] += (Int32)groupsArray[i][j];
00154 }
00155 }
00156 return arrayG;
00157 }
00158
00164 public Int32 N()
00165 {
00166 if (groupsArray == null)
00167 return 0;
00168 return groupsArray[0].n();
00169 }
00170
00176 public NUMBER p()
00177 {
00178 return (NUMBER)Distributions.ProbabilityChiSq((double)Q(), DegreesOfFreedom());
00179 }
00180
00186 public NUMBER Q()
00187 {
00188 Int32 [] aG = G();
00189 Int32 [] aB = B();
00190 NUMBER decA = 0.0m;
00191
00192 for (int i = 0; i < k(); i++)
00193 {
00194 decA += aG[i] * aG[i];
00195 }
00196
00197 NUMBER decB = 0.0m;
00198
00199 for (int i = 0; i < k(); i++)
00200 {
00201 decB += aG[i];
00202 }
00203
00204 NUMBER decC = decB * decB / (NUMBER)k();
00205
00206 NUMBER decD = (k() - 1) * (decA - decC);
00207
00208 NUMBER decE = 0.0m;
00209
00210 for (int j = 0; j < N(); j++)
00211 {
00212 decE += aB[j];
00213 }
00214
00215 NUMBER decF = 0.0m;
00216 for (int j = 0; j < N(); j++)
00217 {
00218 decF += aB[j] * aB[j];
00219 }
00220
00221 NUMBER decG = decF / k();
00222
00223 NUMBER decH = decE - decG;
00224 NUMBER decQ = decD / decH;
00225 return decQ;
00226 }
00227 }
00228 }