% matplotlib inline import numpy as np import matplotlib.pyplot as matlab from scipy.stats import poisson class Person : def __init__ ( self , name ): self . beers = 10 self . age_years = 0 self . name = name def drink ( self ): if self . beers > 0 : self . beers -= poisson . rvs ( 1.5 ) self . beers = max ( 0 , self . beers ) if self . beers == 0 : print ' %s : Ung... I am drunk.!' % self . name def buys_beer ( self ): self . beers += poisson . rvs ( 12 ) def go_by_week ( self ): self . buys_beer () person = Person ( 'myself' ) beer_history = [] while person . beers > 0 : for i in xrange ( 7 ): beer_history . append ( person . beers ) person . drink () if person . beers == 0 : break beer_history . append ( person . beers ) if person . beers == 0 : break person . buys_beer () beer_history = np . asarray ( beer_history ) days = len ( beer_history ) beers_per_day = np . concatenate ([ np . asarray ([ 0 ]), - np . diff ( beer_history , 1 )]) beers_per_day = np . asarray ([ max ( 0 , x ) for x in beers_per_day ]) matlab . plot ( beer_history ) matlab . hold ( True ) matlab . stem ( beers_per_day ) matlab . xlabel ( 'day' ) matlab . ylabel ( 'beers remaining and beers/day' ) matlab . title ( ' %s \' s beers' % person . name ) matlab . show ()