Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.36 KB | None | 0 0
  1. package yawning.mew;
  2.  
  3. import org.apache.commons.math.distribution.NormalDistributionImpl;
  4.  
  5. import ec.util.MersenneTwisterFast;
  6.  
  7. public class TestCDF {
  8.     private static double ATTACKS = 3.0;
  9.     private static double PCRIT = 0.6;
  10.     private static int ITERATIONS = 1000000;
  11.     private static double STEP = 0.01;
  12.  
  13.     public static void main(String args[]) {
  14.         try {
  15.             MersenneTwisterFast random = new MersenneTwisterFast();
  16.  
  17.             for (double nrAttacks = ATTACKS; nrAttacks <= ATTACKS + 1; nrAttacks += STEP) {
  18. //              System.out.println("Attacks: " + nrAttacks);
  19.  
  20.                 // Gaussian CDF
  21.                 double meanCP = nrAttacks + nrAttacks * PCRIT;
  22.                 double sdCP = Math.sqrt(nrAttacks * PCRIT * (1 - PCRIT));
  23.                 double p5cp = 1 - new NormalDistributionImpl(meanCP, sdCP).cumulativeProbability(4.5);
  24. //              System.out.println("p(5 cp) using normal distribution modeling: " + p5cp);
  25.  
  26.                 // Monte carlo
  27.                 int lower = (int)nrAttacks;
  28.                 int upperIterations = (int)((nrAttacks - lower) * ITERATIONS);
  29.                 int hit5cps = 0;
  30.                 for (int i = 0; i < ITERATIONS - upperIterations; i++) {
  31.                     int cp = lower;
  32.                     for (int j = 0; j < lower; j++) if (random.nextBoolean(PCRIT)) cp ++;
  33.                     if (cp >= 5) hit5cps++;
  34.                 }
  35.                 for (int i = 0; i < upperIterations; i++) {
  36.                     int cp = lower + 1;
  37.                     for (int j = 0; j < lower + 1; j++) if (random.nextBoolean(PCRIT)) cp ++;
  38.                     if (cp >= 5) hit5cps++;
  39.                 }
  40. //              System.out.println("p(5 cp) using monte carlo modeling: " + (double)hit5cps/ITERATIONS);
  41. //              System.out.println();
  42.  
  43.                 // Linear approximation
  44.                 double deltaPdeltaA = 0;
  45.                 double meanCPlow = Math.floor(nrAttacks) + Math.floor(nrAttacks) * PCRIT;
  46.                 double sdCPlow = Math.sqrt(Math.floor(nrAttacks) * PCRIT * (1 - PCRIT));
  47.                 double p5cplow = 1 - new NormalDistributionImpl(meanCPlow, sdCPlow).cumulativeProbability(4.5);
  48.                 double meanCPhigh = Math.ceil(nrAttacks) + Math.ceil(nrAttacks) * PCRIT;
  49.                 double sdCPhigh = Math.ceil(Math.floor(nrAttacks) * PCRIT * (1 - PCRIT));
  50.                 double p5cphigh = 1 - new NormalDistributionImpl(meanCPhigh, sdCPhigh).cumulativeProbability(4.5);
  51.                 deltaPdeltaA = p5cphigh - p5cplow;
  52.                 double p5cplol = p5cplow + deltaPdeltaA * (nrAttacks - Math.floor(nrAttacks));
  53.  
  54.                 System.out.println(nrAttacks + "," + p5cp + "," + ((double)hit5cps/ITERATIONS) + "," + p5cplol);
  55.             }
  56.  
  57.         }
  58.         catch (Exception e) {
  59.             e.printStackTrace();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement