Lecture 1: another example of simulating a probabilistic model

A slightly more complex simulation experiment, see slide 44 in the lecture slides.

In [1]:
import numpy as np

The "generative story" is as follows. First the reviewer decides whether to write a book review or a camera review, by flipping a coin (book reviews and camera reviews are equally probable). Then she decides about how long the review should be (the number of words is uniformly distributed between 1 and 100). Finally, for each position in the review, a word is selected randomly from the respective distribution of the review category.

In [2]:
categories = ['book', 'cam']
category_probs = [0.5, 0.5]

book_words = ['the', ',', '.', 'of', 'and', 'to', 'a', 'i', 'is', 'in', 'this', 'it', 'book', 'that', '"', 'for', "'s", 'as', 'with', 'was', 'you', 'not', 'are', 'but', 'on', 'have', 'be', 'he', 'his', ')', '(', "n't", 'or', 'one', 'about', 'by', 'from', 'all', 'an', 'read', 'if', 'at', 'who', 'my', '-', 'so', 'more', 'has', 'do', 'what', 'her', '!', ';', 'would', 'like', 'they', 'there', 'very', 'how', "'", 'just', 'can', 'some', 'out', 'will', '?', 'which', 'me', 'when', 'we', 'she', 'their', 'good', 'no', 'only', 'books', 'many', 'had', 'even', 'up', 'than', ':', 'other', 'does', 'time', 'author', 'did', 'people', 'much', 'your', 'most', 'story', 'were', 'been', 'into', 'first', 'could', 'great', 'also', 'well', 'really', 'get', 'any', 'reading', 'life', 'because', 'them', 'these', 'work', 'new', 'then', 'way', 'know', 'its', 'after', 'make', 'too', 'such', 'over', 'written', 'find', 'our', 'through', '&', 'those', 'world', 'should', 'characters', 'am', 'him', 'years', 'see', 'think', 'say', 'writing', 'being', 'while', 'better', 'why', 'where', '/', 'little', 'found', 'us', 'never', 'own', 'want', 'two', 'may', 'reader', 'however', 'love', 'novel', 'pages', 'history', 'best', "'m", 'quot', 'each', 'both', 'now', 'few', 'another', 'still', "'ve", 'made', 'something', 'man', 'anyone', 'children', 'here', 'before', 'down', 'back', 'information', 'every', 'need', 'things', 'must', 'seems', 'interesting', 'end', 'thought', 'same', 'use', 'times', 'chapter', 'take', 'go', 'real', 'without', 'ever', 'makes', 'nothing', 'understand', 'long', 'between', 'lot', 'page', 'might', 'recommend', "'ll", 'off', 'always', 'though', 'point', 'since', 'war', 'give', 'believe', 'fact', 'far', 'character', 'series', 'look', 'enough', 'different', 'help', 'actually', 'again', '`', 'put', 'last', 'feel', 'everything', 'used', 'got', 'bad', 'going', 'stories', 'thing', 'part', 'looking', 'quite', 'rather', 'american', 'hard', 'ca', 'true', 'often', 'old', 'come', 'learn', 'anything', 'right', 'buy', 'course', 'money', "'re", 'others', 'yet', 'women', 'least', 'sense', 'several', 'someone', 'mind', 'god', 'write', 'text', 'almost', 'readers', 'example', 'highly', 'plot', 'simply', 'instead', 'day', 'young', 'having', 'sure', 'especially', 'left', 'family', 'style', 'subject', 'three', 'wrong', 'easy', 'whole', 'school', 'works', 'away', 'myself', 'goes', 'around', 'case', 'making', 'words', 'language', 'problem', 'human', 'home', 'place', 'done', 'himself', 'start', 'less', 'short', 'ideas']
book_probs = [0.06948977838859308, 0.05973806804975897, 0.05868205382135673, 0.03849146599027857, 0.037581980051941744, 0.03530826520609964, 0.03239285750376425, 0.023419262912173975, 0.023095890134098653, 0.02133249795365663, 0.01837666865406188, 0.018184666067079646, 0.017078124842103144, 0.01672948856574067, 0.01408692664490638, 0.011798053700091963, 0.011181624341885871, 0.009554655052194388, 0.009322230867952749, 0.009160544478915083, 0.008594642117283264, 0.00855422052002385, 0.007897369564558346, 0.0077761047727800976, 0.007760946673807817, 0.006997989025536346, 0.006699879745748153, 0.006664510848146165, 0.00620471517932031, 0.005921763998504399, 0.005689339814262763, 0.005623654718716211, 0.0051891225481774895, 0.005143648251260648, 0.005032488858797255, 0.004971856462908131, 0.004835433572157605, 0.004789959275240759, 0.004739432278666492, 0.004633325585860525, 0.004370585203674322, 0.004284689309498066, 0.004274583910183209, 0.004239215012581221, 0.004198793415321808, 0.004016896227654437, 0.0038956314358761872, 0.0038956314358761872, 0.003870367937589053, 0.0038097355416999313, 0.003754155845468234, 0.003607627555402851, 0.00352678436088402, 0.0034762573643097503, 0.003430783067392909, 0.00342573036773548, 0.003354992572531503, 0.003117515688632437, 0.0030770940913730214, 0.0029861454975393365, 0.002970987398567054, 0.0029659346989096285, 0.002960881999252199, 0.002874986105075942, 0.002859828006103662, 0.0028497226067888083, 0.002844669907131381, 0.0028143537091868194, 0.0027082470163808524, 0.0026476146204917283, 0.0026021403235748867, 0.002592034924260032, 0.002480875531796639, 0.002465717432824359, 0.0024404539345372223, 0.002400032337277808, 0.0023393999413886843, 0.0023292945420738303, 0.0023090837434441235, 0.002298978344129268, 0.0022939256444718423, 0.002263609446527279, 0.002248451347554999, 0.002248451347554999, 0.002238345948240145, 0.002238345948240145, 0.00216255545337874, 0.0021423446547490324, 0.0021372919550916047, 0.0021372919550916047, 0.002132239255434179, 0.002096870357832189, 0.0020766595592024814, 0.0020463433612579194, 0.002041290661600494, 0.0020362379619430653, 0.0020059217639985054, 0.0019857109653687965, 0.0019705528663965157, 0.001960447467081662, 0.0019099204705073927, 0.0018745515729054034, 0.001859393473933123, 0.0018543407742756962, 0.0018189718766737071, 0.001808866477358853, 0.001743181381812303, 0.0017179178835251682, 0.0017179178835251682, 0.0016926543852380323, 0.0016673908869508974, 0.0016673908869508974, 0.001601705791404347, 0.0015966530917469208, 0.0015916003920894938, 0.0014551775013389658, 0.0014501248016815388, 0.0014198086037369763, 0.0014046505047646957, 0.0014046505047646957, 0.0013945451054498414, 0.0013945451054498414, 0.0013692816071627072, 0.001354123508190426, 0.0013389654092181452, 0.0012833857129864487, 0.001278333013329022, 0.0012732803136715949, 0.0012631749143567402, 0.001217700617439898, 0.0012126479177824712, 0.0011974898188101906, 0.0011873844194953366, 0.0011722263205230555, 0.0011621209212082017, 0.0011570682215507752, 0.001152015521893348, 0.001146962822235921, 0.001146962822235921, 0.0011419101225784941, 0.0011419101225784941, 0.0011368574229210669, 0.0011368574229210669, 0.0011318047232636394, 0.0011267520236062129, 0.0011267520236062129, 0.0011065412249765053, 0.0011065412249765053, 0.0010913831260042244, 0.0010863304263467968, 0.0010812777266893698, 0.0010610669280596622, 0.0010408561294299548, 0.0010307507301151, 0.0010307507301151, 0.0010256980304576733, 0.0010105399314853923, 0.0010054872318279657, 0.0010004345321705387, 0.00096506563456855, 0.00096506563456855, 0.0009499075355962692, 0.0009296967369665615, 0.0009296967369665615, 0.0009145386379942807, 0.0009094859383368534, 0.0009044332386794263, 0.0009044332386794263, 0.0009044332386794263, 0.0008993805390219994, 0.0008892751397071452, 0.0008892751397071452, 0.0008842224400497189, 0.0008791697403922917, 0.000864011641420011, 0.000864011641420011, 0.0008589589417625841, 0.0008539062421051569, 0.0008488535424477297, 0.0008488535424477297, 0.0008387481431328764, 0.0008387481431328764, 0.0008387481431328764, 0.0008286427438180224, 0.000823590044160595, 0.000823590044160595, 0.000823590044160595, 0.0008084319451883139, 0.0008084319451883139, 0.0007983265458734604, 0.0007882211465586064, 0.0007882211465586064, 0.0007781157472437527, 0.0007730630475863253, 0.0007680103479288985, 0.0007629576482714714, 0.0007528522489566175, 0.0007427468496417635, 0.0007376941499843366, 0.0007376941499843366, 0.0007326414503269096, 0.0007275887506694828, 0.0007275887506694828, 0.0007225360510120556, 0.0007174833513546289, 0.0007174833513546289, 0.0007073779520397745, 0.0007073779520397745, 0.0007073779520397745, 0.0007073779520397745, 0.0006922198530674941, 0.0006871671534100672, 0.0006871671534100672, 0.0006821144537526401, 0.000677061754095213, 0.000677061754095213, 0.000677061754095213, 0.0006720090544377861, 0.0006720090544377861, 0.000666956354780359, 0.000666956354780359, 0.000661903655122932, 0.0006568509554655052, 0.0006416928564932243, 0.0006416928564932243, 0.0006366401568357974, 0.0006366401568357974, 0.0006366401568357974, 0.0006366401568357974, 0.0006366401568357974, 0.0006315874571783707, 0.0006315874571783707, 0.0006315874571783707, 0.0006265347575209436, 0.0006214820578635163, 0.0006164293582060897, 0.0006063239588912355, 0.0006063239588912355, 0.0006063239588912355, 0.0006012712592338088, 0.0006012712592338088, 0.0006012712592338088, 0.0005962185595763819, 0.0005911658599189549, 0.0005861131602615277, 0.0005861131602615277, 0.0005861131602615277, 0.0005810604606041009, 0.0005810604606041009, 0.000576007760946674, 0.000576007760946674, 0.0005709550612892471, 0.0005709550612892471, 0.0005709550612892471, 0.0005659023616318196, 0.0005608496619743928, 0.0005608496619743928, 0.0005557969623169661, 0.0005557969623169661, 0.0005557969623169661, 0.0005507442626595389, 0.0005456915630021122, 0.0005406388633446843, 0.0005406388633446843, 0.0005305334640298315, 0.0005305334640298315, 0.0005305334640298315, 0.0005305334640298315, 0.0005254807643724041, 0.0005254807643724041, 0.0005204280647149773, 0.0005204280647149773, 0.0005204280647149773, 0.0005153753650575505, 0.0005103226654001239, 0.0005103226654001239, 0.0005103226654001239, 0.0005103226654001239, 0.0005103226654001239, 0.0005103226654001239, 0.0005052699657426966, 0.0005002172660852688, 0.0004901118667704154, 0.0004850591671129884, 0.0004850591671129884, 0.0004850591671129884, 0.0004850591671129884, 0.00048000646745556113, 0.00048000646745556113, 0.000474953767798135, 0.000474953767798135, 0.000474953767798135, 0.000474953767798135, 0.00046990106814070776, 0.00046484836848328075, 0.00045979566882585357, 0.00045979566882585357, 0.00045979566882585357, 0.00045979566882585357, 0.00045979566882585357]
cam_words = ['the', '.', ',', 'i', 'a', 'to', 'and', 'it', 'is', 'this', 'of', 'camera', 'for', 'in', 'you', 'that', 'with', 'my', 'on', 'have', 'not', 'but', 'was', "n't", ')', 'as', '(', "'s", 'are', 'lens', '"', 'be', 'so', 'very', 'if', 'one', 'at', 'or', '!', 'pictures', 'do', 'use', 'good', 'can', 'all', 'would', 'will', 'when', 'had', 'out', 'from', 'just', 'get', 'great', 'an', 'has', '-', 'they', 'quality', 'about', 'canon', 'like', 'your', 'no', 'more', 'me', 'only', 'digital', 'there', 'battery', 'than', 'up', 'does', 'time', 'after', 'bought', 'what', 'even', 'also', 'buy', 'which', 'take', 'better', 'because', 'well', 'some', 'flash', 'by', 'picture', 'am', '/', 'other', 'really', 'did', 'light', "'ve", 'much', 'still', 'them', 'then', 'cameras', 'video', 'been', 'sony', 'got', 'price', ':', 'used', 'could', 'case', 'any', 'we', 'using', 'product', 'easy', 'now', 'zoom', 'were', 'work', "'m", 'its', 'photos', 'batteries', 'want', 'image', 'small', 'shots', 'problem', 'back', 'too', 'little', 'need', 'see', 'first', 'same', 'off', 'over', 'before', 'takes', 'while', 'new', 'amazon', 'two', 'screen', 'card', 'these', 'never', 'most', 'without', 'another', 'think', 'make', 'nice', 'many', 'best', 'way', '?', 'few', 'money', 'purchased', 'nikon', ';', '2', 'their', 'go', 'love', 'camcorder', 'lot', 'should', 'low', 'took', 'bag', 'since', 'memory', 'however', 'focus', 'months', 'size', 'taking', 'into', 'works', 'how', 'thing', 'going', 'find', 'shoot', 'far', 'frame', 'photo', 'recommend', 'know', 'long', 'lenses', 'look', 'sure', 'mode', 'computer', 'who', '3', 'found', 'day', 'made', 'enough', 'again', 'put', 'right', 'looking', 'images', 'unit', 'ca', 'manual', 'come', 'came', 'excellent', 'less', 'say', 'having', 'old', 'being', 'makes', 'set', 'buying', 'features', 'years', 'around', 'reviews', 'lcd', 'both', 'problems', 'film', 'point', 'big', 'purchase', 'shooting', 'high', 'bad', 'able', 'down', 'tried', "'re", 'software', 'people', 'times', 'through', 'always', 'feature', '1', 'shot', 'fit', 'may', 'year', 'tripod', 'pretty', 'service', 'filter', 'anything', 'those', 'something', 'almost', 'kodak', 'extra', 'bit', 'charger', 'thought', 'color', 'read', 'taken', 'happy', 'fine', 'every', 'item', '4', 'getting', 'end', 'might', 'days', 'worked', 'different', 'worth', 'cost', 'keep', 'fast', 'ago', 'panasonic', 'comes', 'said', 'several', 'compact', 'though', 'power', 'hard', 'must', 'actually', 'life', 'away', 'settings', 'filters', 'ever', 'our', 'turn']
cam_probs = [0.06548592880061889, 0.06198556735025174, 0.04518129589468482, 0.040209768037641605, 0.031959821938134944, 0.03145886441172368, 0.03140813453563144, 0.029670636279470895, 0.02255577115752896, 0.019790992910499815, 0.01731157021648976, 0.016759882813986233, 0.01592283985846366, 0.01336732235031517, 0.013322933708734426, 0.012942459638042341, 0.012555644332838727, 0.010222070032593948, 0.009562581643394344, 0.00948648682925592, 0.009397709546094436, 0.008649443873733346, 0.008516277948991116, 0.00662659006455377, 0.006442694263719266, 0.006214409821304012, 0.006170021179723271, 0.006144656241677134, 0.006112950069119459, 0.006011490316934902, 0.006005149082423369, 0.005656381184288962, 0.005459802914431383, 0.0053583431622468295, 0.005187129830435389, 0.004882750573881724, 0.00486372687034712, 0.004838361932300983, 0.004800314525231774, 0.004736902180116428, 0.004476911565143504, 0.004432522923562761, 0.004185214777612908, 0.0041471673705437, 0.004096437494451421, 0.00402668391482454, 0.0040076602112899375, 0.003969612804220727, 0.003937906631663055, 0.003925224162639985, 0.003785717003386222, 0.0037223046582708754, 0.003576456264505577, 0.003576456264505577, 0.003538408857436367, 0.0035320676229248335, 0.0035320676229248335, 0.003519385153901763, 0.003513043919390229, 0.0033291481185557218, 0.0032023234283250295, 0.0032023234283250295, 0.003157934786744285, 0.0031389110832096808, 0.003088181207117403, 0.0030564750345597295, 0.0029930626894443817, 0.002853555530190619, 0.002790143185075271, 0.0027711194815406674, 0.0027520957780060645, 0.00272038960544839, 0.002625271087775369, 0.002606247384240764, 0.002549176273636953, 0.002536493804613883, 0.0025047876320562104, 0.0024984463975446757, 0.0024033278798716545, 0.0023462567692678414, 0.0023462567692678414, 0.0023208918312217024, 0.002308209362198634, 0.002232114548060217, 0.002225773313548682, 0.002156019733921802, 0.0021369960303871966, 0.002130654795875662, 0.002124313561364127, 0.002124313561364127, 0.0020989486233179886, 0.0020418775127141768, 0.0020355362782026416, 0.0020228538091795717, 0.0019531002295526895, 0.001946758995041155, 0.001921394056995016, 0.0019150528224834823, 0.0019023703534604124, 0.0018389580083450654, 0.001819934304810462, 0.0017945693667643229, 0.0017882281322527886, 0.0017818868977412525, 0.0017184745526259062, 0.0016931096145797677, 0.0016867683800682327, 0.0016804271455566975, 0.001674085911045163, 0.001674085911045163, 0.0016550622075105596, 0.001642379738487489, 0.0016296972694644207, 0.001610673565929816, 0.0016043323314182815, 0.0015979910969067467, 0.0015979910969067467, 0.0015916498623952113, 0.0015853086278836768, 0.001578967393372142, 0.0015662849243490734, 0.0015662849243490734, 0.0015409199863029338, 0.0015282375172798646, 0.00152189628276833, 0.0015155550482567957, 0.0014838488756991224, 0.0014775076411875866, 0.0014648251721645173, 0.0014267777650953093, 0.0014140952960722403, 0.001407754061560705, 0.0013697066544914975, 0.001344341716445358, 0.0013316592474222886, 0.0013189767783992193, 0.0012999530748646158, 0.0012872706058415458, 0.0012428819642608024, 0.0012301994952377336, 0.0012238582607261989, 0.0012238582607261989, 0.0012175170262146641, 0.0012175170262146641, 0.0012111757917031294, 0.0012111757917031294, 0.0012111757917031294, 0.0011858108536569908, 0.0011731283846339207, 0.0011604459156108523, 0.0011477634465877828, 0.0011350809775647127, 0.0011287397430531781, 0.0011287397430531781, 0.0011160572740301082, 0.0011033748050070394, 0.001097033570495505, 0.001097033570495505, 0.0010906923359839697, 0.0010906923359839697, 0.0010843511014724356, 0.0010589861634262966, 0.0010589861634262966, 0.0010589861634262966, 0.0010526449289147623, 0.0010463036944032267, 0.0010399624598916926, 0.0010209387563570882, 0.001014597521845553, 0.001014597521845553, 0.0010082562873340187, 0.0010082562873340187, 0.0010019150528224842, 0.0010019150528224842, 0.0010019150528224842, 0.0010019150528224842, 0.000995573818310949, 0.0009892325837994147, 0.0009638676457532758, 0.0009385027077071365, 0.0009385027077071365, 0.000932161473195602, 0.000932161473195602, 0.000932161473195602, 0.0009258202386840674, 0.0009194790041725325, 0.0009131377696609982, 0.0009131377696609982, 0.000906796535149463, 0.000906796535149463, 0.0009004553006379287, 0.0008877728316148589, 0.0008814315971033243, 0.0008750903625917901, 0.0008750903625917901, 0.0008687491280802553, 0.0008687491280802553, 0.0008560666590571858, 0.0008560666590571858, 0.0008560666590571858, 0.0008560666590571858, 0.0008370429555225813, 0.0008307017210110465, 0.0008307017210110465, 0.0008307017210110465, 0.000824360486499512, 0.000824360486499512, 0.000824360486499512, 0.0008116780174764425, 0.0008116780174764425, 0.000805336782964908, 0.000805336782964908, 0.0007989955484533734, 0.0007926543139418388, 0.0007926543139418388, 0.0007926543139418388, 0.000786313079430304, 0.000786313079430304, 0.0007736306104072347, 0.0007672893758956999, 0.0007609481413841649, 0.0007609481413841649, 0.0007609481413841649, 0.0007546069068726306, 0.0007546069068726306, 0.000748265672361096, 0.000748265672361096, 0.0007419244378495611, 0.0007419244378495611, 0.0007419244378495611, 0.0007419244378495611, 0.0007355832033380262, 0.0007355832033380262, 0.0007292419688264916, 0.0007292419688264916, 0.0007292419688264916, 0.000722900734314957, 0.0007165594998034222, 0.0007165594998034222, 0.0007102182652918872, 0.0007038770307803525, 0.0007038770307803525, 0.0007038770307803525, 0.0007038770307803525, 0.0006975357962688182, 0.0006975357962688182, 0.0006911945617572831, 0.0006911945617572831, 0.0006911945617572831, 0.0006911945617572831, 0.0006911945617572831, 0.0006848533272457488, 0.0006848533272457488, 0.0006785120927342136, 0.0006785120927342136, 0.0006785120927342136, 0.0006785120927342136, 0.000672170858222679, 0.000672170858222679, 0.000672170858222679, 0.000672170858222679, 0.0006658296237111443, 0.0006658296237111443, 0.0006658296237111443, 0.0006658296237111443, 0.0006594883891996096, 0.0006594883891996096, 0.0006531471546880751, 0.0006468059201765401, 0.0006404646856650058, 0.0006341234511534711, 0.0006341234511534711, 0.0006341234511534711, 0.0006277822166419361, 0.0006214409821304018, 0.0006214409821304018, 0.0006214409821304018, 0.0006214409821304018, 0.0006214409821304018, 0.0006150997476188668, 0.0006087585131073321, 0.0006024172785957973, 0.0006024172785957973, 0.0005960760440842626, 0.0005897348095727282, 0.0005833935750611933, 0.0005833935750611933, 0.0005833935750611933, 0.0005770523405496583, 0.0005707111060381238, 0.0005707111060381238, 0.0005707111060381238, 0.0005707111060381238, 0.0005707111060381238, 0.000564369871526589, 0.000564369871526589, 0.000564369871526589, 0.000564369871526589, 0.0005580286370150545, 0.0005580286370150545]

max_review_length = 100
In [3]:
def generate_random_review():
    category = np.random.choice(['book', 'cam'], p=category_probs)
    doc_len = np.random.randint(1, 101)
    if category == 'book':
        output = np.random.choice(book_words, p=book_probs, size=doc_len)
    else:
        output = np.random.choice(cam_words, p=cam_probs, size=doc_len)
    return output
    
In [10]:
generate_random_review()
Out[10]:
array(['batteries', 'it', 'need', 'and', 'camera', 'panasonic', 'and',
       'my', 'in', 'for', 'of', 'picture', 'and', 'for', 'the', 'had',
       '-', 'should', 'bit', 'up', 'best', 'not', 'the', 'of', '"', 'to',
       'and', 'has', 'not', 'if', 'a', '.', 'amazon', 'for', '.', 'get',
       '!', 'thing', ':', '(', 'i', 'camcorder', 'lenses', '"', 'to',
       'was', 'it', 'it', 'them', 'you', 'the', 'an', 'and', 'look',
       'canon', 'the', 'been', 'that', 'very', 'this', 'my', "'s"],
      dtype='<U9')