tag:blogger.com,1999:blog-15324198057018363862024-03-18T02:48:29.929-07:00Random ProblemsHere are solutions to some random problemstheboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.comBlogger149125tag:blogger.com,1999:blog-1532419805701836386.post-74532270022513660992024-01-12T22:10:00.000-08:002024-01-12T22:14:41.938-08:00Visualizing the 3n+1 ProblemYou might have heard of the 3n+1 problem at some point. It is pretty cool to visualize.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQH_5dS4ROgQlH5wwJ6pDGJE8qs2l34xMqRvOkVUkD_Amnsz_puS6Y4XZmc3PFwEguOeAk65wo5G-m3itqNO-OicrS92k_kvdfwOZFU758Cex9SYxml6eQ2cvIBK7RJu5wq7it_Stpi39jusl7nn87BqiSvG0ypqCaFYGfe0xsC7tBFTS6Fmg8vnf1bmGu/s1809/3n1.gif" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="585" data-original-width="1809" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQH_5dS4ROgQlH5wwJ6pDGJE8qs2l34xMqRvOkVUkD_Amnsz_puS6Y4XZmc3PFwEguOeAk65wo5G-m3itqNO-OicrS92k_kvdfwOZFU758Cex9SYxml6eQ2cvIBK7RJu5wq7it_Stpi39jusl7nn87BqiSvG0ypqCaFYGfe0xsC7tBFTS6Fmg8vnf1bmGu/s1600/3n1.gif" width="0%" /></a></div><a name='more'></a><h4 style="text-align: left;">Problem</h4><br><div>The problem basically states that if you do the following:<br /><ul style="text-align: left;"><li>take any positive integer</li><li>if it's even, divide it by 2</li><li>if it's odd, multiply it by 3 and add 1 to it</li><li>repeat this and stop once you've gotten to 1</li></ul></div><div>you will eventually stop at 1. Take 7 for example:<br /><ul style="text-align: left;"><li>7 is odd so next number is 3(7) + 1, or 22</li><li>22 is even so next number is 22/2, or 11</li><li>11 is odd so next number is 3(11) + 1, or 34</li><li>34 is even so next number is 34/2, or 17</li><li>17 is odd so next number is 3(17) + 1, or 52</li><li>52 is even so next number is 52/2, or 26</li><li>26 is even so next number is 26/2, or 13</li><li>13 is odd so next number is 3(13) + 1, or 40</li><li>40 is even so next number is 40/2, or 20</li><li>20 is even so next number is 20/2, or 10</li><li>10 is even so next number is 10/2, or 5</li><li>5 is odd so next number is 3(5) + 1, or 16</li><li>16 is even so next number is 16/2, or 8</li><li>8 is even so next number is 8/2, or 4</li><li>4 is even so next number is 4/2, or 2</li><li>2 is even so next number is 2/2, or 1</li><li>1 is the stopping point</li></ul><div></div></div><div>You can see that it bounces around a bit. What does that bouncing look like? Here are the paths for the numbers 2 to 31 (watch until at least 27):</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQH_5dS4ROgQlH5wwJ6pDGJE8qs2l34xMqRvOkVUkD_Amnsz_puS6Y4XZmc3PFwEguOeAk65wo5G-m3itqNO-OicrS92k_kvdfwOZFU758Cex9SYxml6eQ2cvIBK7RJu5wq7it_Stpi39jusl7nn87BqiSvG0ypqCaFYGfe0xsC7tBFTS6Fmg8vnf1bmGu/s1809/3n1.gif" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="585" data-original-width="1809" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQH_5dS4ROgQlH5wwJ6pDGJE8qs2l34xMqRvOkVUkD_Amnsz_puS6Y4XZmc3PFwEguOeAk65wo5G-m3itqNO-OicrS92k_kvdfwOZFU758Cex9SYxml6eQ2cvIBK7RJu5wq7it_Stpi39jusl7nn87BqiSvG0ypqCaFYGfe0xsC7tBFTS6Fmg8vnf1bmGu/s1600/3n1.gif" width="80%" /></a></div><br /><div></div><div><br /></div><div>Pretty cool that 27 seems to explode out of nowhere. How does the iterations required to get to 1 change based on the starting value?</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0YQK1s1EmbLI1S0_Qcs69gmf67eT0181LbLIieZ9ufL_tSHEpgkOm7Er3yAso-UvQ7puiULiKWkroOO9Eo0iQBQd5b2HmNwJlY24vfIX6dhumpejrGvmcnr1DsPeT-0HM-TCyaeUA7Wzips5WZEEVeZEwRKRtxCEtHYMqB1Fby-efocfvtYiAecI0bFwI/s1809/3n1_iterations.gif" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="585" data-original-width="1809" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0YQK1s1EmbLI1S0_Qcs69gmf67eT0181LbLIieZ9ufL_tSHEpgkOm7Er3yAso-UvQ7puiULiKWkroOO9Eo0iQBQd5b2HmNwJlY24vfIX6dhumpejrGvmcnr1DsPeT-0HM-TCyaeUA7Wzips5WZEEVeZEwRKRtxCEtHYMqB1Fby-efocfvtYiAecI0bFwI/s1600/3n1_iterations.gif" width="80%" /></a></div><div><br />And finally, what's the max value you get from each starting value?</div><br /><div>
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh99UyTTugeGNlxOS5Xa0oy2VyncrfUeGxCz9kQDSDE0loBGGOTUYwkgq-CIoa75NHPZQfUYDnU4jiBsZ6KRrsanFnk5oeNm46R_pqac5KU4J2rpv4HaM3fyzGIUBI8L6t6Qf2iuj7mkqGY_wMBeQAiDKlDdeh07x9B7NdNDQdbXUBuBbKKd1gKQ879JdpT/s1809/3n1_maxval.gif" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="500" data-original-width="1809" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh99UyTTugeGNlxOS5Xa0oy2VyncrfUeGxCz9kQDSDE0loBGGOTUYwkgq-CIoa75NHPZQfUYDnU4jiBsZ6KRrsanFnk5oeNm46R_pqac5KU4J2rpv4HaM3fyzGIUBI8L6t6Qf2iuj7mkqGY_wMBeQAiDKlDdeh07x9B7NdNDQdbXUBuBbKKd1gKQ879JdpT/s1600/3n1_maxval.gif" width="80%" /></a></div><br /><div><br /><br /><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com0tag:blogger.com,1999:blog-1532419805701836386.post-12736682830879974262023-03-04T21:57:00.004-08:002023-03-05T13:20:54.780-08:00What Were the Most Dominant Dynasties in College Football?It's hard to get a sense of how much Nick Saban's Alabama teams are dominating compared with Bobby Bowden's FSU ones.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw5anUaBzfvmdgFAYQNpbUiu_5s6j4Qbo5tpr2KjpdOCwJOmnLPAhIPAHFXPOXMpofsKV3c9lwz2dNZRgWwwVq8ih4KYtA85MgxvNs7v_XJHs8ocMH0AEtPiFkex7VG7RFAPtqJCAxEiVhhOHmVsA8uTjEUUmV7RXY7WUf7D5mzYcnXgV4jW2J5w4cLg/s922/cfb%20momentum%20map.gif" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="681" data-original-width="922" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw5anUaBzfvmdgFAYQNpbUiu_5s6j4Qbo5tpr2KjpdOCwJOmnLPAhIPAHFXPOXMpofsKV3c9lwz2dNZRgWwwVq8ih4KYtA85MgxvNs7v_XJHs8ocMH0AEtPiFkex7VG7RFAPtqJCAxEiVhhOHmVsA8uTjEUUmV7RXY7WUf7D5mzYcnXgV4jW2J5w4cLg/s1600/cfb%20momentum%20map.gif" width="0%" /></a></div><a name='more'></a>To try to visualize it, I created a score to represent a program's momentum. The algorithm works like this for each season:
<br />
<ul>
<li>1st place adds 6 pts</li>
<li>2nd place adds 3 pts</li>
<li>3rd place adds 2 pts</li>
<li>4th place adds 1 pts</li>
<li>Lose one point if you finished outside the top-4 two years straight</li>
<li>Increment the points you lose by 1 for each year past two for the previous bullet</li>
<li>Reset points lost increment if you finish in the top-4</li>
</ul>
I used the AP poll for all rankings because it exists and is easily mineable for all years in this post.
As an example, consider this table of score vs year and position:
<table style="margin-left: auto; margin-right: auto; text-align: center;">
<tbody><tr style="background-color: #e6a117;"><th>year</th><th>position</th><th>score</th></tr>
<tr><td>2000</td><td>10th</td><td>0</td></tr>
<tr><td>2001</td><td>1st</td><td>6</td></tr>
<tr><td>2002</td><td>10th</td><td>6</td></tr>
<tr><td>2003</td><td>1st</td><td>11</td></tr>
<tr><td>2004</td><td>10th</td><td>11</td></tr>
<tr><td>2005</td><td>10th</td><td>10</td></tr>
<tr><td>2006</td><td>10th</td><td>8</td></tr>
<tr><td>2007</td><td>3rd</td><td>10</td></tr>
<tr><td>2008</td><td>10th</td><td>10</td></tr>
<tr><td>2009</td><td>6th</td><td>9</td></tr>
<tr><td>2010</td><td>10th</td><td>7</td></tr>
<tr><td>2011</td><td>8th</td><td>4</td></tr>
<tr><td>2012</td><td>10th</td><td>0</td></tr>
</tbody></table><br /><div>
The general idea is that you grow momentum by consistently finishing near the top, championships boost it by a lot, and you need sustained seasons outside the top to lose momentum. </div><div><br /></div><div>One interesting way to view this is geographically. In the plot below, each circle's size is proportional to the score of the team located at the center of the circle: <div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw5anUaBzfvmdgFAYQNpbUiu_5s6j4Qbo5tpr2KjpdOCwJOmnLPAhIPAHFXPOXMpofsKV3c9lwz2dNZRgWwwVq8ih4KYtA85MgxvNs7v_XJHs8ocMH0AEtPiFkex7VG7RFAPtqJCAxEiVhhOHmVsA8uTjEUUmV7RXY7WUf7D5mzYcnXgV4jW2J5w4cLg/s922/cfb%20momentum%20map.gif" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="681" data-original-width="922" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw5anUaBzfvmdgFAYQNpbUiu_5s6j4Qbo5tpr2KjpdOCwJOmnLPAhIPAHFXPOXMpofsKV3c9lwz2dNZRgWwwVq8ih4KYtA85MgxvNs7v_XJHs8ocMH0AEtPiFkex7VG7RFAPtqJCAxEiVhhOHmVsA8uTjEUUmV7RXY7WUf7D5mzYcnXgV4jW2J5w4cLg/s1600/cfb%20momentum%20map.gif" width="80%" /></a></div><br /><div><br /></div><div>You clearly see giant circles appear for famous dynasties like Bowden at Florida State and Saban at Alabama. Another interesting takeaway from this view is how much dominance has shifted to a small geographic area in the southeast and Ohio. </div><div><br /></div><div>Focusing in on top dynasties, here are those plotted over time:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUOVDOTvTItvRiFQOQthgo-jIg00bZXw49Us_e02WlgBWJiua-Y2Xw5RGQrJfssbdymVvD962fuH4UpdQG5xVJWi4nOR_s-fPRYENbmL90ngV1y4qny5uunA9LQc6GOElhiDBzyXRQcJKsIdo34PZMm6Bw8cHwTtR5akMtLnwpq266DAAtPTl5Xx0ZpA/s1479/cfb%20momentum%20vs%20time.gif" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="681" data-original-width="1479" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUOVDOTvTItvRiFQOQthgo-jIg00bZXw49Us_e02WlgBWJiua-Y2Xw5RGQrJfssbdymVvD962fuH4UpdQG5xVJWi4nOR_s-fPRYENbmL90ngV1y4qny5uunA9LQc6GOElhiDBzyXRQcJKsIdo34PZMm6Bw8cHwTtR5akMtLnwpq266DAAtPTl5Xx0ZpA/s1600/cfb%20momentum%20vs%20time.gif" width="80%" /></a></div><br /><div><br />Pretty cool. From this metric at least, Saban's current run at Alabama is historic.</div></div><div><br /></div><div>Overall, I'm surprised at how non-dominant Nebraska and Penn State were. I viewed their reigns as similar to Miami's, and they aren't really near that level from this metric at least.</div><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-28062818121746530902022-07-01T21:28:00.005-07:002022-07-01T21:30:49.172-07:00Mario Party's 'Hide and Sneak' is not BalancedAre you consistently losing to a 6 year old and looking for an excuse. If so, you've come to the right place.<div class="separator" style="clear: both; text-align: center;"><a href="https://upload.wikimedia.org/wikipedia/en/9/91/MP3DSlogo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="51" data-original-width="300" height="51" src="https://upload.wikimedia.org/wikipedia/en/9/91/MP3DSlogo.png" width="0%" /></a></div><a name='more'></a>'Hide and Sneak' is one of the mini-games in Mario Party. The basic rules are that 3 people hide and 1 person finds them. In round 1, there are 4 hiding spots. In round 2, 3. In round 3, 2. <p>Each player on the hiding team picks a spot to hide and the seeking player picks one spot. If a player is hiding in the picked spot, he's out.</p><p>If a player is out he doesn't participate in later rounds.</p><p>If any players remain on the hiding team after 3 rounds, the hiding team wins.</p><p>Is this game 50/50? Working through the math, start with 1 hider:</p><p></p><ul style="text-align: left;"><li>round 1, there's a 3/4 chance of not being found</li><li>round 2, there's a 2/3 chance of not being found</li><li>round 3, there's a 1/2 chance of not being found</li></ul><div>Thus, for an individual player on the hiding team, there's a (3/4) * (2/3) * (1/2) chance of not being found. That's 1/4, or 25%.</div><div><br /></div><div>Inverting that, the seeker has a 3/4, or 75% chance of finding a given player after 3 rounds.</div><div><br /></div><div><b>Since there are 3 players and their hiding decisions are independent, the chance of the seeker finding all 3 players in 3 rounds is just (3/4)^3, or 27/64. That's only 42%. It isn't balanced at all.</b></div><div><br /></div><div>Is there an obvious way to balance it? What if we did 4 rounds with 5 starting hiding spots. For one hider:</div><div></div><p></p><ul><li>round 1, there's a 4/5 chance of not being found</li><li>round 2, there's a 3/4 chance of not being found</li><li>round 3, there's a 2/3 chance of not being found</li><li>round 4, there's a 1/2 chance of not being found</li></ul><div>Multiplied out, that's a 1/5, or 20% chance of not being found so 4/5, or 80% success chance for the seeker. <b>The chance that the seeker finds all 3 players in 4 rounds then is (4/5)^3, or 51%. 4 rounds is way more balanced than 3 rounds.</b></div><div><br /></div><div><br /></div><p><br /></p>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com3tag:blogger.com,1999:blog-1532419805701836386.post-57948306009156736622022-05-30T20:20:00.005-07:002023-11-04T22:37:47.673-07:00A Simple Dice GameMy son asked about a seemingly simple dice game and I didn't know how to
answer immediately. The question is:<br /><br /><i>Player 1 rolls a 6-sided die. Player 2 rolls a 6-sided die. If player 2
rolls the same as player 1, he rolls again. If he again rolls the same, he
loses. How often will each player win?</i> <div><br /><div><img src="https://upload.wikimedia.org/wikipedia/commons/1/1c/6sided_dice_%28cropped%29.jpg" width="0%" />
<a name='more'></a>It seems obvious that player 1 will win since rolling a 6 guarantees
a win for him and getting the same thing twice guarantees a loss for player 2.
The atual math was interesting to reason through. We can simply go through each
possibility.
<ul>
<li>
Player 1 rolls a 1
<ul>
<li>
Player 2 wins with anything else (5/6). If he rolls a 1 (1/6 chance), roll again with a 5/6 chance of winning
</li>
<li>
Player 2 win chance is 5/6 + (1/6)*(5/6) = 35/36
</li>
</ul>
</li>
<li>
Player 1 rolls a 2
<ul>
<li>
Player 2 wins with a 3 or up (4/6). If he rolls a 1 (1/6 chance), roll again with a 4/6 chance of winning
</li>
<li>
Player 2 win chance is 4/6 + (1/6)*(4/6) = 28/36
</li>
</ul>
</li>
<li>
Player 1 rolls a 3
<ul>
<li>
Player 2 wins with a 4 or up (3/6). If he rolls a 1 (1/6 chance), roll again with a 3/6 chance of winning
</li>
<li>
Player 2 win chance is 3/6 + (1/6)*(3/6) = 21/36
</li>
</ul>
</li>
<li>
Player 1 rolls a 4
<ul>
<li>
Player 2 wins with a 5 or up (2/6). If he rolls a 1 (1/6 chance), roll again with a 2/6 chance of winning
</li>
<li>
Player 2 win chance is 2/6 + (1/6)*(2/6) = 14/36
</li>
</ul>
</li>
<li>
Player 1 rolls a 5
<ul>
<li>
Player 2 wins with a 6 (1/6). If he rolls a 1 (1/6 chance), roll again with a 1/6 chance of winning
</li>
<li>
Player 2 win chance is 1/6 + (1/6)*(1/6) = 7/36
</li>
</ul>
</li>
<li>
Player 1 rolls a 6; he wins automatically
</li></ul>
To get Player 2's win chance then, we just sum his chance for all possibilities and divide by the number of possibilities (6). That's (1/6)*(35/36 + 28/36 + 21/36 + 14/36 + 7/36) = 105/216 = 48.6%.
<div><br /></div><div>Thus, Player 1 wins 100 - 48.6, or 51.4% of the time.</div><div><br /></div><div>What if we change the rule slightly? Try this game:<br /><br /><i>Player 1 rolls a 6-sided die. Player 2 rolls a 6-sided die. If player 2 rolls the same as or less than player 1, he rolls again. If he again rolls the same, he loses. If he rolls higher in the second roll, he wins. How often will each player win?</i></div><div><i><br /></i></div><div>I'll just do player 1 rolls a 2 as an example then present the answer since the logic is so similar. </div><div><br /></div><div>If Player 1 rolls a 2, Player 2 will win with a 3 or above so 4/6. He will reroll for a 2 or below so 2/6 of the time, and his chance of winning that second roll is 4/6. Thus, if Player 1 rolls a 3, Player 2 wins 4/6 + (2/6)*(4/6), or 32/36.</div><div><br /></div><div>Summing all paths up again, Player 1 wins this game 42.2% of the time.</div><div><br /></div><div>To try to make things a bit fairer, consider one more game:<br /><br /><i>Player 1 rolls a 6-sided die. Player 2 rolls a 6-sided die. If player 2 rolls the same as or less than player 1, he rolls again and subtracts 1 from his roll. If he again scores the same, he loses. If he scores higher in the second role, he wins. How often will each player win?</i></div><div><i><br /></i></div><div>Going through the same logic, this is the fairest of these games. Player 1 wins 49.0% of the time.</div><div><i><br /></i></div></div></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com0tag:blogger.com,1999:blog-1532419805701836386.post-45270509865386997172021-11-10T23:10:00.008-08:002021-11-15T22:42:14.666-08:00Simple Tutorial for Hosting a CRUD Node App on AWS Elastic BeanstalkI had trouble finding (working) simple tutorials for running Node.js CRUD apps on AWS using Elastic Beanstalk so I wrote one from scratch and documented it.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUM9fhqh3QDn62oHGCsbDIrP4BKtJmCqT2K5wg6VKR_gt-Thz82QAT5dUAIK-PeZbhTdgYyAuLtW2ekuE45v2bAXhexEeyhXgo4njQUpa8Z7LDuf17_MqLX6caxUK2h1CWI0qVcS8TeNCx/s1804/initial+working+environment+screenshot.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="677" data-original-width="1804" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUM9fhqh3QDn62oHGCsbDIrP4BKtJmCqT2K5wg6VKR_gt-Thz82QAT5dUAIK-PeZbhTdgYyAuLtW2ekuE45v2bAXhexEeyhXgo4njQUpa8Z7LDuf17_MqLX6caxUK2h1CWI0qVcS8TeNCx/s1300/initial+working+environment+screenshot.PNG" width="0%" /></a></div><a name='more'></a><h4 style="text-align: left;">The app</h4><div>I've put two phases together. The <a href="https://github.com/rhamner/express_hello_world" target="_blank">first is a simple hello world</a> using Node.js, and the <a href="https://github.com/rhamner/simple_nodemysql_crud_app" target="_blank">second is a simple app</a> that lets you add a number to a MySQL database and read back all numbers that you've added. Below is what it looks like when it's working:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimQIu8rPsL3DysUGFef0Xi885yVCUnEuQw6m24RbTT0PwdsFQaJBqWg15hJtcL7333q2jwt-2cwQ1u8qgWm84RB5fBzYaDrJy6vheHgYzzwjMGzl986vGMiaafzc4SZLrmEIwIQ53Fmmhj/s1500/working+crud+page.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="66" data-original-width="436" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimQIu8rPsL3DysUGFef0Xi885yVCUnEuQw6m24RbTT0PwdsFQaJBqWg15hJtcL7333q2jwt-2cwQ1u8qgWm84RB5fBzYaDrJy6vheHgYzzwjMGzl986vGMiaafzc4SZLrmEIwIQ53Fmmhj/s320/working+crud+page.PNG" width="35%" /></a></div><br /><div><br /></div><div>Please do not use this as a reference for writing Node cleanly or anything like that. This is structured to (hopefully) be very minimal and easy to follow example for setting up Node.js and MySQL through this service and isn't intended to be a serious app.<br /><br /><h4 style="text-align: left;">Setting up your AWS environment</h4></div><div>Set up an environment using the <a href="https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/GettingStarted.html" target="_blank">AWS documentation</a>. When writing this, the steps were:</div><div><ul style="text-align: left;"><li>create an environment</li></ul><ul style="text-align: left;"><li>choose 'Web server environment'</li></ul><ul style="text-align: left;"><li>configure environment</li><ul><li>give it a name</li><li>choose node js for type</li><li>use defaults for node and linux</li><li>choose 'upload your code' at the bottom</li><li>upload a zipped folder with the code from one of the examples I listed above; it should look like this (i.e., files directly in the zipped folder and not a folder containing them)</li></ul></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQj-cy7hPEc1fAH0WF87C5-QLrzZpmH57n82DcaEvNvGvrYbGQgv4qRXGo8FNMYK1FOsv1jJiOQ4D1-2tZREv1OTHslsx1hxbof9CMzHgr8rcEFKsZBPYCJp-ynWyod6SfadNAvxI9jfKn/s1172/zipped+folder.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="281" data-original-width="1172" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQj-cy7hPEc1fAH0WF87C5-QLrzZpmH57n82DcaEvNvGvrYbGQgv4qRXGo8FNMYK1FOsv1jJiOQ4D1-2tZREv1OTHslsx1hxbof9CMzHgr8rcEFKsZBPYCJp-ynWyod6SfadNAvxI9jfKn/s1500/zipped+folder.PNG" width="70%" /></a></div><div><br /></div><ul style="text-align: left;"><ul><li>configure more options</li><ul><li>create a database and choose mysql</li></ul></ul></ul><ul style="text-align: left;"><li>create</li></ul><ul style="text-align: left;"><li>wait 10 minutes or so</li></ul><div>These might change though so the AWS documentation is likely a good place to start for initial environment creation.</div></div><div><br /></div><div>When complete, it should look something like this:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUM9fhqh3QDn62oHGCsbDIrP4BKtJmCqT2K5wg6VKR_gt-Thz82QAT5dUAIK-PeZbhTdgYyAuLtW2ekuE45v2bAXhexEeyhXgo4njQUpa8Z7LDuf17_MqLX6caxUK2h1CWI0qVcS8TeNCx/s1804/initial+working+environment+screenshot.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="677" data-original-width="1804" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUM9fhqh3QDn62oHGCsbDIrP4BKtJmCqT2K5wg6VKR_gt-Thz82QAT5dUAIK-PeZbhTdgYyAuLtW2ekuE45v2bAXhexEeyhXgo4njQUpa8Z7LDuf17_MqLX6caxUK2h1CWI0qVcS8TeNCx/s1300/initial+working+environment+screenshot.PNG" width="70%" /></a></div><div><br /></div><div><br /></div><div><h4 style="text-align: left;">Quick exploration of the environment</h4><div>Two things that you might want to do right away are manage versions of your app, and look at logs. <br /><br />To manage versions, simply go to 'Application versions' on the left and you can deploy, delete, etc. any of them:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj78ixnaqSWww_POF9S8QvGoskG0Yy626QHsfeL-_yDt3Y8_NQBrWA3ly-VaAljns7bIbK6sfuQqKuDQj67ybGyBmFr9Lp1sgeatMia8M2qWsNzFX1gakKSp4jAMPqi-OtRykv31Ow-b8BM/s1246/application+versions+image.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="261" data-original-width="1246" height="67" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj78ixnaqSWww_POF9S8QvGoskG0Yy626QHsfeL-_yDt3Y8_NQBrWA3ly-VaAljns7bIbK6sfuQqKuDQj67ybGyBmFr9Lp1sgeatMia8M2qWsNzFX1gakKSp4jAMPqi-OtRykv31Ow-b8BM/s1300/application+versions+image.PNG" width="70%" /></a></div><div><br /><br />To look at logs, simply go to 'Logs' on the left and you can pull them.</div><div><br /></div><h4 style="text-align: left;">Quick overview of the hello world app</h4><div><i><br /></i></div><div><i>package file:</i></div><div><br /></div><div>The package file for this is very simple. It just defines express v3.1.0 as a dependency and tells the environment that 'node app.js' is the starting script to run.</div><div><br /></div><div><i>app:</i></div><div><i><br /></i></div><div>Things worth noting here:</div><div><br /></div><div><ul style="text-align: left;"><li>AWS manages the ports for you; 'const port = process.env.PORT || 3000;' says 'use port 3000 unless the host has configured a port for you'</li></ul><ul style="text-align: left;"><li>'const dir = `${__dirname}/public/`;' grabs the public folder for the app and puts it in dir</li></ul><ul style="text-align: left;"><li>'app.get("/", (req, res) => { res.sendfile(dir + "index.html"); });' serves up the index.html file in the public folder when the default url is visited</li></ul></div><div>That's it.</div><div><br /></div><h4 style="text-align: left;">What the CRUD app adds to the hello world example</h4><div><i><br /></i></div><div><i>package file:</i></div><div><i><br /></i></div><div>This app needs to use mysql and there's a convenient async package for smoothing out async usage in node that I used here.</div><div><br /></div><div><i>app:</i></div><div><i><br /></i></div><div>There are a few new things here:<br /><ul style="text-align: left;"><li>database configuration</li><ul><li>AWS manages the db so generate a connection using the environment variables for it</li><li>in order</li><ul><li>create the db if it doesn't exist</li><li>set it as the db to use</li><li>create the 'numbers' table if it doesn't exist</li></ul><li>log error or success message depending on how that sequence of calls went</li></ul></ul><ul style="text-align: left;"><li>route configuration</li><ul><li>the app has two endpoints</li><ul><li>history: return all numbers entered so far</li><li>new: add a new number</li></ul></ul></ul></div></div><div><i>route:</i></div><div><i><br /></i></div><div>The new endpoints are just SQL queries and those are implemented in 'numbers.js' in the routes folder.</div><div><br /></div><div><i>index.html</i></div><div><i><br /></i></div><div>This is a basic numeric input and button. Page load gets the history of numbers entered. Clicking the button adds the number in the input then gets the history.</div><div><br /></div><div>Basic summary then is:<br /><ul style="text-align: left;"><li>navigating to the page returns index.html</li></ul><ul style="text-align: left;"><li>index.html auto-calls the history and displays a formatted response</li><ul><li>calling history actually calls the 'getNumbers' method in numbers.js</li></ul></ul><ul style="text-align: left;"><li>submitting a new number calls new and then history</li><ul><li>calling new actually calls the 'addNumber' method in numbers.js</li></ul></ul><h4 style="text-align: left;">Summary</h4></div><div>That's all that's required to host a simple Node.js + MySQL app on AWS Elastic Beanstalk. Hopefully nothing has changed significantly by the time you read this that breaks this tutorial as that happening to a couple of others I read through is what prompted me to write this. Feel free to comment if you hit issues.</div><div><br /></div><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com2tag:blogger.com,1999:blog-1532419805701836386.post-16993490142652990452021-10-24T21:43:00.003-07:002021-10-24T21:43:23.572-07:00How Do You Subtract Binary Numbers?What is the value of something like 101001 - 1101?<img height="auto" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/Oudjat.SVG/240px-Oudjat.SVG.png" width="0%" /><a name='more'></a><p>It's probably easiest to understand this by first going through subtraction of normal (base 10) numbers. What is 119 - 35? You do the following:<br /></p><ul style="text-align: left;"><li>start with 1's digit; 9 - 5 = 4</li><li>move to 10's digit; 1 - 3 = -2; negative numbers make this hard, so 'borrow' 10 from the 100's digit; now the 10's digit is 11, so 11 - 3 = 8</li><li>100's digit lost 1 in the top number in the borrowing, so it's now 0; bottom number's 100's digit is 0 also</li></ul><div>So you have 0 in the 100's place, 8 in the 10's, and 4 in the 1's, so 84 is the result.</div><div><br /></div><div>Subtracting binary numbers works identically, except instead of borrowing 10 and using powers of 10 (1's, 10's, 100's, etc.) as places, you use powers of 2 (1's, 2's, 4's, etc.).</div><div><br /></div><div>Starting with an easy one: 10 - 1 (in binary):</div><div><ul style="text-align: left;"><li>start with 1's digit; 0 - 1 = -1; negative numbers make this hard, so 'borrow' 2 from the 2's digit; now the 1's digit is 2 - 1 = 1</li><li>move to 2's digit; it lost 1 in the borrowing, so it's now 0; bottom number's 100's digit is 0 also</li></ul><div>So you have 0 in the 2's place, and 1 in the 1's place, so the answer is 1. Double-checking, 10 in binary is 2, and 1 is 1, so 10 - 1 in binary is the same as 2 - 1 in normal (base 10) which is obviously 1.</div></div><div><br /></div><div>That's it. It works the exact same way as base 10 subtraction except that instead of borrowing 10, you borrow 2.</div><div><br /></div><div>Now for the original problem: 101001 - 1101</div><div><ul style="text-align: left;"><li>1's digit is 1 - 1 = 0</li><li>2's is 0 - 0 = 0</li><li>4's is 0 - 1; borrow from the 8's to get 2 - 1 = 1</li><li>8's had the borrow on the top, so it's now 0 - 1; borrow from the 16's to get 2 - 1 = 1</li><li>16's had the borrow on the top, so now it's -1 - 0; borrow from the 32's to get 1 - 0 = 1</li><li>32's had the borrow on the top, so now it's 0 - 0 = 0</li></ul><div>So the result is just 11100. Converting that to base 10, that's 28. Checking by converting the original problem, that was 41 - 13, and the answer is also 28.</div></div><p></p>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com0tag:blogger.com,1999:blog-1532419805701836386.post-48949351483822666052021-10-14T22:23:00.009-07:002021-10-14T22:24:34.482-07:00How Do I Determine My Raise Given Inflation?If you get a 10% raise, and inflation is 6%, did you actually get a raise?<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNMUaxqTS_ebvDNeNIk-WVZSQjulkRXmMVP6urQ4I5bbbcY2tOQ5JBX70LFg6dypPydQGWysMH2tPIivuHs37TliPmWpo8S4jeHXMqxnb1-sEwjxYWzvrvUImmYKlGOXJmneUls-4V8Fe6/s896/raise+equation.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="157" data-original-width="896" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNMUaxqTS_ebvDNeNIk-WVZSQjulkRXmMVP6urQ4I5bbbcY2tOQ5JBX70LFg6dypPydQGWysMH2tPIivuHs37TliPmWpo8S4jeHXMqxnb1-sEwjxYWzvrvUImmYKlGOXJmneUls-4V8Fe6/s1600/raise+equation.PNG" width="0%" /></a></div><a name='more'></a><br /><p></p><p>To get it out of the way, your actual raise is given by:<br /><br /></p><script async="" id="MathJax-script" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script><style>.MathJax { font-size: 1.3em !important; }</style><div style="font-size: 24px;"> \[{\text{actual raise = }\frac{\text{new salary}}{\text{old salary * (1 + inflation rate)}}} - 1\]</div><br /><div>Where does this come from? It's maybe easiest to think of this in terms of units. Say you make $50,000 now, and you made $40,000 last year. You make 20% more right? Not exactly. What the $ there really represents is some purchasing power. Inflation is a drop in purchasing power, so what you really need to do is convert the $ before and after to the same unit. To determine the value of $ in the current year in terms of the $ in the previous year, you just divide it by 1 + inflation rate. That gives you the equation above.</div><div><br /></div><div>Plugging in the numbers in the initial question then, the actual raise is:<br /><br /><br /></div><div style="font-size: 24px;"> \[{\frac{\text{new salary}}{\text{old salary * (1 + inflation rate)}}} - 1\]</div><br /><div style="font-size: 24px;"> \[{\frac{\text{old salary * (1 + 0.10)}}{\text{old salary * (1 + 0.06)}}} - 1\]</div><br /><div>Which is just 0.038, so the actual raise is 3.8%.</div><div><br /></div><div>It is very important to understand your raise in terms of local inflation. If you get a 5% raise but your area gets 10% more expensive, you actually got a paycut (4.5% paycut given those numbers).</div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com0tag:blogger.com,1999:blog-1532419805701836386.post-4511167942799153352021-10-02T22:32:00.003-07:002021-10-04T22:01:50.933-07:00Can You Confirm Performance Improvements With Noisy Software Benchmarks?Say you run 20 tests before and after a code change meant to speed up the code, but there's a lot of noise in your benchmarks. Some simple statistical tests can help you determine if you actually have an improvement in that noise.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0M-zx_qojsoO6_2672VjkDe4nPxK4EcF4Y0LI2hDtxrsDdguZWefSWFm_EsYgMTYM9xp3FVVmChxcgUv50In63NsP5zx6E46QJPv1NQfbAM_BiPPu8rCIRE-56pZdtoRheTIU1xHF7Lyz/s1346/benchmark+sample+times.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="510" data-original-width="1346" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0M-zx_qojsoO6_2672VjkDe4nPxK4EcF4Y0LI2hDtxrsDdguZWefSWFm_EsYgMTYM9xp3FVVmChxcgUv50In63NsP5zx6E46QJPv1NQfbAM_BiPPu8rCIRE-56pZdtoRheTIU1xHF7Lyz/s1600/benchmark+sample+times.PNG" width="0%" /></a></div><a name='more'></a><h4 style="text-align: left;">Sample Data</h4><div style="text-align: left;">Imagine your 20 runs before and after look like this:</div><div style="text-align: left;"><br /></div><div><table style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr style="background-color: #e6a117;"><th>Before (ms)</th><th>After (ms)</th></tr><tr><td>241</td><td>272</td></tr><tr><td>224</td><td>211</td></tr><tr><td>202</td><td>226</td></tr><tr><td>243</td><td>234</td></tr><tr><td>246</td><td>205</td></tr><tr><td>229</td><td>279</td></tr><tr><td>209</td><td>208</td></tr><tr><td>231</td><td>212</td></tr><tr><td>258</td><td>218</td></tr><tr><td>287</td><td>198</td></tr><tr><td>270</td><td>215</td></tr><tr><td>262</td><td>244</td></tr><tr><td>227</td><td>215</td></tr><tr><td>200</td><td>175</td></tr><tr><td>291</td><td>220</td></tr><tr><td>290</td><td>218</td></tr><tr><td>184</td><td>218</td></tr><tr><td>319</td><td>247</td></tr><tr><td>250</td><td>245</td></tr><tr><td>229</td><td>199</td></tr></tbody></table><br />In case you prefer histograms:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0M-zx_qojsoO6_2672VjkDe4nPxK4EcF4Y0LI2hDtxrsDdguZWefSWFm_EsYgMTYM9xp3FVVmChxcgUv50In63NsP5zx6E46QJPv1NQfbAM_BiPPu8rCIRE-56pZdtoRheTIU1xHF7Lyz/s1346/benchmark+sample+times.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="510" data-original-width="1346" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0M-zx_qojsoO6_2672VjkDe4nPxK4EcF4Y0LI2hDtxrsDdguZWefSWFm_EsYgMTYM9xp3FVVmChxcgUv50In63NsP5zx6E46QJPv1NQfbAM_BiPPu8rCIRE-56pZdtoRheTIU1xHF7Lyz/s1600/benchmark+sample+times.PNG" width="90%" /></a></div><br /><div><br /><br /></div><div>The 'after' numbers look like they're maybe smaller. If you take the average you get 245 ms before and 223 ms after. Is that really better though or are you just seeing noise?</div><div><br /></div><h4 style="text-align: left;">T-Test</h4><div>Assuming your benchmarking noise is roughly normally distributed, you can use a <a href="https://en.wikipedia.org/wiki/Student%27s_t-test" rel="" target="_blank">T-Test</a>. If you have never seen a T-test, a really rough description is that it will take two groups of numbers, and tell you if the means of the two groups are significantly different (i.e., the difference between them probably isn't just noise). </div><div><br /></div><div>What does 'probably' mean here? You get a p value out of T-Tests that is the probability that they're the same. E.g., a p value of 0.05 would mean roughly 'there's a 5% chance that the ~20 ms difference here is just noise'. </div><div><br /></div><div>You can do this in excel, google sheets, any of the many websites that do it, etc. I tend to use Python for this sort of stuff so a simple overview of how to do it in Python is:</div><div><ul style="text-align: left;"><li>import stats from scipy</li><li>call the <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html" target="_blank">ttest_ind</a> method in it with the before numbers as the first arg and the after numbers as the second</li><li>the t value returned should be positive (since before should be higher than after) and the p value should be 2*target probability</li></ul><div>For the numbers in the example here, I get a p value of 0.03 which is less than the common target of 0.05, and recall earlier that I noted it's 2*target probability, so this is effectively a probability of 1.5% (p value of 0.015) which would generally mean 'significant difference'. Note that 'significant' here doesn't mean important...just unlikely to be noise. The difference in means is still the primary metric here. </div><div><br /></div><div>To summarize this then, you could say that the update significantly altered the benchmark time, and the difference in means is ~20 ms (or a ~10% performance improvement).</div><div><br /></div><div><i>Why divide by 2?</i></div><div><i><br /></i></div><div>This is an artefact of the method you use. In this case, the method I gave for testing this tests both sides of the assumption (i.e., tests both before > after and before < after). We only care about the before > after side though. This method actually handles this for you in current versions but I have an older version installed and wanted to put the more generic.</div><div><br /></div><div><i>Why ttest_ind?</i></div><div><i><br /></i></div><div>There are a lot of variants of T-Tests you can run. It's worth reading through them but I won't rewrite tons of info on them here. The ttest_ind I used is for independent samples of data. You might argue that a paired one is better here since 'making code faster' is sort of changing one aspect of a thing and testing it again, but ttest_ind works well in general usage.</div><div><br /></div><h4 style="text-align: left;">Mann-Whitney</h4><div>What if you have outliers and/or do not have a normal distribution of noise in your benchmarks? For a concrete example, what if the first number in the 'after' column is 600 instead of 272? T-Tests are not valid in these situations. Running it blindly returns a p of 0.4 which would indicate not significantly different, all from that single bad outlier.</div><div><br /></div><div>You can auto-exclude best and worst n times. You can manually scrub data. That sounds really manual though and we want to automate things. You can also use another type of test. One that's useful here is the <a href="https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test" target="_blank">Mann-Whitney U test</a>.</div><div><br /></div><div>The results are similar to a T-Test but the test itself is looking for something slightly different. Roughly, this test tells you how likely it is that the results are such that a random value chosen from after is just as likely to be greater than a random value chosen from before as vice-versa. Since it doesn't care about the magnitudes (only the orders), it is fine for outliers and non-normally distributed data.</div><div><br /></div><div>Same basic flow in Python:</div><div><ul><li>import stats from scipy</li><li>call the <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mannwhitneyu.html" target="_blank">mannwhitneyu</a> method in it with the before numbers as the first arg and the after numbers as the second; also pass in 'two-sided' as the alternative to be consistent with the T-Test above if you want</li><li>the p value should be 2*target probability</li></ul><div>With the numbers here, I get a p value of 0.04, so dividing by 2, 0.02. This test was not tripped up by the outlier.</div></div><div><br /></div></div><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-26210373466399322642021-09-13T22:18:00.014-07:002021-09-13T23:35:59.987-07:00Why Do We Multiply the Way We Do?We could just repeatedly add the numbers but we don't. Is the algorithm we use actually faster?<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICgwbt0JsY0TcORWDBvfg85PDcWkOHIkfoU8TDCbp_kgVcprhUQJ16TthINNMpE-X-fJA8gGd9_r5SqaAkN3owvsqkGmtNXESt7pWzGMkNRCM4c3JeSiWdCkSpTLaV0ESpt4n_dlemLe1/s1353/multiplication+algorithm+compare.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="772" data-original-width="1353" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICgwbt0JsY0TcORWDBvfg85PDcWkOHIkfoU8TDCbp_kgVcprhUQJ16TthINNMpE-X-fJA8gGd9_r5SqaAkN3owvsqkGmtNXESt7pWzGMkNRCM4c3JeSiWdCkSpTLaV0ESpt4n_dlemLe1/s1600/multiplication+algorithm+compare.PNG" width="0%" /></a></div><a name='more'></a><p>What I'm talking about here is multiplying 219 x 87 like the following:</p><p></p><ul style="text-align: left;"><li>7x9 to get 63</li><li>7x1 to get 7 and add a 0 to get 70</li><li>7x2 to get 14 and add two 0's to get 1400</li><li>8x9 to get 72 and add a 0 to get 720</li><li>8x1 to get 8 and add two 0's to get 800</li><li>8x2 to get 16 and add three 0's to get 16000</li><li>add all those together to get 19,053</li></ul><div>That's 6 simple multiplications and 6 additions. If we just added 219 to itself 87 times, that's 87 operations so clearly more steps with one big assumption: </div><div><br /></div><div style="text-align: center;"><i>you've memorized m x n for all integers m and n from 2 to 10. </i></div><div><br /></div><div>This is why we all had to learn times tables. How does this generalize as an algorithm?</div><div><br /></div><div>Repeated addition is ~ operation per 'smallest of the two numbers', so that is just O(n) where n is the smaller of the numbers.</div><div><br /></div><div>The algorithm we actually use is a bit harder. It scales as a x b where a and b are the number of digits in the two numbers. How does 'number of digits' scale? That's O(logn) where n is the number. Since it scales as the product of those, that algorithm scales as O(logm * logn) where m and n are the two numbers. </div><div><br /></div><div>What about the memorized simple multiplications? I have no idea how our memory access scales, but I'm going to just guess it's a constant time operation for simple multiplication so O(1) which doesn't contribute.</div><div><br /></div><div>For an example with actual calculations, here is the cost of multiplying each number up to 99 by 99 using each algorithm:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICgwbt0JsY0TcORWDBvfg85PDcWkOHIkfoU8TDCbp_kgVcprhUQJ16TthINNMpE-X-fJA8gGd9_r5SqaAkN3owvsqkGmtNXESt7pWzGMkNRCM4c3JeSiWdCkSpTLaV0ESpt4n_dlemLe1/s1353/multiplication+algorithm+compare.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="772" data-original-width="1353" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICgwbt0JsY0TcORWDBvfg85PDcWkOHIkfoU8TDCbp_kgVcprhUQJ16TthINNMpE-X-fJA8gGd9_r5SqaAkN3owvsqkGmtNXESt7pWzGMkNRCM4c3JeSiWdCkSpTLaV0ESpt4n_dlemLe1/s1600/multiplication+algorithm+compare.PNG" width="90%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><div><br /></div>It might not be obvious that O(logm * logn) is faster than O(n) but with actual numbers in the plot there it becomes pretty clear.</div><div><br /></div><div>It's cool to me that a basic math thing we all learn when we're little kids effectively uses a <a href="https://en.wikipedia.org/wiki/Dynamic_programming" target="_blank">dynamic programming</a> algorithm (memorize all m x n for m and n up to 10; convert every multiplication problem into a combination of m x n problems that you already solved).<br /><br /></div><p></p>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com0tag:blogger.com,1999:blog-1532419805701836386.post-68750193908089284782021-08-31T21:43:00.000-07:002021-08-31T21:43:01.891-07:00Exploring Senior Software Engineer Salary Data in levels.fyi<a href="https://www.levels.fyi/">levels.fyi</a> is a great resource for software salary info and it's easily mineable. I was curious how salaries in what are sometimes considered medium cost-of-living cities compare.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRtbVoRXxA92uKkDMuAP46ShyphenhyphenLntgAw-Yc1lznFBYPm2oUAV53dubIS3P-dthBipWl3cr3oBhiX2Y4rEzorDgWPyLzdwUF23Q41czKg0_EWJcwiwzfa2Y7WR7KM79rOtwLxPV9Vtt7hD6y/s1074/senior+sw+by+city.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="673" data-original-width="1074" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRtbVoRXxA92uKkDMuAP46ShyphenhyphenLntgAw-Yc1lznFBYPm2oUAV53dubIS3P-dthBipWl3cr3oBhiX2Y4rEzorDgWPyLzdwUF23Q41czKg0_EWJcwiwzfa2Y7WR7KM79rOtwLxPV9Vtt7hD6y/s1600/senior+sw+by+city.png" width="0%" /></a></div><a name='more'></a><p>Software careers often have levels (hence the site name). Typically there's entry with 0-2.5 years, next at 2-5 years, then career level at 5-10 years. Some go above that (principal, chief, etc.). The one I'll play with here is the 5-10 year one. 5-10 year is often called 'senior software engineer'.</p><p>Here are the rough pay distributions in levels for that experience range in mid-priced cities (this is total compensation and not base salary):</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRtbVoRXxA92uKkDMuAP46ShyphenhyphenLntgAw-Yc1lznFBYPm2oUAV53dubIS3P-dthBipWl3cr3oBhiX2Y4rEzorDgWPyLzdwUF23Q41czKg0_EWJcwiwzfa2Y7WR7KM79rOtwLxPV9Vtt7hD6y/s1074/senior+sw+by+city.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="673" data-original-width="1074" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRtbVoRXxA92uKkDMuAP46ShyphenhyphenLntgAw-Yc1lznFBYPm2oUAV53dubIS3P-dthBipWl3cr3oBhiX2Y4rEzorDgWPyLzdwUF23Q41czKg0_EWJcwiwzfa2Y7WR7KM79rOtwLxPV9Vtt7hD6y/s1600/senior+sw+by+city.png" width="95%" /></a></div><p>A comparison is hard because it's not clear that each city represents the same thing. For example, many are state capitals so if 90% of the jobs are state ones then you'd expect them to be lower. Here's a list of the top-3 included employers for each city in that plot to hopefully provide more context:</p><p></p><ul style="text-align: left;"><li>Pittsburgh: Google, Uber, Argo AI<br /><br /></li><li>Chicago: Paypal, Expedia, Accenture<br /><br /></li><li>Denver: Amazon, Deloitte, Gusto<br /><br /></li><li>Austin: Apple, IBM, Amazon<br /><br /></li><li>Detroit: Amazon, General Motors, Quicken Loans<br /><br /></li><li>Atlanta: VMWare, Salesforce, Microsoft<br /><br /></li><li>Raleigh: IBM, Cisco, Microsoft<br /><br /></li><li>Nashville: Amazon, Asurion, HCA Healthcare<br /><br /></li><li>Phoenix: American Express, Intel, Amazon</li></ul><p></p><p>Amazon is everywhere apparently...</p><p>These numbers aren't perfect obviously. Many people do work for the state for example and they don't seem to be providing salaries here, so I'd wager that levels.fyi is biased towards higher-paying companies. Fun data though.</p><p><br /></p>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com0tag:blogger.com,1999:blog-1532419805701836386.post-72554932798682197152021-08-01T21:26:00.002-07:002021-08-01T21:26:53.364-07:00How to Add a Vertical Scrollbar to PlotlyPlotly doesn't have the built-in ability to scroll vertically with a fixed x axis unfortunately, but you can mimic that fairly easily...<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1QDSJhjQtPVJNvSqanJszudB8YQF_-KvEB9Jhy3zbooBQOU7VG8MVizriSptfh3dBfsKccZCjDiTcsESy9Kn9dB_RQR1bJ3ppTq4qIVcqBHiwPITB2uE6lotJTVpf7jcE9OkhHBs2oyip/s817/scrolling+plotly.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="360" data-original-width="817" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1QDSJhjQtPVJNvSqanJszudB8YQF_-KvEB9Jhy3zbooBQOU7VG8MVizriSptfh3dBfsKccZCjDiTcsESy9Kn9dB_RQR1bJ3ppTq4qIVcqBHiwPITB2uE6lotJTVpf7jcE9OkhHBs2oyip/s1600/scrolling+plotly.PNG" width="0" /></a></div><a name='more'></a><p>First, here's the demo:</p><p class="codepen" data-height="450" data-slug-hash="dyWeQJp" data-user="rhamner" style="align-items: center; border: 2px solid; box-sizing: border-box; display: flex; height: 450px; justify-content: center; margin: 1em 0px; padding: 1em;">
<span>See the Pen <a href="https://codepen.io/rhamner/pen/dyWeQJp">
vertical scroll plotly</a> by Robert Hamner (<a href="https://codepen.io/rhamner">@rhamner</a>)
on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async="" src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script><p><br /></p><p>The basic model here is two stack two plots directly on top of each other where top is in a scrollable div and bottom is not.</p><p></p><ul style="text-align: left;"><li>Make two divs</li><ul><li>plot div</li><ul><li>scrollable</li><li>width = plot width + scroll width</li></ul><li>xaxis div</li><ul><li>not scrollable</li><li>width = plot width</li></ul></ul><li>Make two plots</li><ul><li>plot</li><ul><li>goes in plot div</li><li>y-axis zeroline is hidden</li><li>bottom margin is 0</li></ul><li>xaxis</li><ul><li>0 top margin</li><li>hide the modebar</li></ul></ul><li>Make the plot xaxis ranges equal</li></ul><div>You can then get as complicated as you need to here. I added really crude layout event linking to the demo...I'm hitting a weird double-click bug (should autoscale but isn't) but this works pretty easily/cleanly as the basic concept.</div><div><br /></div><br /><div><br /></div><p></p>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-65129511760470397342021-06-30T21:52:00.004-07:002021-06-30T21:54:43.698-07:00If 10 Vaccinated and 10 Unvaccinated People Die, Can We Still Say Vaccines Work?You will almost certainly be seeing headlines about vaccinated people dying and might even see that more vaccinated than unvaccinated die. <a href="https://www.wsj.com/articles/covid-19-killed-26-indonesian-doctors-in-juneat-least-10-had-taken-chinas-sinovac-vaccine-11624769885">Here's one from the week that I wrote this post</a>. Why do we still say vaccines work if this is happening?<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxUprtIH3fJG_0ieXRlLQncSfia_TMqK_AzgLMMOvaWx3NbtdH4UsPzciLRDsxrncfgaHpoOehnITS3nQKegIqX6QhaI7VtNe-Qj_tZRMsAWBY0ity0A8lWWvnYzWMwjEiJK0AVuHm2Qz_/s1258/distribution.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="663" data-original-width="1258" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxUprtIH3fJG_0ieXRlLQncSfia_TMqK_AzgLMMOvaWx3NbtdH4UsPzciLRDsxrncfgaHpoOehnITS3nQKegIqX6QhaI7VtNe-Qj_tZRMsAWBY0ity0A8lWWvnYzWMwjEiJK0AVuHm2Qz_/s1600/distribution.png" width="0%" /></a></div><a name='more'></a>Imagine as an example that you see '10 vaccinated and 10 unvaccinated doctors died from COVID-19 today'. Your brain probably thinks 'well...the vaccine didn't work I guess.' We see those numbers, then just assume that the populations were similar. They're all doctors right?<p>Digging more, say that it turns out that 90% of the doctors were vaccinated. To make it easy, assume that there are 1,000 total doctors. 90% vaccinated means there were 900 vaccinated and 100 unvaccinated. If 10 died from each group, that means:<br /></p><ul style="text-align: left;"><li>10 / 100, or 10% of unvaccinated doctors died</li><li>10 / 900, or 1.1% of vaccinated doctors died</li></ul><div>Unvaccinated doctors were 9 times as likely to die as vaccinated ones. Another way of phrasing that is that the vaccine's efficacy was:<br /><br /><div style="text-align: center;"><b>vaccine efficacy</b> = 1 - (vaccinated risk/unvaccinated risk) = 1 - (0.011/0.1) = <b>89%</b></div></div><div style="text-align: center;"><b><br /></b></div><div style="text-align: left;">This is how you have to think about things like this. Vaccines, masks, seat belts, helmets, etc. aren't 100% effective. Use the calculation above whenever you see headlines like this and want to know the actual story. </div><div style="text-align: left;"><br /></div><div style="text-align: left;">You can even have more vaccinated deaths than unvaccinated. Imagine for the 89% efficacy vaccine above, you have 99% of the population vaccinated. For 10,000 doctors in that example, you'd expect to have 10% of the 100 unvaccinated die and 1.1% of the vaccinated 9900 die, so that's 10 unvaccinated deaths and 120 vaccinated deaths. <b>A highly effective vaccine can still have more vaccinated people die than unvaccinated ones.</b></div><div style="text-align: left;"><br /></div><div style="text-align: left;">In case a visual helps, here is the initial example's distribution as a colored grid (red = dead and green = alive):</div><div style="text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxUprtIH3fJG_0ieXRlLQncSfia_TMqK_AzgLMMOvaWx3NbtdH4UsPzciLRDsxrncfgaHpoOehnITS3nQKegIqX6QhaI7VtNe-Qj_tZRMsAWBY0ity0A8lWWvnYzWMwjEiJK0AVuHm2Qz_/s1258/distribution.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="663" data-original-width="1258" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxUprtIH3fJG_0ieXRlLQncSfia_TMqK_AzgLMMOvaWx3NbtdH4UsPzciLRDsxrncfgaHpoOehnITS3nQKegIqX6QhaI7VtNe-Qj_tZRMsAWBY0ity0A8lWWvnYzWMwjEiJK0AVuHm2Qz_/s1600/distribution.png" width="80%" /></a></div><br /><div style="text-align: left;"><br /></div><p></p>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-71486582668205143522021-06-13T21:56:00.005-07:002021-06-14T07:51:13.203-07:00Wheel Options Strategy SimulationsThe 'Wheel' is an options strategy that combines cash-secured puts with covered calls. I sometimes have trouble really grasping options strategies in my head, so simulating some scenarios gives me a better feel.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwuXBAhe4oaYTHgpxL0iYSIhopNsM9j2dVnahDYwKF6_S4j8lpcw8QiuztCPepl3RFE6JssvVFJR5t51WaLBvjvep0MyKyY_dwj0KogD-hBB6KSn-T1cSOvrglulYVOt3riQY28cZkB9Qc/s917/simulations.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="643" data-original-width="917" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwuXBAhe4oaYTHgpxL0iYSIhopNsM9j2dVnahDYwKF6_S4j8lpcw8QiuztCPepl3RFE6JssvVFJR5t51WaLBvjvep0MyKyY_dwj0KogD-hBB6KSn-T1cSOvrglulYVOt3riQY28cZkB9Qc/s1600/simulations.png" width="0%" /></a></div><a name='more'></a><h4 style="text-align: left;">Basic strategy</h4>To keep it simple, I will just deal with 'at the money' (ATM) options here. The basic strategy then is:<br /><ol style="text-align: left;"><li>Sell a cash-secured put to start<br /><br /></li><li>If the stock goes up, the put expires so you sell another put<br /><br /></li><li>If the stock goes down, the put is exercised, you're assigned the shares, so you can sell a covered call<br /><br /></li><li>If the stock goes down from there, the call expires so you sell another call<br /><br /></li><li>If the stock goes up from there, the call is exercised, you sell the shares, so you sell another put<br /><br /></li><li>repeat...</li></ol><div>You can see that when you are assigned shares, you just sell a call, and when a call is exercised, you just use the cash to sell a put. This repeats indefinitely. Isn't this just free money? Sort of...what you're trading off here is a bit hard to see immediately. This is where playing with some numbers can make it easier to understand what's happening.</div><div><br /></div><h4 style="text-align: left;">Simple examples</h4><div>To get a better idea of how this works, let's look at 3 simple examples:<br /><ol style="text-align: left;"><li>stock doesn't change much<br /><br /></li><li>stock drops ~15% in a year<br /><br /></li><li>stock gains ~15% in a year<br /><br /></li><li>stock crashes ~15% and rebounds in a year</li></ol><div>In each scenario, I'll add a bit of noise and assume that selling a put yields $1.25/month, selling a call yields $1/month, and these are all monthly expirations and ATM strikes with a starting value of $100.</div></div><div><br /></div><div>Imagine in the first one the price for the first 5 months is 100, 102, 101, 97, 101. What does the wheel strategy look like?<br /><ul style="text-align: left;"><li>sell put for $1.25 with a $100 strike; gain $1.25 from the sell and lose nothing in stock</li></ul><ul style="text-align: left;"><li>price hits $102; that's above the $100 strike so it expires; sell another $1.25 put with a $102 strike and lose nothing in stock</li></ul><ul style="text-align: left;"><li>price hits $101; that's below the $102 strike so pay $102 for the shares and sell a $1 call with a $101 strike</li></ul><ul style="text-align: left;"><li>price hits $97; that's below the $101 strike so it expires; sell another $1 call with a $97 strike</li></ul><ul style="text-align: left;"><li>price hits $101; that's above the $97 strike so sell at $97 and sell another $1.25 put with a $101 strike</li></ul><div>Overall, the wheel earned $5.75 from selling options, but lost $4 in the stock (bought at $101 and sold at $97). That stock loss is the most obvious loss here but there's another more subtle one. Look at the first put again. The gain from selling the option was $1.25, but the stock itself gained $2 then. The gain was effectively capped at $1.25. The same is not true for the loss. When the stock fell, the entire loss was absorbed. Capping gains while having to absorb losses is a primary tradeoff here (some other ones are poor tax performance, low-liquidity, and potentially missing dividends).</div></div><div><br /></div><div>Now that that's understood, it's helpful to me to see this graphically, so here are sample runs of the examples from above:</div><div><br /><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhySWWa7xkS_H37nhGE9zTgdfepmMHF8IIG7UAilM1N2cHNmwQ7oqR1UtrINvqGFWrCvRxUmBkyX3Z8ftol1vCfcdV2tKQ1wQYgIEpGqABc8syn7hvuHHqGcN0PJrKIziuu-6SVgkDuKTLR/s898/flat+stock.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="458" data-original-width="898" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhySWWa7xkS_H37nhGE9zTgdfepmMHF8IIG7UAilM1N2cHNmwQ7oqR1UtrINvqGFWrCvRxUmBkyX3Z8ftol1vCfcdV2tKQ1wQYgIEpGqABc8syn7hvuHHqGcN0PJrKIziuu-6SVgkDuKTLR/s1600/flat+stock.PNG" width="75%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNDWq9PW9_wp0YH26Cf1bQrEhvdIc0ik3uiGIgrm0YOcmaGCyiJMxTEe_O2dBK-TYzmDED8oP5CpgGObO6mDkFWz3FTe5fEdYWR7LoJJgjqBnfdzsvQWNLson9HIf-K5krTcIMXdjM8mo8/s899/declining+stock.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="460" data-original-width="899" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNDWq9PW9_wp0YH26Cf1bQrEhvdIc0ik3uiGIgrm0YOcmaGCyiJMxTEe_O2dBK-TYzmDED8oP5CpgGObO6mDkFWz3FTe5fEdYWR7LoJJgjqBnfdzsvQWNLson9HIf-K5krTcIMXdjM8mo8/s1600/declining+stock.PNG" width="75%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivPaHQ9szKwlOzKmMMQdY3wpLHMZU9JHMlj5p7iNfCDZqR-bdb0JBmHV1Mr2Wxd1q0wDCDRuJNfMNs2kn9Oqruns6UMO3X3vcvM4SMFj9oi47KrzILDvl9feJG4n7QU6ptf3x3aFPKNsdi/s898/growing+stock.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="461" data-original-width="898" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivPaHQ9szKwlOzKmMMQdY3wpLHMZU9JHMlj5p7iNfCDZqR-bdb0JBmHV1Mr2Wxd1q0wDCDRuJNfMNs2kn9Oqruns6UMO3X3vcvM4SMFj9oi47KrzILDvl9feJG4n7QU6ptf3x3aFPKNsdi/s1600/growing+stock.PNG" width="75%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGcC-e553ZMzZ-GIZ7YrfTJJgQWsWmNUc6gET-9-fnr_3uOBEUKe_XdU17BB6USERvZBZQBXIVdNHAP5OTACaLZT3hLPvmKZvdCsyjM9hqiYX0L1krR6AUQSiZ6mqdjaDOJxTIlXdXHNeT/s897/crash+and+rebound.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="454" data-original-width="897" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGcC-e553ZMzZ-GIZ7YrfTJJgQWsWmNUc6gET-9-fnr_3uOBEUKe_XdU17BB6USERvZBZQBXIVdNHAP5OTACaLZT3hLPvmKZvdCsyjM9hqiYX0L1krR6AUQSiZ6mqdjaDOJxTIlXdXHNeT/s1600/crash+and+rebound.PNG" width="75%" /></a></div><br /><div><br /></div><div>The general behavior here is that the wheel smooths out the plots a bit. Increases and decreases aren't quite as big. You can control how smooth it is by changing expiration dates and strike price offsets (e.g., selling calls with a strike price 10% above current price will allow for larger gains but give you less option premium, so the performance looks more like buy and hold). When a stock crashes, you'll probably do a bit better with the wheel. When a stock surges, you'll probably do a bit worse with the wheel.</div><div><br /></div><div>The above plots are single trials of the simulation. What does it look like if this is run thousands of times?</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwuXBAhe4oaYTHgpxL0iYSIhopNsM9j2dVnahDYwKF6_S4j8lpcw8QiuztCPepl3RFE6JssvVFJR5t51WaLBvjvep0MyKyY_dwj0KogD-hBB6KSn-T1cSOvrglulYVOt3riQY28cZkB9Qc/s917/simulations.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="643" data-original-width="917" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwuXBAhe4oaYTHgpxL0iYSIhopNsM9j2dVnahDYwKF6_S4j8lpcw8QiuztCPepl3RFE6JssvVFJR5t51WaLBvjvep0MyKyY_dwj0KogD-hBB6KSn-T1cSOvrglulYVOt3riQY28cZkB9Qc/s1600/simulations.png" width="75%" /></a></div><br /><div>That's much clearer to me. In down periods, the wheel just minimizes your losses a bit (loss is stock loss, but you gain option premium). In good periods, the wheel caps your gains so you get a flattened distribution (max gain is the option premium).</div><div><br /></div><h4 style="text-align: left;">Summary</h4><div>Should you use this strategy? There's no perfect answer for that. In an extremely long bull market (like current), it's likely going to underperform. It does give you a bit of protection against drops and can do better in neutral markets. I personally don't like the thought of capping gains while not capping losses so this isn't a favorite of mine (see the fourth image with the crash and rebound to understand why that can be bad), but it's definitely viable if you want to smooth out your returns a bit.</div><div><br /></div><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com6tag:blogger.com,1999:blog-1532419805701836386.post-10687219980270690432021-05-21T20:15:00.004-07:002021-05-21T20:18:12.028-07:00Negative values with a log axis in PlotlyAlthough log10(<any number less than or equal to 0>) is not defined, there are situations where you want to visualize data as if it were. How can you get plotly to do that? Another way of asking is 'how can you mimic symlog functionality in plotly?'<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvAcsLf58_-QqR-MVuZ4vAZlV7YM3-QiWShA08XJVjz_1yA_HK8rbjxJoJKNiCaOuTca_Y-VRdv4ilqEccUMzMbOSMmBwj2-jd78DAg-qnL8r7yIp-LGw9JrPojyPikimqYksBFtjbANay/s1862/symlog.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="348" data-original-width="1862" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvAcsLf58_-QqR-MVuZ4vAZlV7YM3-QiWShA08XJVjz_1yA_HK8rbjxJoJKNiCaOuTca_Y-VRdv4ilqEccUMzMbOSMmBwj2-jd78DAg-qnL8r7yIp-LGw9JrPojyPikimqYksBFtjbANay/s1600/symlog.PNG" width="0%" /></a></div><a name='more'></a>First...a real example of when you'd want this. Imagine you do the following:<div><ul style="text-align: left;"><li>generate a 1 GHz tone</li><li>measure amplitude at +/- 10 kHz, +/- 100 kHz, +/- 1 MHz, ...</li><li>generate a 2 GHz tone</li><li>measure amplitude at +/- 10 kHz, +/- 100 kHz, +/- 1 MHz, ...</li><li>want to overlay those offset amplitude curves</li></ul><div>You could just plot vs absolute frequency to see one, but to overlay you need to center around a tone, and it just makes sense to show 'offset from tone' as the x axis. However, those steps imply a log scale.</div></div><div><br /></div><div>Below is a working example of exactly this situation in plotly.js. I've included the ideal here with both positive and negative on a log scale, and the normal linear plot so that the difference in parsing it quickly is obvious:</div><div><br /></div><p class="codepen" data-default-tab="js,result" data-height="500" data-pen-title="symlog approximation" data-slug-hash="KKWamLW" data-theme-id="light" data-user="rhamner" style="align-items: center; border: 2px solid; box-sizing: border-box; display: flex; height: 500px; justify-content: center; margin: 1em 0px; padding: 1em;">
<span>See the Pen <a href="https://codepen.io/rhamner/pen/KKWamLW">
symlog approximation</a> by Robert Hamner (<a href="https://codepen.io/rhamner">@rhamner</a>)
on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async="" src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script><div><br /></div><div><br /></div><div>The basic algorithm is pretty simple:</div><div><ul style="text-align: left;"><li>Determine the max and min values and the value closest to zero; largest of max and abs(min) is upper bound...value closest to zero is lower<br /><br /></li><li>Split all traces into positive and negative (x values here since I just did this for x in the demo)<br /><br /></li><li>Create two x-axes: one for positive and one for negative</li><ul><li>give both the same bounds</li><li>reverse the negative x-axis</li><li>assign ticks with positive values but negative labels to the negative x-axis</li><li>put a small buffer between them to represent that zero is undefined<br /><br /></li></ul><li>Plot positive traces vs positive x-axis and negative traces vs negative x-axis, but make the negative x values positive<br /></li></ul><div>In that demo above you can just step through the javascript code and it should all be pretty clear.</div></div><div><br /></div><div>If you want a slight variant of this that matches <a href="https://matplotlib.org/stable/gallery/scales/symlog_demo.html">'symlog' in matplotlib</a>, just add a third, linear axis to connect these two instead of leaving a gap. I personally prefer the gap for this situation.</div><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com0tag:blogger.com,1999:blog-1532419805701836386.post-51085009306756570162021-05-02T21:45:00.008-07:002021-05-02T22:26:45.703-07:00Simple way to see code coverage in pythonSometimes you want to quickly see unit test coverage of your code. <a href="https://coverage.readthedocs.io/en/coverage-5.5/">Coverage.py</a> makes that really simple.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjMOZtBsBTMKvLE-9Y8tahKDynwygkPGDNQjz-q2qZcWRqW5HRCvDIGahkbT1DV-Cx5aKV6el7_QZzGOcZjU0uh2fD0dpLdlPuK7_KFgZvAIGNuLjazDnvphg944vOs5caYeRP9Gc3S1PO/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="264" data-original-width="599" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjMOZtBsBTMKvLE-9Y8tahKDynwygkPGDNQjz-q2qZcWRqW5HRCvDIGahkbT1DV-Cx5aKV6el7_QZzGOcZjU0uh2fD0dpLdlPuK7_KFgZvAIGNuLjazDnvphg944vOs5caYeRP9Gc3S1PO/" width="0%" /></a></div><a name='more'></a>First, what do I mean by test coverage? Below is an example for a really simple usage:<div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjMOZtBsBTMKvLE-9Y8tahKDynwygkPGDNQjz-q2qZcWRqW5HRCvDIGahkbT1DV-Cx5aKV6el7_QZzGOcZjU0uh2fD0dpLdlPuK7_KFgZvAIGNuLjazDnvphg944vOs5caYeRP9Gc3S1PO/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="264" data-original-width="599" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjMOZtBsBTMKvLE-9Y8tahKDynwygkPGDNQjz-q2qZcWRqW5HRCvDIGahkbT1DV-Cx5aKV6el7_QZzGOcZjU0uh2fD0dpLdlPuK7_KFgZvAIGNuLjazDnvphg944vOs5caYeRP9Gc3S1PO/" /></a></div><br /><br /></div><div>That tells me how much of the code is executed when I executed my tests (start with test_* here). For that example, I just have two files with two methods in each. The files are identical and look like this:</div><div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOM7LE5qYeUGVQpWH5j-InhPfjnBPiGbsqNec4fH34AxOxt4d8TatK_V2uIH3r3qtRP88xNC3-cs3-Kc90IE2lITrZsD1cfbQOFHkD-6M9cKoXfzwH7Prz635n5hczAJ6ElrAaMf_BFrY8/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="368" data-original-width="354" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOM7LE5qYeUGVQpWH5j-InhPfjnBPiGbsqNec4fH34AxOxt4d8TatK_V2uIH3r3qtRP88xNC3-cs3-Kc90IE2lITrZsD1cfbQOFHkD-6M9cKoXfzwH7Prz635n5hczAJ6ElrAaMf_BFrY8/" width="231" /></a></div><br /></div><div><br /></div><div>I have one file with unit tests. I'm using <a href="https://docs.pytest.org/en/6.2.x/">pytest</a> for this but coverage works with other test frameworks. Here is the unit test file:</div><div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAID_R7svaD3NJyCtRJpB8b3ATRmkBsg9UijfDRUgy6xp-X3ujS7tpj6rnhBTEIfNOFHx2Ob9BWepk05gb85ml-xH1xWJkO-wCSKi0gP3Ps2MExzDDcYwPrrlG-WLm2yYVszumpHIMeGlf/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="256" data-original-width="536" height="153" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAID_R7svaD3NJyCtRJpB8b3ATRmkBsg9UijfDRUgy6xp-X3ujS7tpj6rnhBTEIfNOFHx2Ob9BWepk05gb85ml-xH1xWJkO-wCSKi0gP3Ps2MExzDDcYwPrrlG-WLm2yYVszumpHIMeGlf/" width="320" /></a></div><br /></div><div><br /></div><div>With a file this simple, we can see some obvious test gaps like:<br /><ul style="text-align: left;"><li>it doesn't test all functions in all files</li><li>it doesn't test all paths in the functions (e.g., no 0 test in file1)</li></ul><div>You can imagine how hard it is to see that for any realistic code though. This is where coverage checks can help. Clicking into the coverage for file2 here we get:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgltdaovmf9p3ekWX37pNUMLZK0q39dUr1aY4nZsvlBUmKoxOzDWH4O0OXZpnLUew9H5rXf5YEskEJ9X3qapi1exSVbHVzhjHWtrSRhCpAeLXuCXDxzNBf-BvlncAh1WiPR496oay-lCHXb/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="404" data-original-width="505" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgltdaovmf9p3ekWX37pNUMLZK0q39dUr1aY4nZsvlBUmKoxOzDWH4O0OXZpnLUew9H5rXf5YEskEJ9X3qapi1exSVbHVzhjHWtrSRhCpAeLXuCXDxzNBf-BvlncAh1WiPR496oay-lCHXb/" width="300" /></a></div></div><div><br /></div><div>Really simple to see that we missed the non-zero case in is_zero and that we didn't call the is_zero_wrapper function at all in any of the tested paths.</div><div><br /></div><div>It is important to note that 100% coverage doesn't mean your code is perfectly tested and that less than 100% coverage doesn't mean your codebase is garbage. This is just one of many useful metrics for gauging test coverage and testing gaps.</div><div><br /></div><div>To set this up and run it:<br /><ol style="text-align: left;"><li>install coverage (e.g., 'pip install coverage')</li><li>install pytest (e.g., 'pip install pytest')</li><li>run 'coverage run -m pytest'</li><li>run 'coverage html'</li><li>open the index.html file in the htmlcov folder that it created</li></ol><div>That index.html file is what I have in the screenshot at the start.</div></div><div><br /></div><div>If you want to use my exact code to test this, it's available <a href="https://github.com/rhamner/coverage_example">here</a>.</div><div><br /></div></div></div><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-14912543741358950362021-04-06T21:11:00.009-07:002021-04-06T22:51:23.444-07:00Thinking in terms of probabilitiesWe suck at probability. A common trap we fall into is failing to realize this and thinking in terms of absolutes.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgQcmriUGV7UgFg8dt93oJscEUcJbCyupTJYXCGL4t-k1W7EiAXNnqhIIoGeXGe8YRKySObM5VHSD5Wq5EYCF3vKhK75l1C0n7s6hxTzeqZIwPKD8i2bUUxJwVwzOCW6MCDJpdwaobNqXa/s1058/wealth+quintiles.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="449" data-original-width="1058" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgQcmriUGV7UgFg8dt93oJscEUcJbCyupTJYXCGL4t-k1W7EiAXNnqhIIoGeXGe8YRKySObM5VHSD5Wq5EYCF3vKhK75l1C0n7s6hxTzeqZIwPKD8i2bUUxJwVwzOCW6MCDJpdwaobNqXa/s1600/wealth+quintiles.png" width="0%" /></a></div><a name='more'></a>What's an example of this? One I've encountered many times is something like 'if you work hard you can stop being poor' or 'everyone decides their own wealth'. Is this true? This is what I mean...there is no absolute yes or no answer. Consider the following plot (<a href="https://www.stlouisfed.org/publications/regional-economist/july-2016/which-persists-more-from-generation-to-generation-income-or-wealth" target="_blank">source data</a>):<div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgQcmriUGV7UgFg8dt93oJscEUcJbCyupTJYXCGL4t-k1W7EiAXNnqhIIoGeXGe8YRKySObM5VHSD5Wq5EYCF3vKhK75l1C0n7s6hxTzeqZIwPKD8i2bUUxJwVwzOCW6MCDJpdwaobNqXa/s1058/wealth+quintiles.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="449" data-original-width="1058" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgQcmriUGV7UgFg8dt93oJscEUcJbCyupTJYXCGL4t-k1W7EiAXNnqhIIoGeXGe8YRKySObM5VHSD5Wq5EYCF3vKhK75l1C0n7s6hxTzeqZIwPKD8i2bUUxJwVwzOCW6MCDJpdwaobNqXa/s1600/wealth+quintiles.png" width="95%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div></div><div>The following statements are all true:</div><div><ul style="text-align: left;"><li>some kids from the poorest households end up wealthy</li><li>some kids from the wealthiest households end up poor</li><li>most poor kids stay poor</li><li>most rich kids stay rich</li><li>parental wealth is a good predictor of your wealth as an adult</li></ul><div>Many people will see someone claim that last bullet and jump to 'what about this guy that grew up poor and made it?' The plot clearly shows that's possible and doesn't negate the last bullet. Thinking in terms of what's likely is a better model for this.</div></div><div><br /></div><div>Another common example is the classic 'it's cold today so global warming isn't real'. If you don't think of the distributions of temperatures, this is an easy fallacy to fall victim to. Here are two plots of temperature distributions for Denver, Colorado summer highs in 1900 and 2000 (respectively):<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivOHvPKNgiYB-xYhaSNMrmSD092C7nuD-WKTGdmCZpknTdlEgpBIZ7a9rf8-UgBC3xSQalAo8akS1hqYBPLf9AJi-Iby9nwGR9S3yLrEGPb2Sq6KPzN33ZCBT-OAp_9fZXpCqU7Tlzkd16/s1072/1900+denver+summers.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="496" data-original-width="1072" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivOHvPKNgiYB-xYhaSNMrmSD092C7nuD-WKTGdmCZpknTdlEgpBIZ7a9rf8-UgBC3xSQalAo8akS1hqYBPLf9AJi-Iby9nwGR9S3yLrEGPb2Sq6KPzN33ZCBT-OAp_9fZXpCqU7Tlzkd16/s1600/1900+denver+summers.PNG" width="95%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXCKCpatXWkXlsuTaaoeBHLJ3mqpHhw5UvvDnh0JiB-CzBDzx8ItD4iFReFu6EAhTBJ2ueNB2mwXEUqHF2ZEk41RmaEXgS4Gi6yqIVg5OXf_rK8_c5kLmqMXEWPe61D61mfHtsV3ut99HQ/s1071/2000+denver+summers.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="498" data-original-width="1071" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXCKCpatXWkXlsuTaaoeBHLJ3mqpHhw5UvvDnh0JiB-CzBDzx8ItD4iFReFu6EAhTBJ2ueNB2mwXEUqHF2ZEk41RmaEXgS4Gi6yqIVg5OXf_rK8_c5kLmqMXEWPe61D61mfHtsV3ut99HQ/s1600/2000+denver+summers.PNG" width="95%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><br /></div><br />There are clearly cold (for summer) days in both. There is also a clear shift towards higher temperatures in 2000 vs 1900. That 'the distribution has shifted towards higher temperatures' is the best mental model for global warming in my opinion. If you want to see more of these <a href="https://cityprojections.com/summerHighHistograms.html">I pulled them from this page.</a></div><div><br /></div><div>This could go on forever but I hope the general idea is clear. Many things are distribution-based and can be understood much more easily if thought of in terms of 'how does this distribution shift/compare?'</div><div><br /></div><div>If you're interested in a great book on this general topic, <a href="https://www.amazon.com/gp/product/0307275175/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=rhamner-20&creative=9325&linkCode=as2&creativeASIN=0307275175&linkId=b33c0c74e4c510ecadee926cad4195b0" target="_blank">I liked 'The Drunkard's Walk'.</a></div><div><br /></div><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-57591309907030021152021-02-07T22:11:00.002-08:002021-02-07T22:24:51.939-08:00If the square root of -1 is i, what is the cube root of -1?You probably learned at some point that the square root of -1 is i. What about the cubed root of it? There's the obvious answer of (-1)^3 = -1, but the answer isn't actually that simple.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Eefba37l9OfrJdzjrg73H-zjO3rafQ-3GMFtBCk3hG_PQht1c3-Bv62hXrfsOQ3k7AX-VE-F7WxiVQ_hEZijUUFFt2xPzRx8Gd77HgoMxVqag0Ig2bqW9ubT0awEECEyqDU1V4JbGPEx/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="1688" data-original-width="1200" height="449" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Eefba37l9OfrJdzjrg73H-zjO3rafQ-3GMFtBCk3hG_PQht1c3-Bv62hXrfsOQ3k7AX-VE-F7WxiVQ_hEZijUUFFt2xPzRx8Gd77HgoMxVqag0Ig2bqW9ubT0awEECEyqDU1V4JbGPEx/w320-h449/image.png" width="0%" /></div><a name='more'></a><script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script async="" id="MathJax-script" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"><style>.MathJax { font-size: 1.3em !important; }</style>
</script>To answer this, we'll need Euler's identity which is:<div style="font-size: 60px;"> \[e^{i\pi}=-1\]</div>Just take the cubed root of each side:<div style="font-size: 60px;"> \[e^{i\pi*\frac{1}{3}}=-1^{\frac{1}{3}}\]</div><div style="font-size: 60px;"> \[e^{\frac{i\pi}{3}}=-1^{\frac{1}{3}}\]</div>Now we just need the following definition:<div style="font-size: 60px;"> \[e^{ix}=cos(x)+i*sin(x)\]</div>Plugging in our value:<div style="font-size: 60px;"> \[e^{\frac{i\pi}{3}}=-1^{\frac{1}{3}}\]</div><div style="font-size: 60px;"> \[cos(\frac{\pi}{3}) + i*sin(\frac{\pi}{3})=-1^{\frac{1}{3}}\]</div><div style="font-size: 60px;"> \[\frac{1}{2} + i*\frac{\sqrt{3}}{2}=-1^{\frac{1}{3}}\]</div>And that's it...there's another cube root of -1.<div><br />What does that actually mean? Consider this coordinate system:
</div><div><br /></div></a><div><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Eefba37l9OfrJdzjrg73H-zjO3rafQ-3GMFtBCk3hG_PQht1c3-Bv62hXrfsOQ3k7AX-VE-F7WxiVQ_hEZijUUFFt2xPzRx8Gd77HgoMxVqag0Ig2bqW9ubT0awEECEyqDU1V4JbGPEx/" style="margin-left: 1em; margin-right: 1em;"></a><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Eefba37l9OfrJdzjrg73H-zjO3rafQ-3GMFtBCk3hG_PQht1c3-Bv62hXrfsOQ3k7AX-VE-F7WxiVQ_hEZijUUFFt2xPzRx8Gd77HgoMxVqag0Ig2bqW9ubT0awEECEyqDU1V4JbGPEx/" style="margin-left: 1em; margin-right: 1em;"></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Eefba37l9OfrJdzjrg73H-zjO3rafQ-3GMFtBCk3hG_PQht1c3-Bv62hXrfsOQ3k7AX-VE-F7WxiVQ_hEZijUUFFt2xPzRx8Gd77HgoMxVqag0Ig2bqW9ubT0awEECEyqDU1V4JbGPEx/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="1688" data-original-width="1200" height="449" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Eefba37l9OfrJdzjrg73H-zjO3rafQ-3GMFtBCk3hG_PQht1c3-Bv62hXrfsOQ3k7AX-VE-F7WxiVQ_hEZijUUFFt2xPzRx8Gd77HgoMxVqag0Ig2bqW9ubT0awEECEyqDU1V4JbGPEx/w320-h449/image.png" width="320" /></a></div><br /><br /></div><div>With real numbers on the horizontal axis and imaginary numbers on the vertical axis, you can draw complex numbers as vectors. This has a cool property. We got pi/3 radians as our angle there. That's equal to 60 degrees, or one-sixth of a full rotation. Looking at that coordinate system, if r = 1:<br /><br /><ul style="text-align: left;"><li>0 degrees = 1</li><li>90 degrees = i</li><li>180 degrees = -1</li><li>270 degrees = -i</li><li>360 degrees = 1</li><li>450 degrees = i</li><li>...</li></ul><div>It rotates around. Since an angle of pi/3 represents 60 degrees, cubing the value with r = 1 and angle = 60 degrees gives you the same thing as r = 1 and angle = 180 degrees, which is -1.</div></div><div><br /></div><div>Thinking through it a bit more, that's not unique. What if we used 300 degrees instead? Rotating by 300 degrees 3 times gives you 900 degrees which is just 2 revolutions + 180 degrees. Will that give you -1 also?</div><div><br /></div><div>60 degree answer cubed:<div style="font-size: 60px;"> \[(\frac{1}{2} + i*\frac{\sqrt{3}}{2})*(\frac{1}{2} + i*\frac{\sqrt{3}}{2})*(\frac{1}{2} + i*\frac{\sqrt{3}}{2})\]</div><div style="font-size: 60px;"> \[(\frac{1}{4} + i*\frac{\sqrt{3}}{2} - \frac{3}{4})*(\frac{1}{2} + i*\frac{\sqrt{3}}{2})\]</div><div style="font-size: 60px;"> \[\frac{1}{8} + i*\frac{\sqrt{3}}{8} + i*\frac{\sqrt{3}}{4} - \frac{3}{4} - \frac{3}{8} - i*\frac{3*\sqrt{3}}{8}\]</div>That adds up to -1 which is what we wanted.<br /><br />300 degree answer cubed:<div style="font-size: 60px;"> \[(\frac{1}{2} - i*\frac{\sqrt{3}}{2})*(\frac{1}{2} - i*\frac{\sqrt{3}}{2})*(\frac{1}{2} - i*\frac{\sqrt{3}}{2})\]</div><div style="font-size: 60px;"> \[(\frac{1}{4} - i*\frac{\sqrt{3}}{2} + \frac{3}{4})*(\frac{1}{2} + i*\frac{\sqrt{3}}{2})\]</div><div style="font-size: 60px;"> \[\frac{1}{8} - i*\frac{\sqrt{3}}{8} - i*\frac{\sqrt{3}}{4} + \frac{3}{4} + \frac{3}{8} + i*\frac{3*\sqrt{3}}{8}\]</div>That also adds up to -1 which is what we wanted. Finally, we have the -1^3 = -1 answer which is just the 180 degree one.<br /><br />Thus, we found three cubed roots of -1: 0.5 + 0.866i, 0.5 - 0.866i, and -1.<br><br>For the one we all learned...'square root of -1 is i'...is that really the only answer? Doing a similar exercise, you want to end up at m*360 + 180 degrees after n rotations where n is the root and m is an integer. Here, n = 2. That means 2*rotation = m*360 + 180, or rotation = 180*m + 90. Start with m = 0. rotation = 90 which means i is an answer which we know. Try m = 1. rotation = 270 which means -i is answer. Trying that out...-i * -i = i^2 = -1. That works. Try m = 2. rotation = 450 which is just 90 + 1 full cycle, so we're repeating now. i and -i are our square roots of -1.</div><div><br /></div><div><br /></div><div><br /></div><div><br /></div></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-79837416741239166202021-01-24T22:49:00.009-08:002021-01-25T15:48:05.898-08:00Regression Toward the Mean in the NFLI wanted to run some quick tests to see if <a href="https://en.wikipedia.org/wiki/Regression_toward_the_mean#:~:text=In%20statistics%2C%20regression%20toward%20the,or%20average%20on%20further%20measurements.&text=The%20answer%20was%20not%20%27on%20average%20directly%20above%27.">regression toward the mean</a> shows up clearly in NFL data.<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHRBnC8rM6O4wkrFC84qTq2cfUL2acDRRPVWS4n_FXZCQwlXzXtw3Q0QsIh-2_NrTcBJwfrJ-7DbWK9sKXyVZ_XvsmNaSFOPWpe8SWlfkYlas_GlRKmKbRmFkl0Bw9W2yn7gGglXkKrkcL/s1296/passing+yards+scatter.png" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="632" data-original-width="1296" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHRBnC8rM6O4wkrFC84qTq2cfUL2acDRRPVWS4n_FXZCQwlXzXtw3Q0QsIh-2_NrTcBJwfrJ-7DbWK9sKXyVZ_XvsmNaSFOPWpe8SWlfkYlas_GlRKmKbRmFkl0Bw9W2yn7gGglXkKrkcL/s1600/passing+yards+scatter.png" width="0%" /></a><a name='more'></a><h4 style="text-align: left;"><br /></h4><h4 style="text-align: left;">Background</h4><div>In case you aren't familiar, 'regression toward the mean' roughly means that if a random variable is an outlier, a future instance is likely to be closer to the mean. For a really simple model to make this easy to understand for something like NFL player performance, imagine that each player's performance is X% skill and Y% luck. If X is 100 and Y is 0, then previous years will nearly perfectly predict future years. If Y is 100 and X is 0, then there will be no relationship between performance from one year to the next. If X and Y are both between 0 and 100, there will be some relationship between performance from year to year but it won't be perfect.
<div><br /></div><div>There are two easy ways for me to look at this phenomenon:<br /><ol style="text-align: left;"><li>plot one year's performance against the previous year's along with a line with a slope of 1 (X = 100%) and a best-fitting line<br /><br /></li><li>bin the data by previous year's performance and look at how each bin shifted in the next year</li></ol><div>What might we see? There are many possibilities, but here are a few examples:</div><div><ul style="text-align: left;"><li>"Players that performed well perform even better the next season": plot 1 will show a slope greater than 1 and plot 2 will show the bottom bin doing worse and the top bin doing better<br /><br /></li><li>"Performance is driven by skill so it's the same year-to-year": plot 1 will show a slope of 1 and plot 2 will show all bins at roughly zero<br /><br /></li><li>"Performance is a mix of skill and luck so top performers will move back towards average and poor performers will move up towards average (<b>this is the regression toward the mean case</b>)": plot 1 will show a slope between 0 and 1, and plot 2 will show the bottom bin doing better and the top bin doing worse<br /><br /></li><li>"It's all random/luck": plot 1 will show a slope of ~0 and plot 2 will show all bins at roughly 0<br /><br /></li><li>"Poor performers overcompensate and end up better than average next season": plot 1 will show a slope less than 1 and plot 2 will show the bottom bin doing better and the top bin doing worse</li></ul></div><div><div>To test it out I ran with 5 different stats using data from all starters from 2000-2020. For example, for a 2010-2011 compare, year 1 is 2010 and year 2 is 2011. You would expect the best performers in 2010 to do a bit worse in 2011, and the worst in 2010 to do a bit better in 2011. In the bar plots, the 'bottom third' means the 33% of players that were worst in season 1 from the plot above.</div></div></div><div><br /></div><h4 style="text-align: left;">Results</h4><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHRBnC8rM6O4wkrFC84qTq2cfUL2acDRRPVWS4n_FXZCQwlXzXtw3Q0QsIh-2_NrTcBJwfrJ-7DbWK9sKXyVZ_XvsmNaSFOPWpe8SWlfkYlas_GlRKmKbRmFkl0Bw9W2yn7gGglXkKrkcL/s1296/passing+yards+scatter.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="632" data-original-width="1296" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHRBnC8rM6O4wkrFC84qTq2cfUL2acDRRPVWS4n_FXZCQwlXzXtw3Q0QsIh-2_NrTcBJwfrJ-7DbWK9sKXyVZ_XvsmNaSFOPWpe8SWlfkYlas_GlRKmKbRmFkl0Bw9W2yn7gGglXkKrkcL/s1600/passing+yards+scatter.png" width="100%" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNUasc2lZFCv3Jis5LDcGwrNR8CHGQ9SFeDcTBttcwl39uA6FVYentT5jmMFU_BNlEU4hrxxTyX8qCOLKhBYmOkJjIICFVBDV67A7Z_q2Xm4m4OddVr3puGnvI5gEisIv_l_ry7qboDeWv/s966/passing+yards+bar.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="599" data-original-width="966" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNUasc2lZFCv3Jis5LDcGwrNR8CHGQ9SFeDcTBttcwl39uA6FVYentT5jmMFU_BNlEU4hrxxTyX8qCOLKhBYmOkJjIICFVBDV67A7Z_q2Xm4m4OddVr3puGnvI5gEisIv_l_ry7qboDeWv/s1600/passing+yards+bar.png" width="75%" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVJpNI62pBKzgHBfnFUSIVJSu0PCHkv3aOUVVgy3TAJQCh4wvexkN47rp8970ZNKkKyEr_CfxcePy0zdzDWKPFlWDjJReWcbh9QkDsvoUp9z3PiU3p6W7nZTqvgVxzsh-CagtQmPfoAZbh/s1298/interceptions+scatter.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="632" data-original-width="1298" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVJpNI62pBKzgHBfnFUSIVJSu0PCHkv3aOUVVgy3TAJQCh4wvexkN47rp8970ZNKkKyEr_CfxcePy0zdzDWKPFlWDjJReWcbh9QkDsvoUp9z3PiU3p6W7nZTqvgVxzsh-CagtQmPfoAZbh/s1600/interceptions+scatter.png" width="100%" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVD3x0QhsYNz-mgtK7G0zNGYTTJ3vn9ccm8nhf6ghpuhEUqNobFG75hlab9xvA5ZfGaH0rNvwCo8aLgoUkFQ1ZucnHKGRdTFr_V1zYS0OsRn7_tREZU9muMuCBWwTYbm-B-2Eg6Y-gfGzy/s964/interceptions+bar.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="599" data-original-width="964" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVD3x0QhsYNz-mgtK7G0zNGYTTJ3vn9ccm8nhf6ghpuhEUqNobFG75hlab9xvA5ZfGaH0rNvwCo8aLgoUkFQ1ZucnHKGRdTFr_V1zYS0OsRn7_tREZU9muMuCBWwTYbm-B-2Eg6Y-gfGzy/s1600/interceptions+bar.png" width="75%" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-W2ia80ueOLg5FVM6vVi6Ui2HcZYNdGAIUv597AigXIdVhDk70LNuWwt9U2Mu9p1N6o5zuk9K5gLedIdkbwt4ErYIKlyWrM7eNhhQdd1AX4RtJEeGlmLckAfjf0DdGWsbxG52e0u3eVxc/s1289/passing+tds+scatter.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="632" data-original-width="1289" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-W2ia80ueOLg5FVM6vVi6Ui2HcZYNdGAIUv597AigXIdVhDk70LNuWwt9U2Mu9p1N6o5zuk9K5gLedIdkbwt4ErYIKlyWrM7eNhhQdd1AX4RtJEeGlmLckAfjf0DdGWsbxG52e0u3eVxc/s1600/passing+tds+scatter.png" width="100%" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHe6lnh0_8wya3coGbuYhFk57yG5yZ2a2jNSYsQEhS8H8F9ewsUD_ZhLxHHQ62zHai4AcRusYl3Wvt6VcYmmriLayWKDiPAU1TOXlNYgW9EiQP0i7A25qX2mFzK28g01jz3Y6WLzeM3hua/s957/passing+tds+bar.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="599" data-original-width="957" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHe6lnh0_8wya3coGbuYhFk57yG5yZ2a2jNSYsQEhS8H8F9ewsUD_ZhLxHHQ62zHai4AcRusYl3Wvt6VcYmmriLayWKDiPAU1TOXlNYgW9EiQP0i7A25qX2mFzK28g01jz3Y6WLzeM3hua/s1600/passing+tds+bar.png" width="75%" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheH0mnlYZdZJ6UR1gnERvEc9hfaG6Uo0f_Iy0m-reJBXnReFyHCAYthPJvJAhn1rF31Mn04Dr3WYNgb8whyphenhyphenMfG5SZF3anwFmkfLVEPEWhkNt5o69Jt9rvdeIqs9t9KqYgA09UBEqJf0z2n/s1298/rushing+tds+scatter.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="632" data-original-width="1298" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheH0mnlYZdZJ6UR1gnERvEc9hfaG6Uo0f_Iy0m-reJBXnReFyHCAYthPJvJAhn1rF31Mn04Dr3WYNgb8whyphenhyphenMfG5SZF3anwFmkfLVEPEWhkNt5o69Jt9rvdeIqs9t9KqYgA09UBEqJf0z2n/s1600/rushing+tds+scatter.png" width="100%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtCjasJPhKih-cnlDH3Diq7839XySE07v4-XuWUM3kP7J1GNlTbIxyVDb08mTn5SVK-axdbSGkHIbizL3HJv-e1QhU868R4YdNwQqqSqczM-4x3X-PFtOioMKx3hPut-zX5QshNtqo4b29/s1019/rushing+tds+bar.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="599" data-original-width="1019" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtCjasJPhKih-cnlDH3Diq7839XySE07v4-XuWUM3kP7J1GNlTbIxyVDb08mTn5SVK-axdbSGkHIbizL3HJv-e1QhU868R4YdNwQqqSqczM-4x3X-PFtOioMKx3hPut-zX5QshNtqo4b29/s1600/rushing+tds+bar.png" width="75%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp_0Xzm18I4gBiZbZTtu5-kPBXhFHGyn_Y2kuUEZB3ITULtc-pjeWaMBRMpzmGd-sR_YcneuGf22WEt8MrL0oME6CLAqCozYR9xu5D6Qp6cAkz24D2Ju5aDjzlFaT4lWAetnaPWwacKDdS/s1293/rushing+yards+scatter.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="632" data-original-width="1293" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp_0Xzm18I4gBiZbZTtu5-kPBXhFHGyn_Y2kuUEZB3ITULtc-pjeWaMBRMpzmGd-sR_YcneuGf22WEt8MrL0oME6CLAqCozYR9xu5D6Qp6cAkz24D2Ju5aDjzlFaT4lWAetnaPWwacKDdS/s1600/rushing+yards+scatter.png" width="100%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgz4o1UJBzRyPfqnCU5ajSqXmsNW129H2A9ttefrVVHzWnCFHP-6Mbopn-ukCFr2tMGNkyLqRSNykl2GFTuwkKROJ1qxMoi2iGvFynfh3r2n3zHqjbUBfD_mH2hyXcMD8Uju8XHJ364zxCS/s965/rushing+yards+bar.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="599" data-original-width="965" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgz4o1UJBzRyPfqnCU5ajSqXmsNW129H2A9ttefrVVHzWnCFHP-6Mbopn-ukCFr2tMGNkyLqRSNykl2GFTuwkKROJ1qxMoi2iGvFynfh3r2n3zHqjbUBfD_mH2hyXcMD8Uju8XHJ364zxCS/s1600/rushing+yards+bar.png" width="75%" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><div>and the data show regression toward the mean. Every stat I've tried (with a luck component obviously) followed the pattern above.</div><div><br /></div><div><br /></div></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com3tag:blogger.com,1999:blog-1532419805701836386.post-35851241756957448582021-01-15T22:00:00.003-08:002021-01-15T22:00:17.004-08:00Fourier Series AnimationsIt always seemed magical to me that you can get a square wave from adding together sine waves, so I threw together some animations of Fourier series.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqx4dhw3pMer6Yu-sYy9ax28KTdzMKXxtSgSQnr5zvYuL1PsUMcYNHgTnSZCr6aAmYdnNwpjgG3Ve5RKW4gkh5D7RjI-EO6iK7G103LSCrVo0xc73rOcu4dQVGS1pH6k1jRA-r8BlczkUP/s1329/fourier_square.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="542" data-original-width="1329" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqx4dhw3pMer6Yu-sYy9ax28KTdzMKXxtSgSQnr5zvYuL1PsUMcYNHgTnSZCr6aAmYdnNwpjgG3Ve5RKW4gkh5D7RjI-EO6iK7G103LSCrVo0xc73rOcu4dQVGS1pH6k1jRA-r8BlczkUP/s1600/fourier_square.gif" width="0%" /><a name='more'></a><h4 style="text-align: left;">Square Wave</h4><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqx4dhw3pMer6Yu-sYy9ax28KTdzMKXxtSgSQnr5zvYuL1PsUMcYNHgTnSZCr6aAmYdnNwpjgG3Ve5RKW4gkh5D7RjI-EO6iK7G103LSCrVo0xc73rOcu4dQVGS1pH6k1jRA-r8BlczkUP/s1329/fourier_square.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="542" data-original-width="1329" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqx4dhw3pMer6Yu-sYy9ax28KTdzMKXxtSgSQnr5zvYuL1PsUMcYNHgTnSZCr6aAmYdnNwpjgG3Ve5RKW4gkh5D7RjI-EO6iK7G103LSCrVo0xc73rOcu4dQVGS1pH6k1jRA-r8BlczkUP/s1600/fourier_square.gif" width="100%" /></a></div><br /><div><br /></div><h4 style="text-align: left;">Pulse</h4><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj91DuQmWhd4yN3DSwH1rMnouorfUt0y7_Us3mMFyZx5towsxnwbfRaxCoXHSCvZb1-JovaUbted-KXDsw6ht_SlYg-yeOahm4UUV4dqNPEhgIa3wcTiGt6p194GTgsmUgPJkxHjSaWYieH/s1329/fourier_pulse.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="542" data-original-width="1329" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj91DuQmWhd4yN3DSwH1rMnouorfUt0y7_Us3mMFyZx5towsxnwbfRaxCoXHSCvZb1-JovaUbted-KXDsw6ht_SlYg-yeOahm4UUV4dqNPEhgIa3wcTiGt6p194GTgsmUgPJkxHjSaWYieH/s1600/fourier_pulse.gif" width="100%" /></a></div><br /><div><br /></div><h4 style="text-align: left;">Parabola</h4><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfsjG-jgPLorCAWQkEY2cMsZjRxbz31uYLTP0f0Kmg570WHLSMhzwOPHsZ7i7cUR5hkqBkrEoQtMKTmvltU-vbcvZNCH26bdgjEhnB7EDHRDeTNy7rKL9u-VZlZNlhO1S7wAa7RTmTqd90/s1329/fourier_parabolas.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="542" data-original-width="1329" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfsjG-jgPLorCAWQkEY2cMsZjRxbz31uYLTP0f0Kmg570WHLSMhzwOPHsZ7i7cUR5hkqBkrEoQtMKTmvltU-vbcvZNCH26bdgjEhnB7EDHRDeTNy7rKL9u-VZlZNlhO1S7wAa7RTmTqd90/s1600/fourier_parabolas.gif" width="100%" /></a></div><br /><div><br /></div><h4 style="text-align: left;">Pulse Variation</h4><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr07YEnvywv5G_PS4yIinAzipwS3QqTcNCV560ucFp-ZJMiNUrLV_YMPOtdccdQTwrJ788ORMtaddeFx8Yx9MeZlmmvh4pj6-Xx-O32ef4pkAPjuTnlwvDKnLAVCnXaaLS0OfvVk05baaH/s1329/fourier_sinpulse.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="542" data-original-width="1329" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr07YEnvywv5G_PS4yIinAzipwS3QqTcNCV560ucFp-ZJMiNUrLV_YMPOtdccdQTwrJ788ORMtaddeFx8Yx9MeZlmmvh4pj6-Xx-O32ef4pkAPjuTnlwvDKnLAVCnXaaLS0OfvVk05baaH/s1600/fourier_sinpulse.gif" width="100%" /></a></div><br /><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-27595343247794166902021-01-02T22:12:00.003-08:002021-01-02T22:13:31.752-08:00How Should You Bet on a Biased Coin Toss?If you know a coin is biased to come up heads 75% of the time, what betting strategy should you use to bet on the outcome of a flip?<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhf5wj64Xgqlm5VeXAcsXdkSRfhV_XbhSrwR0SO5B6ppynkw7rko83BaZbuVEHJfGo3m9jWuniIq7i13xjPvdoDZ6L27p7xDqXh6R7mz4VZDLhdAabkzHISXPLmPYxl47qXeclX-FS5fjl2/s1600/United_States_Quarter.jpg" width=0%"><a name='more'></a></img><p>What might seem intuitive is to have a mixed strategy of 75% heads and 25% tails. Maybe something like 'flip an unbiased coin twice and bet tails if you get tails twice and heads for any other outcome'. What result will that give you?</p><p>There are four possibilities here:</p><p></p><ol style="text-align: left;"><li>Coin lands on heads and you bet heads (75%*75% = 56.25% of the time)</li><li>Coin lands on heads and you bet tails (75%*25% = 18.75% of the time)</li><li>Coin lands on tails and you bet heads (25%*75% = 18.75% of the time)</li><li>Coin lands on tails and you bet tails (25%*25% = 6.25% of the time)</li></ol><div>1 and 4 are winning situations, so you'll win 62.5% of the time this way (just sum the 1 and 4 win rates).</div><div><br /></div><div>You might immediately notice that 62.5% is less than 75%. What if you just always bet heads? Filling out the same list as above:</div><div></div><div><ol><li>Coin lands on heads and you bet heads (75%*100% = 75% of the time)</li><li>Coin lands on heads and you bet tails (75%*0% = 0% of the time)</li><li>Coin lands on tails and you bet heads (25%*100% = 25% of the time)</li><li>Coin lands on tails and you bet tails (25%*0% = 0% of the time)</li></ol><div>1 and 4 are winning situations, so you'll win 75% of the time this way. In this situation, the general win rate is:<br /><br />(coin bias*head bet percentage) + [(1 - coin bias)*(1 - head bet percentage)] = win rate</div></div><div><br /></div><div>We want to maximize this. Using b for 'coin bias' and h for 'heat bet percentage':</div><div><br /></div><div>b*h + (1 - b)*(1 - h) = win rate</div><div><br /></div><div>b*h + 1 - h - b + b*h = win rate</div><div><br /></div><div>2*b*h + 1 - h - b = win rate</div><div><br /></div><div>h*(2*b - 1) + 1 - b = win rate</div><div><br /></div><div>At this point, we have an equation for a line. Win rate vs h is a line with a slope of 2*b - 1 and an x-intercept of 1 - b. Anytime 2*b - 1 is positive, this line will go up and to the right so h = 1 is the best bet (heads 100% of the time). Anytime 2*b - 1 is negative, h = 0 is the best bet (tails 100% of the time). 2*b - 1 is positive whenever b is greater than 0.5. Thus, the optimal strategy here is bet in the direction of the bias 100% of the time when you have a known, biased coin.</div><div><br /></div><div><br /></div><p></p>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com12tag:blogger.com,1999:blog-1532419805701836386.post-55627131940206901142020-12-27T22:02:00.004-08:002020-12-27T22:04:38.093-08:00Making a CSS Flashlight Effect Using Conic-gradientsThis is just a quick tutorial of conic-gradients showing a flashlight effect with very little code.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX-yV5csr0n2l56YEgZ7mSd897ijru_mdpADtq62ENv6tlAB3wktQ-MN_GX31jlEuk8QR1eupZ2e9lUgctUlO54gDnfowyP8-pl1R9lDxhwfrXLWQvgZYNSGo6r1xkVFd5WzhUUV0qT8Kj/s1867/Capture.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="654" data-original-width="1867" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX-yV5csr0n2l56YEgZ7mSd897ijru_mdpADtq62ENv6tlAB3wktQ-MN_GX31jlEuk8QR1eupZ2e9lUgctUlO54gDnfowyP8-pl1R9lDxhwfrXLWQvgZYNSGo6r1xkVFd5WzhUUV0qT8Kj/s1600/Capture.PNG" width="0%" /></a></div><a name='more'></a><p>The basic idea here is to use a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/conic-gradient()" target="_blank">conic-gradient</a> and do the following:<br /></p><ul style="text-align: left;"><li>set it to be the flashlight color and fairly transparent for the bright area (yellow from -25 to 25 degrees in the example here)</li><li>set it to be dark and fairly opaque for the dark area (black with 95% opacity from 25 to 335 degrees in the example here)</li><li>make the flashlight layer(s) fixed position and sit on top of the page</li><li>to keep it from starting as a point, offset it (vertical location of 110% in the example here puts it 10% below the bottom of the page)</li></ul><div>And that's it...it's actually really simple. Here is a working example on top of a dummy html page:</div><div><br /></div><iframe allowfullscreen="true" allowtransparency="true" frameborder="no" height="600" loading="lazy" scrolling="no" src="https://codepen.io/rhamner/embed/VwKzNLd?height=602&theme-id=light&default-tab=css,result" style="width: 100%;" title="Flashlight">
See the Pen <a href='https://codepen.io/rhamner/pen/VwKzNLd'>Flashlight</a> by Robert Hamner
(<a href='https://codepen.io/rhamner'>@rhamner</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe><div><br /></div><div><br /></div><div>It's really clean and requires no javascript. It's probably possible to make it cleaner. An obvious question you might have is 'can I make a flashlight that moves with the mouse?' and the answer is sure...simply set the gradient position to the cursor location (this requires javascript but is simple):</div><div><br /></div><iframe allowfullscreen="true" allowtransparency="true" frameborder="no" height="600" loading="lazy" scrolling="no" src="https://codepen.io/rhamner/embed/dypJGZZ?height=476&theme-id=light&default-tab=css,result" style="width: 100%;" title="Flashlight mouse">
See the Pen <a href='https://codepen.io/rhamner/pen/dypJGZZ'>Flashlight mouse</a> by Robert Hamner
(<a href='https://codepen.io/rhamner'>@rhamner</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe><div><br /></div><div>All that took was adding a listener to the page for mouse or touch movements, updating --X and --Y variables on those events, and setting the conic-gradient position to be var(--X) var(--Y). Simple and looks pretty cool.</div><div><br /></div><div><br /></div><p></p>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-60114563780967415932020-12-11T22:03:00.011-08:002021-10-03T23:24:31.961-07:00What Are the Most Impressive NFL Combine Performances Ever?If you combine the major tests and adjust for weight and height, which NFL player had the most impressive combine performance?<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIdbT2ry5bda2DXaBAzwXZ8Iikq6tWumHqZSlnDgD5ml1jLl-YL9PSVssrZvSUDZdgUixubZzqgB-WWDb3KcjNPmeluYpMo7LZmZZKxxdNeIthJ4Z_kZJ_94EaJGXAD6RdVALPw79Wa32n/s472/40+times.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="472" data-original-width="403" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIdbT2ry5bda2DXaBAzwXZ8Iikq6tWumHqZSlnDgD5ml1jLl-YL9PSVssrZvSUDZdgUixubZzqgB-WWDb3KcjNPmeluYpMo7LZmZZKxxdNeIthJ4Z_kZJ_94EaJGXAD6RdVALPw79Wa32n/s1600/40+times.PNG" width="0%" /></a></div><a name='more'></a><h4 style="text-align: left;">Data</h4>Unfortunately, the modern combine hasn't existed for that long, so I only have data back to the year <a href="https://www.pro-football-reference.com/draft/2000-combine.htm" target="_blank">2000</a>. Still, that gives us a good-sized data set (~5000 players with at least some data).<p>To try to find 'best performance ever', I wanted to do two things:<br /></p><ol style="text-align: left;"><li>Adjust for weight and height...a 200 lb guy running a 4.5 forty is way less impressive than a 250 lb guy doing it.</li><li>Try to combine all metrics...a 200 lb guy running a 4.5 forty and getting 4 bench reps is way less impressive than a 200 lb running a 4.5 forty and getting 24 bench reps.</li></ol><div>The metrics that seem to be available for most people are:<br /><ul style="text-align: left;"><li>40 yard dash time</li><li>bench press reps (number of times they bench press 225 lbs)</li><li>broad jump</li><li>vertical jump</li></ul><div>So I used those.</div></div><div><br /></div><h4 style="text-align: left;">Calculation</h4><div>To calculate this, I used a three step process:</div><div><ol style="text-align: left;"><li>Perform linear regression for each metric using weight and height as inputs ('metric = C1*weight + C2*height + C3').</li><li>Divide actual value by value predicted from the regression for each metric to get a score. E.g., if a player ran a 4.5 40 and the model predicted a 4.7 one for his weight and height, he'd get 4.5/4.7, or 0.957 for that metric.</li><li>Calculate an overall score that's a weighted rss of the individual scores. The weights are 1, 1/5, 1/2, 1/2 for the four metrics in that order.</li></ol></div><div>It doesn't affect the calculation much, but throughout, I use weight as an input for everything but bench reps, and weight^2/3 as an input for bench reps.</div><div><br /></div><h4 style="text-align: left;">Results</h4><div>Using the calculation described above, these are the greatest combine performances (actual value to the left and predicted value in parentheses to the right):</div><div><br /></div><div><table style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr style="background-color: #e6a117;"><th>player</th><th>40 time (s)</th><th>bench (reps)</th><th>broad (inches)</th><th>vertical (inches)</th></tr><tr><td>Vernon Davis</td><td>4.38 (4.82)</td><td>33 (21)</td><td>128 (114)</td><td>42 (33)</td></tr><tr><td>Terna Nande</td><td>4.51 (4.70)</td><td>41 (20)</td><td>124 (115)</td><td>39 (34)</td></tr><tr><td>Vic Beasley</td><td>4.53 (4.78)</td><td>35 (20)</td><td>130 (115)</td><td>41 (33)</td></tr><tr><td>Mario Williams</td><td>4.70 (5.06)</td><td>35 (23)</td><td>120 (109)</td><td>40 (30)</td></tr><tr><td>Cornelius Washington</td><td>4.55 (4.89)</td><td>36 (22)</td><td>128 (112)</td><td>39 (32)</td></tr><tr><td>Myles Garrett</td><td>4.64 (4.93)</td><td>33 (23)</td><td>128 (111)</td><td>41 (31)</td></tr><tr><td>Nick Perry</td><td>4.55 (4.92)</td><td>35 (23)</td><td>124 (111)</td><td>38 (31)</td></tr><tr><td>Margus Hunt</td><td>4.62 (4.95)</td><td>38 (21)</td><td>121 (113)</td><td>34 (32)</td></tr><tr><td>D.K. Metcalf</td><td>4.33 (4.67)</td><td>27 (18)</td><td>134 (118)</td><td>40 (34)</td></tr><tr><td>Jerick McKinnon</td><td>4.41 (4.57)</td><td>32 (19)</td><td>132 (117)</td><td>40 (35)</td></tr><tr><td>Davis Tull</td><td>4.57 (4.78)</td><td>26 (21)</td><td>132 (114)</td><td>42 (33)</td></tr><tr><td>Jon Alston</td><td>4.50 (4.65)</td><td>30 (19)</td><td>132 (118)</td><td>40 (34)</td></tr><tr><td>Vernon Gholston</td><td>4.65 (4.89)</td><td>37 (23)</td><td>125 (112)</td><td>36 (32)</td></tr><tr><td>Sean Weatherspoon</td><td>4.62 (4.74)</td><td>34 (21)</td><td>123 (115)</td><td>40 (33)</td></tr><tr><td>Demario Davis</td><td>4.49 (4.71)</td><td>32 (19)</td><td>124 (116)</td><td>38 (34)</td></tr><tr><td>Scott Young</td><td>5.08 (5.15)</td><td>43 (27)</td><td>115 (104)</td><td>35 (29)</td></tr><tr><td>Michael Johnson</td><td>4.61 (4.89)</td><td>28 (20)</td><td>128 (114)</td><td>38 (32)</td></tr><tr><td>Alex Barnes</td><td>4.59 (4.66)</td><td>34 (20)</td><td>126 (117)</td><td>38 (34)</td></tr><tr><td>Benjamin Watson</td><td>4.50 (4.85)</td><td>34 (22)</td><td>123 (113)</td><td>36 (32)</td></tr><tr><td>Virgil Green</td><td>4.54 (4.79)</td><td>23 (20)</td><td>130 (115)</td><td>42 (33)</td></tr></tbody></table></div><div></div><div><br /></div><div>#1 there did not surprise me. Vernon Davis's 40 time is pretty well known as an insane combine performance.</div><div><br /></div><div>The first really odd one in that list is actually #2, Terna Nande. He had an extremely short NFL career with a single tackle in his entire career. However, at just 230 pounds he pulled off 41 reps on the bench, and all of his other performances were above average. No other non-lineman in history has gotten more than 40 reps. The rest of the top few had or are currently having pretty good NFL careers.</div><div><br /></div><div>Since the 40 time is the one that seems most discussed, here is the same analysis if you use only the 40 time to rank:<br /><br /></div><table style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr style="background-color: #e6a117;"><th>player</th><th>weight (lbs)</th><th>40 time (s)</th></tr><tr><td>Montez Sweat</td><td>260</td><td>4.41 (4.86)</td></tr><tr><td>Vernon Davis</td><td>254</td><td>4.38 (4.82)</td></tr><tr><td>Bryan Thomas</td><td>266</td><td>4.47 (4.89)</td></tr><tr><td>Dontari Poe</td><td>346</td><td>4.89 (5.35)</td></tr><tr><td>Dwight Freeney</td><td>266</td><td>4.48 (4.89)</td></tr><tr><td>Tank Johnson</td><td>304</td><td>4.69 (5.11)</td></tr><tr><td>Calvin Johnson</td><td>239</td><td>4.35 (4.74)</td></tr><tr><td>Dontay Moch</td><td>248</td><td>4.40 (4.79)</td></tr><tr><td>Matt Jones</td><td>242</td><td>4.37 (4.75)</td></tr><tr><td>Bruce Campbell</td><td>314</td><td>4.75 (5.16)</td></tr><tr><td>Taylor Mays</td><td>230</td><td>4.31 (4.69)</td></tr><tr><td>Terron Armstead</td><td>306</td><td>4.71 (5.12)</td></tr><tr><td>James Hanna</td><td>252</td><td>4.43 (4.81)</td></tr><tr><td>Martez Wilson</td><td>250</td><td>4.42 (4.80)</td></tr><tr><td>T.J. Duckett</td><td>254</td><td>4.45 (4.82)</td></tr><tr><td>Bruce Irvin</td><td>245</td><td>4.41 (4.77)</td></tr><tr><td>Rashan Gary</td><td>277</td><td>4.58 (4.95)</td></tr><tr><td>Connor Barwin</td><td>256</td><td>4.47 (4.83)</td></tr><tr><td>Nick Perry</td><td>271</td><td>4.55 (4.92)</td></tr><tr><td>Lane Johnson</td><td>303</td><td>4.72 (5.10)</td></tr></tbody></table><div><br /></div><div>It's interesting looking through both of these that the really legendary players aren't at the top. Many of them are good players, but Calvin Johnson and J.J Watt are the only ones near the top in either table that will definitely go down as all-time greats. Aaron Donald, Derrick Henry, etc. had above average combine performances but some that did clearly better went on to worse careers.</div><div><br /></div><div>I was curious about that and decided to go in the other direction. What great players had bad combine performances? To do that, I took all all-pro players and matched with names from the combine, and the worst were Max Unger, Tyrann Mathieu, and Tarik Cohen. All under-performed estimates in every metric here. The worst performance from an all-time great here was Adrian Peterson. He was roughly average, but I would have guessed his 40 time was way better (4.68 s).</div><div><br /></div><div><br /></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-76553892138031591622020-12-04T22:13:00.004-08:002020-12-04T22:14:41.646-08:00Split Violin Plots in plotly.jsSplit violins are a cool way to compare distributions, and plotly makes them simple.<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyS-RBS5K3vWC18WAEx5tqDkJFDipWp7fM0Zlc0qUa_Uyf3XmA6lWadbx-4W0aUf_rAFdUR0vAg0XTiv4rcTLBKyCcDfsHAlPi0MdKPUbnNVuCt3uqKzgyHz4eAYykKTfgHus4LiSXG_dF/s1459/split+violins.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="373" data-original-width="1459" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyS-RBS5K3vWC18WAEx5tqDkJFDipWp7fM0Zlc0qUa_Uyf3XmA6lWadbx-4W0aUf_rAFdUR0vAg0XTiv4rcTLBKyCcDfsHAlPi0MdKPUbnNVuCt3uqKzgyHz4eAYykKTfgHus4LiSXG_dF/s1600/split+violins.PNG" width="0%" /></a></div><a name='more'></a>There isn't much to explain here. I've embedded an example below showing how to use it. You just make a normal violin plot, but specify one trace as the negative side and another as the positive side, and plotly handles the rest.<br /><br /><iframe allowfullscreen="true" allowtransparency="true" frameborder="no" height="600px" loading="lazy" scrolling="no" src="https://codepen.io/rhamner/embed/qBaZdpe?height=265&theme-id=light&default-tab=js,result" style="width: 100%;" title="split violins in plotly js">
See the Pen <a href='https://codepen.io/rhamner/pen/qBaZdpe'>split violins in plotly js</a> by Robert Hamner
(<a href='https://codepen.io/rhamner'>@rhamner</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe><br /><br />If you've ever wanted to plot multiple distributions side-by-side, this is an easy option.<br />theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com0tag:blogger.com,1999:blog-1532419805701836386.post-20952557958848481262020-11-17T22:14:00.023-08:002020-11-18T21:13:43.569-08:00How Long Until My Investments Start Making Money?Say you invest some fixed amount of money every year. How long does it take for the investments to grow faster than the amount you're putting into them?<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxh5VgcNTJvhA2zDI6tPxc6B0isgN3qJ2ASCxGBpNTLmhDWwagCv5ZdLM25FseYMbNFGbhde3rqU8rsnrhVDxdSU61k8KtB1P90-HSzCGXpPTo5Sknp3qy5kG3aOXogqHCQNz1iqixU2Ur/s1356/plot.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="449" data-original-width="1356" height="auto" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxh5VgcNTJvhA2zDI6tPxc6B0isgN3qJ2ASCxGBpNTLmhDWwagCv5ZdLM25FseYMbNFGbhde3rqU8rsnrhVDxdSU61k8KtB1P90-HSzCGXpPTo5Sknp3qy5kG3aOXogqHCQNz1iqixU2Ur/s16000/plot.png" width="0%" /></a></div><br /><a name='more'></a><h4 style="text-align: left;">Basic math problem</h4> <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script async="" id="MathJax-script" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"><style>.MathJax { font-size: 1.3em !important; }</style>
</script><div>You invest $X per year in an account that yields R in gains. How long does it take for the gain in a year to be greater than $X?</div><div><br /></div><div>Another way of asking this is 'when does R times the future value of investing X each year exceed X?'</div><div><br /></div><div>The future value of a regular yearly investment where N = number of years, X = yearly investment, and R = growth rate of the investment is:</div><div><br /></div><div><br /></div><div style="font-size: 40px;">\[FV = \frac{X*((1+R)^N - 1)}{R}\]</div><div style="font-size: 28px;"><br /></div><div>What we're looking for is the number of years it takes for R times that to exceed X. That is, we want to solve:</div><div><br /></div><div style="font-size: 40px;">\[\frac{R*X*((1+R)^N - 1)}{R} > X\]</div><div><br /></div><div>Noticing that the R's cancel in numerator and denominator and dividing both sides by X you get:</div><div style="font-size: 28px;"><br /></div><div style="font-size: 40px;">\[((1+R)^N - 1) > 1\]</div><div style="font-size: 28px;"><br /></div><div>Adding 1 to both sides:</div><div style="font-size: 28px;"><br /></div><div style="font-size: 40px;">\[((1+R)^N) > 2\]</div><div style="font-size: 28px;"><br /></div><div>Simplifying:</div><div style="font-size: 28px;"><br /></div><div style="font-size: 40px;">\[log((1+R)^N) > log(2)\]</div><div style="font-size: 28px;"><br /></div><div style="font-size: 40px;">\[N*log((1+R)) > log(2)\]</div><div style="font-size: 28px;"><br /></div><div style="font-size: 40px;">\[N > \frac{log(2)}{log(1+R)}\]</div><div style="font-size: 28px;"><br /></div><div>This is kind of cool. You might not recognize it right away, but that says '<a href="https://www.somesolvedproblems.com/2016/04/why-does-dividing-72-by-your-interest.html" target="_blank">N is greater than the doubling time of the investment</a>'. It's really cool that it works out that way. Because of some pretty good approximations that work out, that means that the investment growth takes over the new money moved in after roughly '72 divided by annual interest rate' years.</div><div><br /></div><div>For a quick concrete example of what this means...say you invest $10,000 per year into an account yielding 6%. The time it takes for the 6% yield each year to exceed $10,000 is log(2)/log(1.06) which is ~12 years.</div><div style="font-size: 28px;"><br /></div><h4 style="text-align: left;">Simple plot</h4><div>Here's a simple interactive plot showing the breakdown between money invested and money from gains for an annual $10,000 investment using the interest rate that you enter below:</div><div><br /></div><div><br /></div>Interest rate (%) <input id="rate" onchange="updatePlot(this.value)" value="6" />
<div id="plot" style="height: 400px; width: 100%;"></div> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script><script>function updatePlot(rate) {
rate = parseFloat(rate);
let time = [];
let input = [];
let yield = [];
let value = 0;
for (let i = 0; i < 25; i++) {
time.push(i);
value = (value*(1 + (rate/100))) + 10000;
input.push(i*10000);
yield.push(value - i*10000);
}
Plotly.react('plot', [
{ x: time, y: input, stackgroup: 'one', name: 'total invested' },
{ x: time, y: yield, stackgroup: 'one', name: 'total gain' }
],
{
font: { size: 16 },
yaxis: { title: 'balance ($)' },
xaxis: { title: 'years' },
title: 'Comparing contributions from amount invested and investment yields'
}, {
responsive: true
});
}
updatePlot(6);</script>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com1tag:blogger.com,1999:blog-1532419805701836386.post-53313873600885337662020-11-01T22:30:00.012-08:002020-11-03T14:55:37.762-08:00How Do American Betting Odds Convert to Percent Chance?If you've looked at betting odds, you've probably seen something like +140 and -175. What % chance does that imply for each participant?<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHX8ckIBd2gRXqW2IWy7qzUj2ccCvQRC1-Ri1aAO6C8k6Jof4INJgrC8rPKLbIHjBhOutx1YgSyNIq6HobsynES9hnpi_xUJsve8-rmFAmadQhjUEtvIYfZTF5Ne6qciXXcD9QPU2bGYvf/s800/800px-Las_Vegas_sportsbook.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="533" data-original-width="800" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHX8ckIBd2gRXqW2IWy7qzUj2ccCvQRC1-Ri1aAO6C8k6Jof4INJgrC8rPKLbIHjBhOutx1YgSyNIq6HobsynES9hnpi_xUJsve8-rmFAmadQhjUEtvIYfZTF5Ne6qciXXcD9QPU2bGYvf/s1600/800px-Las_Vegas_sportsbook.jpg" width="0" /></a></div><a name='more'></a><h4 style="text-align: left;">Definition</h4>First, what do those numbers mean? A -175 means 'you win $100 for each $175 that you bet' and a +140 means 'you win $140 for each $100 that you bet'.<div><br /><h4 style="text-align: left;">Example</h4>Now, consider a matchup that's 60% chance for A and 40% chance for B. What does that convert to?<p>Assuming no cost to bet, if there were 10 matches and you bet $100 on A each time, you'd put in $1000 and expect to get out $1000. Since A wins 60% of the time, you'd get 6 payouts and they would sum to $1000 (since you lose the bet on the 40% where A loses). Each bet would pay out $167, and subtracting off the initial $100 means a profit of $67. Thus, a $100 bet on A yields a profit of $67 when A wins which means that to get a profit of $100 you'd bet 100/.67, or $150. From the definition above, that means that a 60% chance of winning is a line of -150.<br /></p><p>Doing the same with the 40% one, you'd get 4 payouts that sum to $1000, so $250 per payout and a profit of $150. You bet $100, and profit $150 on a win, so the line is +150.</p><p>Thus, a 60/40 matchup corresponds to a line of -150/+150. The general equation for the logic above is:<br /></p><ul style="text-align: left;"><li><b>favorite: </b>American line = - 100/[( 1/percent - 1)]</li><li><b>underdog: </b>American line = 100*[( 1/percent - 1)]</li></ul><div>Going the other direction:</div><p></p><ul><li><b>favorite: </b>percent = 1/[(100/-American) + 1]</li><li><b>underdog: </b>percent = 1/[(American/100) + 1]</li></ul><div></div><p></p><h4 style="text-align: left;">Real Life</h4><div>It's not quite this easy. The person offering the bets (bookie) needs to make money. Imagine in the above that the person offering the bet wants to make $10 for every $100 bet. How does that change things?</div><div><br /></div><div>Consider bets on A. You make 10 bets on A. A wins 60% of the time, so you should get $1000 back like before except that you pay $10 per bet so you get $900 back. 6 payout that give $900 means $150 per payout and subtracting initial investment means $50 profit. That means you'd bet 100/0.5, or $200 for each $100 profit which means the line is -200.</div><div><br /></div><div>For B...4 payouts gets $900, so $225 payout per win which is $125 profit per win after subtracting initial investment. $125 profit on a $100 bet means +125 is the line.</div><div><br /></div><div>How can you factor out this $10 cost (margin)?</div><div><br /></div><div>You get a line of -200/+125 to start and want to see what the margin is on this. It's actually easy from what we did earlier. Simply convert these lines to the percentage versions, and add them together. Taking these specific numbers:</div><div><ul style="text-align: left;"><li>-200 => 66.67%</li><li>+125 => 44.44%</li><li>sum = 111.11%</li></ul><div>That is, for every $100 that is bet, the bookie gets $11.11 (or in the earlier terms, for every $90 that is bet you pay an additional $10).</div></div><div><br /></div><div>Finally...how do you get the implied chance of each option winning from odds that have the margin factored in like these? Simply divide each percentage by the sum.</div><div><ul style="text-align: left;"><li>favorite: 66.67% / 1.1111 = 60%</li><li>underdog: 44.44% / 1.1111 = 40%</li></ul><div>And we recovered the original odds.</div></div><div><br /></div><div><a href="https://docs.google.com/spreadsheets/d/1heJ4vmXLJVEiGkFBJskJk6NlgSITJOwtzfb-G3PkVMo/edit?usp=sharing">You can play with this in a spreadsheet here if you want.</a></div><div><br /></div><div><br /></div><p></p></div>theboathttp://www.blogger.com/profile/01260139398901806725noreply@blogger.com12