The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
Binomial Probability Distribution
05-24-2015, 05:58 PM (This post was last modified: 05-26-2015 03:46 PM by Dave Britten.)
Post: #1
Binomial Probability Distribution
This program calculates the odds of a specified number of successes from independent trials with a known probability of success.

This requires a combinations function named COMB that calculates nCr. If you need one, see here for a few options.

Inputs:
t: Number of trials
z: Individual probability of success
y: Minimum number of successes
x: Maximum number of successes

Probability will be returned to x.

Example:
What is the probability of obtaining at least two even numbers when rolling five standard 6-sided dice?

5 ENTER
.5 ENTER
2 ENTER
5 XEQ BINPR

Result: 0.8125 (81.25%)

Edit: Rewritten with Dieter's suggested optimizations. It's a bit longer, but faster. I'm sure there's still room for more elegant stack/register use here. I'll wait until those shake out before I pencil the new listing into my Moleskine. Smile

Running it with inputs 120, 1/6, 40, 120 yields 6.419629920E-6 in just over a minute. Compare to the version on my 48 using a direct summation of terms and repeated COMB calls returning 6.4196298769E-6. That's an error of 6.71E-7%, which isn't a terrible sacrifice for having the program finish inside of a lunar cycle (or possibly solar orbit) now.

Code:
t: Number of trials
z: Individual probability of success
y: Minimum number of successes
x: Maximum number of successes

01 LBL "BINPR"
02 X<>Y
03 -
04 STO 00
05 RDN
06 STO 01
07 X<>Y
08 STO 02
09 LASTX
10 STO 03
11 XEQ "COMB"
12 RCL 01
13 RCL 03
14 Y^X
15 *
16 1
17 RCL 01
18 -
19 RCL 02
20 RCL 03
21 -
22 Y^X
23 *
24 RCL 00
25 X>0?
26 GTO 00
27 RDN
28 RTN
29 LBL 00
30 RDN
31 STO 04
32 RCL 01
33 ENTER
34 ENTER
35 1
36 X<>Y
37 -
38 /
39 X<>Y
40 LBL 01
41 RCL Y
42 *
43 RCL 03
44 1
45 +
46 /
47 RCL 02
48 RCL 03
49 -
50 *
51 ST+ 04
52 1
53 ST+ 03
54 RDN
55 DSE 00
56 GTO 01
57 RCL 04
58 RTN
59 END
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Binomial Probability Distribution - Dave Britten - 05-24-2015 05:58 PM



User(s) browsing this thread: 4 Guest(s)