{"id":7914,"date":"2016-10-14T09:00:21","date_gmt":"2016-10-14T13:00:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=7914"},"modified":"2017-09-24T20:37:08","modified_gmt":"2017-09-25T00:37:08","slug":"blackjack","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/10\/14\/blackjack\/","title":{"rendered":"Blackjack!"},"content":{"rendered":"<div class=\"content\"><!--introduction--><!--\/introduction--><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4404-blackjack\"><tt>blackjack<\/tt><\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/349729-cleve-moler\">Cleve Moler<\/a>.<\/p><p>Last night, while preparing for a seminar on <a href=\"https:\/\/www.mathworks.com\/solutions\/machine-learning.html\">machine learning<\/a> I found myself with a lot of time to wait while a training algorithm ran. I was trying to create a classifier to differentiate images from 101 different categories, and things were taking a very long time.<\/p><p>A bit bored, I started browsing the File Exchange, and I came across a blackjack implementation by MathWorks's chief mathematician, chairman, and cofounder Cleve Moler. Intrigued, I downloaded it and spent the next hour happily playing cards while my computer churned away in the background.<\/p><p>The first thing I noticed when I ran Cleve's code was that, while it ran without errors, my Command Window was filled with red warnings that indicated that the 'symbol' font is no longer supported--as noted by user Bill R in 2015. (Cleve first submitted the file back in 2004.) The cards were readable, and they were properly colored, but the representations of the suits were awry. And though suits are irrelevant, the warning was annoying. Instead of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/suppress-warnings.html\">suppressing the warning<\/a>, I thought I'd take a quick stab at fixing it.<\/p><p>It took me a few seconds to find the offending line in Cleve's code:<\/p><pre>  text(x,y+.025,char(166+s),'fontname','symbol','fontsize',fs,'color',redblack)<\/pre><p>...and to replace it with a simple indexing call to work around the issue:<\/p><pre>charInd = [67,68,72,83];\r\ntext(x,y+0.025,char(charInd(s)),'fontsize',fs,'color',redblack)<\/pre><p>In short order, I was playing again, this time with no warnings. (The replacement of the symbols with letters didn't bother me at all, and certainly didn't affect the game.)<\/p><p>Cleve's commentary in the file is also interesting; he provides some background information on the game, and a rudimentary approach to maximizing your likelihood of winning. (Or rather, to minimize your loss--without \"counting strategies,\" the game is always slightly skewed in favor of the dealer, no matter how well you play--assuming you're not counting cards. And counting cards just doesn't work when a random number generator is dealing from a \"continuous shuffling machine\"!)<\/p><p>Armed with Cleve's strategy (\"You stand on any total over 11 when the dealer is showing a six or less.  Split aces and split 8's. Do not split anything else.  Double down with 11, or with 10 if the dealer is showing a six or less.\"), I played 35 hands...and ended up $20 in the hole! If only there had been someone bringing me free drinks while I played, the experience would have been complete!<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/capture-1-1.gif\" alt=\"\"> <\/p><p>As it turns out, Cleve has <a href=\"https:\/\/www.mathworks.com\/company\/newsletters\/articles\/simulating-blackjack-with-matlab.html\">blogged about blackjack<\/a> before. The article makes for an interesting read--highly recommended. Among other things, I learned that, with a \"basic strategy, the house advantage is only about one half of one percent of the original bet.\" (The article provides some more depth on strategy, and has some nice references.)<\/p><p>To test that \"1\/2-percent\" idea, I turned to Cleve's companion code, <tt>blackjacksim<\/tt>, which provides a cummulative sum of <i>n<\/i> simulated runs of well-played blackjack hands. But instead of just simulating a million serial hands (which I did):<\/p><pre>s = blackjacksim(1e6);\r\ns(end)= -32025<\/pre><p>(meaning that even if I played well, after a million hands I would be $32,000 in the hole!)...<\/p><p>I also simulated a million single hands, and looked at the distribution of 1-hand results (and the expected value of a single hand):<\/p><pre>sv = zeros(1e6,1);\r\nfor ii = 1:1e6\r\n   sv(ii) = blackjacksim(1);\r\nend\r\nmean(sv)<\/pre><pre>ans =\r\n  -0.01606<\/pre><p>(Pretty close to -1.5%!)<\/p><pre>histogram(sv,9)<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/blackjackPayouts.png\" alt=\"\"> <\/p><p>Thanks, Cleve. That was fun!<\/p><p>As always, I welcome your <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=7914#respond\">thoughts and comments<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_c0d1e5c10b9f4700a9063323d5752fa4() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='c0d1e5c10b9f4700a9063323d5752fa4 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' c0d1e5c10b9f4700a9063323d5752fa4';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_c0d1e5c10b9f4700a9063323d5752fa4()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016b<br><\/p><\/div><!--\r\nc0d1e5c10b9f4700a9063323d5752fa4 ##### SOURCE BEGIN #####\r\n%% Blackjack!\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week\r\n% is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4404-blackjack |blackjack|>, \r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/349729-cleve-moler Cleve Moler>.\r\n%\r\n%%\r\n% Last night, while preparing for a seminar on <https:\/\/www.mathworks.com\/solutions\/machine-learning.html machine learning>\r\n% I found myself with a lot of time to wait while a training algorithm ran. I was trying to create a classifier to \r\n% differentiate images from <https:\/\/www.mathworks.com\/examples\/matlab-computer-vision\/mw\/vision_product-ImageCategoryClassificationExample-image-category-classification-using-bag-of-features 101 different categories>,\r\n% and things were taking a very long time.\r\n%\r\n%% \r\n% A bit bored, I started browsing the File Exchange, and I came across\r\n% a blackjack implementation by MATLAB's chief mathematician,\r\n% chairman, and cofounder Cleve Moler. Intrigued, I downloaded it and\r\n% spent the next hour happily playing cards while my computer churned\r\n% away in the background.\r\n%\r\n% The first thing I noticed when I ran Cleve's code was that, while it\r\n% ran without errors, my Command Window was filled with red warnings\r\n% that indicated that the 'symbol' font is no longer supportedREPLACE_WITH_DASH_DASHas\r\n% noted by user Bill R in 2015. (Cleve first submitted the file back\r\n% in 2004.) The cards were readable, and they were properly colored,\r\n% but the representations of the suits were awry. And though suits\r\n% are irrelevant, the warning was annoying. Instead of \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/suppress-warnings.html suppressing the warning>,\r\n% I thought I'd take a quick stab at fixing it. \r\n%\r\n% It took me a few seconds to find the offending line in Cleve's code:\r\n%\r\n%%\r\n%    text(x,y+.025,char(166+s),'fontname','symbol','fontsize',fs,'color',redblack)\r\n%\r\n% ...and to replace it with a simple indexing call to work around the\r\n% issue:\r\n%\r\n%%\r\n%  charInd = [67,68,72,83];\r\n%  text(x,y+0.025,char(charInd(s)),'fontsize',fs,'color',redblack)\r\n%\r\n% In short order, I was playing again, this time with no warnings.\r\n% (The replacement of the symbols with letters didn't bother me at\r\n% all, and certainly didn't affect the game.)\r\n%\r\n% Cleve's commentary in the file is also interesting; he provides some\r\n% background information on the game, and a rudimentary approach to\r\n% maximizing your likelihood of winning. (Or rather, to minimize your\r\n% lossREPLACE_WITH_DASH_DASHwithout \"counting strategies,\" the game is always slightly\r\n% skewed in favor of the dealer, no matter how well you playREPLACE_WITH_DASH_DASHassuming you're not counting cards. And\r\n% counting cards just doesn't work when a random number generator is\r\n% dealing from a \"continuous shuffling machine\"!)\r\n%\r\n% Armed with Cleve's strategy (\"You stand on any total over 11 when the\r\n% dealder is showing a six or less.  Split aces and split 8's. Do not\r\n% split anything else.  Double down with 11, or with 10 if the dealer\r\n% is showing a six or less.\"), I played 35 hands...and ended up $20 in\r\n% the hole! If only there had been someone bringing me free drinks\r\n% while I played, the experience would have been complete!\r\n%\r\n%%\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/capture-1-1.gif>>\r\n% \r\n%\r\n% As it turns out, Cleve has\r\n% <https:\/\/www.mathworks.com\/company\/newsletters\/articles\/simulating-blackjack-with-matlab.html blogged about blackjack>\r\n% before. The article makes for an interesting readREPLACE_WITH_DASH_DASHhighly\r\n% recommended. Among other things, I learned that, with a \"basic\r\n% strategy, the house advantage is only about one half of one percent\r\n% of the original bet.\" (The article provides some more depth on\r\n% strategy, and has some nice references.)\r\n%\r\n% To test that \"1\/2-percent\" idea, I turned to Cleve's companion code,\r\n% |blackjacksim|, which provides a cummulative sum of _n_ simulated\r\n% runs of well-played blackjack hands. But instead of just simulating a\r\n% million serial hands (which I did):\r\n%\r\n%%\r\n%  s = blackjacksim(1e6);\r\n%  s(end)= -32025\r\n% \r\n% (meaning that even if I played well, after a million hands I would\r\n% be $32,000 in the hole!)...\r\n%\r\n% I also simulated a million single hands, and looked at the\r\n% distribution of 1-hand results (and the expected value of a single\r\n% hand):\r\n%\r\n%%\r\n%  sv = zeros(1e6,1);\r\n%  for ii = 1:1e6\r\n%     sv(ii) = blackjacksim(1);\r\n%  end\r\n%  mean(sv)\r\n%\r\n%  ans =\r\n%    -0.01606\r\n% \r\n% (Pretty close to -1.5%!)\r\n%\r\n%  histogram(sv,9)\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/blackjackPayouts.png>>\r\n% \r\n% Thanks, Cleve. That was fun!\r\n%\r\n%%\r\n% As always, I welcome your\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=7914#respond thoughts and comments>.\r\n\r\n##### SOURCE END ##### c0d1e5c10b9f4700a9063323d5752fa4\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/capture-1-1.gif\" onError=\"this.style.display ='none';\" \/><\/div><p>Brett's Pick this week is blackjack, by Cleve Moler.Last night, while preparing for a seminar on machine learning I found myself with a lot of time to wait while a training algorithm ran. I was... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/10\/14\/blackjack\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7914"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=7914"}],"version-history":[{"count":13,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7914\/revisions"}],"predecessor-version":[{"id":8868,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7914\/revisions\/8868"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=7914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=7914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=7914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}