<div dir="ltr">Very cool! I&#39;ve run into this problem a few times.<div><br></div><div style>And here&#39;s an example where it happens right now ....</div><div style><br></div><div style><a href="http://drdr.racket-lang.org/26021/collects/tests/future/random-future.rkt">http://drdr.racket-lang.org/26021/collects/tests/future/random-future.rkt</a><br>
</div><div><br></div><div>Robby</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jan 4, 2013 at 3:45 PM, Neil Toronto <span dir="ltr">&lt;<a href="mailto:neil.toronto@gmail.com" target="_blank">neil.toronto@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I get excited about applying statistics to programming. Here&#39;s something exciting I found today.<br>
<br>
I&#39;m working on a Typed Racket implementation of Chris Okasaki&#39;s purely functional random-access lists, which are O(1) for `cons&#39;, `first&#39; and `rest&#39;, and basically O(log(n)) for random access. I wanted solid randomized tests, and I figured the best way would be bisimulation: do exactly the same, random thing to a (Listof Integer) and an (RAList Integer), then ensure the lists are the same. Iterate N times, each time using the altered lists for the next bisimulation step.<br>

<br>
There&#39;s a problem with this, though: the test runtimes are all over the place for any fixed N, and are especially wild with large N. Sometimes it first does a bunch of `cons&#39; operations in a row. Because each bisimulation step is O(n) in the list length (to check equality), this makes the remaining steps very slow. Sometimes it follows up with a bunch of `rest&#39; operations, which makes the remaining steps very fast.<br>

<br>
More precisely, random bisimulation does a &quot;simple random walk&quot; over test list lengths, where `cons&#39; is a step from n to n+1 and `rest&#39; is a step from n to n-1. Unfortunately, the n&#39;s stepped on in a simple random walk have no fixed probability distribution, and thus no fixed average or variance. That means each bisimulation step has no fixed average runtime or fixed variation in runtime. Wildness ensues.<br>

<br>
One possible solution is to generate fresh test lists from a fixed distribution, for each bisimulation step. This is a bad solution, though, because I want to see whether RAList instances behave correctly *over time* as they&#39;re operated on.<br>

<br>
The right solution is to use a Metropolis-Hastings random walk instead of a simple random walk, to sample list lengths from a fixed distribution. Then, the list lengths will have a fixed average, meaning that each bisimulation step will have a fixed average runtime, and my overall average test runtime will be O(N) instead of wild and crazy.<br>

<br>
First, I choose a distribution. The geometric family is good because list lengths are nonnegative. Geometric with p = 0.05 means list lengths should be (1-p)/p = 19 on average, but are empty with probability 0.05 and very large with very low probability.<br>

<br>
So I add these two lines:<br>
<br>
  (require math/distributions)<br>
<br>
  (define length-dist (geometric-dist 0.05))<br>
<br>
In a Metropolis-Hastings random walk, you step from n to n&#39; with probability min(1,P(n&#39;)/P(n)); this is called &quot;accepting&quot; the step. Otherwise, you &quot;reject&quot; the step by staying in the same spot. Adding this new test takes only two defines and a `cond&#39;:<br>

<br>
  (define r (random 5))  ; Randomly choose an operation<br>
  (cond<br>
    [(= r 0)<br>
     ;; New! Only cons with probability P(n+1)/P(n)<br>
     (define n (length lst))<br>
     (define accept (/ (pdf length-dist (+ n 1))<br>
                       (pdf length-dist n)))<br>
     (cond [((random) . &lt; . accept)<br>
            .... Accept: cons and ensure continued equality ....]<br>
           [else<br>
            .... Reject: return the same lists ....])]<br>
    [(= r 1)<br>
     .... Accept: `rest&#39; each list and ensure continued equality ....]<br>
    .... Other random operations ....)<br>
<br>
I left off the acceptance test for `rest&#39; because P(n-1) &gt; P(n) ==&gt; P(n-1)/P(n) &gt; 1, so the test would always pass. (This is because the geometric distribution&#39;s pdf is monotone. If I had chosen a Poisson distribution, which has a bump in the middle, I&#39;d have to do the acceptance test for both `cons&#39; and `rest&#39;.)<br>

<br>
I instrument the test loop to record list lengths, verify that their mean is about 19, and do some density plots vs. the geometric distribution. It all looks good: the randomized bisimulation test does indeed operate on lists whose length is distributed Geometric(0.05), so the overall average test runtime is O(N), and it still tests them as they&#39;re operated on over time.<br>

<br>
Neil ⊥<br>
____________________<br>
 Racket Users list:<br>
 <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/<u></u>users</a><br>
</blockquote></div><br></div>