yojimbos_law

expected queue contents computer

Feb 19th, 2022 (edited)
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1.  
  2. //my stupid expectation formula requires iterating over all sequences in {0,...,n-1}^k.
  3. //the following two functions let us do that by iterating instead over {0,...,n^k -1},
  4. //which is significantly easier.
  5.  
  6.  
  7. //this takes a number a between 0 and base^power - 1 (inclusive) and converts it into
  8. //the corresponding natural sequence embedded in {0,...,base}^power.
  9. int[int] to_sequence(int a, int base, int power){
  10. int[int] seq;
  11. int leftovers = a;
  12.  
  13. //doesn't matter for our purposes whether we preserve orientation, so we might not.
  14. for i from 1 to (power){
  15. seq[power-i] = leftovers/(base**(power - i));
  16. //debugging.
  17. //print((base**(power - i))+" goes into "+leftovers+" this many times: "+seq[power-i]);
  18. leftovers -= seq[power-i]* (base**(power - i));
  19. }
  20. return seq;
  21. }
  22.  
  23. //turns those sequences into sets for cleaner use in foreach loops.
  24. boolean[int] to_set(int[int] seq){
  25. boolean[int] set;
  26. foreach i in seq{
  27. set[seq[i]] = true;
  28. }
  29. return set;
  30. }
  31.  
  32. //for any appropriate number X, "foreach i in X.to_sequence(base,power).to_set()" iterates
  33. //i over precisely the unique digits found in the sequence corresponding to X.
  34. //if you don't discard the multiplicity here, the iteration becomes quite a bit messier.
  35. //Coding natural morphisms is tricky, huh?
  36.  
  37.  
  38. //the formula includes the multiplicity function, so we write that here.
  39. //this'll return the number of times a digit j appears in the sequence seq.
  40. int mult(int[int] seq, int j){
  41. int count;
  42. foreach i in seq{
  43. if(seq[i] == j){
  44. count++;
  45. }
  46. }
  47. return count;
  48. }
  49.  
  50. //phi is the underlying probability distribution. I guess we'll normalize it in this function
  51. //for ease of use (so any proportional distribution could be input).
  52. //r will be the rejection rate array with positions corresponding to entries in phi.
  53. //k will be queue length, since this can't be deduced from r and phi.
  54. float expectation(float[int] phi, float[int] r, int k){
  55. //deduce size of outcome set from input rather than requiring that input.
  56. int n = count(phi);
  57. //normalize phi.
  58. float phisum = 0.0;
  59. foreach i in phi{
  60. phisum += phi[i];
  61. }
  62. foreach i in phi{
  63. phi[i] /= phisum;
  64. }
  65. //so now phi is a probability distribution unless some sick fart put negative/complex values in.
  66.  
  67. //so now we have to do this. https://i.imgur.com/DsYTyrd.png
  68. //the top and bottom are nearly identical, so we'll be computing these simultaneously
  69. //while leaving out that factor of #a from the summands on the bottom.
  70. float top_outer_sum = 0.0;
  71. float bottom_outer_sum = 0.0;
  72. float inner_sum;
  73. float indexed_product;
  74. int[int] index_seq;
  75. boolean[int] index_set;
  76. for index from 0 to ((n**k) -1){
  77. clear(index_seq);
  78. clear(index_set);
  79. inner_sum = 0.0;
  80. indexed_product = 1.0;
  81. index_seq = to_sequence(index,n,k);
  82. index_set = to_set(index_seq);
  83.  
  84. //make the inner sum.
  85. foreach j in index_set{
  86. inner_sum += phi[j]*r[j];
  87. }
  88. //do the 1- thing.
  89. inner_sum = 1 - inner_sum;
  90.  
  91. //now make the indexed product.
  92.  
  93. foreach j in index_set{
  94. indexed_product *= ( ( phi[j]*(1-r[j]) ) ** mult(index_seq,j) ) / ( 1 - r[j] );
  95. }
  96.  
  97. //now we increment the bottom by those two things multiplied together.
  98. bottom_outer_sum += inner_sum * indexed_product ;
  99.  
  100. //and increment the top by the same thing but with a factor of #a.
  101. top_outer_sum += inner_sum * indexed_product * count(index_set);
  102.  
  103. }
  104.  
  105. //smush the top and bottom together and return it.
  106. return (top_outer_sum / bottom_outer_sum );
  107. }
  108.  
  109. /*
  110.  
  111. //check that this works by looking at dice.
  112. float[int] fee = {1,1,1,1,1,1};
  113. float[int] arrr = {0,0,0,0,0,0};
  114. float[int] herman_cain = {0.999,0.999,0.999,0.999,0.999,0.999};
  115. print(expectation(fee , arrr ,1));
  116. //the above should print the average number of unique die roll outcomes occurring
  117. //in the last 1 die rolls. If it's not 1.0, we're very sad.
  118. //dear lord it returns exactly 1.0.
  119.  
  120. debugging stuff
  121. print("let\'s write "+122+" in base 5 with 4 digits");
  122. int[int] boop = 122.to_sequence(5,4);
  123. foreach i in boop{
  124. print(i+": "+boop[i]);
  125. }
  126.  
  127. print("let\'s write "+69+" in base 3 with 11 digits");
  128. boop = 69.to_sequence(3,11);
  129. foreach i in boop{
  130. print(i+": "+boop[i]);
  131. }
  132. */
  133.  
  134.  
  135. //let's make data for some very pretty graphs.
  136. float[float] data;
  137. float[int] fee = {1,1,1,1,1};
  138. float[int] arrr = {0,0,0,0,0};
  139.  
  140. //graphing how much rejection rate matters to steady state frequency.
  141. for i from 1 to 8{
  142. arrr[4] = 0.0;
  143. for j from 0 to 999{
  144. arrr[4] += 0.001;
  145. data[arrr[4]] = expectation(fee,arrr,i)/(i.to_float());
  146. }
  147. //when i is 1, this should generate a text file with a column of 1000 1.0's.
  148. //only letting script do that for debugging.
  149. map_to_file(data,"plot of normalized expected variety in uniform phi with rejection on one outcome for n=5 k="+i+".txt");
  150. clear(data);
  151. print("done with making graph number "+i);
  152. }
  153.  
Add Comment
Please, Sign In to add comment