Advertisement
superfunnytogo

Probability1

Jun 24th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 KB | None | 0 0
  1. //REPLACEMENTS
  2. function getList($A,$k) {
  3.     $result = [];
  4.  
  5.     while (count($result) < $k) {
  6.         $sum = 0;
  7.         foreach ($A as $key => $value) {
  8.             $sum += $value;
  9.         }
  10.  
  11.         $rnd = rand(0,$sum * 100000) / 100000;
  12.  
  13.         $sum = 0;
  14.         foreach ($A as $key => $value) {
  15.             $sum += $value;
  16.             if ($rnd <= $sum) {
  17.                 $result[] = $key;
  18.                 break;
  19.             }
  20.         }
  21.     }
  22.     return $result;
  23. }
  24.  
  25. function getQi($A,$k) {
  26.     foreach ($A as $key => $value) {
  27.         $A[$key] = 1 - pow(1 - $value,1.0 / $k);
  28.     }
  29.     return $A;
  30. }
  31.  
  32. //P_i percentages for list A. SUMS to 10 or in this case K
  33. $list = [
  34.     "A" => .25,
  35.     "B" => .25,
  36.     "C" => .25,
  37.     "D" => .25,
  38.     "E" => .65,
  39.     "F" => .65,
  40.     "G" => .25,
  41.     "H" => .25,
  42.     "I" => .52,    
  43.     "J" => .5,  
  44.     "K" => .25,
  45.     "L" => .25,
  46.     "M" => .25,
  47.     "N" => .25,
  48.     "O" => .25,
  49.     "P" => .25,
  50.     "Q" => .28,
  51.     "R" => .3,
  52.     "S" => .25,
  53.     "T" => .25,
  54.     "U" => .5,  
  55.     "V" => .5,  
  56.     "W" => .5,  
  57.     "X" => .5,  
  58.     "Y" => .7,  
  59.     "Z" => .9  
  60. ];
  61.  
  62.  
  63. //Occurrences list for keeping track
  64. $occurences = [
  65.     "A" => 0,
  66.     "B" => 0,
  67.     "C" => 0,
  68.     "D" => 0,
  69.     "E" => 0,
  70.     "F" => 0,
  71.     "G" => 0,
  72.     "H" => 0,
  73.     "I" => 0,
  74.     "J" => 0,
  75.     "K" => 0,
  76.     "L" => 0,
  77.     "M" => 0,
  78.     "N" => 0,
  79.     "O" => 0,
  80.     "P" => 0,
  81.     "Q" => 0,
  82.     "R" => 0,
  83.     "S" => 0,
  84.     "T" => 0,
  85.     "U" => 0,
  86.     "V" => 0,
  87.     "W" => 0,
  88.     "X" => 0,
  89.     "Y" => 0,
  90.     "Z" => 0
  91. ];
  92.  
  93. //Algorithm
  94. //First get the Q_i percentages
  95. $probs = getQi($list,10);
  96.  
  97. //Do it 100,000 times
  98. for ($i=0; $i < 100000; $i++) {
  99.     //Generate the list using getList function Algorithm
  100.     $result = getList($probs,10);
  101.  
  102.     //Go through each letter A to Z
  103.     foreach ($occurences as $key => $value) {
  104.         //If the Generated list $result contains the letter at least once, increment its occurrence variable
  105.         if (in_array($key,$result))
  106.             $occurences[$key] += 1;
  107.     }
  108. }
  109.  
  110. foreach ($occurences as $key => $value) {
  111.     $occurences[$key] = $value / 1000;
  112. }
  113. printArray($occurences);
  114. //Result of printing is
  115. /*
  116. Array
  117. (
  118.     [A] => 20.585
  119.     [B] => 20.312
  120.     [C] => 20.692
  121.     [D] => 20.599
  122.     [E] => 20.591
  123.     [F] => 20.729
  124.     [G] => 20.557
  125.     [H] => 20.597
  126.     [I] => 42.436
  127.     [J] => 42.436
  128.     [K] => 20.734
  129.     [L] => 20.322
  130.     [M] => 20.56
  131.     [N] => 20.691
  132.     [O] => 20.567
  133.     [P] => 20.551
  134.     [Q] => 23.119
  135.     [R] => 24.879
  136.     [S] => 20.81
  137.     [T] => 20.605
  138.     [U] => 42.478
  139.     [V] => 42.379
  140.     [W] => 42.774
  141.     [X] => 42.765
  142.     [Y] => 61.587
  143.     [Z] => 83.658
  144. )
  145. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement