Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168742
Image: ubuntu2004
# First, create a "first head" random variable, which simulates flipping a coin until it is heads, # and records which flip is the first head. def first_head(): coin=0 # 0 for tails, 1 for heads. Start by setting the coin to tails count=0 # Keep track of the number of flips before first head while coin == 0: # the two lines in this "while loop" will be executed as long as the coin value is 0 # but once it equals 1, the loop ends and we move to the final line of the function def. coin=randint(0,1) # set coin equal to 0 or 1 randomly count += 1 # increment the count return count # this is the count value when the coin first equals 1 (and thus, the while loop ends). # test a few outputs and compute the average payoff (which, theoretically, is INFINITE). sum=0 nTrials=10 for _ in range(nTrials): fh = first_head() print('First head at flip # %s. You win %s dollars!'%(fh, 2^fh)) sum += 2^fh print('Average payoff was %s.' %float(sum/nTrials))
First head at flip # 1. You win 2 dollars! First head at flip # 2. You win 4 dollars! First head at flip # 3. You win 8 dollars! First head at flip # 5. You win 32 dollars! First head at flip # 1. You win 2 dollars! First head at flip # 3. You win 8 dollars! First head at flip # 1. You win 2 dollars! First head at flip # 1. You win 2 dollars! First head at flip # 5. You win 32 dollars! First head at flip # 3. You win 8 dollars! Average payoff was 10.0.
################################################################################ # Here is one simulation of playing 1000 games, where the price to play each # game is 5 dollars. The result is a plot of the money on hand after each play, # using the list_plot function. ################################################################################ price = 5 #cost to play the game once nGames = 1000 accumulation = 0 #player's profit sequence = [] #keep track of the first_head() outputs daily = [] #accumlation on each day; to be built for _ in range(nGames): fh = first_head() sequence.append(fh) accumulation += 2^fh-price daily.append(accumulation) list_plot(daily,plotjoined = True)
######################################################################################### # This is an INTERACTIVE simulation which allows you to change the price per game # and the total number of games. It also prints out the maximum payout, and # when this happened, the final amount of money on hand, the minimum amount, and the maximum, # and the average payout (which, theoretically, is INFINITY). # # Note the use of the @interactive command, followed by a function definition (the # name of the function can be anything, but we choose the generic "_" since we will never use the # function's name. You can look up the use of Sage interactive online, or just study this example. # It's pretty flexible, and easier to use than Mathematica's Manipulate command, although less slick. ############################################################################################## # We include the already-defined first_head function, so that this is a stand-alone cell. def first_head(): coin=0 # 0 for tails, 1 for heads. Start by setting the coin to tails count=0 # Keep track of the number of flips before first head while coin == 0: # the two lines in this "while loop" will be executed as long as the coin value is 0 # but once it equals 1, the loop ends and we move to the final line of the function def. coin = randint(0,1) # set coin equal to 0 or 1 randomly count += 1 # increment the count return count # this is the count value when the coin first equals 1 (and thus, the while loop ends). ############################################################################################################ # interactive part starts here ############################################################################################################ @interact def _(price= [5,10,15,20], nGames = [100,1000,10000,100000]): # note that the selection boxes for price and nGames are defined in the function defn above. accumulation = 0 #player's profit sequence = [] #keep track of the first_head() outputs daily = [] #accumlation on each day; to be built for _ in range(nGames): fh = first_head() sequence.append(fh) accumulation += 2^fh-price daily.append(accumulation) # Compute average payout totalPayout = 0 for i in range(len(sequence)): totalPayout += 2^sequence[i] avg = float(totalPayout/nGames) # Compute longest run of tails, max min, etc. longestRun=max(sequence) longestRunIndex=sequence.index(longestRun) print('Luckiest: game # %s, first head at position %s, winning $%s.'%(longestRunIndex, longestRun, 2^longestRun)) print('Lowest: %s'%min(daily)) print('Highest: %s'%max(daily)) print('Final: %s'%daily[nGames-1]) print('Average payout: %s.'%avg) # Plot the money on hand. l=list_plot(daily,plotjoined = True,gridlines=True) show(l) #in interactive mode, the plot doesn't display with just the list_plot() command. #instead, we use the show() function.
price 
nGames 
[removed]
[removed]
[removed]
[removed]