R2DeltaSquared

    • Probability and Non-Transitive Dice

      • 03/01/12
      • 6:34PM

      Today I wrote a program to analyse the properties of a set of "Non-Transitive" dice.

      Cross makes a list of lists of all the possible combinations of elements of those lists. Left Side Wins takes two lists representing sides of dice, or sums of sides of dice, and counts the number of times the left side wins.

      The code looks like:

      import collections
      
      blue = [2,2,2,7,7,7]
      magenta = [1,1,6,6,6,6]
      olive = [0,5,5,5,5,5]
      red = [9,4,4,4,4,4]
      yellow = [3,3,3,3,8,8]
      d6 = [1,2,3,4,5,6]
      
      t1 = [1,2]
      t2 = [3,4]
      
      def iterablep(x):
          return isinstance(x, collections.Iterable)
      
      def wrap(lst):
          return [[x] if not iterablep(x) else x for x in lst]
      
      def flat(x):
          return [item for sublist in x for item in sublist]
      
      def flatten(x):
          return flat(wrap(x))
      
      def cross(a, b):
          return [[x, y] for x in a for y in b]
      
      def left_side_wins(left, right):
          return len([x for x,y in cross(left, right) if x > y])
      
      def debug(left, right):
          return [[x,y] for x,y in cross(left, right) if x > y]
      
      def twice(x):
          return map(sum, cross(x,x))
      
      def triple(x):
          return map(sum, map(flatten,cross(cross(x,x),x)))
      
      def quad(x):
          return twice(twice(x))
      
      def report(x,y):
          games = len(cross(x,y))
          wins = left_side_wins(x,y)
          pwins = float(wins) / float(games) * 100
          print "The left side won %i times out of %i matches" % (wins,games)
          print "That is %f4 percent" % pwins
      
    • More Functional Python Tricks

      • 02/01/12
      • 5:25PM

      I finally finished chapter two of Think Stats. I found some more functional sytax in python. The reduce function is quite handy. Say you have a dictionary pmf that maps a key x to a probability px and you want to find the mean of the set stored in pmf. The calculation would be:

      
      mu = reduce(lambda x_0, x_1: x_0 + x_1, [px * x for x, px in pmf.items()])
      
      

      Now python has a built in sum function, so the lambda is not the clearest way to express this. The lambda notation gives us the flexibility to reduce using a different aggregate function like x*y for instance. In this case better way to do the calculation would be say:

      
      mu = sum([x * px for x,px in pmf.items()])
      
      

      That is getting pretty close to the TeX notation for the actual formula:

      
      \mu=\sum_{i}p_ix_i
      
      
    • More Thinking about Statistics

      • 01/31/12
      • 4:13PM

      Tonight I made it further into Think Stats. The best thing I learned about that it is possible to use a lambda to tell sorted what field to sort by.

      If you have a dictionary d and you want to order it by value the python code to get the sorted list is:

      import operator
      sorted(d.items(), key=lambda value: value[1])
      


      If you want the largest value first the code would be:

      sorted(d.items(), key=lambda value: value[1], reverse=True)
      


      I used this to find the mode of a provided histogram.

      What I like about this syntax is that it allows me to compose sorts into a list comprehension, removing unwanted free variables and loop constructs from my code.

    • Installing Matplotlib on OS X Lion — the Homebrew Challenge

      • 01/30/12
      • 5:29PM

      I learned a lot about the default python installs on OSX, more about the homebrew package manager, and the intricacies of building matplotlib under OSX.

      The problem stems from some incompatibility between gcc 4.2 and LLVM. The libraires that matplotlib need compile under gcc 4.2, sadly they don't compile under LLVM yet. Apple switched to using LLVM exclusively which makes building all the prerequisites for mathplotlib interesting.

      So at the moment if you are wanting to get all the code from Think Stats working on your shiney new Macbook, be sure get a working copy of GCC 4.2 first. Then build the Github version of matplotlib.

    • Think Stats!

      • 01/30/12
      • 1:56PM

      I got the book Think Stats by Allen B. Downey in the post today. So far it is an interesting read. In anticipation I did the exercises from the first chapter available on the the books web site this weekend.

      Tonights goal is chapter 2.

      The topic of chapter two is Means and Averages which happens to be the topic of this weeks Car Talk puzzler.

    • Arbitrary Precision Long Division

      • 01/26/12
      • 5:01PM
      ;So I got Long Division Working
      ;I was reading about the ratio
      ;1 / 998001 which expands to ever 3 digit sequence in order
      ;There was sample code in Python, but I thought I would 
      ;write it in Common Lisp instead
      ;You will need a flatten procedure Doug Hoyte has a nice one posted
      ;(long-div 1 / 998001 3000) is makes interesting if long output
      
      (defun long-div (dividend divisor depth)
          (cond
              ((> depth 0)
                  (flatten (list
                  (truncate (/ dividend divisor))
                  (long-div (* 10 (mod dividend divisor)) divisor (- depth 1)))))
           (t ())))
      
    • Decimal Time and Internet Time

      • 01/24/12
      • 1:39PM

      So the number 86.4 in the previous post was not chosen at random. There are 86400 seconds in the day. So all we are doing is finding the percentage of seconds elapsed in the day and shifting the radix point over and rounding.

      If one wanted to calculate the decimal day, one could add a parameter for the UTC offset to add timezone information. Shift the radix point some more and wind up with decimal Hours, and minutes. (100 beats make a decimal hour, 1 beat is a decimal minute.)

      People did try to use that time system during the French Revolution but it caught on roughly as well as swatch-time did.

    • Common Lisp Code to Find Swatch Internet time.

      • 01/20/12
      • 5:41PM
      ; Finds the internet time, this time with an entry point and
      ; a nice string formatter. 
      (defun beat ()
          (multiple-value-bind (beat)
              (round
                  (multiple-value-bind (s m h) (get-decoded-time)
                      (/
                          (+ s (* 60 m) (* 3600 (+ 1 h)))
                          86.4)))
          (mod beat 1000)))
      
      (defun main ()
          (format t "@ ~D~%" (beat)))
      ; #comon_lisp #swatch_internet_time #lisp
      
    • Hello Blog

      • 01/20/12
      • 5:40PM

      This is the First Blog Post. I need to do a lot of stuff to get this working but at least it is there.

      --D2