invsq2pi=1.0/sqrt(2.0*pi);
// Test special cases
sbeps=1.e-15;
m=0; s=1; u=0;
Normaldist norm1 = new Normaldist(m,s);
localflag = localflag || abs(norm1.p(u) - invsq2pi) > sbeps;
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Normaldist: Special case #1 failed");
}
m=1; s=1; u=m;
Normaldist norm2 = new Normaldist(m,s);
localflag = localflag || abs(norm2.p(u) - invsq2pi) > sbeps;
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Normaldist: Special case #2 failed");
}
m=1; s=1; u=0;
Normaldist norm3 = new Normaldist(m,s);
localflag = localflag || abs(norm3.p(u) - invsq2pi*exp(-0.5)) > sbeps;
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Normaldist: Special case #3 failed");
}
m=1; s=2; u=1;
Normaldist norm4 = new Normaldist(m,s);
localflag = localflag || abs(norm4.p(u) - invsq2pi/2.0) > sbeps;
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Normaldist: Special case #4 failed");
}
m=1; s=2; u=0;
Normaldist norm5 = new Normaldist(m,s);
localflag = localflag || abs(norm5.p(u) - invsq2pi/2.0*exp(-1.0/8.0)) > sbeps;
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Normaldist: Special case #5 failed");
}
// integral of distribution is one
sbeps=1.e-10;
m=1.0;s=2.0;
func_Normaldist dist = new func_Normaldist(m,s);
localflag = abs(1.0-qsimp(dist,-20.0,20.0)) > sbeps;
// System.out.printf(setprecision(15) << 1.0-qsimp(dist,-20.0,20.0));
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Normaldist: Distribution is not normalized to 1.0");
}
// cdf agrees with incomplete integral
sbeps=1.e-10;
m=0.5;s=1.5;
func_Normaldist dist2 =new func_Normaldist(m,s);
Normaldist normcdf = new Normaldist(m,s);
for (i=0;i<N;i++) {
c[i]=qsimp(dist2,-20.0,x[i]);
d[i]=normcdf.cdf(x[i]);
// System.out.printf(c[i]-d[i]);
localflag = localflag || abs(c[i]-d[i]) > sbeps;
}
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Normaldist: cdf does not agree with result of quadrature");
}
// inverse cdf agrees with cdf
m=0.5;s=1.5;
Normaldist normc = new Normaldist(m,s);
Ran myran = new Ran(17);
sbeps=5.0e-14;
for (i=0;i<1000;i++) {
u=m-3.0*s+6.0*s*myran.doub();
a=normc.cdf(u);
b=normc.invcdf(a);
// System.out.printf(setprecision(15) << u << " %f\n", b << " %f\n", abs(u-b));
localflag = localflag || abs(u-b) > sbeps;
}
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Normaldist: Inverse cdf does not accurately invert the cdf");
}
// Fingerprint test
m=0.5;s=1.5;
Normaldist normf = new Normaldist(m,s);
for (i=0;i<N;i++) {
p[i]=normf.p(x[i]);
// System.out.printf(setprecision(17) << p[i] << " %f\n", pexp[i]);
}
// System.out.println("Normaldist: Maximum discrepancy = %f\n", maxel(vecsub(p,pexp)));
localflag = localflag || maxel(vecsub(p,pexp)) > sbeps;
globalflag = globalflag || localflag;