{"id":3713,"date":"2023-08-19T14:20:47","date_gmt":"2023-08-19T19:20:47","guid":{"rendered":"https:\/\/laurentlessard.com\/bookproofs\/?p=3713"},"modified":"2023-08-21T15:55:27","modified_gmt":"2023-08-21T20:55:27","slug":"optimal-baseball-lineup","status":"publish","type":"post","link":"https:\/\/laurentlessard.com\/bookproofs\/optimal-baseball-lineup\/","title":{"rendered":"Optimal baseball lineup"},"content":{"rendered":"<p>This week&#8217;s <a href=\"https:\/\/thefiddler.substack.com\/p\/can-you-set-a-winning-baseball-lineup\">Fiddler<\/a> is a problem about how to set the optimal baseball lineup.<\/p>\n<blockquote><p>\nEight of your nine batters are &#8220;pure contact&#8221; hitters. One-third of the time, each of them gets a single, advancing any runners already on base by exactly one base. (The only way to score is with a single with a runner on 3rd). The other two-thirds of the time, they record an out, and no runners advance to the next base. Your ninth batter is the slugger. One-tenth of the time, he hits a home run. But the remaining nine-tenths of the time, he strikes out. Your goal is to score as many runs as possible, on average, in the first inning. Where in your lineup (first, second, third, etc.) should you place your home run slugger?<\/p>\n<p><em>Extra Credit<\/em>: Instead of scoring as many runs as possible in the first inning, you now want to score as many runs as possible over the course of nine innings. What\u2019s more, instead of just having one home run slugger, you now have two sluggers in your lineup. The other seven batters remain pure contact hitters. Where in the lineup should you place your two sluggers to maximize the average number of runs scored over nine innings?\n<\/p><\/blockquote>\n<p>My solution:<br \/>\n<a href=\"javascript:Solution('soln_optbaseball','toggle_optbaseball')\" id=\"toggle_optbaseball\">[Show Solution]<\/a><\/p>\n<div id=\"soln_optbaseball\" style=\"display: none\">\n<p>We will attack this problem using <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dynamic_programming\">dynamic programming<\/a>. Rather than solving for the average number of runs for a particular configuration (of outs, runners on base, current batter, etc.), we will solve for <em>all possible configurations<\/em>! To this effect define the following:<br \/>\n\\[<br \/>\nV(o,r,b,i) = \\left\\{<br \/>\n\\begin{array}{l}<br \/>\n\\text{Expected number of runs at end of}\\\\<br \/>\n\\text{game given that we are currently in}\\\\<br \/>\n\\text{the $i^\\text{th}$ inning with $o$ outs, $r$ runners}\\\\<br \/>\n\\text{on base, and the $b^\\text{th}$ batter is up.}<br \/>\n\\end{array}\\right\\}<br \/>\n\\]Each index can only take on certain values. Define the sets:<br \/>\n\\begin{align}<br \/>\nO &#038;= \\{0,1,2,3\\} &#038;&#038; \\text{(number of outs)} \\\\<br \/>\nR &#038;= \\{0,1,2,3\\} &#038;&#038; \\text{(runners on base)} \\\\<br \/>\nB &#038;= \\{1,2,\\dots,9\\} &#038;&#038; \\text{(batter currently up)} \\\\<br \/>\nI &#038;= \\{1,2,\\dots,9\\} &#038;&#038; \\text{(current inning)}<br \/>\n\\end{align}Therefore there are $4\\times 4\\times 9\\times 9 = 1296$ possible configurations we must solve for. Finally, some subset of the batters are the <em>sluggers<\/em>. Let this subset be $S \\subseteq B$. The remaining batters are the pure contact hitters, which we denote $\\bar S$. Therefore $S\\cap \\bar S = \\emptyset$ and $S \\cup \\bar S = B$.<\/p>\n<p>Finally, define $p$ to be the on-base average for pure contact hitters, and $q$ to be the slugging percentage for the sluggers. In the problem statement, we have $p=\\frac{1}{3}$ and $q=\\frac{1}{10}$. <\/p>\n<p>We can now write equations that determine how the various configuration are related to each other:<\/p>\n<ul>\n<li> When we have 3 outs, clear bases and advance to next inning, or end the game if we are in the 9th inning.\\begin{align}<br \/>\nV(3,r,b,i) &#038;= V(0,0,b,i+1) &#038;&#038; \\forall r\\in R, b\\in B, i\\in\\{1,\\dots,8\\}\\\\<br \/>\nV(3,r,b,9) &#038;= 0 &#038;&#038; \\forall r\\in R, b\\in B<br \/>\n\\end{align}This is a total of $4\\times 9\\times 9 = 324$ equations.<\/p>\n<li> When a pure contact hitter is up to bat, they either advance runners (and possibly score if bases are loaded), or they strike out.<br \/>\n\\begin{align}<br \/>\nV(o,r,b,i) &#038;= p V(o,r+1,b+1,i) + (1-p) V(o+1,r,b+1,i) &#038;&#038; \\forall o\\in \\{0,1,2\\}, r\\in \\{0,1,2\\}, b\\in \\bar S, i\\in I \\\\<br \/>\nV(o,3,b,i) &#038;= p (1+V(o,3,b+1,i)) + (1-p) V(o+1,3,b+1,i) &#038;&#038; \\forall o\\in \\{0,1,2\\}, b\\in \\bar S, i \\in I<br \/>\n\\end{align}This is a total of $3\\times 4\\times 9\\times |\\bar S| = 108|\\bar S|$ equations.<\/p>\n<li> When a slugger is up to bat, they either hit a home run (and clear the bases), or they strike out.<br \/>\n\\begin{align}<br \/>\nV(o,r,b,i) &#038;= q (r+1+V(o,0,b+1,i)) + (1-q) V(o+1,r,b+1,i) &#038;&#038; \\forall o\\in\\{0,1,2\\}, r\\in R, b\\in S, i\\in I<br \/>\n\\end{align}This is a total of $3\\times 4\\times 9\\times |S| = 108|S|$ equations.\n<\/ul>\n<p>In all the equations above, when we write &#8220;$b+1$&#8221;, we mean that if we get to the end of the order, we should cycle back to the top of the order.<br \/>\nThis adds up to a total of $324+108|\\bar S|+108|S| = 1296$ equations. We have the same number of equations as variables, which tells us that we accounted for everything!<\/p>\n<p>I used Mathematica to input all the equations programmatically and solve them, and here is what I found.<\/p>\n<h3>First part: one inning, one slugger<\/h3>\n<p>When looking at a single inning with a single slugger, we pick $I = \\{1\\}$, and we can choose $S = \\{9\\}$ for example, and then ask, which batter should bat first to maximize score? this amounts to see which of<br \/>\n\\[<br \/>\nV(0,0,b,1),\\qquad b=1,2,\\dots,9<br \/>\n\\]is largest. Using the hitting percentages from the problem ($p=\\frac{1}{3}$ and $q=\\frac{1}{10}$), we obtain the following bar chart:<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball1.png\" alt=\"\" width=\"492\" height=\"365\" class=\"aligncenter size-full wp-image-3727\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball1.png 492w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball1-300x223.png 300w\" sizes=\"auto, (max-width: 492px) 85vw, 492px\" \/><\/a><\/p>\n<p>So it is best to put the slugger fourth in the batting order. This leads to an expected score of $\\frac{3051922249868633}{12708748019768805} \\approx 0.240143$ runs scored in the first inning. This is only narrowly better than putting the slugger third, which leads to an average of $0.23937$ runs scored. Overall, here is what we find:<br \/>\n\\[<br \/>\n\\begin{array}{r|ccccccccc}<br \/>\n\\text{slugger position} &#038; 1 &#038; 2 &#038; 3 &#038; 4 &#038; 5 &#038; 6 &#038; 7 &#038; 8 &#038; 9  \\\\ \\hline<br \/>\n\\text{expected runs} &#038; 0.184128 &#038; 0.210732 &#038; 0.23937 &#038; 0.240143 &#038; 0.202726 &#038; 0.156235 &#038; 0.168336 &#038; 0.174246 &#038; 0.176961<br \/>\n\\end{array}<br \/>\n\\]<\/p>\n<p>By changing $p$ and $q$, we get different solutions. For example, if we make the pure contact hitters better, say $p=\\frac{2}{3}$, but we keep the slugging at $q=\\frac{1}{10}$, we obtain:<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball2.png\" alt=\"\" width=\"492\" height=\"366\" class=\"aligncenter size-full wp-image-3725\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball2.png 492w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball2-300x223.png 300w\" sizes=\"auto, (max-width: 492px) 85vw, 492px\" \/><\/a><\/p>\n<p>So in this case, the slugger is best placed last, presumably because they get out so much more frequently than the pure contact hitters.<\/p>\n<h3>Second part: nine innings, two sluggers<\/h3>\n<p>With nine innings and two sluggers, I iterated through all possible two-element subsets $S \\subseteq B$, and then computed $V(0,0,1,1)$ for each case. This led to the following picture:<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball3.png\" alt=\"\" width=\"795\" height=\"653\" class=\"aligncenter size-full wp-image-3728\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball3.png 795w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball3-300x246.png 300w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball3-768x631.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>This plot shows the expected number of runs after 9 innings as a function of the two sluggers&#8217; positions in the batting order. The best outcome occurs when the sluggers are 3rd and 4th in the batting order. This is the yellow bar in the (3,4) coordinate, which corresponds to an average of 1.99771 runs per game. Note that the y-axis has a relatively small range; it only ranges from 1.85 to 2.0. This goes to show that over the course of 9 innings, the batting order does not make that much difference. Here is a table showing all the results:<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball4.png\" alt=\"\" width=\"842\" height=\"203\" class=\"aligncenter size-full wp-image-3730\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball4.png 842w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball4-300x72.png 300w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/08\/optbaseball4-768x185.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/p>\n<h3>Code<\/h3>\n<p>To solve this problem analytically, I used Mathematica to specify each of the equations above, and then called the &#8220;Solve&#8221; function on that system of equations. Each of the equations can be collected into a list using the &#8220;Table&#8221; function, which makes it easy to enumerate the equations without having to write each one down manually. You can view my Mathematica code <a href=\"https:\/\/www.wolframcloud.com\/obj\/laurent.lessard\/Published\/baseball_lineup.nb\">here<\/a>, or in the window below.<\/p>\n<p><iframe width=\"100%\" height='600' src='https:\/\/www.wolframcloud.com\/obj\/laurent.lessard\/Published\/baseball_lineup.nb' frameborder='0'><\/iframe><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This week&#8217;s Fiddler is a problem about how to set the optimal baseball lineup. Eight of your nine batters are &#8220;pure contact&#8221; hitters. One-third of the time, each of them gets a single, advancing any runners already on base by exactly one base. (The only way to score is with a single with a runner &hellip; <a href=\"https:\/\/laurentlessard.com\/bookproofs\/optimal-baseball-lineup\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Optimal baseball lineup&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3728,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[42],"tags":[16,43,8],"class_list":["post-3713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-fiddler","tag-dynamic-programming","tag-fiddler","tag-probability"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/3713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/comments?post=3713"}],"version-history":[{"count":12,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/3713\/revisions"}],"predecessor-version":[{"id":3736,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/3713\/revisions\/3736"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media\/3728"}],"wp:attachment":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media?parent=3713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/categories?post=3713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/tags?post=3713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}