Showing posts with label geometric distribution. Show all posts
Showing posts with label geometric distribution. Show all posts

Saturday, January 13, 2024

TI-30Xa Algorithms: Probability

TI-30Xa Algorithms:  Probability



Introduction


Even when a calculator isn't (technically) programmable, algorithms can be applied to scientific and financial calculations.


The calculations take numerical arguments that are stored in the TI-30Xa's three memory slots:  M1, M2, and M3.  Store amounts into the memory registers by the [ STO ] key.  


Careful:  For the solar versions of the TI-30Xa, do not press the [ON/AC] button as doing so clears the memory registers.


The registers used:


M1 = n 

M2 = k 

M3 = p 



Repeated Combinations


nHk = (n + k -1)Ck = (n + k - 1)! ÷ (k! × (n - 1)!)


Registers:


M1:  n = number of objects

M2:  k = number of objects chosen in the population


nHk = number of combinations of picking k from n objects, assuming repeats are allowed


Algorithm:


[ ( ] [ RCL ] 1 [ + ] [ RCL ] 2 [ - ] 1 [ ) ] [ 2nd ] (nCr) [ RCL ] 2 [ = ]


Example:


M1: n = 50

M2: k = 5


Result:  3,162,510



Binomial Probability Distribution


Prob(k) = nCk × p^k × (1 - p)^(n - k) = n! ÷ (k! × (n - k)!) × p^k × (1 - p)^(n - k) 


Registers:


M1:  n = number of trials

M2:  k = number of successes

M3:  p = probability of success (in decimal; i.e. enter 0.30 for 30%)


Prob =  probability of k successes out of n trials with a success probability p


Algorithm:  


[ RCL ] 1 [ 2nd ] (nCr) [ RCL ] 2 [ × ] [ RCL ] 3 [ y^x ] [ RCL ] 2 [ × ] [ ( ] 1 [ - ] [ RCL ] 3  [ ) ] [ y^x ] [ ( ] [ RCL ] 1 [ - ] [ RCL ] 2 [ ) ] [ = ] 


Example:


M1: n = 50

M2: k = 5

M3: p = 0.8


Result:  2.442763967 × 10^-26



Geometric Probability Distribution


Prob(k) = (1 - p)^(k - 1) × p


Registers:


M2:  k = number of failures before the first success

M3:  p = probability of success (in decimal) 


Prob = probability of a event taking k trials before the first success


Algorithm:


[ ( ] 1 [ - ] [ RCL ] 3 [ ) ] [ y^x ] [ ( ] [ RCL ] 2 [ - ] 1 [ ) ] [ × ] [ RCL ] 3 [ = ]


Example:


M2: k = 5

M3: p = 0.8


Result:  0.00128



A Simple Pseudorandom Number Generator


The TI-30Xa does not have a random number function.  To generate random numbers, a pseudorandom number generator algorithm must be used in the form of:


x_n+1 = f(x_n)


where x_0 is the initial value, known as the seed value.  



A simple pseudorandom  number generator to generate numbers between 0 and 1 is:


x_n+1 = frac(997 × x_n + π)



Algorithm:


With x_n in display:

[ × ] 997 [ + ] [ π ] [ = ] 

[ - ] the integer part of the number in the display [ = ]


Result:  x_n+1.   


There is no fraction part or integer part functions are not available on the TI-30Xa.  



Example:   


Starting seed:   0.7896   


[ × ] 997 [ + ] 

[ π ] [ = ]        Display:  799.3457927

[ - ] 799 [ = ]  Display:  0.345792654


[ × ] 997 [ + ] 

[ π ] [ = ]        Display:  347.8968683

[ - ] 347 [ = ]  Display:  0.896868283


[ × ] 997 [ + ] 

[ π ] [ = ]        Display:  897.3192706

[ - ] 897 [ = ]  Display:  0.319270625


[ × ] 997 [ + ] 

[ π ] [ = ]        Display:  321.4544059

[ - ] 321 [ = ]  Display:  0.454405908


and so on...


The random numbers with the starting seed 0.7896 are:

0.345792654

0.896868283

0.319270625

0.454405908


Take as many decimal points as you wish.  




If you enjoy this post, I will consider making a series using the TI-30Xa calculator (and similar simple scientific calculators).  Until next time,


Eddie 


All original content copyright, © 2011-2024.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, September 9, 2023

Casio fx-9750GIII: Coin Flips and Probability of Winning

Casio fx-9750GIII:  Coin Flips and Probability of Winning




Introduction


The program COINPROB answers two questions in probability:



Take a game, with the chance of winning p.    There are only wins (success) and losses (failures).  Each game is independent and has the same chance of win.


1.  What is the chance of having an amount of wins in a set amount of games?   The binomial distribution is used to answer this question.


2. How many losses must be endured before a win occurs?  For this question, we turn to the geometric distribution.


Three results are listed are:


PDF:  the probability

MEAN:  expected value

VAR:  variance


The probability of success is also listed.



Casio fx-9750GIII Program:  COINPROB


Program Code:

(most spaces are added for readability)


"EWS 2023-06-25"

.5 → P

Locate 1, 4, "P(WIN) = P"

Locate 1, 5, "P(LOSS) = 1-P"

Locate 1, 6, "GAMES = TRIALS" ◢


Lbl 0

ClrText

Menu "PROB WIN VS LOSS", "SETTINGS", S, "# WINS IN GAMES", 1,

"# LOSS BEF. WIN", 2, "EXIT", E


Lbl S

Menu "SETTINGS", "P(WIN) = 0.5", F, "SET P(WIN)", U

Lbl F

.5 → F

"P(WIN) = .5"

"P(LOSS) = .5" ◢

Goto 0


Lbl U

"P(WIN)"? → P

"P(LOSS) = "

1 - P ◢

Goto 0


Lbl 1

"# GAMES"? → N

"# WINS"? → S

N nCr S × P^S × (1 - P)^(N - S) → D

N × P → M

M × (1 - P) → V

Goto R


Lbl 2

"# LOSSES"? → F

(1 - P)^(F - 1) × P → D

P⁻¹ → M

M × (1 - P) ÷ P → V

Goto R


Lbl R

ClrText

Locate 1, 3, "PDF= "

Locate 7, 3, D

Locate 1, 4, "MEAN="

Locate 7, 4, M

Locate 1, 5, "VAR="

Locate 7, 5, V

Locate 1, 6, "P(WIN)="

Locate 9, 6, P ◢

Goto 0


Lbl E

"THANK YOU"


Note:  The bold C is the combination function (nCr).  


A typo has been corrected (see line in red).  I thank Richard Antley for pointing out my error. - 9/27/2023 


Examples



1.  A fair coin is flipped.  You win if the coin flipped is heads.   What is the probability that you win 7 out of 10 times?


P = 0.5


# GAMES?  10

# WINS?  7


Problem Type:  # WINS IN GAMES


Results:

PDF=  0.1171875

MEAN= 5

VAR= 2.5

P(WIN)= 0.5


2.  What is the probability that you flip 7 tails before flipping a head?  Assume a fair coin.


P = 0.5


Problem Type:  # LOSS BEF. WIN


# LOSSES? 7


Results:


PDF=  0.0078125

MEAN= 2

VAR= 2

P(WIN)= 0.5


3.  On any given day in Luau Town, the chance of rain is 35% each day.   On a given week, how many days are expected to be sunny?


WIN:  sunny days (because that is what we want)

P(WIN) = 1 - 35% = 65% = 0.65

We want the mean (expected value).


Problem Type:  # WINS IN GAMES


Change P(WIN) to 0.65 in settings.  


# GAMES?  7  (7 days)

# WINS? (since we are interested in the mean, we can enter any number 0 to 7)


Results:


PDF= (N/A)

MEAN= 4.55

VAR= 1.5925

P(WIN)= 0.65


A week is expected to have about 4.55 sunny days.  Expected value does not have to be an integer.


4.  Assuming the chance of rain is 35% in Luau Town, what is the chance that there are 3 rainy days before a sunny day?


WIN:  sunny days, P(WIN) = 0.65


# LOSSES?  3


Results:  


PDF= 0.079625

MEAN= 1.538461538

VAR= 0.8284023669

P(WIN)= 0.65



Eddie


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Python in Numworks: Duplicating and Grayscale

Python in Numworks: Duplicating and Grayscale All three scripts presented today use the math, random, and the Numworks specific ...