📄 d13r10.cpp
字号:
#include "iostream.h"
#include "stdlib.h"
#include "math.h"
double ran1(long& idum)
{
int j,iff=-1;
static long ix1,ix2,ix3;
static double r[98];
long m1 = 259200; long m2 = 134456; long m3 = 243000;
long ia1 = 7141; long ia2 = 8121; long ia3 = 4561;
long ic1 = 54773; long ic2 = 28411; long ic3 = 51349;
double rm1 = 0.0000038580247; double rm2 = 0.0000074373773;
if (idum < 0 || iff == 0)
{
iff = 1;
ix1 = (ic1 - idum) % m1;
ix1 = (ia1 * ix1 + ic1) % m1;
ix2 = ix1 % m2;
ix1 = (ia1 * ix1 + ic1) % m1;
ix3 = ix1 % m3;
for (j = 1; j<=97; j++)
{
ix1 = (ia1 * ix1 + ic1) % m1;
ix2 = (ia2 * ix2 + ic2) % m2;
r[j] = (double(ix1) + double(ix2) * rm2) * rm1;
}
idum = 1;
}
ix1 = (ia1 * ix1 + ic1) % m1;
ix2 = (ia2 * ix2 + ic2) % m2;
ix3 = (ia3 * ix3 + ic3) % m3;
j = 1 + int((97 * ix3) / m3);
if (j > 97 || j < 1)
{
cout<<"abnormal exit in ran1"<<endl;
exit(1);
}
double temp=r[j];
r[j] = (double(ix1) + double(ix2) * rm2) * rm1;
return temp;
}
double gasdev(long& idum)
{
static int iset;
static double gset;
double v1,v2,r,fac;
if (iset == 0)
{
do
{
v1 = 2.0 * ran1(idum) - 1.0;
v2 = 2.0 * ran1(idum) - 1.0;
r = v1 * v1 + v2 * v2;
}while (r >= 1.0 || r == 0);
fac = sqrt(-2.0 * log(r) / r);
gset = v1 * fac;
iset = 1;
return v2 * fac;
}
else
{
iset = 0;
return gset;
}
}
double gammln(double xx)
{
double cof[7];
cof[1] = 76.18009173;
cof[2] = -86.50532033;
cof[3] = 24.01409822;
cof[4] = -1.231739516;
cof[5] = 0.00120858003;
cof[6] = -0.00000536382;
double ser,x,tmp,stp = 2.50662827465;
double half = 0.5;
double one = 1.0;
double fpf = 5.5;
int j;
x = xx - one;
tmp = x + fpf;
tmp = (x + half) * log(tmp) - tmp;
ser = one;
for (j = 1; j<=6; j++)
{
x = x + one;
ser = ser + cof[j] / x;
}
return tmp + log(stp * ser);
}
void gcf(double& gammcf, double a, double x, double& gln)
{
int an,n,itmax = 100;
double anf,g,ana,eps = 0.0000003;
gln = gammln(a);
double gold = 0.0;
double a1,a0 = 1.0;
a1 = x;
double b0 = 0.0;
double b1 = 1.0;
double fac = 1.0;
for (n = 1; n<=itmax; n++)
{
an = n;
ana = an - a;
a0 = (a1 + a0 * ana) * fac;
b0 = (b1 + b0 * ana) * fac;
anf = an * fac;
a1 = x * a0 + anf * a1;
b1 = x * b0 + anf * b1;
if (a1 != 0.0)
{
fac = 1.0 / a1;
g = b1 * fac;
if (fabs((g - gold) / g) < eps)
{
gammcf = exp(-x + a * log(x) - gln) * g;
return;
}
gold = g;
}
}
cout<< "a too large, itmax too small"<<endl;
gammcf = exp(-x + a * log(x) - gln) * g;
}
void gser(double& gamser, double a, double x, double& gln)
{
int n,itmax = 100;
double ap,sum,del,eps = 0.0000003;
gln = gammln(a);
if (x <= 0.0)
{
if (x < 0.0)
{
cout<<"pause in gser"<<endl;
return;
}
gamser = 0.0;
return;
}
ap = a;
sum = 1.0 / a;
del = sum;
for (n = 1; n<=itmax; n++)
{
ap = ap + 1.0;
del = del * x / ap;
sum = sum + del;
if (fabs(del) < fabs(sum) * eps)
{
gamser = sum * exp(-x + a * log(x) - gln);
return;
}
}
cout<< "a too large, itmax too small";
gamser = sum * exp(-x + a * log(x) - gln);
}
double gammp(double a, double x)
{
double gln;
if (x < 0.0 || a <= 0.0)
{
cout<< "pause in gammp"<<endl;
exit(1);
}
if (x < a + 1.0)
{
double gamser;
gser(gamser, a, x, gln);
return gamser;
}
else
{
double gammcf;
gcf(gammcf, a, x, gln);
return 1.0 - gammcf;
}
}
void sort(int n, double ra[])
{
int i,j,l,ir;
double rra;
l = n / 2 + 1;
ir = n;
do
{
if (l > 1)
{
l = l - 1;
rra = ra[l];
}
else
{
rra = ra[ir];
ra[ir] = ra[1];
ir = ir - 1;
if (ir == 1)
{
ra[1] = rra;
return;
}
}
i = l;
j = l + l;
while (j <= ir)
{
if (j < ir)
{
if (ra[j] < ra[j + 1])
{
j = j + 1;
}
}
if (rra < ra[j])
{
ra[i] = ra[j];
i = j;
j = j + j;
}
else
{
j = ir + 1;
}
}
ra[i] = rra;
}while(1);
}
double probks(double alam)
{
double eps1 = 0.001;
double eps2 = 0.00000001;
double a2 = -2.0 * alam * alam;
double fac = 2.0;
double aaa,temp = 0.0;
double term,termbf = 0.0;
for (int j = 1; j<=100; j++)
{
term = fac * exp(a2 * j * j);
temp = temp + term;
aaa = fabs(term);
if (aaa < eps1 * termbf || aaa < eps2 * temp)
{
return temp;
}
fac = -fac;
termbf = fabs(term);
}
return 1.0;
}
double erf(double x)
{
if (x < 0.0)
{
return -gammp(0.5, x * x);
}
else
{
return gammp(0.5, x * x);
}
}
double func(double x)
{
double y;
y = x / sqrt(2.0);
return erf(y);
}
void ksone(double data[], int n, double& d, double& prob)
{
sort(n, data);
int en = n;
d = 0.0;
double fq,ff,dt,fo = 0.0;
for (int j = 1; j<=n; j++)
{
fq = (double)j /(double)en;
ff = func(data[j]);
dt = fabs(fo - ff);
if (fabs(fq - ff) > dt)
{
dt = fabs(fq - ff);
}
if (dt > d)
{
d = dt;
}
fo = fq;
}
prob = probks(sqrt(en) * d);
}
void main()
{
//program d13r10
//driver for routine ksone
int i,j,npts = 1000;
double var,factr,d,prob,eps = 0.1;
double data[1001];
long idum = -5;
cout<<endl;
cout<<"Variance ratio K-S statistic Probability"<<endl;
cout.setf(ios::fixed|ios::left);
cout.precision(4);
for (i = 1; i<=11; i++)
{
var = 1.0 + (i - 1) * eps;
factr = sqrt(var);
for (j = 1; j<=npts; j++)
{
data[j] = factr * fabs(gasdev(idum));
}
ksone(data, npts, d, prob);
cout<<" ";
cout.width(14);
cout<<var;
cout.width(14);
cout<<d;
cout.width(14);
cout.unsetf(ios::fixed);
cout.setf(ios::scientific);
cout<<prob<<endl;
cout.unsetf(ios::scientific);
cout.setf(ios::fixed);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -