Hydrological Analysis and Calculation-average annual traffic trend test (Mann-Kendall method and linear regression method)

Source: Internet
Author: User

[Cpp]
// Average annual traffic trend test. h
 
// Trend analysis of average annual traffic using the Mann-Kendall Method
Void MannKendall ()
{
Using namespace std;
Int S = 0; // The Statistical variable for the test
Double VarS, // returns the variance of variable S.
Z; // standard normal statistical variable variance
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 <"average annual traffic trend test -- Mann-Kendall test:" <endl
<"Standard normal statistical variable Z =" <Z <endl
<"Input 1 if the Mann-Kendall test is passed; otherwise, disable it! "<Endl;
Cin> Z; // stay on the console
Cout <endl;
}
 
 
// Trend Analysis Based on Linear Regression of average annual traffic
Double Normal (double z)
{// Returns the density function of the standard normal distribution.
Double temp;
Temp = exp (-1) * z/2)/sqrt (2 * PI );
Return temp;
}
Double NormSDist (const double z)
{// Returns the cumulative frequency function of the standard normal distribution.
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 ()
{// Linear regression test
Using namespace std;
Double AverageYearQ = 0,
AverageT = (1 + Y)/2.0,
A, B, // undetermined Regression Coefficient
R = 0, // linear correlation coefficient
T, // t statistic
SigmaT = 0,
SigmaYearQ = 0, // Mean Deviation
NewYearQ [Y], // average annual traffic in ascending order
Fn, // sample accumulation frequency
F0, // theoretical accumulation frequency
D_n_alpha = 0.202737, // rejection threshold when the notable level is alpha and the sample size is n
MaxD = 0, // max (| Fn-F0 |)
Temp,
Sigmonoclonal = 0; // regression coefficient B standard variance
// Int order; // average annual traffic in ascending 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 <"average annual traffic trend test-linear regression test:" <endl
<"Normal Distribution K-S test statistic D =" <MaxD <endl
<"Critical Rejection Value of K-S test D (n, a) =" <D_n_alpha <endl;
B = r * sigmaYearQ/sigmaT;
A = AverageYearQ-B * AverageT;
For (int I = 0; I <Y; I ++)
Sigmonoclonal + = pow (YearQ [I]-(a + B * I), 2 );
Sigmonoclonal = pow (sigmonoclonal/(Y-2), 0.5)/(pow (sigmaT, 2) * (Y-1 ));
T = B/sigmonoclonal;
Cout <"linear correlation coefficient r =" <r <endl
<"The regression coefficient of the Q-dependent time series t of the average annual traffic is:" <endl
<"A =" <a <endl
<"B =" <B <endl
<"Hypothesis test statistic t =" <t <endl
<"Enter 1 if the linear regression test is passed; otherwise, disable it! "<Endl;
Cin> t; // stay on the console
Cout <endl;
}
By: Superwen_go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.