Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. /*
  2. avgRepsToProbAvg is a program that takes:
  3. * a probability of an event occuring
  4. * the accuracy the model needs to stop running the indivual simulation
  5. * number of times that simulation is run (this produces allows for an average to be produced)
  6. to find the number of repetitions something would require to meet its
  7. average/predicted probability
  8. this number is then averaged after the program is run some given amount of times
  9.  
  10. Uncomment lines 41 and 72 for more reports on the ourcomes in each run and the successRate after each "shot"
  11. */
  12.  
  13. public class avgRepsToProbAvg {
  14. //probability of event occuring (ex. 0.4534 equals 45.34% of event occuring)
  15. public static double probability ;
  16. //accuracy of needed to stop the program
  17. public static double accuracy = 0.01 ; //default to accuracy within 1%
  18. //number of times the program is rerun and averaged with the other results
  19. public static int runs = 100 ; //default to 100 run-throughs of the program
  20.  
  21. public static void printCorrectSyntax() {
  22. System.out.println("Correct syntax:") ;
  23. System.out.println("\t java RepsToAvg [avg/predicted probability]") ;
  24. System.out.println("\tOR: java RepsToAvg [avg/predicted probability] [accuracy of outcome to stop] [number of times program is looped to find avg]") ;
  25. }
  26. public static void main (String[] args) {
  27. if (args.length == 1) {
  28. probability = Double.parseDouble(args[0]) ;
  29. } else if (args.length == 3) {
  30. probability = Double.parseDouble(args[0]) ;
  31. accuracy = Double.parseDouble(args[1]) ;
  32. runs = Integer.parseInt(args[2]) ;
  33. } else {
  34. System.out.println("Incorrect number of arguments.") ;
  35. printCorrectSyntax() ;
  36. System.exit(0) ;
  37. }
  38. int outcomes[] = new int[runs] ;
  39. for (int i = 0; i < runs; i++) {
  40. outcomes[i] = repsToAvg() ;
  41. System.out.println(outcomes[i] + "\n") ;
  42. }
  43. double avgOutcome = averageOfArray(outcomes) ;
  44. System.out.println(avgOutcome) ;
  45. System.exit(1) ;
  46. }
  47.  
  48. public static int repsToAvg() {
  49. double successRate = 0 ;
  50. int hit = 0 ;
  51. int miss = 0 ;
  52. int total = hit + miss ;
  53. double lowerProb = probability - accuracy ;
  54. double upperProb = probability + accuracy ;
  55. //System.out.println("\n" + lowerProb + " - " + upperProb) ;
  56. //going to run the little while loop once so we don't encounter a divide by zero issue:
  57. if (Math.random() < probability) {
  58. hit++;
  59. } else {
  60. miss++;
  61. }
  62. total = hit + miss;
  63. while (((successRate) < lowerProb) || ((successRate) > upperProb)) {
  64. //System.out.println(Math.random() < probability) ;
  65. if (Math.random() < probability) {
  66. hit++ ;
  67. } else {
  68. miss++ ;
  69. }
  70. total = hit + miss ;
  71. successRate = (double) hit / (double) total ;
  72. System.out.println(successRate) ;
  73. //System.out.println(hit + " " + miss + " " + total + " " + (double) hit / (double) total);
  74. }
  75. //System.out.println(successRate) ;
  76. return total ;
  77. }
  78.  
  79. public static double averageOfArray(int[] array) {
  80. int sum = 0 ;
  81. for (int i = 0; i < array.length; i++) {
  82. sum += array[i] ;
  83. }
  84. return (double) sum / (double) array.length ;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement