Pages

Monday, 2 May 2011

Programming for Probability and Expected Value

When you roll two six sided dice, the total is a number from 2 to 12, with a greater probability toward the middle of that range.  The distribution of results is shown in the graph generated by the program below.

Dim dict As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer) Dim d1, d2, t As Integer For d1 = 1 To 6 For d2 = 1 To 6 t = d1 + d2 If dict.ContainsKey(t) Then dict(t) += 1 Else dict.Add(t, 1) End If Next d2 Next d1 ChartIt("Distribution", dict) Private Sub ChartIt(ByVal title As String, ByVal dict As Dictionary(Of Integer, Integer)) With Chart1 .Series(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column .Legends(0).Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Top .Series(0).Name = title For Each k As Integer In dict.Keys .Series(0).Points.AddXY(k, dict(k)) Next k End With End Sub

The dice rolls have 36 possible results ("1 and 6" is considered to be different from "6 and 1"), of which six have a total of 7.  So the "probability" of rolling a 7 is 6/36, or 1/6.  Probability is usually expressed as a fraction, with 1 (100%) meaning that an event must happen.  There is a probability of 1 that the total of 2 six-sided dice will not be 20.

Nature doesn't always follow probability.  In the graph below, I performed 1000 dice rolls and plotted the results.  As you can see, the shape is similar, but 8 came up as the most frequent total.  (Running a larger simulation would be more likely to give a truer picture, but that is not always feasible.)

Dim dict As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer) Dim r As Random = New Random Randomize() Dim i, d1, d2, t As Integer For i = 1 To 1000 d1 = r.Next(6) + 1 d2 = r.Next(6) + 1 t = d1 + d2 If dict.ContainsKey(t) Then dict(t) += 1 Else dict.Add(t, 1) End If Next i ChartIt("Simulation", dict)

How many dice rolls should it take until you roll a 7?  You might think that since 7 has a 1/6 probability, that you get a cluster around 6 with the numbers falling off on either side, but the graph shape is quite different.  Unlike the first two graphs, the upper limit is not fixed.  It's not likely, but you could roll 20 times without getting a seven.  And the most probable result is one roll, with the probabilty decreasing with each successive roll.  Why?  Well, each roll has a 1/6 chance of being the last one.  That means that each roll removes 1/6 of the remaining distribution, and that 5/6 is accounted for by the numbers "to the right".  The "Expected Value" is the average of (1 * (occurrences in 1 roll) + 2 * (occurrences  in 2 rolls) + ... n * (occurrences in n rolls)) / occurrences. 

Dim dict As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer) Dim r As Random = New Random Randomize() Dim i, d1, d2, t As Integer Dim numer As Decimal = 0 Dim denom As Decimal = 0 For i = 1 To 10000 t = 0 d1 = 0 d2 = 0 Do Until (d1 + d2 = 7) t += 1 d1 = r.Next(6) + 1 d2 = r.Next(6) + 1 Loop If dict.ContainsKey(t) Then dict(t) += 1 Else dict.Add(t, 1) End If numer += t denom += 1 Next i ChartIt("Rolls to 7", dict) Dim ev As Decimal = numer / denom Me.Show() MessageBox.Show(ev.ToString())

If you run this code, you'll find that the Expected Value does come out very close to 6.  In a future article, I'll show you how to get the exact number rather than a simulation.  Several Project Euler problems require this calculation.

For more information on the Chart control, see some of my earlier posts (for example, this one ).

Posted Apr 10 2011, 09:12 AM by LarryBlake Filed under: Dictionary, VB, Chart control, Expected Value, Project Euler, Probability

View the original article here

0 comments:

Post a Comment

 
Powered by Blogger