445,758 Members | 1,223 Online Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

# Error when calculating probablity

 P: n/a In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation where the number of ways of failure (pFal) is calculated Both errors happen in the code section x1 = Draw - MatchesReq x2 = Field - Selections For Counter = 1 To (Draw - MatchesReq - 1) x1 = x1 * (Draw - MatchesReq - Counter) x2 = x2 * (Field - Selections - Counter) Next pFal = x2 / x1 The first type of error is created when the number of matches required is equal to the number of balls drawn. This makes x1 = 0 so when it hits the calculation pFal a divide by zero occurs. The second type of error is created when the number of balls selected is equal to the field. This makes x1 = 0 so when it hits the calculation pFal a divide by zero occurs. If anyone can spot my error I would greatly appreciate it. Lisa ==== SOURCE ====== Public Function CalOdds(Field, Draw, Selections, MatchesReq) '================================================= ' Field......Number of balls in selection pool ' Draw .....Number of balls drawn in the game ' Selections.......Number of selections made ' MatchesReq......Number of selections that must match '================================================= On Error GoTo ErrorExit Dim x1 ' Temporary working buffer Dim x2 ' Temporary working buffer Dim pSel ' Total number of ways of selecting objects Dim pSuc ' Total number of ways of getting successes Dim pFal ' Total number of ways of getting failures Dim Counter ' Counter used in calculations loops ' ' Calculate the three main factors needed for the final calculation ' First: Calculate total number of ways of selecting objects ' x1 = Draw x2 = Field For Counter = 1 To (Draw - 1) x1 = x1 * (Draw - Counter) x2 = x2 * (Field - Counter) Next pSel = x2 / x1 ' ' Second: Calculate ways to get success ' x1 = MatchesReq x2 = Selections For Counter = 1 To (MatchesReq - 1) x1 = x1 * (MatchesReq - Counter) x2 = x2 * (Selections - Counter) Next pSuc = x2 / x1 ' ' Third: Calculate ways to get failure ' x1 = Draw - MatchesReq x2 = Field - Selections For Counter = 1 To (Draw - MatchesReq - 1) x1 = x1 * (Draw - MatchesReq - Counter) x2 = x2 * (Field - Selections - Counter) Next pFal = x2 / x1 ' ' Do final calculation ' CalOdds = pSel / (pSuc * pFal) Exit Function ' ' Sub Routine ' ErrorExit: CalOdds = "Error" End Function ==== FORMULA ====== Assume we have F objects from which to choose. (For Powerball, this would be the 49 white balls.) Of these F, M are successes (numbers that match the ones on your ticket, or 5 in the case of Powerball) and F-M are failures (for Powerball, the 42 balls in the machine that don't match a number on your ticket). Next, we conduct n Bernoulli trials - we draw n balls (5 for Powerball) from the machine. What we need to know is the probability of getting p successes and n-p failures in those n trials. To match all five white balls, for example, you calculate the probability of 5 successes and 0 failures in 5 trials. The general formula for probability is (Equation 1): (number of ways of getting p successes) x (number of ways getting n-p failures) --------------------------------------------------------------------------- ------------------------------------------ (total number of ways of selecting objects from a set of F Each of these terms boils down to counting. To calculate the denominator, begin by realizing that there are F ways of selecting the first object, F-1, ways of selecting the second object, and so on to F-n ways of selecting the nth object. The total number of ways of making this selection, therefore, is F(F-1)(F-2)...(F-n). However, in our case the order of selection does not matter -- (1,2,3,4,5) is the same as (5,4,3,2,1). We need to adjust for the number of combinations that are identical save for order. To see this, imagine a drawing of two balls from a set of three. There are 3 ways of picking the first ball and two ways of picking the second, for a total of 6 outcomes: (1,2), (1,3), (2,1), (2,3), (3,1), and (3,2). However, only three of these are distinct: (1,2), (1,3), and (2,3) -- the others are merely reorderings. In general, if we're picking n objects, there will be (n)(n-1)(n-2)... ways of arranging each unique combination, so we need to divide our first calculation by this term. There's a mathematical shorthand for this, called a "binomial coefficient." Using a binomial coefficient, the total number of ways of selecting n objects from a set of F is: F! ------------ n!(F-n)! where F! is shorthand for (F)x(F-1)x(F-2) ...x3x2x1. Jan 2 '07 #1
10 Replies

 P: n/a "Lisa" 's wild thoughts were released on Tue, 02 Jan 2007 04:42:52 GMT bearing the following fruit: >In translating the formula for calculating lottery odds for variousconditions into a Visual Basic Program, I have apparently missed somethingin that I get errors in the part of the calculation where the number of waysof failure (pFal) is calculatedBoth errors happen in the code section x1 = Draw - MatchesReq x2 = Field - Selections For Counter = 1 To (Draw - MatchesReq - 1) x1 = x1 * (Draw - MatchesReq - Counter) x2 = x2 * (Field - Selections - Counter) Next pFal = x2 / x1The first type of error is created when the number of matches required isequal to the number of balls drawn.This makes x1 = 0 so when it hits the calculation pFal a divide by zerooccurs.The second type of error is created when the number of balls selected isequal to the field.This makes x1 = 0 so when it hits the calculation pFal a divide by zerooccurs.If anyone can spot my error I would greatly appreciate it.Lisa Haven't you just identified the error? Divide by zero? If zero is a possibility then you should always check for it in code. J Jan Hyde (VB MVP) -- So I was in Tesco's and I saw this man and woman wrapped in a barcode. I said "Are you two an item?" Jan 2 '07 #2

 P: n/a "Lisa" The first type of error is created when the number of matches required is equal to the number of balls drawn. This makes x1 = 0 so when it hits the calculation pFal a divide by zero occurs. If x1 = 0 then ' handle pFal differently Else pFal = x2 / x1 End If Jan 2 '07 #3

 P: n/a "Jan Hyde" 's wild thoughts were released on Tue, 02 Jan 2007 04:42:52 GMT bearing the following fruit: In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation where the number of ways of failure (pFal) is calculated Both errors happen in the code section x1 = Draw - MatchesReq x2 = Field - Selections For Counter = 1 To (Draw - MatchesReq - 1) x1 = x1 * (Draw - MatchesReq - Counter) x2 = x2 * (Field - Selections - Counter) Next pFal = x2 / x1 The first type of error is created when the number of matches required is equal to the number of balls drawn. This makes x1 = 0 so when it hits the calculation pFal a divide by zero occurs. The second type of error is created when the number of balls selected is equal to the field. This makes x1 = 0 so when it hits the calculation pFal a divide by zero occurs. If anyone can spot my error I would greatly appreciate it. Lisa Haven't you just identified the error? Divide by zero? If zero is a possibility then you should always check for it in code. Jan Hyde (VB MVP) Hi Jan The problem is zero should not be a possibility and it is occurring because I have not properly converted this part of the math formula to code. I have talked to some of the math teachers but they have not a clue as to how it should be written in VB coding. This is going to take someone that is knowledgably in both probability math and VB. Thanks for the response Lisa Jan 2 '07 #4

 P: n/a "Raoul Watson" "Lisa"

 P: n/a "Lisa" 's wild thoughts were released on Tue, 02 Jan 2007 16:32:19 GMT bearing the following fruit: >"Jan Hyde" "Lisa" 's wild thoughts were releasedon Tue, 02 Jan 2007 04:42:52 GMT bearing the followingfruit: >In translating the formula for calculating lottery odds for variousconditions into a Visual Basic Program, I have apparently missed something >in that I get errors in the part of the calculation where the number of ways >of failure (pFal) is calculatedBoth errors happen in the code section x1 = Draw - MatchesReq x2 = Field - Selections For Counter = 1 To (Draw - MatchesReq - 1) x1 = x1 * (Draw - MatchesReq - Counter) x2 = x2 * (Field - Selections - Counter) Next pFal = x2 / x1The first type of error is created when the number of matches required isequal to the number of balls drawn.This makes x1 = 0 so when it hits the calculation pFal a divide by zerooccurs.The second type of error is created when the number of balls selected isequal to the field.This makes x1 = 0 so when it hits the calculation pFal a divide by zerooccurs.If anyone can spot my error I would greatly appreciate it.Lisa Haven't you just identified the error? Divide by zero? Ifzero is a possibility then you should always check for it incode.Jan Hyde (VB MVP) Hi JanThe problem is zero should not be a possibility and it is occurring becauseI have not properly converted this part of the math formula to code.I have talked to some of the math teachers but they have not a clue as tohow it should be written in VB coding.This is going to take someone that is knowledgably in both probability mathand VB.Thanks for the responseLisa Then the question is where are you getting the values for 'Draw' and 'MatchesReq'? Regardless, it's still wise to check for zero even if it 'shouldn't' happen. Jan Hyde (VB MVP) -- Arch Criminal: One who robs shoe stores (Stan Kegel) Jan 2 '07 #6

 P: n/a The problem is zero should not be a possibility and it is occurring because I have not properly converted this part of the math formula to code. Are you trying to implement this formula which you posted in your first message? F! ------------ n!(F-n)! where F! is shorthand for (F)x(F-1)x(F-2) ...x3x2x1. If so, you can add the BigFactorial function I show below to your project and then just write the formula directly. That is, the above formula would become this in code... BigFactorial(F) / (BigFactorial(n) * BigFactorial(F - n)) Note that there is some important discussions included with the BigFactorial function, so you should read it carefully. While I don't say so in the text below, you should be aware that your calculations will become meaningless if the factorial is returned in exponential format due to the loss of accuracy inherent in having to shove large numbers into that format. Rick Below is a combination of two posts I given in the past to someone who asked about implementing a Factorial function... You could cast (you can't Dim) a Variant variable as a Decimal type (96-bit number) and get some 28 or 29 digits of accuracy depending if there is a decimal in the answer or not. Simply Dim a variable as Variant and CDec a number (any number will do) into it to make it the Decimal type. Thereafter, that variable will track 28/29 digits of accuracy. For example the following function will calculate factorials up to 29 digits of display before reverting to exponential display. Function BigFactorial(ByVal N As Integer) As Variant If N < 28 Then BigFactorial = CDec(1) Else BigFactorial = CDbl(1) End If For x = 1 To N BigFactorial = x * BigFactorial Next End Function However, you have to watch out for overflows with Decimal data types -- once over 28/29 characters, they will produce an overflow error. So, if you tried to use the above function like this Debug.Print 10*BigFactorial(27) you would get an overflow error but Debug.Print 10*BigFactorial(28) would work fine (the difference being in the first case BigFactorial has a Decimal subtype and in the second case the subtype is a Double). More generally, if a Variant variable is assigned a value that was cast to Decimal, any calculation involving that variable will be "performed" as a Decimal; and then the result cast back to the variable receiving it. If the result is assigned back to the variable that was originally cast to Decimal, that variable continues to contain a Decimal type value. For example, X = CDec(135.6) X = X - 135 X = X / 7 Print X == 0.0857142857142857142857142857 You have to be careful with this though . . . all VB functions return non-Decimal data.and assigning *that* back to the Variant that was cast as Decimal "collapses" it back to a less robust data type. For example, continuing the code above X = Sqr(X) Print X == 0.29277002188456 Jan 2 '07 #7

 P: n/a "Lisa" 's wild thoughts were released on Tue, 02 Jan 2007 10:41:32 GMT bearing the following fruit: Hi Jan The problem is zero should not be a possibility and it is occurring because I have not properly converted this part of the math formula to code. I have talked to some of the math teachers but they have not a clue as to how it should be written in VB coding. This is going to take someone that is knowledgably in both probability math and VB. Thanks for the response Lisa Then the question is where are you getting the values for 'Draw' and 'MatchesReq'? The values are come from an ACCEPTABLE range of user input. The problem is in how I have translated the math formula into code. > Regardless, it's still wise to check for zero even if it 'shouldn't' happen. If you look I did a ''blanket trap'' for errors in the first line ''On Error GoTo ErrorExit'' > Jan Hyde (VB MVP) -- Arch Criminal: One who robs shoe stores (Stan Kegel) Jan 3 '07 #8

 P: n/a "Rick Rothstein (MVP - VB)" Jan 3 '07 #9

 P: n/a "Rick Rothstein (MVP - VB)"

 P: n/a IT WORKED!!!!!! As you said, I had to be careful to set the correct variant type to keep from overflows and all went exactly as it should. Great! I'm glad it worked out for you. You are absolutely SUPER!! I would have never gotten it on my own. MANY, MANY HUGS Lisa Awwww, I'm blushing. Rick Jan 5 '07 #11

### This discussion thread is closed

Replies have been disabled for this discussion. 