Showing posts with label expected value. Show all posts
Showing posts with label expected value. Show all posts

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. 


Saturday, August 5, 2023

HP 12C: Log-Normal Distribution Parameter Conversions

HP 12C:   Log-Normal Distribution Parameter Conversions


Introduction

The log-normal distribution is transformation of a standard normal variable, where for a standard normal variable t, then a random variable x follows a log-normal distribution, with the form:

x = e^(μ + t * σ)

where:

μ = mean
σ = standard deviation (sample)

The distribution takes the positive values of x.  The cumulative distributive function of the log-normal distribution (the area between 0 and x) is:

pdf = 1/2 * (1 + erf((ln x - μ) ÷ (σ * √2)) )

erf is the error function.

erf(θ) = 2 ÷ √(π) * ∫(e^(-s^2) ds, s = 0 to s = θ)


This program on today's blog focuses on the relationship between the distribution mean (μ), standard deviation (σ), the arithmetic expected value (E[x]), and the arithmetic variance (Var[x]):

E[x] =e^(μ + σ^2 ÷ 2)

Var[x] = (e^(σ^2) - 1) * e^(2 * μ + σ^2)

μ = ln( E[x]^2 ÷ √(Var[x] + E[x]^2) )

σ = √( ln (1 + Var[x] ÷ E[x]^2 ) )


HP 12C Program:  Log-Normal Distribution Parameter Conversions


Calculate E[x] and Var[x] from μ and σ

Instructions:

To find E[x] and Var[x]:
1.  Store μ in memory register 1
2.  Store σ in memory register 2
3.  Run the program.  E[x] is shown in the X stack and is stored in memory register 3.  Var[x] is shown in the Y stack in memory register 4.  

Code:
(Step:  Key Code:   Key)
(assume program starts with step 00)

01:  45, 2:   RCL  2
02:  2:    2
03:  21:  y^x
04:  44, 0:   STO 0
05:  43, 22:  e^x
06:  1:  1
07:  30:  -
08:  2:  2
09:  45, 1:  RCL 1
10:  20:  ×
11:  45, 0:  RCL 0
12:  40:  +
13:  43, 22:  e^x
14:  20:  ×
15:  44, 4:  STO 4
16:  45, 0:  RCL 0
17:  2:   2
18:  10:  ÷
19:  45, 1:  RCL 1
20:  40:   +
21:  43, 22:  e^x
22:  44, 3:  STO 3
23:  44, 33, 00:  GTO 00

Lines 01 to 03:   Store σ^2 in memory register 0


Examples  (answers are rounded to four decimal places):

Example 1
Inputs:  μ = 1, σ = 0.5
Results:  E[x] = 3.0802, Var[x] = 2.6948

Example 2
Inputs:  μ = 0, σ = 1
Results:  E[x] = 1.6487,  Var[x] = 4.6708


Calculate μ and σ from E[x] and Var[x]


Instructions

To find μ and σ:
1.  Store E[x] in memory register 3
2.  Store Var[x] in memory register 4
3.  Run the program.  μ is shown in the X stack and is stored in memory register 1.  σ is shown in the Y stack in memory register 2.  

Code:
(Step:  Key Code:   Key)
(assume program starts with step 00)

01:  45, 4:  RCL 4
02:  45, 3:  RCL 3
03:  2:   2
04:  21:  y^x
05:  44, 0:  STO 0
06:  10:  ÷
07:  1:  1
08:  40:  +
09:  43, 23:  LN
10:  43, 21:  √
11:  44, 2:  STO 2
12:  45, 0:  RCL 0
13:  45, 0:  RCL 0
14:  45, 4:  RCL 4
15:  40:  +
16:  43, 21:  √
17:  10:  ÷
18:  43, 23:  LN
19:  44, 1:  STO 1
20:  43, 33, 00:  GTO 00

Lines 01 to 03:   Store E[x]^2 in memory register 0
Lines 12 to 13:   Put two copies of memory register 0 on to the stack

Examples  (answers are rounded to four decimal places):

Example 1
Inputs:  E[x] = 1.84, Var[x] = 0.36
Results:  μ = 0.5592,  σ = 0.3180

Example 2
Inputs:  E[x] = 5.03, Var[x] = 1.72
Results:  μ = 1.5825,  σ = 0.2565

Memory Registers:
R1 = μ
R2 = σ
R3 = E[x]
R4 = Var[x]


Source

"Log-normal distribution"  Wikipedia.  Last Edited May 18, 2023 and retrieved May 24, 2023.  https://en.wikipedia.org/wiki/Log-normal_distribution

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 ...