1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#ifndef STATS_H
#define STATS_H
#include <cmath>
//Calculates Mean and returns the result
template <class T>
double Mean ( T array[], int size ){
double sum = 0;
int i;
for ( i = 0; i < size; i++ ){
sum += (double) array[i];
}
return ( sum / size );
}
//Calculates the Population Standard Deviation and returns the result
//It is overloaded to call Mean() when not given Mean
template <class T>
double Pop_Std_Dev ( T array[], int size ){
double sum = 0, mean = Mean( array, size );
int i;
for ( i = 0; i < size; i++ ){
sum += pow( ( mean - (double)array[i] ), 2);
}
return sqrt( ( sum / size ) );
}
template <class T>
double Pop_Std_Dev ( T array[], int size, double mean ){
double sum = 0;
int i;
for ( i = 0; i < size; i++ ){
sum += pow( ( mean - (double)array[i] ), 2);
}
return sqrt( ( sum / size ) );
}
//Calculates the Standard Deviation of a Sample and returns the result
//It is overloaded to call Mean() when not given Mean
template <class T>
double Samp_Std_Dev ( T array[], int size ){
double sum = 0, mean = Mean( array, size );
int i;
for ( i = 0; i < size; i++ ){
sum += pow( ( mean - (double)array[i] ), 2);
}
return sqrt( ( sum / ( size - 1) ) );
}
template <class T>
double Samp_Std_Dev ( T array[], int size, double mean ){
double sum = 0;
int i;
for ( i = 0; i < size; i++ ){
sum += pow( ( mean - (double)array[i] ), 2);
}
return sqrt( ( sum / ( size - 1) ) );
}
//Calculates the Z Score of a value, or array of values, then returns the result(s)
//It is overloaded to call Mean(), or Pop_Std_Dev() depending on what values it is called with
//Due to the Z Score only being values with a known population mean it calls Pop_Std_Dev() instead of Samp_Std_Dev()
template <class T>
double Z_Score ( T value, double mean, double Std ){
return (double)( ( value - mean ) / Std );
}
template <class T>
double Z_Score ( T array[], int size ){
int i;
double mean = Mean( array, size ), Std = Pop_Std_Dev( array, size, mean );
double * Z_array = new double [size];
for ( i = 0; i < size; i++ )
{
Z_array[i] = ( ( array[i] - mean ) / Std );
}
return Z_array;
}
template <class T>
double Z_Score ( T array[], int size, double mean ){
int i;
double Std = Pop_Std_Dev( array, size, mean );
double * Z_array = new double [size];
for ( i = 0; i < size; i++ )
{
Z_array[i] = ( ( array[i] - mean ) / Std );
}
return Z_array;
}
template <class T>
double Z_Score ( T array[], int size, double mean, double Std ){
int i;
double * Std_array = new double [size];
for ( i = 0; i < size; i++ )
{
Std_array[i] = ( ( array[i] - mean ) / Std );
}
return Std_array;
}
//Calculates the T Score of a value, or array of values, then returns the result(s)
//It is overloaded to call Mean(), or Samp_Std_Dev() depending on what values it is called with
//Due to the T Score being values with a sample mean not population it calls Samp_Std_Dev() instead of Pop_Std_Dev()
//***Note***: Since mean and standard deviation are inputed for the first function and last they are identical to their Z_Score() companions
template <class T>
double T_Score ( T value, double mean, double Std ){
return (double)( ( value - mean ) / Std );
}
template <class T>
double T_Score ( T array[], int size ){
int i;
double mean = Mean( array, size ), Std = Samp_Std_Dev( array, size, mean );
double * Z_array = new double [size];
for ( i = 0; i < size; i++ )
{
Z_array[i] = ( ( array[i] - mean ) / Std );
}
return Z_array;
}
template <class T>
double T_Score ( T array[], int size, double mean ){
int i;
double Std = Samp_Std_Dev( array, size, mean );
double * Z_array = new double [size];
for ( i = 0; i < size; i++ )
{
Z_array[i] = ( ( array[i] - mean ) / Std );
}
return Z_array;
}
template <class T>
double T_Score ( T array[], int size, double mean, double Std ){
int i;
double * Std_array = new double [size];
for ( i = 0; i < size; i++ )
{
Std_array[i] = ( ( array[i] - mean ) / Std );
}
return Std_array;
}
#endif
|