<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hypomnemata</title>
	<atom:link href="http://blog.appliedplatonics.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.appliedplatonics.com</link>
	<description>Applied Platonics&#039; Lab Notebook</description>
	<lastBuildDate>Sat, 04 Sep 2010 01:36:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Monte Carlo: part 23</title>
		<link>http://blog.appliedplatonics.com/2010/09/03/monte-carlo-part-23/</link>
		<comments>http://blog.appliedplatonics.com/2010/09/03/monte-carlo-part-23/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 01:36:03 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=279</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 37: We need $40 to get on the bus home from Vegas tomorrow, but are down to [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 37: We need $40 to get on the bus home from Vegas tomorrow, but are down to $20.  The plan is to play evens in roulette (2:1 payout, 18/38 probability of winning), but we&#8217;re missing one detail.  Do we bet it all one time and walk away with $0 or $40, or do we bet it a dollar at a time?
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

# Bold play or cautious play?  We need $40 to get on the
# bus home from Vegas tomorrow, but are down to $20.  Do
# we bet it all at once on evens in roulette, or do we
# bet it a dollar at a time?

TRIALS=10000

P_WIN = 18.0/38.0 # 18 evens on a 38-slot roulette wheel

def play_bold()
  return rand() &lt; P_WIN
end

def play_cautious()
  bank = 20

  plays_remaining = 10000 # limit how long we can play

  while (bank &lt; 40 &amp;&amp; bank &gt; 0 &amp;&amp; plays_remaining &gt; 0)
    if (rand() &lt; P_WIN)
      bank += 1
    else
      bank -= 1
    end
    plays_remaining -= 1
  end

  return bank == 40
end

win_bold = 0
win_cautious = 0

TRIALS.times {
  win_bold += 1 if play_bold()
  win_cautious += 1 if play_cautious()
}

puts "After #{TRIALS} times, wins:"
puts "  bold: #{win_bold}"
puts "  cautious: #{win_cautious}"
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/09/03/monte-carlo-part-23/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 22</title>
		<link>http://blog.appliedplatonics.com/2010/08/31/monte-carlo-part-22/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/31/monte-carlo-part-22/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 01:35:20 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=277</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 39: a set of glass rods, with one end blue-dotted and the other red-dotted, are dropped. Many [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 39: a set of glass rods, with one end blue-dotted and the other red-dotted, are dropped.  Many break into 3 parts.  What is the average length of the blue-dotted pieces of these broken-in-three rods?
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

TRIALS=100000

# Much like 42 and 43, this is pretty trivial

cum_blue = 0.0

TRIALS.times {
  # Choose two breaking points
  b1 = rand()
  b2 = rand()

  # put them on a stick reasonably...
  if (b1 &gt; b2)
    x = b1
    b1 = b2
    b2 = x
  end

  # and let's say the blue is always on the left
  # that is, the length of the blue segment is b1

  cum_blue += b1
}

puts "After #{TRIALS} trials, average length:"
puts cum_blue/TRIALS
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/31/monte-carlo-part-22/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 21</title>
		<link>http://blog.appliedplatonics.com/2010/08/28/monte-carlo-part-21/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/28/monte-carlo-part-21/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 01:34:42 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=273</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 43: A bar is broken at random in two places. Find the average length of the shortest, [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 43: A bar is broken at random in two places.  Find the average length of the shortest, middle-est, and longest pieces.
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

TRIALS=100000

# Much like 42, this is pretty trivial stuff.

cum_short = 0.0
cum_med = 0.0
cum_long = 0.0

TRIALS.times {
  b1 = rand()
  b2 = rand()

  # put them on a stick reasonably...
  if (b1 &gt; b2)
    x = b1
    b1 = b2
    b2 = x
  end

  ls = [b1, b2-b1, 1.0-b2].sort

  cum_short += ls[0]
  cum_med += ls[1]
  cum_long += ls[2]
}

m_short = cum_short/TRIALS
m_med = cum_med/TRIALS
m_long = cum_long/TRIALS

puts "After #{TRIALS} trials, average lengths:"
puts " #{m_short}, #{m_med}, #{m_long}"
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/28/monte-carlo-part-21/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 20</title>
		<link>http://blog.appliedplatonics.com/2010/08/25/monte-carlo-part-20/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/25/monte-carlo-part-20/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 01:33:44 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=268</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 42: If a stick is broken in two at random, what is the average length of the [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 42: If a stick is broken in two at random, what is the average length of the shorter half?  What is the average ratio of the shorter side to the longer side?
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

TRIALS = 1000000

# This is pretty trivial: we just choose a breakpoint somewhere
# along a unit-length stick and keep track of the total
# length of the short ends, and the ratio for (b)

cum_short_len = 0.0
cum_ratio = 0.0

TRIALS.times {
  breakpoint = rand()
  breakpoint = 1.0 - breakpoint if breakpoint &gt; 0.5
  cum_short_len += breakpoint

  cum_ratio += breakpoint / (1.0 - breakpoint)
}

mean = cum_short_len / TRIALS
mean_ratio = cum_ratio / TRIALS

puts "After #{TRIALS} attempts, mean(short_len) = #{mean}"
puts "   mean(ratio) = #{mean_ratio}"
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/25/monte-carlo-part-20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 19</title>
		<link>http://blog.appliedplatonics.com/2010/08/22/monte-carlo-part-19/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/22/monte-carlo-part-19/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 00:50:33 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=265</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 46: As in 45, but this time: what is the probability of r matches? #!/usr/bin/env ruby TRIALS=1000000 [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 46: As in 45, but this time: what is the probability of r matches?
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

TRIALS=1000000

deck = (0..51).to_a

# We shuffle the deck, then shuffle another.
# We then walk the two arrays side-by-side, checking
# for matches

# a count of how many times we got a given number of matches
matches = deck.map { 0 }

TRIALS.times do
  upper = deck.shuffle
  lower = deck.shuffle

  match = 0

  upper.each_with_index do |u_i, i|
    l_i = lower[i]
    match += 1 if l_i == u_i
  end
  matches[match] += 1
end

puts "Out of #{TRIALS}, we got the following distribution:"
matches.each_with_index { |m_i, i| puts "\t#{i}\t#{m_i}" }
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/22/monte-carlo-part-19/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 18</title>
		<link>http://blog.appliedplatonics.com/2010/08/19/monte-carlo-part-18/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/19/monte-carlo-part-18/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 00:50:01 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=262</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 45: Let us shuffle two decks of cards and lay them out in two lines, one over [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 45: Let us shuffle two decks of cards and lay them out in two lines, one over the other.  On average, how many cards will &#8220;line up&#8221; with themselves (ie: the 3 of clubs over the 3 of clubs)?
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

TRIALS=10000

deck = (0..51).to_a

# We shuffle the deck, then shuffle another.  We then walk the
# two arrays together, looking for matches.

match = 0

TRIALS.times do
  upper = deck.shuffle
  lower = deck.shuffle

  upper.each_with_index do |u_i, i|
    l_i = lower[i]
    match += 1 if l_i == u_i
  end
end

puts "Out of #{TRIALS}, #{match} cards matched."
puts "That gives us an expected number of matches as: "
puts "  #{match.to_f/TRIALS.to_f}"
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/19/monte-carlo-part-18/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 17</title>
		<link>http://blog.appliedplatonics.com/2010/08/16/monte-carlo-part-17/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/16/monte-carlo-part-17/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 00:39:53 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=259</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 35: After imbibing a few too many, a drunk man finds himself stumbling at the edge of [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 35: After imbibing a few too many, a drunk man finds himself stumbling at the edge of a cliff.  One step to the left will send him over, so he&#8217;s trying to move right as well as he can.  Except that he&#8217;s drunk, so he only moves to the right with probability 2/3.  What are his chances of living into sobriety?
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

TRIALS=10000

POS_START=1  # Initial position

# Probability of a step in the positive direction
P_POSITIVE=0.666667

# What position he should be at to "get away"
#
# Note that this doesn't actually guarantee that our friend
# lives to the morrow, but it's close enough.
#
GOT_AWAY = 100000

MAX_STEPS = 1000000 # How long do we wait?

wins=losses=0

TRIALS.times do
  pos = POS_START
  steps = 0

  while (pos &gt; 0 &amp;&amp; pos &lt; GOT_AWAY &amp;&amp; steps &lt; MAX_STEPS)
    pos += rand() &lt; P_POSITIVE ? 1 : -1
    steps += 1
  end

  losses += 1 if (pos == 0)
  wins += 1 if (pos &gt; 0)

end

print "Wins: #{wins}, losses: #{losses}"
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/16/monte-carlo-part-17/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 16</title>
		<link>http://blog.appliedplatonics.com/2010/08/13/monte-carlo-part-16/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/13/monte-carlo-part-16/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 00:37:59 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=251</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 20: in a stepwise, three cornered duel between Adam, Bryan, and Costello, what should Adam&#8217;s strategy be? [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 20: in a stepwise, three cornered duel between Adam, Bryan, and Costello, what should Adam&#8217;s strategy be?  Adam shoots first, then Bryan, then Costello, then Adam, and so on, until only one stands.  The probability of Adam hitting his target is 0.3, Bryan is a perfect shot, and C is 50/50.
</p></blockquote>
<p>Note that the code below doesn&#8217;t actually answer the question the same way as Mosteller.  He admits a possibility our code below doesn&#8217;t, which, personally, I feel is a breach of honor in such an esteemed tradition as settling arguments by the well-reasoned method of shooting at each other.</p>
<p><code>
<pre>
#!/usr/bin/env ruby

# I don't like the answer in the book here;
# code of honor is rough.

class Duel
  P_A = 0.3
  P_B = 1.0
  P_C = 0.5

  def initialize()
    @a_live = true
    @b_live = true
    @c_live = true

    @a_moves = []
  end

  attr_reader :a_moves

  def over?()
    !@a_live || !(@b_live || @c_live)
  end

  def a_live?()
    return @a_live
  end

  def run_round(moves)
    @a_moves.push(moves[0])
    if rand() &lt; P_A
      if moves[0] == 0 &amp;&amp; @b_live
        @b_live = false
      else
        @c_live = false
      end
    end

    if @b_live &amp;&amp; rand() &lt; P_B
      if moves[1] == 0 || !@c_live
        @a_live = false
      else
        @c_live = false
      end
    end

    if @c_live &amp;&amp; rand() &lt; P_C
      if moves[1] == 0 &amp;&amp; @b_live
        @b_live = false
      else
        @a_live = false
      end
    end
  end

end

move_space = []
8.times { |move_mask|
  move_a = move_mask &amp; 1 == 0 ? 1 : 0
  move_b = move_mask &amp; 2 == 0 ? 1 : 0
  move_c = move_mask &amp; 4 == 0 ? 1 : 0

  move_space.push( [move_a, move_b, move_c] )

}

a_lives =  Hash.new { |h,k| h[k] = 0 }
a_attempts =  Hash.new { |h,k| h[k] = 0 }

two_move_space = []

al1 = [0,0]
aa1 = [0,0]

move_space.each { |m1|
  a1 = m1[0]
  move_space.each { |m2|

    100.times {
      d = Duel.new
      d.run_round(m1)
      d.run_round(m2)
      while (!d.over?)
        d.run_round(m2) # doesn't matter, we only have c left if we're still in the game
      end

      aa1[a1] += 1
      al1[a1] += 1 if d.a_live?
      a_lives[ d.a_moves  ] += 1 if d.a_live?
      a_attempts[ d.a_moves  ] += 1
    }
  }
}

puts a_lives.inspect
puts a_attempts.inspect

lives = [0,0]
attempts = [0,0]

a_lives.each { |k,v|
  first_move = k[0]

  att = a_attempts[k]
  lives[first_move] += v
  attempts[first_move] += att
}

puts lives.inspect
puts attempts.inspect

puts

puts aa1.inspect
puts al1.inspect

puts [ al1[0]/aa1[0].to_f, al1[1]/aa1[1].to_f ].inspect
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/13/monte-carlo-part-16/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 15</title>
		<link>http://blog.appliedplatonics.com/2010/08/10/monte-carlo-part-15/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/10/monte-carlo-part-15/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 00:37:12 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=249</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 19: Which is most likely: a) at least 1 six when rolling 6 dice, b) at least [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 19: Which is most likely: a) at least 1 six when rolling 6 dice, b) at least 2 sixes in 12 dice rolled, or c) at least 3 sixes of 18 dice?
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

TRIALS=10000

n_ones = 0
n_twos = 0
n_threes = 0

def roll
  return 1+rand(6)
end

TRIALS.times do
  six = (1..6).to_a.map { roll() }
  twelve = (1..12).to_a.map { roll() }
  eighteen = (1..18).to_a.map { roll() }

  n_ones += 1 if six.select { |r| r == 6 }.length &gt;= 1
  n_twos += 1 if twelve.select { |r| r == 6 }.length &gt;= 2
  n_threes += 1 if eighteen.select { |r| r == 6 }.length &gt;= 3

end

puts "After #{TRIALS}, got #{n_ones} / #{n_twos} / #{n_threes}"
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/10/monte-carlo-part-15/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo: part 14</title>
		<link>http://blog.appliedplatonics.com/2010/08/07/monte-carlo-part-14/</link>
		<comments>http://blog.appliedplatonics.com/2010/08/07/monte-carlo-part-14/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 00:36:08 +0000</pubDate>
		<dc:creator>jbm</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Reading Notes]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fifty Problems]]></category>
		<category><![CDATA[Monte Carlo methods]]></category>

		<guid isPermaLink="false">http://blog.appliedplatonics.com/?p=246</guid>
		<description><![CDATA[(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.) Problem 18: What&#8217;s the probability of getting 50 tails in 100 toin cosses? #!/usr/bin/env ruby # This is [...]]]></description>
			<content:encoded><![CDATA[<p>(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods.  In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)</p>
<blockquote><p>Problem 18: What&#8217;s the probability of getting 50 tails in 100 toin cosses?
</p></blockquote>
<p><code>
<pre>
#!/usr/bin/env ruby

# This is pretty straightforward modeling:
#   we just count the # of 50s we get.

TRIALS=1000000

n_fifties = 0
TRIALS.times do
  heads = 0
  100.times { heads += rand(2) }

  n_fifties += 1 if 50 == heads
end

puts "Out of #{TRIALS} trials"
puts "  we got #{n_fifties} even splits, so "
puts "  P=#{n_fifties/TRIALS.to_f}"
</pre>
<p></code></p>
<p>I&#8217;ve been coding my way through <i><a href="http://www.amazon.com/Fifty-Challenging-Problems-Probability-Solutions/dp/0486653552" target="_blank">Fifty Challenging Problems in Probabilities with Solutions</a></i>.  This post is a part of the <i>Fifty Challenging Problems</i> series. Read all of the <a href="http://blog.appliedplatonics.com/index.php?s=Fifty+Problems">Fifty Problems posts by clicking here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appliedplatonics.com/2010/08/07/monte-carlo-part-14/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
