计算 变异系数 标准差 标准偏差 相关系数 平滑指数 回归系数等C++ 模板类
生活随笔
收集整理的這篇文章主要介紹了
计算 变异系数 标准差 标准偏差 相关系数 平滑指数 回归系数等C++ 模板类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <math.h>
#define MINZERO (0.0000001)class CCalculate
{
public:CCalculate();virtual ~CCalculate();//獲取最大值template<class T>static T GetMax( T* x,int nLen) {T Temp,Max;Max = *x;for (int i=0; i<nLen;i++){Temp = *(x+i);if (Temp >= Max){Max = Temp;}}return Max;}//獲取最大值序號 從0開始計算template<class T>static int GetMaxNum( T* x,int nLen) {T Temp,Max;Max = *x;int n = 0;for (int i=0; i<nLen;i++){Temp = *(x+i);if (Temp >= Max){Max = Temp;n = i;}}return n;}//獲取最小值template<class T>static T GetMin( T* x,int nLen) {T Temp,Min;Min = *x;for (int i=0;i<nLen;i++){Temp = *(x+i);if(Temp <= Min){Min = Temp;}}return Min;}//獲取最小值序號 從0開始計算template<class T>static int GetMinNum( T* x,int nLen) {T Temp,Min;Min = *x;int n = 0;for (int i=0;i<nLen;i++){Temp = *(x+i);if(Temp <= Min){Min = Temp;n = i;}}return n;}//獲取平均值template<class T>static double GetAve( T* x,int nLen){double sum = 0.00;double Ave = 0.00;for (int i=0;i<nLen;i++){sum += (double)*(x+i);}Ave = sum/nLen;return Ave;}//獲取標準差=標準偏差template<class T>static double GetSD( T* x,int nLen){if (nLen <= 0){return -1;}double sum = 0.00;double Ave = 0.00;Ave = GetAve(x,nLen);for (int i=0; i< nLen; ++i){sum += (*(x+i)-Ave)*(*(x+i)-Ave);}double SD = sqrt((sum/nLen));return SD;}//獲取標準誤差SEtemplate<class T>static double GetSE(IN T* x, IN int nLen) {double SD = 0.00;SD = GetSD(x,nLen);double SE = double(SD/sqrt((double)nLen));return SE;}//獲取變異系數CVtemplate<class T>static BOOL GetCV(IN T* x, IN int nLen, OUT double& CV) {double Ave = 0.00;double SD = 0.00;Ave = GetAve(x,nLen);if(Ave < MINZERO && Ave > -MINZERO){return FALSE;}SD = GetSD(x,nLen);CV = SD/(double)Ave;return TRUE;}//獲取平滑指數SI:Smooth Indextemplate<class T>static BOOL GetSI(IN T* x, IN int nLen,OUT double& SI){double CV = 0.00;if(!GetCV(x, nLen,CV)){return FALSE;}if (CV < MINZERO && CV > -MINZERO){return FALSE;}SI = 1.00/CV;return TRUE;}//獲取大于thres的數據的百分比template<class T>static double GetUpPercent(T* x, int nLen, T thres){double dPer = 0.00;int nCount = 0;for (int i=0;i<nLen;i++){if(*(x+i) > thres){nCount++;}}dPer = ((double)nCount)/nLen;return dPer;}//獲取大于thres的數據的個數template<class T>static int GetUpNum(T* x, int nLen, T thres){int nCount = 0;for (int i=0;i<nLen;i++){if(*(x+i) > thres){nCount++;}}return nCount;}//獲取小于thres的數據的百分比template<class T>static double GetDownPercent(T* x, int nLen, T thres){double dPer = 0.00;int nCount = 0;for (int i=0;i<nLen;i++){if(*(x+i) < thres){nCount++;}}dPer = ((double)nCount)/nLen;return dPer;}//獲取小于thres的數據的個數template<class T>static int GetDownNum(T* x, int nLen, T thres){int nCount = 0;for (int i=0;i<nLen;i++){if(*(x+i) < thres){nCount++;}}return nCount;}//獲取兩組數據的相關系數,無前后關系template<class T>static BOOL GetCorrCoef(IN T* x,IN T* y,IN int nLen,OUT double& r){double xAve = GetAve(x,nLen);double yAve = GetAve(y,nLen);double Sum = 0.00;double xSum2 = 0.00;double ySum2 = 0.00;for (int i=0;i<nLen;i++){Sum += (*(x+i)-xAve)*(*(y+i)-yAve);xSum2 += (*(x+i)-xAve)*(*(x+i)-xAve);ySum2 += (*(y+i)-yAve)*(*(y+i)-yAve);}double res = sqrt(xSum2*ySum2);if (res < MINZERO && res > -MINZERO){return FALSE;}r = Sum/res; return TRUE;}//獲取兩組數據的回歸系數,有前后關系 x自變量,y因變量 b斜率 a截距template<class T>static BOOL GetRegressCoef(IN T* x,IN T* y,IN int nLen,OUT double& b,OUT double& a){double xAve = GetAve(x,nLen);double yAve = GetAve(y,nLen);double Sum = 0.00;double xSum2 = 0.00;for (int i=0;i<nLen;i++){Sum += (*(x+i)-xAve)*(*(y+i)-yAve);xSum2 += (*(x+i)-xAve)*(*(x+i)-xAve);}if (xSum2 < MINZERO && xSum2 > -MINZERO){return FALSE;}b = Sum/xSum2; a = yAve - xAve*b;return TRUE;}
};
CCalculate::CCalculate() {}CCalculate::~CCalculate() {}
CCalculate::CCalculate() {}CCalculate::~CCalculate() {}
總結
以上是生活随笔為你收集整理的计算 变异系数 标准差 标准偏差 相关系数 平滑指数 回归系数等C++ 模板类的全部內容,希望文章能夠幫你解決所遇到的問題。