[cpp]
//年均流量趨勢檢驗.h
//年均流量Mann-Kendall法趨勢分析
void MannKendall()
{
using namespace std;
int S = 0;//檢驗的統計變量
double VarS,//統計變量S的方差
Z ;//標准正態統計變量方差
S = 0;
for(int i = 0; i < Y; i++)
for(int j = i + 1; j < Y; j++)
{
if(YearQ[j]>YearQ[i]) S++;
if(YearQ[j]<YearQ[i]) S--;
}
VarS = 0;
VarS = Y*(Y - 1)*(2*Y + 5)/18.0;
if(S > 0) Z = (S - 1)/pow(VarS, 0.5);
if(S < 0) Z = (S + 1)/pow(VarS, 0.5);
cout<<"年均流量趨勢檢驗——Mann-Kendall檢驗:"<<endl
<<"標准正態統計變量Z= "<<Z<<endl
<<"Mann-Kendall檢驗通過請輸入1,否則請關閉!"<<endl;
cin>>Z;//控制台停留
cout<<endl;
}
//年均流量線性回歸法法趨勢分析
double Normal(double z)
{//返回標准正態分布的密度函數
double temp;
temp=exp((-1)*z*z/2)/sqrt(2*PI);
return temp;
}
double NormSDist(const double z)
{//返回標准正態分布的累積頻率函數
if(z > 6) return 1;
if(z < -6) return 0;
static const double gamma = 0.231641900,
a1 = 0.319381530,
a2 = -0.356563782,
a3 = 1.781477973,
a4 = -1.821255978,
a5 = 1.330274429;
double k = 1.0 / (1 + fabs(z) * gamma);
double n = k * (a1 + k * (a2 + k * (a3 + k * (a4 + k * a5))));
n = 1 - Normal(z) * n;
if(z < 0)
return 1.0 - n;
return n;
}
void XianXingJianYan()
{//線性回歸檢驗
using namespace std;
double AverageYearQ = 0,
AverageT = (1+Y)/2.0,
a, b,//待定回歸系數
r = 0,//線性相關系數
t,//t統計量
sigmaT =0,
sigmaYearQ = 0,//均方差
NewYearQ[Y],//按升序排列的年均流量
Fn,//樣本累積頻率
F0,//理論累積頻率
D_n_alpha = 0.202737,//顯著水平為alpha且樣本容量為n時的拒絕臨界值
MaxD = 0,//max(|Fn - F0|)
temp,
sigmab = 0;//回歸系數b標准方差
// int order;//升序排序年均流量
for(int i = 0; i < Y; i++)
{
AverageYearQ += YearQ[i];
}
AverageYearQ /= Y;
for(int i = 0; i < Y; i++)
{
r += (i - AverageT)*(YearQ[i] - AverageYearQ);
sigmaT += pow(i - AverageT, 2);
sigmaYearQ += pow(YearQ[i] - AverageYearQ, 2);
}
r /= pow(sigmaT*sigmaYearQ, 0.5);
sigmaT = pow(sigmaT/(Y - 1), 0.5);
sigmaYearQ = pow(sigmaYearQ/(Y - 1), 0.5);
for(int i = 0; i < Y; i++)
NewYearQ[i] = YearQ[i];
for(int i = 0; i < Y - 1; i++)
{
for(int j = i + 1; j < Y; j++)
if(NewYearQ[i] > NewYearQ[j])
{
temp = NewYearQ[i];
NewYearQ[i] = NewYearQ[j];
NewYearQ[j] = temp;
}
}
for(int i = 0; i < Y; i++)
{
Fn = (double)(i+1)/(Y + 1);
F0 = NormSDist((NewYearQ[i] - AverageYearQ)/sigmaYearQ);
if(MaxD < fabs(Fn - F0)) MaxD = fabs(Fn - F0);
}
cout<<"年均流量趨勢檢驗——線性回歸檢驗:"<<endl
<<"正態分布K-S檢驗統計量D ="<<MaxD<<endl
<<"K-S檢驗拒絕臨界值D(n, a)="<<D_n_alpha<<endl;
b = r*sigmaYearQ/sigmaT;
a = AverageYearQ - b*AverageT;
for(int i = 0; i < Y; i++)
sigmab += pow(YearQ[i] - (a + b*i), 2);
sigmab = pow(sigmab/(Y - 2), 0.5)/(pow(sigmaT, 2)*(Y - 1));
t = b/sigmab;
cout<<"線性相關系數r = "<<r<<endl
<<"年均流量Q倚時序t的回歸系數估計值分別為:"<<endl
<<"a = "<<a<<endl
<<"b = "<<b<<endl
<<"假設檢驗統計量t = "<<t<<endl
<<"線性回歸檢驗通過請輸入1,否則請關閉!"<<endl;
cin>>t;//控制台停留
cout<<endl<<endl<<endl;
}
作者:Superwen_go