KSquare Utilities
StatisticalFunctions.cpp
Go to the documentation of this file.
1 /* StatisticalFunctions.cpp -- Basic Statistical Functions
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #include "FirstIncludes.h"
6 
7 #include <stdio.h>
8 #include <math.h>
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12 #include <vector>
13 
14 #include "MemoryDebug.h"
15 using namespace std;
16 
17 
19 #include "KKBaseTypes.h"
20 #include "OSservices.h"
21 using namespace KKB;
22 
23 
24 float KKB::PairedTTest (const VectorFloat& set1,
25  const VectorFloat& set2
26  )
27 {
28  // Formula was taken from MathWorld
29  // http://mathworld.wolfram.com/Pairedt-Test.html
30 
31  //ofstream out ("T-Test_Stats.txt", ios_base::app);
32  double numOfPairs = (double)set1.size ();
33 
34  if (numOfPairs != set2.size ())
35  {
36  cerr << std::endl
37  << std::endl
38  << "PairedTTest *** ERROR *** The two sets are not the same length." << std::endl
39  << std::endl
40  << " set1.size [" << set1.size () << "] set2.size [" << set2.size () << "]" << std::endl
41  << std::endl;
42  osWaitForEnter ();
43  exit (-1);
44  }
45 
46 
47  double xTotal = 0.0f;
48  double yTotal = 0.0f;
49 
50  kkint32 foldNum = 0;
51 
52 
53  //out << "X";
54  //for (foldNum = 0; foldNum < numOfPairs; foldNum++)
55  // out << "\t" << set1[foldNum];
56  //out << endl;
57 
58  //out << "Y";
59  //for (foldNum = 0; foldNum < numOfPairs; foldNum++)
60  // out << "\t" << set2[foldNum];
61  //out << endl;
62 
63 
64  for (foldNum = 0; foldNum < numOfPairs; foldNum++)
65  {
66  xTotal += set1[foldNum];
67  yTotal += set2[foldNum];
68  }
69 
70  double xMean = xTotal / numOfPairs;
71  double yMean = yTotal / numOfPairs;
72 
73  //out << endl
74  // << "X_Mean = " << xMean << endl
75  // << "Y_Mean = " << yMean << endl;
76 
77  double totalDeltaSquared = 0.0f;
78 
79  for (foldNum = 0; foldNum < numOfPairs; foldNum++)
80  {
81  double xDelta = set1[foldNum] - xMean;
82  double yDelta = set2[foldNum] - yMean;
83 
84  double deltaDelta = xDelta - yDelta;
85  totalDeltaSquared += (deltaDelta * deltaDelta);
86  }
87 
88  //out << endl
89  // << "totalDeltaSquared = "
90  // << totalDeltaSquared << endl;
91 
92 
93  double tValue = 0.0f;
94  if (totalDeltaSquared != 0.0)
95  tValue = fabs ((xMean - yMean) * sqrt ((numOfPairs * (numOfPairs - 1.0)) / totalDeltaSquared));
96 
97  //out << endl
98  // << "tValue = " << tValue << endl
99  // << endl;
100 
101  //out.close ();
102 
103  return float (tValue);
104 } /* PairedTTest */
105 
106 
107 
108 
109 float KKB::McNemarsTest (kkint32 size,
110  const bool* classedCorrectly1,
111  const bool* classedCorrectly2
112  )
113 {
114  kkint32 n00 = 0;
115  kkint32 n01 = 0;
116  kkint32 n10 = 0;
117  kkint32 n11 = 0;
118 
119  kkint32 x;
120 
121  for (x = 0; x < size; x++)
122  {
123  bool ca = classedCorrectly1[x];
124  bool cb = classedCorrectly2[x];
125 
126  if (!ca && !cb)
127  n00++; // true neg.
128 
129  else if (!ca && cb)
130  n01++; // false pos.
131 
132  else if (ca && !cb)
133  n10++; // false neg.
134 
135  else if (ca && cb)
136  n11++; // true pos
137  }
138 
139  float y = (float)fabs ((float)(n01 - n10)) - 1.0f; // (false pos - false neg) - 1.0;
140 
141  float mcNemars = 0.0;
142  float divisor = (float)n01 + (float)n10;
143 
144  if (divisor != 0.0f)
145  mcNemars = y * y / divisor;
146 
147  return mcNemars;
148 } /* McNemarsTest */
149 
150 
151 
152 /*
153 template <class T> T Min (T a,
154  T b
155  )
156 {
157  if (a <= b)
158  return a;
159  else
160  return b;
161 }
162 */
163 
164 
165 
167 {
168  // As defined by Andrew Remsen
169  // also look at http://www.pmel.noaa.gov/pubs/outstand/stab1646/statistics.shtml
170 
171  if (bins.size () < 1)
172  return 0.0f;
173 
174  kkuint32 x;
175  double total = 0.0;
176  double mean = 0.0;
177 
178  for (x = 0; x < bins.size (); x++)
179  total += bins[x];
180 
181  mean = total / double (bins.size ());
182 
183  double totalDeltaSquared = 0.0;
184 
185  for (x = 0; x < bins.size (); x++)
186  {
187  double delta = double (bins[x]) - mean;
188  totalDeltaSquared += delta * delta;
189  }
190 
191  double var = totalDeltaSquared / bins.size ();
192 
193 
194  double varDivMean = var / mean;
195  double oneDivMean = 1.0 / mean;
196 
197  float p = float ((varDivMean - 1.0 + mean) * oneDivMean);
198 
199  return p;
200 } /* LLoydsIndexOfPatchiness */
__int32 kkint32
Definition: KKBaseTypes.h:88
float McNemarsTest(kkint32 size, const bool *classedCorrectly1, const bool *classedCorrectly2)
std::vector< int > VectorInt
Definition: KKBaseTypes.h:138
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
KKTHread * KKTHreadPtr
std::vector< float > VectorFloat
Definition: KKBaseTypes.h:149
float PairedTTest(const KKB::VectorFloat &set1, const KKB::VectorFloat &set2)
float LLoydsIndexOfPatchiness(const KKB::VectorInt &bins)