This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Aside from your courtesy, your wiki MONIKER and date as a signature and minimal good faith of any internet post are the rules of this TCL-WIKI. Its very hard to reply reasonably without some background of the correspondent on his WIKI bio page. Thanks, gold 3/8/2024



Gamblers Ruin Expectation E(P(N)) in reference to Probability V2


Preface


gold Update 3/7/2024. The Gamblers' Ruin Expectation E(P(N)) is included under the topics of Random Walks. Console program outputs data as table in TCL table format and comma delimited spreadsheet. These auxiliary decks are used to proof features or subroutines of the prospective gui program. The Monopoly page from GWM seems closest in theme to what I was trying to learn. Using dice and running with random throws along a track or path like Snakes and Ladders.



gold Update 3/7/2024. The author is retired engineer on Windows 10 and no longer has proofing access to Unix machines. Unix is respected after use of so many decades in engineering, but my wings are lost. I did find a useful online IDE, jdoodle. I can paste from a text file, edit, and run an output using this online IDE.



Introduction



Not a Replacement for TCL Core


This page on developing pseudocode examples and one line procedures is not a replacement for the current Tcl core and Tcllib, which is much improved since Tcl version 4, and other <faster> language constructs. math ops, Tcllib routines, and other compiled routines can reduce the cost of big-data tasks by about 1/3. The time savings of the core are not always obvious on small quantities of data, like 4 or 5 numbers. Performance of one-line programs may suffer degradation due to lengthy recursion calls, and may be limited by constraints on recursion. Dependence on math operator notation, helper procedures, math check examples, degradation due to lengthy recursion calls, and special library functions should be noted in the comment lines.


gold 3/8/2024 Draft & Check. The Gambler's Ruin Problem involves two gamblers, A and B, with initial fortunes of $a$ and $b$, respectively. They play a game in which one of them wins a dollar from the other, and this continues until one of them has no money left. The probability of player A winning the game is $a/(a+b)$. The expected duration of the game is $(a+b)^2/(4ab)$, and the probability of player A winning all their money before player B does is $a/(a+b)$.


The Gamblers Ruin Expectation refers to the expected amount of money that a player will have at the end of the game, given the initial conditions and the rules of the game. This expectation over several games can be calculated using probability theory and mathematical formulas to determine the average outcome over multiple rounds of the game.



Gamblers_Ruin_Expectation & Probability over 1 or more games



gold 3/8/2024. The Gamblers Ruin Expectation E(P(N)) is included under the topics of Random Walks. As defined here, expectation E(P(N)) is the combined probability over 1 or more games with a constant win probability for each game. The example here is 50 percent chance of winning for each game and complement percentage for losing. The Expectation E(P(N)) or Gamblers chance of going broke after N successive games is E(P(N)) = < 0.5 + 0.25 + 0.125 + . . Nth game >. The general E(P(N) formula over the total games until finish is formula E(P(N)) = expr {1. - (1./2.)**$N} .


gold 3/8/2024. The more games, the closer the E(P(N)) approaches 1. The Expectation E(P(N) for an infinite set of games is 1, meaning a dead loss over all total games. Check for 1 game is expr {0.5 } returns 0.5. Check for 2 games is expr {0.5 + 0.25 } returns 0.75 Check for 3 games is expr {0.5 + 0.25 + 0.125 } returns 0.875 and ''expr { 1. - (1./2.)**3. } returns 0.875. Check for 4 games is expr { 1. - (1./2.)**4. } returns 0.9375. The Gamblers Expectation of win over all games or original flush state at E(P(N)) = 0 is not expected here, but still can not be ruled out.


The Gamblers Ruin Expectation refers to the expected amount of money that a player will have at the end of the game, given the initial conditions and the rules of the game. This expectation can be calculated using probability theory and mathematical formulas to determine the average outcome over multiple rounds of the game.


When considering the probability over one or more games, it is important to analyze the likelihood of different outcomes occurring in each round and how they affect the overall outcome of the game. The probability of winning or losing in each round, as well as the initial amount of money each player has, will influence the overall probability of winning or losing over multiple games.


By understanding the concepts of probability and Gamblers Ruin Expectation, players can make informed decisions about their betting strategies and assess the risks involved in playing games of chance. It is essential to consider factors such as the odds, the rules of the game, and the initial conditions to determine the expected outcome and make strategic decisions while playing.


Body


gold 3/8/2024. The Gamblers Ruin Expectation E(P(N)) is included under the topics of Random Walks. As defined here, expectation E(P(N)) is the combined probability over 1 or more games with a constant win probability for each game. The example here is 50 percent chance of winning for each game and complement percentage for losing. The Expectation E(P(N)) or Gamblers chance of going broke after N successive games is E(P(N)) = < 0.5 + 0.25 + 0.125 + . . Nth game >. The general E(P(N) formula over the total games until finish is formula E(P(N)) = expr {1. - (1./2.)**$N} . The more games, the closer the E(P(N)) approaches 1. The Expectation E(P(N) for an infinte set of games is 1, meaning a dead loss over all total games. Check for 1 game is expr {0.5 } returns 0.5. Check for 2 games is expr {0.5 + 0.25 } returns 0.75 Check for 3 games is expr {0.5 + 0.25 + 0.125 } returns 0.875 and ''expr { 1. - (1./2.)**3. } returns 0.875. Check for 4 games is expr { 1. - (1./2.)**4. } returns 0.9375. The Gamblers Expectation of win over all games or original flush state at E(P(N)) = 0 is not expected here, but still can not be ruled out.


Outline for Applications of the Gambler's Ruin Problem


1. Finance: The Gambler's Ruin Problem is used in finance to analyze the risks involved in investment strategies. It helps investors understand the probability of losing their entire investment before achieving a certain level of profit. This can be applied to stock market investments, portfolio management, and options trading.


2. Engineering: In engineering, the Gambler's Ruin Problem is used to analyze the reliability of systems. It can be applied to the study of components in a system, where the failure of one component can lead to the failure of the entire system. The Gambler's Ruin Problem helps engineers assess the probability of system failure and make informed decisions about component design and system redundancy.


3. Computer Science: In computer science, the Gambler's Ruin Problem is used in the study of algorithms and data structures. It can be applied to the analysis of randomized algorithms, probabilistic data structures, and the study of Markov chains. The Gambler's Ruin Problem helps researchers understand the behavior of algorithms and data structures under random conditions.



Gist: Gamblers' Ruin and Probability


1. Introduction. The Gamblers' Ruin Expectation are connected to probability analysis.


2. Collatz Sequences. Terence Tao's Collatz Sequences developments involve large real precision numbers. The Orbit Expectation E(P(N)) has a negative value or reduction over the region < 0 ... N >.


3. Comparing Collatz Sequences and Gauss-Legendre Formula. The Gauss-Legendre formula is a reduction of numbers to primes on the region < 0 ... N >. The Orbit Expectation E(P(N)) is a reduction of the probability envelope striking individual N's over the same region.



4. Gamblers' Ruin Expectation. The Gamblers' Ruin Expectation E(P(N)) is included under the topics of Random Walks. The example given is a 50% chance of winning for each game, and the Expectation E(P(N)) is the combined probability over 1 or more games.


5. General E(P(N)) Formula. The general formula for the Expectation E(P(N)) over the total games until finish is E(P(N)) = 1. - (1./2.)^N. The more games, the closer the E(P(N)) approaches 1.


6. Infinite Set of Games. The Expectation E(P(N)) for an infinite set of games is 1, meaning a dead loss over all total games.


7. Conclusion. The Collatz Sequences and Gamblers' Ruin Expectation are interconnected through probability analysis. Understanding these concepts can provide insights into various applications and mathematical concepts.


Pseudocode Section for Gamblers Ruin


#Pseudocode with sample problems
procedure Gamblers_Ruin_Expectation(games, probability_win)
    if games < 1 then
        return error: "The games must be 1 or larger"
    end if
    return (1 - (probability_win ^ games))
end procedure

# Examples:
# Gamblers_Ruin_Expectation(1, 0.5) returns 0.5
# Gamblers_Ruin_Expectation(2, 0.5) returns 0.75
# Gamblers_Ruin_Expectation(3, 0.5) returns 0.875
# Gamblers_Ruin_Expectation(4, 0.5) returns 0.9375
# Gamblers_Ruin_Expectation(3, 0.14) returns 0.997256
# House ^ is power sign in some math languages.

The pseudocode defines a procedure named Gamblers_Ruin_Expectation that takes two parameters: games and probability_win. The procedure checks if the number of games is less than 1 and returns an error message if it is. The procedure then calculates the expected value using the formula (1 - (probability_win ^ games)). The procedure returns the calculated expected value. The provided examples demonstrate how to use the Gamblers_Ruin_Expectation procedure with different input values and expected return values. This pseudocode performs the same operation as the TCL code, but in a more readable and platform independent manner.


TCL code Section

        proc Gamblers_Ruin_Expectation {games probability_win} {
            if { $games <1 } {
                return -code error "The games must be 1 or larger "
            }
            expr {(1.-($probability_win**$games))}       }
        # game  1 0.5 returns   0.5 
        # game  2 0.5 returns  0.75 
        # game 3 0.5 returns  0.875 
        # game  4 0.5 returns  0.9375 
        # game  3   .14  returns 0.997256
        proc collatz_sequences_Krasikov_Lagarias_limit {limit} {
            if { $limit <= 1 } {
                return -code error "The limit must be larger than 1"
            }
            expr {$limit** 0.84}
        }
        # Krasikov and Lagarias  limit 4  3.204
        # Krasikov and Lagarias limit 5  3.864
        # Krasikov and Lagarias limit  10  6.918
        # Krasikov and Lagarias limit  20  12.384
        # Krasikov and Lagarias limit  50  26.738
        set  Rehder_limit_collatz_sequences [ return [ expr { (log(3)/log(2))/(log(3)/log(2)+1.)} ] ]
        #  returns 0.61314719276545848 or or 61.3 percent
        set  Rehder_limit_collatz_sequences [ return [ expr { 1.- (log(3)/log(2))/(log(3)/log(2)+1.)} ] ]
        #  returns  0.3868528072345415 or 38.6 percent
        # Crandal developed some limits on his Collatz-like crandal_sequences,
        # which are much  different and shorter from Collatz_Sequences.
        proc crandal_sequences_height_crandal   {m }  {return [expr {( log($m)) / ( log(4./3.))}]} 
        proc crandal_sequences_height_crandal_modified {n}  {return [expr { 2.* (log( $n))/(log(16./9.))}]} 


Pseudocode Section for Krasikov-Lagarias limit ref Collatz Sequences



#Pseudocode with test cases
Procedure collatz_sequences_Krasikov_Lagarias_limit(limit):
    if limit <= 1:
        return "The limit must be larger than 1"
    else:
        return limit ** 0.84

# Testcases
# Krasikov and Lagarias limit 4: 3.204
# Krasikov and Lagarias limit 5: 3.864
# Krasikov and Lagarias limit 10: 6.918
# Krasikov and Lagarias limit 20: 12.384
# Krasikov and Lagarias limit 50: 26.738

The pseudocode defines a procedure called "collatz_sequences_Krasikov_Lagarias_limit" that takes a "limit" parameter. It first checks if the limit is less than or equal to 1. If so, it returns an error message indicating that the limit must be larger than 1. Otherwise, it calculates the square root of the limit raised to the power of 0.84 and returns the result. This pseudocode performs the same operation as the TCL code, but in a more readable and platform independent manner.



Pseudocode Section for Crandal Sequences Height ref Collatz Sequences



#pseudocode
procedure crandal_sequences_height_crandal_modified(n):
    return 2 * (log(n) / log(16/9))
end
# Testcases >> but need to check results here
# crandal_sequences_height_crandal_modified(9) = 4
# crandal_sequences_height_crandal_modified(16) = 5.631
# crandal_sequences_height_crandal_modified(25) = 7.244
# crandal_sequences_height_crandal_modified(36) = 8.857
# crandal_sequences_height_crandal_modified(49) = 10.470

The pseudocode defines a procedure called "crandal_sequences_height_crandal_modified" that takes a single parameter "n". It calculates the natural logarithm of "n" divided by the natural logarithm of 16/9, and then multiplies the result by 2 to get the final output. This pseudocode performs the same operation as the TCL code, but in a more readable and platform independent manner.


References:


Omitting outside links here, too transitory on internet.


  • Wikipedia: Gambler's Ruin
  • Khan Academy: Gambler's Ruin
  • MathWorld: Gambler's Ruin
  • Wolfram Alpha: Gambler's Ruin Problem
  • Quora: What are some practical applications of the Gambler's Ruin Problem?
  • The Gambler's Ruin Problem" - Discrete Mathematics with Applications, by Kenneth Rosen:
  • A chapter from the textbook explaining the Gambler's Ruin Problem
  • and its applications in discrete mathematics
  • "The Gambler's Ruin Problem: A Case Study in Probability" - MIT OpenCourseWare:
  • A lecture on the Gambler's Ruin Problem
  • "The Gambler's Ruin Problem" - Khan Academy: A video lesson explaining
  • the Gambler's Ruin Problem and its applications in probability and statistics.


  • GoDuck search engine < Functional Programming >
  • GoDuck search engine < Imperative Programming >
  • GoDuck search engine < Programming Examples >
  • Google search engine < vaporware >
  • Tcllib math::special Special mathematical functions
  • Tcllib math::figurate Evaluate figurate numbers
  • Tcllib simulation::random Pseudo-random number generators
  • Tcllib simulation::montecarlo Monte Carlo simulations
  • Wikipedia search engine < Lehmer random number generator >
  • Professor Frisby's Mostly Adequate Guide to Functional Programming on internet archive
  • Writing code using the Pseudocode Programming Process, article by David Zych
  • Mathematical Methods in Large-scale Computing Units
  • by Derrick H. Lehmer
  • L’Ecuyer, Pierre (January 1999). "Tables of linear congruential generators of different sizes and good lattice structure"
  • Mathematics of Computation. 68
  • A Comprehensive Review of Quantum Random Number PDF
  • Good Pedagogical Random Number Generators
  • from J. Stanley Warford
  • Coding the Lehmer Pseudo random Number Generator
  • from W. H. PAYNE
  • The most commonly used version of the Mersenne Twister algorithm
  • is based on the Mersenne prime expr { 2**19937-1 }
  • TWISTER = 431542479738816264805523551633791983905393504322.....
  • GoDuck search engine < TCL version >
  • One Liners Programs Pie in the Sky
  • One Liners
  • One Liners Programs Compendium [L1 ]
  • WIKI BOOKS, Programming_Examples pdf
  • WIKI BOOKS, Tcl_Programming_Introduction pdf
  • Note. I did find a useful online IDE, jdoodle
  • Note. I can paste, edit, and run an output using this online IDE.
  • How Do I Write Pseudocode? video by Mr. Brown CS
  • Refers to rand, RandMT, Mersenne Twister, & random
  • Suchenworth RS on Horseracing in Tcl.
  • Random Number Generators: Good Ones Are Hard to Find, Keith Willam Miller, pdf online
  • Throwing Two Dice GWM and Dice by Keith Vetter.
  • int from RLE has dice expression expr {1 + int(rand()*6)} RLE.
  • Several Dr. Math emails may reduce some dice issues to simple paths, internet archive.
  • Counting Elements in a List from RWT.


Extra Credit on One Line Procedures




  • Wikipedia search engine < random >
  • Wikipedia search engine < dice >
  • Wikipedia search engine < Programming Examples >
  • Google search engine < vaporware >
  • One Liners Programs Pie in the Sky
  • One Liners
  • One Liners Programs Compendium [L3 ]
  • WIKI BOOKS, Programming_Examples pdf
  • WIKI BOOKS, Tcl_Programming_Introduction pdf
  • google search engine < HgA1c to Average Blood Glucose>
  • Tcllib math::numtheory::numberPrimesGauss N
  • math::numtheory::numberPrimesLegendre N
  • math::numtheory::numberPrimesLegendreModified N
  • math::numtheory::differenceNumberPrimesLegendreModified lower upper
  • math::numtheory::listPrimePairs lower upper step
  • math::numtheory::listPrimeProgressions lower upper step



Auxiliary Code Under Test


#Puesdocode
function geometric_mean(args):
    product = 1
    N = length(args)
    for each val in args:
        product = product * val
    exponent = 1/N
    geometric_mean = product ^ exponent
    return geometric_mean

output = geometric_mean([1, 2, 3, 4])
print output
# TESTCASES
# (1*2)**(1/2) = 1.41421356237
# (1*2*3)**(1/3) = 1.81712059283
# (1*2*3*4)**(1/4)  = 2.2133638394
# (4*36*45*50*75)**(1/5) =30

Draft. The procedure ::math::geometric_mean takes two or more values as input and calculates their geometric mean. The code first initializes a variable product to 1, which will be used to store the product of all the input values. The procedure then determines the number of values in the input list args and stores it in the variable N. The code then iterates through each value in the input list, multiplying the current value by the product variable. After the loop, the code calculates the exponent by dividing 1 by the number of values (N). Finally, the code calculates the geometric mean by raising the product variable to the power of the exponent and returns the result. The example usage demonstrates how to call the procedure with values 1, 2, 3, and 4, and prints the result (2.2133638).


            set N [ expr { [ llength $args ] + 1 } ]
            if { $N == 1  } { return 0 }
# gm (1 2 3 4) = 2.213363839400643     
# gm (1 2 3 )  =  1.8171205928321397     
# gm (1 2  )  =  1.4142135623730951      
# gm (4 36 45 50 75) =    30.000  
#  gm {blank }   =   0 

        #    geometric_mean  arithmetic geometric_mean value
        proc geometric_mean { args } {
             set N [ expr { [ llength $args ] + 1 } ]
            if { $N == 1  } { return 0 }
            set product 1.
            set N [ expr { [ llength $args ] } ]
            foreach val $args {
                set product [ expr { $product*$val } ]
            }
            set exponent [ expr { 1./$N } ]
            set geometric_mean [ expr { $product**$exponent } ]
            #puts " $N   $exponent $product  $args "
            set geometric_mean
        }
        puts " gm (1 2 3 4) = [ geometric_mean   1 2 3 4 ]     "
        puts " gm (1 2 3 ) = [ geometric_mean   1 2 3  ]     "
        puts " gm (1 2) =  [ geometric_mean   1 2  ]      "
        puts "  gm (4 36 45 50 75)  =  [ geometric_mean   4 36 45 50 75  ]  "
        puts "  gm {blank }   = [ geometric_mean     ]  "

Template Subroutine for study here


  • see arithmetic mean on tcl-wiki Stats 2011-05-22, RLE
  • The min and max calculations are taken from the Tcl core tcllib package --> PSE

 # ::math::mean --
 #
 # Return the mean of two or more values
 #
 # Arguments:
 #    val  first value
 #    args other values
 #
 # Results:
 #    mean  arithmetic mean value
 
 proc ::math::mean {val args} {
      set sum $val
      set N [ expr { [ llength $args ] + 1 } ]
      foreach val $args {
         set sum [ expr { $sum+$val } ]
      }
      set mean [ expr { $sum/$N } ]
      set mean
 }

# Pseudocode
# math_mean
# Return the mean of two or more values
# Arguments:
# val first value
# args other values
# Results:
# mean or arithmetic mean value

proc math_mean {val args} {
    set N [expr {[llength $args] + 1}]
    if {$N == 1} {
        return 0
    }
    set sum $val
    set N [expr {[llength $args] + 1}]
    foreach val $args {
        set sum [expr {$sum + $val}]
    }
    set mean [expr {$sum / $N}]
    return $mean
}

puts "math_mean = [math_mean 1 2 3 4 5 6 7 8 9 10]"

# Testcases
# math_mean (1 2 3 4) = 10/4  = 2.5
# math_mean (1 2 3 ) = 6/3 = 2
# math_mean (1 2 ) = 3/2 = 1.5
# math_mean (4 36 45 50 75) = (4 +36 +45+ 50+ 75)/5 = 42
# math_mean = [math_mean 1 2 3 4 5 6 7 8 9 10] = 55/10 = 5.5
# math_mean { blank } = 0
# math_mean (1) = 0

Pseudocode: The math_mean procedure takes in a value val and additional values args. The procedure calculates the mean of all the values provided. The procedure only one value is provided, it returns 0. The procedure then calculates the sum of all values and divides it by the total number of values to get the mean. The mean value is returned at the end.


Hidden Comments Section

Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, gold 12Aug2020


test edit


Draft. The pseudocode defines a function called geometric_mean that calculates the geometric mean of a list of values passed as arguments. It initializes a variable product to 1 and calculates the product of all values in the list. Then, it calculates the exponent as 1 divided by the number of values in the list and computes the geometric mean using the formula (product ^ exponent). The function returns the calculated geometric mean. Finally, the code calls the geometric_mean function with a list 1, 2, 3, 4 and prints the result.)