#! /usr/bin/env python3 # def full_deck_expected ( card_num ): #*****************************************************************************80 # ## full_deck_expected(): expected value of the general full deck problem. # # Discussion: # # The expected value, given a deck of N distinct cards, is n*Hn, where # Hn is the n-th harmonic number. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # # Reference: # # Herbert Wilf, # Some New Aspects of the Coupon Collector's Problem, # SIAM Review, # Volume 48, Number 3, September 2006, pages 549-565. # # Input: # # integer card_num: the number of cards in the deck. # # Output: # # real ev: the expected number of cards that must be collected in order # to acquire at least one of every kind. # ev = 0.0 for i in range ( 1, card_num + 1 ): ev = ev + card_num / i return ev def full_deck_expected_test ( ): #*****************************************************************************80 # ## full_deck_expected_test() tests full_deck_expected(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # print ( '' ) print ( 'full_deck_expected_test():' ) print ( ' full_deck_expected() reports the expected value for' ) print ( ' the number of cards drawn before seeing every card,' ) print ( ' for the general full deck process, using M cards.' ) print ( '' ) print ( ' card_num Expected' ) print ( '' ) for card_num in [ 10, 52, 100, 365, 500 ]: ev = full_deck_expected ( card_num ) print ( ' %3d %10.2f' % ( card_num, ev ) ) return def full_deck_histogram ( n, card_num ): #*****************************************************************************80 # ## full_deck_histogram() histograms full_deck statistics. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 July 2022 # # Author: # # John Burkardt # # Input: # # integer N, the number of trials to make. # import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'full_deck_histogram():' ) print ( ' Create a histogram of the full deck process.' ) m = np.zeros ( n ) for i in range ( 0, n ): deck = full_deck_try ( card_num ) m[i] = np.sum ( deck ) plt.hist ( m, bins = 20, rwidth = 0.95, density = True ) plt.grid ( True ) plt.xlabel ( '<-- Number of draws -->' ) plt.ylabel ( '<-- Probability -->' ) plt.title ( 'PDF for draws needed to see full deck' ) filename = 'full_deck_pdf.png' plt.savefig ( filename ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( block = False ) plt.close ( ) return def full_deck_simulation_test ( ): #*****************************************************************************80 # ## full_deck_simulation_test() tests full_deck_simulation(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'full_deck_simulation_test():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' full_deck_simulation() simulates a process in which' ) print ( ' a card is drawn from a deck of 52 cards, then returned.' ) print ( ' The process is continued until all cards have been seen' ) print ( ' at least once.' ) full_deck_try_test ( ) full_deck_stats_test ( ) full_deck_expected_test ( ) n = 2000 card_num = 52 full_deck_histogram ( n, card_num ) full_deck_variance_test ( ) # # Terminate. # print ( '' ) print ( 'full_deck_simulation_test():' ) print ( ' Normal end of execution.' ) return def full_deck_stats ( n, card_num ): #*****************************************************************************80 # ## full_deck_stats() returns statistics for N tries of the full deck problem. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # # Input: # # integer n: the number of times the process will be carried out. # # Output: # # integer tries_min, real tries_mean, integer tries_max, real tries_std: # the minimum, average, maximum and std for the number of cards drawn # over all the processes. # import numpy as np m = np.zeros ( n ) for i in range ( 0, n ): deck = full_deck_try ( card_num ) m[i] = np.sum ( deck ) tries_min = np.min ( m ) tries_max = np.max ( m ) tries_mean = np.mean ( m ) tries_std = np.std ( m ) return tries_min, tries_mean, tries_max, tries_std def full_deck_stats_test ( ): #*****************************************************************************80 # ## full_deck_stats_test() tests full_deck_stats(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # print ( '' ) print ( 'full_deck_stats_test():' ) print ( ' full_deck_stats() reports statistics for N instances' ) print ( ' of the 52-card full deck process.' ) print ( '' ) print ( ' N Min Mean Max Std' ) print ( '' ) card_num = 52 for n in [ 10, 20, 50, 100, 1000, 10000 ]: tmin, tmean, tmax, tstd = full_deck_stats ( n, card_num ) print ( ' %5d %3d %6.2f %2d %6.2f' % ( n, tmin, tmean, tmax, tstd ) ) print ( '' ) print ( ' N Min Mean Max Std' ) print ( '' ) card_num = 300 for n in [ 10, 20, 50, 100, 1000, 10000 ]: tmin, tmean, tmax, tstd = full_deck_stats ( n, card_num ) print ( ' %5d %3d %6.2f %2d %6.2f' % ( n, tmin, tmean, tmax, tstd ) ) return def full_deck_try ( card_num ): #*****************************************************************************80 # ## full_deck_try() returns all the cards drawn for a full deck problem. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # # Output: # # integer deck(52): the number of times each card was drawn. # from numpy.random import default_rng import numpy as np rng = default_rng() deck = np.zeros ( card_num ) while ( True ): card = rng.integers ( low = 0, high = card_num ) deck[card] = deck[card] + 1 if ( np.all ( 0 < deck ) ): break return deck def full_deck_try_test ( ): #*****************************************************************************80 # ## full_deck_try_test() tests full_deck_try(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'full_deck_try_test():' ) print ( ' full_deck_try() repeatedly draws a random card' ) print ( ' from a 52-card deck until all cards have been seen' ) print ( ' at least once.' ) print ( '' ) print ( ' Try Min Mean Max Total' ) print ( '' ) card_num = 52 for i in range ( 0, 10 ): cards = full_deck_try ( card_num ) print ( ' %2d %3d %6.2f %2d %4d' % \ ( i, np.min ( cards ), np.mean ( cards ), np.max ( cards ), np.sum ( cards ) ) ) return def full_deck_variance ( card_num ): #*****************************************************************************80 # ## full_deck_variance() returns the variance of the full deck problem. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # # Reference: # # Herbert Wilf, # Some New Aspects of the Coupon Collector's Problem, # SIAM Review, # Volume 48, Number 3, September 2006, pages 549-565. # # Input: # # integer card_num: the number of cards in the deck. # # Output: # # real variance: the variance in the number of cards that must be # collected in order to acquire at least one of every kind. # variance = 0.0 for i in range ( 2, card_num + 1 ): variance = variance + ( i - 1 ) / ( card_num - i + 1 )**2 variance = card_num * variance return variance def full_deck_variance_test ( ): #*****************************************************************************80 # ## full_deck_variance_test() tests full_deck_variance(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2022 # # Author: # # John Burkardt # print ( '' ) print ( 'full_deck_variance_test():' ) print ( ' full_deck_variance() reports the variance for' ) print ( ' the number of cards drawn in the full deck process.' ) print ( '' ) print ( ' N Variance' ) print ( '' ) for card_num in [ 10, 52, 100, 365, 500 ]: variance = full_deck_variance ( card_num ) print ( ' %3d %10.2f' % ( card_num, variance ) ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) full_deck_simulation_test ( ) timestamp ( )