Package com.nr.sf

Examples of com.nr.sf.Binomialdist


    System.out.println("Testing Binomialdist");

    // Test special cases

    n=1; p=0.5; ju=1;
    Binomialdist norm2 = new Binomialdist(n,p);
    localflag = abs(norm2.p(ju)-0.5) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** Binomialdist: Special case #1 failed");
     
    }

    n=2; p=0.5; ju=1;
    Binomialdist norm3 = new Binomialdist(n,p);
    localflag = abs(norm3.p(ju)-0.5) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** Binomialdist: Special case #2 failed");
     
    }

    n=2; p=0.5; ju=2;
    Binomialdist norm4 = new Binomialdist(n,p);
    localflag = abs(norm4.p(ju)-0.25) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** Binomialdist: Special case #3 failed");
     
    }

    n=3; p=1.0/3.0; ju=2;
    Binomialdist norm5 = new Binomialdist(n,p);
    localflag = abs(norm5.p(ju)-2.0/9.0) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** Binomialdist: Special case #4 failed");
     
    }

    // Sum of distribution is one
    sbeps=1.e-14;
    n=10; p=0.4;
    Binomialdist dist1 = new Binomialdist(n,p);
    sum=0.0;
    for (i=0;i<=n;i++)
      sum += dist1.p(i);
    localflag = abs(1.0-sum) > sbeps;
//    System.out.printf(setprecision(15) << 1.0-sum);
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** Binomialdist: Distribution is not normalized to 1.0");
     
    }

    // cdf agrees with incomplete integral
    sbeps=1.e-14;
    n=10; p=0.4;
    Binomialdist dist3 =new Binomialdist(n,p);
    sum=0.0;
    localflag=false;
    for (i=0;i<n;i++) {
      sum += dist3.p(i);
      c[i]=sum;
      d[i]=dist3.cdf(i+1);
//      System.out.printf(c[i]-d[i]);
      localflag = localflag || abs(c[i]-d[i]) > sbeps;
    }
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** Binomialdist: cdf does not agree with summation");
     
    }

    // inverse cdf agrees with cdf
    n=10; p=0.4;
    Binomialdist normc=new Binomialdist(n,p);
    sbeps=5.0e-14;
    localflag=false;
    for (i=1;i<10;i++) {
      a=normc.cdf(i);
      jb=normc.invcdf(a);
//      if (abs(i-jb) > sbeps) {
//        System.out.printf(setprecision(15) << i << " %f\n", jb << " %f\n", abs(i-jb));
//      }
      localflag = localflag || (i-jb) != 0 && (i-jb-1) != 0;
    }
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** Binomialdist: Inverse cdf does not accurately invert the cdf");
     
    }
     
    // Fingerprint test
    n=M-1; p=0.4;
    sbeps=2.e-15;
    Binomialdist normf=new Binomialdist(n,p);
    for (i=0;i<M;i++) {
      pp[i]=normf.p(i);
//      System.out.printf(setprecision(17) << pp[i] << " %f\n", pexp[i]);
    }
//    System.out.println("Binomialdist: Maximum discrepancy = %f\n", maxel(vecsub(pp,pexp)));
    localflag = maxel(vecsub(pp,pexp)) > sbeps;
    globalflag = globalflag || localflag;
View Full Code Here


      fail("*** Binomialdev: dev() does not match fingerprint");
     
    }

    // Check statistics
    Binomialdist expect = new Binomialdist(M,pp);
    for (i=0;i<M;i++) {
      ebins[i]=(N)*expect.p(i);
      bins[i]=0;
    }
    for (i=0;i<N;i++) {
      nbin=myran.dev();
      if ((nbin >= 0) && (nbin < M)) bins[nbin] += 1;
View Full Code Here

TOP

Related Classes of com.nr.sf.Binomialdist

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.