#! /usr/bin/env python3 # def full_deck_expected ( card_num ): #*****************************************************************************80 # ## full_deck_expected() returns the expected value of the 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 MIT 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 MIT 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 in the full deck process.' ) print ( '' ) print ( ' N Expected' ) print ( '' ) for n in [ 10, 52, 100, 365, 500 ]: ev = full_deck_expected ( n ) print ( ' %3d %10.2f' % ( n, ev ) ) return def full_deck_simulation_test ( ): #*****************************************************************************80 # ## full_deck_simulation_test() tests full_deck_simulation(). # # Licensing: # # This code is distributed under the MIT 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 the deck, 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 ( ) full_deck_variance_test ( ) # # Terminate. # print ( '' ) print ( 'full_deck_simulation_test()' ) print ( ' Normal end of execution.' ) return def full_deck_stats ( n ): #*****************************************************************************80 # ## full_deck_stats() returns statistics for N tries of the full deck problem. # # Licensing: # # This code is distributed under the MIT 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_var: # the minimum, average, maximum and variance 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 ( ) m[i] = np.sum ( deck ) tries_min = np.min ( m ) tries_max = np.max ( m ) tries_mean = np.mean ( m ) tries_var = np.var ( m ) return tries_min, tries_mean, tries_max, tries_var def full_deck_stats_test ( ): #*****************************************************************************80 # ## full_deck_stats_test() tests full_deck_stats(). # # Licensing: # # This code is distributed under the MIT 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 full deck process.' ) print ( '' ) print ( ' N Min Mean Max Var' ) print ( '' ) for n in [ 10, 20, 50, 100, 1000, 10000 ]: tmin, tmean, tmax, tvar = full_deck_stats ( n ) print ( ' %5d %3d %6.2f %2d %6.2f' % ( n, tmin, tmean, tmax, tvar ) ) return def full_deck_try ( ): #*****************************************************************************80 # ## full_deck_try() returns all the cards drawn for a full deck problem. # # Licensing: # # This code is distributed under the MIT 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 ( 52 ) while ( True ): card = rng.integers ( low = 0, high = 52 ) 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 MIT 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 ( ' until all cards have been seen at least once.' ) print ( '' ) print ( ' Try Min Mean Max Total' ) print ( '' ) for i in range ( 0, 10 ): cards = full_deck_try ( ) 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 MIT 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 MIT 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 n in [ 10, 52, 100, 365, 500 ]: variance = full_deck_variance ( n ) print ( ' %3d %10.2f' % ( n, variance ) ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT 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 ( )