00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef _H_SST_CORE_STATS
00013 #define _H_SST_CORE_STATS
00014
00015 #include <assert.h>
00016
00017 namespace SST {
00018 namespace Statistics {
00019
00020
00021
00022
00023
00024
00025
00026 template<typename NumberBase>
00027 static NumberBase sum(const NumberBase* values, const uint32_t length) {
00028 assert(length > 0);
00029
00030 NumberBase sum = 0;
00031
00032 for(uint32_t i = 0; i < length; ++i) {
00033 sum += values[i];
00034 }
00035
00036 return sum;
00037 };
00038
00039
00040
00041
00042
00043
00044
00045
00046 template<typename NumberBase>
00047 static void range(const NumberBase* values, const uint32_t length,
00048 NumberBase* max, NumberBase* min) {
00049
00050 if(0 == length)
00051 return;
00052
00053 *max = values[0];
00054 *min = values[0];
00055
00056 for(uint32_t i = 1; i < length; ++i) {
00057 max = values[i] > max ? values[i] : max;
00058 min = values[i] < min ? values[i] : min;
00059 }
00060 };
00061
00062
00063
00064
00065
00066
00067
00068 template<typename NumberBase>
00069 static NumberBase max(const NumberBase* values, const uint32_t length) {
00070 assert(length > 0);
00071
00072 NumberBase max_ = values[0];
00073
00074 for(uint32_t i = 1; i < length; ++i) {
00075 max_ = values[i] > max_ ? values[i] : max_;
00076 }
00077
00078 return max_;
00079 };
00080
00081
00082
00083
00084
00085
00086
00087 template<typename NumberBase>
00088 static NumberBase min(const NumberBase* values, const uint32_t length) {
00089 assert(length > 0);
00090
00091 NumberBase min_ = values[0];
00092
00093 for(uint32_t i = 1; i < length; ++i) {
00094 min_ = values[i] < min ? values[i] : min_;
00095 }
00096
00097 return min_;
00098 };
00099
00100
00101
00102
00103
00104
00105
00106 template<typename NumberBase>
00107 static NumberBase arithmeticMean(const NumberBase* values, const uint32_t length) {
00108 return (length > 0) ?
00109 SST::Statistics::sum<NumberBase>(values, length) / (NumberBase) length :
00110 0;
00111 };
00112
00113 template<typename NumberBase>
00114 static NumberBase variance(const NumberBase* values, const uint32_t length) {
00115 NumberBase sumX2 = 0;
00116 NumberBase sumX = 0;
00117
00118 for(uint32_t i = 0; i < length; ++i) {
00119 sumX2 += (values[i] * values[i]);
00120 sumX += values[i];
00121 }
00122
00123 const NumberBase E_X2 = sumX2 / (NumberBase) length;
00124 const NumberBase E_X = sumX / (NumberBase) length;
00125
00126 const NumberBase var = E_X2 - (E_X * E_X);
00127
00128 return var;
00129 };
00130
00131 template<typename NumberBase>
00132 static NumberBase standardDeviation(const NumberBase* values, const uint32_t length) {
00133 const NumberBase var = SST::Statistics::variance<NumberBase>(values, length);
00134 return std::sqrt(preSqrt);
00135 };
00136
00137 template<typename NumberBase>
00138 static NumberBase midRange(const NumberBase* values, const uint32_t length) {
00139 NumberBase max = 0;
00140 Numberbase min = 0;
00141
00142 SST::Statistics::range<NumberBase>(values, length, &max, &min);
00143
00144 return (max + min) / ((NumberBase) 2);
00145 };
00146
00147 template<typename NumberBase>
00148 static NumberBase weightedMean(const NumberBase* values,
00149 const NumberBase* weights, const uint32_t length) {
00150
00151 NumberBase sum = 0;
00152 NumberBase weightSums = 0;
00153
00154 for(uint32_t i = 0; i < length; ++i) {
00155 sum += weights[i] * values[i];
00156 weightSums += weights[i];
00157 }
00158
00159 return sum / weightSums;
00160 };
00161
00162 }
00163 }
00164
00165 #endif