<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cameron Porteous on Medium]]></title>
        <description><![CDATA[Stories by Cameron Porteous on Medium]]></description>
        <link>https://medium.com/@cjporteo?source=rss-cdf6f519eef5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DhYaPGzt1_KftUGV5Tgq6g.png</url>
            <title>Stories by Cameron Porteous on Medium</title>
            <link>https://medium.com/@cjporteo?source=rss-cdf6f519eef5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 03:03:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cjporteo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Meta Layoffs — Which departments were impacted the most?]]></title>
            <link>https://medium.com/@cjporteo/meta-layoffs-which-departments-were-impacted-the-most-ca31170b5853?source=rss-cdf6f519eef5------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca31170b5853</guid>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[layoffs]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[meta]]></category>
            <dc:creator><![CDATA[Cameron Porteous]]></dc:creator>
            <pubDate>Sat, 12 Nov 2022 20:57:59 GMT</pubDate>
            <atom:updated>2022-11-12T21:21:53.616Z</atom:updated>
            <content:encoded><![CDATA[<h3>Meta Layoffs — Which departments were impacted the most?</h3><h4>A brief analysis into the November 2022 Meta layoffs, examining which departments and levels of seniority were impacted the most.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mzsEuKhE5leBQ3ba_24RlQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@solomin_d?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Dima Solomin</a> on <a href="https://unsplash.com/s/photos/meta?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Preface</h3><p>Layoffs are a somber and saddening subject. Each datapoint making up this analysis represents someone who is currently in a tough situation, and I hope that everyone affected can find strength and land on their feet.</p><p>For the sake of anonymity of those impacted, I’ve removed all <strong>PII </strong>(Personal Identifiable Information) in the cleaned dataset that I’ve pushed to GitHub (link to repo <a href="https://github.com/cjporteo/meta-layoffs-analysis">here</a>).</p><h3>Background</h3><p>On November 9 2022, Meta announced that it was laying off <strong>11,000 employees</strong>, which translates to ~13% of their total headcount. What wasn’t publicized, however, was the distribution of the layoff across different departments. It was speculated that <strong>Recruiting</strong> and <strong>Business</strong> teams were impacted the most, but what do the actual numbers look like? How much was <strong>Engineering</strong> affected? What about <strong>Data Science</strong> and <strong>Analytics</strong>? How many juniors were affected compared to seniors?</p><p>Following the announcement on Wednesday, a <a href="https://docs.google.com/spreadsheets/d/1edAZoFG25lJpfSpTuJzD9hGijSzRfZ8v84H_Zak4gug">public self-reporting MetaMates Talent Directory</a> (public Google Sheets file) was created and shared on <a href="https://layoffs.fyi/">layoffs.fyi</a>. At time of writing, this dataset contains <strong>over 2000 entries</strong> (nearly 20% of total layoffs, a decent sample to draw from). We can clean and compile this data to help answer some of these questions.</p><h3>Findings</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iWzUvfP39FChZ7PKqpgAHg.png" /><figcaption>Source: <a href="https://docs.google.com/spreadsheets/d/1edAZoFG25lJpfSpTuJzD9hGijSzRfZ8v84H_Zak4gug">MetaMates Talent Directory</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_mMQHtRCoBzFTWiMElOcCQ.png" /><figcaption>Source: <a href="https://docs.google.com/spreadsheets/d/1edAZoFG25lJpfSpTuJzD9hGijSzRfZ8v84H_Zak4gug">MetaMates Talent Directory</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WFn5CF6j4BucXajQ03JIog.png" /><figcaption>Source: <a href="https://docs.google.com/spreadsheets/d/1edAZoFG25lJpfSpTuJzD9hGijSzRfZ8v84H_Zak4gug">MetaMates Talent Directory</a></figcaption></figure><h3>Observations</h3><ul><li><strong>Recruiting</strong> was impacted the most, but <strong>Engineering</strong> isn’t too far behind; together, these 2 departments comprise nearly 50% of all self-reported layoffs</li><li><strong>Engineering</strong> layoffs targeted mainly junior employees, whereas <strong>Product</strong>, <strong>Marketing</strong>, and <strong>Sales</strong> layoffs consisted mainly of senior employees</li><li>The department who will need the most visa support is <strong>Engineering </strong>(46% of laid-off engineers will require visa support)</li><li>Although <strong>Recruiting</strong> makes up the largest percentage of layoffs (26%), only 8% of them will require visa support</li></ul><h3><strong>Assumptions and Limitations</strong></h3><ul><li>This analysis <strong>assumes minimal selection bias</strong> in our sample, which likely isn’t true. Some departments might be more/less likely to report their layoffs in the dataset, so our sample probably isn’t perfectly representative of the true population (all 11,000 layoffs).</li><li>There is <strong>no validation/verification</strong> when contributing to this dataset. This is evidenced by “troll” records like “Rahul Ligma” (row 1723) and “Elon Musk” (row 1710). Luckily, datapoints like this are rare and unlikely to skew our insights.</li></ul><h3>💻 Link to Code (Jupyter Notebook)</h3><p><a href="https://github.com/cjporteo/meta-layoffs-analysis/blob/main/eda.ipynb">meta-layoffs-analysis/eda.ipynb at main · cjporteo/meta-layoffs-analysis</a></p><ul><li>Moderate data cleaning had to be performed to categorize departments, since user-provided input wasn’t validated or sanitized. See code/comments for further details.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca31170b5853" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using machine learning to predict NBA All-Stars, Part 2: Modelling]]></title>
            <link>https://medium.com/data-science/using-machine-learning-to-predict-nba-all-stars-part-2-modelling-a66e6b534998?source=rss-cdf6f519eef5------2</link>
            <guid isPermaLink="false">https://medium.com/p/a66e6b534998</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[nba]]></category>
            <dc:creator><![CDATA[Cameron Porteous]]></dc:creator>
            <pubDate>Thu, 18 Jun 2020 23:06:10 GMT</pubDate>
            <atom:updated>2020-06-21T02:58:27.439Z</atom:updated>
            <content:encoded><![CDATA[<h4>An end-to-end deep dive to quantitatively investigate NBA All-Star selection.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JShBmDHCOOnwn19fL7YzRg.png" /><figcaption>Photo taken by Corleone Brown (Unsplash)</figcaption></figure><h3>Review</h3><p>This is <em>Part 2</em> of a two-part series in which our goal is to apply data science methodology to the NBA All-Star selection process. In <a href="https://medium.com/@cjporteo/using-machine-learning-to-predict-nba-all-stars-part-1-data-collection-9fb94d386530">Part 1</a>, we used web scraping techniques to collect this data using BeautifulSoup and Selenium.</p><p>In this part, we will explore model construction, evaluation, and interpretation. Although building an accurate and precise model is our main agenda, we ultimately want to be able to derive some level of explainability from the system and <em>see what it sees</em>, so to speak.</p><p>I’ll go over many of the key concepts here in this article, but for the sake of brevity, I won’t be covering every single line of code. The full notebook (with detailed comments) can be found <a href="https://github.com/cjporteo/ml-NBA-asg-predictor">here</a> on my GitHub.</p><h3>Preprocessing</h3><h4>Handling Outliers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pfqJw0WCUA86rDY5TZv-SQ.png" /></figure><p>Our first order of business will be to address statistical outliers in the training data. In this case, an outlier is any player whose selection decision was not justified by their performance that season, often resulting in widespread disapproval from fans and media.</p><p>If left in the training set, these records can possibly confuse the model and negatively impact our predictions and insights.</p><p>These players fall into the following two categories (or both):</p><blockquote><strong>A:</strong> the player had a long, illustrious career and was selected in their twilight years as a sort of farewell, despite not playing at what would be considered an All-Star level (e.g. Dirk Nowitzki’s selection for the 2019 ASG — very much a respect/tribute pick)</blockquote><blockquote><strong>B: </strong>the player sat out way too many games in the season and was only selected due to overpowering fan favoritism (e.g. Yao Ming in the 2010–2011 season, who only participated in 5 out of a possible 44 games)</blockquote><p>All-Stars in the training data that match either of these criteria (signaled by an uncharacteristically low PIE score or number of games played) are purged from the training set. Since the dataset is pretty small, this is something we can perform manually — otherwise we would resort to something like <a href="https://en.wikipedia.org/wiki/Outlier#Tukey&#39;s_fences">Tukey outlier detection</a>.</p><p>In total, <strong>13 player-seasons were dropped</strong> — the full list (with rationales) can be found <a href="https://github.com/cjporteo/ml-NBA-asg-predictor">in the notebook</a>.</p><h4><strong>Adjusting game statistics</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fhWGznSG_SikSapZYMwhNQ.png" /></figure><p>By nature, this isn’t a time series forecasting problem, but we still have some features that aren’t stationary with time. This has the potential to distort the model and lead to poorer results if left unchecked.</p><p>Average pace is defined by the number of possessions a team uses per game, and this varies across different NBA eras (e.g. 90 in ‘04 versus 100 in ‘18). Consequently, a player averaging 30ppg in 2018 should be held to the same standard as a player averaging 27ppg in 2004.</p><p>So, we divide all the pace-dependent features (points, rebounds, assists, steals, blocks, turnovers, 3pm) by the league average pace in that season to rectify this issue. This way, we aren’t comparing apples to oranges.</p><p>It’s at this point where we create the <em>Play Pct.</em> feature, by dividing the individual’s games played for the season by the team’s. We also drop all records with less than 7 games played for the season, since these can give rise to extreme values for other game stats, possibly throwing off the model.</p><p>We now have 15 candidate features to use for modelling. As a spoiler, not all of these 15 proved to be helpful predictors, but I’ll list them all here for completeness. Unbolded features were ultimately dropped in the final model.</p><ul><li><strong>Adjusted PTS (adjusted points)</strong></li><li><strong>Adjusted REB (adjusted rebounds)</strong></li><li><strong>Adjusted AST (adjusted assists)</strong></li><li>Adjusted STL (adjusted steals) — dropped</li><li><strong>Adjusted BLK (adjusted blocks)</strong></li><li>Adjusted TOV (adjusted turnovers) — dropped</li><li>Adjusted 3PM (adjusted 3 pointers made) — dropped</li><li><strong>DEFWS (defensive win shares)</strong></li><li>TS% (true shooting percentage) — dropped</li><li><strong>USG% (usage rate)</strong></li><li><strong>PIE (Player Impact Estimator)</strong></li><li><strong>Play Pct.</strong></li><li><strong>Team Conference Rank</strong></li><li><strong>Prior ASG Appearances</strong></li><li>AS Last Year? — dropped</li></ul><h3>Exploratory data analysis</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WpCysMPJmvBCvTZXmbYnrw.jpeg" /></figure><p>Before we begin to apply machine learning algorithms to this problem, it’s important to get to know our data. This involves things like comparing attribute distributions between All-Stars and non-All-Stars, and seeing how different variables interact with each other.</p><p>To look at each feature in isolation, we can use the violinplot method from the seaborn library. I’ll show 2 of the 15 plots, along with the code used to generate them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93b0d96a81630efbcd1473fcdd3eacf8/href">https://medium.com/media/93b0d96a81630efbcd1473fcdd3eacf8/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*74SbsXX-ymofvbk3pBbAbw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*Ike2kLob1iYs0qxOzmVs1g.jpeg" /></figure><p>Something immediately interesting here is that team rank <em>definitely </em>matters. Players who are selected for the All-Star game are more likely to come from a good team than a bad one.</p><p>In theory, this seems reasonable. A “star” player should be expected to be able convert their talent into team success, but this line of thinking can expose us to being overcritical towards a player about things that are out of their control, like teammates and coaching. Navigating this disagreement of individual and team success is the subject of much debate among NBA fans, particularly when trying to agree upon a league MVP.</p><p>Later, we’ll explore this further.</p><p>We can use a pairwise scatter matrix to see how our features interact with each other. For this, we use pairplot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f5ccd1ebae71af04cef262c3a26b2f1/href">https://medium.com/media/8f5ccd1ebae71af04cef262c3a26b2f1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*u4K6O4SJm5bgEcRTVaDisA.png" /></figure><p>Finally, we can use <a href="https://en.wikipedia.org/wiki/Principal_component_analysis">Principal Component Analysis</a> to roughly see how distinct our two classes actually are. We apply PCA to reduce the dimensionality of our feature space from 15 to 2 while retaining as much information and variability as possible. Plotting these 2 principal components uncovers some of the innate structure and class separation within our data.</p><p>We use the pca method from sklearn to achieve this. To make the principal components less arbitrary, we scale our input features to unit variance and remove the mean.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe1ee39a29a4c018fa3d23ac822d91d6/href">https://medium.com/media/fe1ee39a29a4c018fa3d23ac822d91d6/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*U34l_n3IiiOwsnBkNhflxQ.png" /></figure><p>Using just 2 principal components, we were still able to retain over half of the total variability from the 15 original features. From this 2-dimensional projection, the 2 classes appear to be reasonably separated from each other.</p><p>To clarify, we’re only using PCA here for data visualization purposes — we won’t use these principal components for modelling. PCA <em>can </em>be used to increase model training speed, since there are less features, but since our dataset is relatively small, training runtime wont be an issue here.</p><h3>Modelling</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-5brhvxw5tmSU7SCa_hnzA.png" /></figure><h4>Partitioning labeled dataset</h4><p>We will split the labeled dataset into 3 groups: train, validation, and test.</p><p>Training data is what the model will directly use to tune its internal parameters. Validation data will be used to guide this procedure, monitor the learning process, and assist in avoiding overfitting. Then, once the model is frozen, we can turn to the testing data to evaluate the model and calculate different benchmarks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7e848c65086bc67d68a1bd97a78b4b7/href">https://medium.com/media/f7e848c65086bc67d68a1bd97a78b4b7/href</a></iframe><h4>A quick note about SMOTE</h4><p>When dealing with any classification problem, we have to make sure our classes are balanced. If we don’t account for class imbalance, the model will develop an inherent bias towards the majority class — something we definitely want to discourage.</p><p>In the training partition of our dataset, we have <strong>368 All-Stars</strong> and <strong>5409 non-All-Stars</strong>. It follows that a crude blanket strategy labeling every player as a non-All-Star would still achieve <strong>93.6%</strong> accuracy. We obviously want to dissuade the model from behaving like this, so we should balance the 2 classes.</p><p>To do this, we have three choices:</p><ul><li>undersample the majority class (drop data points)</li><li>oversample the minority class (create synthetic data points)</li><li>a combination of undersampling and oversampling</li></ul><p>Performance-wise, there actually isn’t a lot of difference between these techniques, but oversampling is the most common approach in practice.</p><p>For this case, we will employ a method called <a href="https://en.wikipedia.org/wiki/Oversampling_and_undersampling_in_data_analysis#SMOTE">SMOTE</a> — Synthetic Minority Oversampling Technique. SMOTE creates synthetic data points within a class by linearly interpolating between a random data point and a nearby neighbor. This process is repeated until the classes are balanced. We will actually be using a variation of SMOTE called Borderline SMOTE that focuses on oversampling data points near the class border.</p><p>This will hopefully coax the model into discriminating edge cases more precisely.</p><p>It is crucial that we only oversample the training data, and leave the validation and test sets untouched. Oversampling before the split will cause information to leak between our three partitions and lead to a model with inflated accuracy metrics.</p><h4>Tuning XGBoost hyperparameters</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0qxjFj3J5hU9rHO3esM8SA.png" /></figure><p>The machine learning algorithm that we will call upon for this supervised learning problem is XGBoost, an implementation of gradient boosted decision trees. For datasets involving unstructured data, artificial neural networks will generally outperform other algorithms. But for smaller datasets involving tabular data (like this one), decision tree frameworks like XGBoost typically reign supreme. Documentation for XGBoost can be found <a href="https://xgboost.readthedocs.io/en/latest/tutorials/model.html">here</a>.</p><p>XGBoost, as with many other MLA’s, requires several different hyperparameters that will control the learning process, such as learning rate, maximum tree depth, and number of trees. We will apply a simple grid search to find the optimal learning rate, and use <em>early stopping rounds </em>to combat overfitting. For this problem, we set the patience parameter to 10. If the validation AUC score goes 10 epochs without improving, we terminate training on that learning rate and record the highest AUC.</p><blockquote>AUC is a comprehensive performance metric for binary classification problems. AUC stands for area under curve, specifically the <a href="https://en.wikipedia.org/wiki/Receiver_operating_characteristic">ROC</a> (receiver operating characteristic) curve. The ROC curve compares TPR (true positive rate) and FPR (false positive rate) at various threshold settings, illustrating a classifier’s diagnostic ability at different levels of discrimination.</blockquote><blockquote>AUC can assume values in the range [0,1]. An AUC of 1 indicates that the model is able to perfectly discriminate positive and negative classes, while an AUC of 0.5 is what we would expect from a model that makes random guesses — it has no discrimination capacity. An AUC of 0 means that the model is consistently inverting the classes (discriminating perfectly but in the wrong direction).</blockquote><p>We want our model to achieve an AUC as close to 1 as possible.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7cef196cc66c5fbfe88d8bfce299e4d/href">https://medium.com/media/d7cef196cc66c5fbfe88d8bfce299e4d/href</a></iframe><h4>Model performance and evaluation</h4><p>The model does a better job at classifying data points in the training set, since this is the data that is directly learning from. Validation performance gives insight to how well the model will fare with data it has never seen before.</p><p>However, the validation set still affects decisions we make in the model tuning process (via <em>early stopping rounds</em>). When we calculate our classification metrics like F1 Score, we want to use data that was completely isolated during training — this is where the test set comes into play. These metrics will be representative of how the model will generalize to data “in the wild”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*20_Z9ichf78oX3DGrt39QQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*9Oo7cmvISpTCevLk1I_g1g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*GBJzaCQaYE5_4dGBfbbPzw.png" /></figure><p>Running the model on the testing dataset, we obtain the following confusion matrix, ROC curve, and classification metrics:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*YVz6n403QSzFGEw7yAY2lQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*FuwnuyozCRacM2v6KRWydg.png" /></figure><p>The dashed blue line indicates the expected performance of a model with zero predictive ability (random guesses). The red curve illustrates our tuned XGB model’s diagnostic power at various threshold levels. Since our AUC is very close to 1, our model is very strong at differentiating between the two classes.</p><h4>Metrics:</h4><ul><li><em>Accuracy:</em> <strong>0.975151</strong></li><li><em>Precision:</em> <strong>0.784314</strong></li><li><em>Recall:</em> <strong>0.842105</strong></li><li><em>F1 Score:</em> <strong>0.812183</strong></li><li><em>Log. Loss:</em> <strong>0.103545</strong></li><li><em>ROC AUC:</em> <strong>0.989187</strong></li></ul><p>These metrics were calculated using a binary classification threshold of <strong>0.835 </strong>(optimized for maximum F1 Score).</p><h3>All-Star predictions for 2019–2020 season</h3><p>Now that we have a working model, we can apply it to the dataset of players from the current season. The classification schema employed here is <em>slightly </em>different — we’ll select the 12 players from each conference with the highest predicted probabilities of being selected, instead of using our static threshold of 0.835.</p><blockquote><strong>Small detail:</strong> In actuality, the breakdown of positions among these 12 roster spots has a restriction: 6 frontcourt players (F/C), 4 guards (G), and 2 wildcards. However, with these 2 wildcard spots in addition to the ever-increasing fluidity between player positions, this restriction is pretty immaterial. Slotting 12 players into these roles is almost always possible.</blockquote><p>Without further ado, here are the predictions:</p><h4>Eastern Conference:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*jWmoOhhSFbpjzaWxEskctw.png" /></figure><h4>Western Conference:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*wfETzEVl5C-5-EkEXM4pWA.png" /></figure><h4>How well did we do?</h4><p>Since the 2020 All-Star Game has long since passed, we can see how many picks we got right.</p><p>In each conference, we got eleven picks correct, and one pick wrong.</p><p>In the East, our model erroneously picked Zach LaVine as an All-Star, which would have been his first ever selection. Instead, Trae Young was selected. Interestingly, our model really didn’t like Trae’s prospects of being picked, ranking him 21st in the conference — even below the likes of Tobias Harris and Nikola Vucevic. We’ll look into this in the next section.</p><p>Looking at the predictions in the West, the model chose Paul George for a roster spot and missed Chris Paul. Actually, if we look at Chris Paul’s projected probability, the model <strong><em>really</em></strong><em> </em>didn’t think he’d be selected. Again, we’ll explore these decisions in the next step.</p><p>So overall, we got <strong>22/24</strong> picks correct, which is good for <strong>91.67%</strong>. Not terrible, considering we didn’t incorporate things like social media metrics or a player’s narrative for that season.</p><h3>Model interpretation</h3><p>We have a model we’re satisfied with, and it seems to make decently accurate predictions. But so far, the model itself is fairly opaque. We can observe its final output probabilities, but how is the model actually working under the hood? Which features are playing the biggest role in determining the output? Why exactly did the model dislike the All-Star prospects of Chris Paul and Trae Young?</p><p>By nature, some models are easier to directly interpret than others. For example, with linear regression models, each feature’s impact is represented by its beta coefficient. But with more complex models, like XGBoost, it’s not as straightforward.</p><p>Thankfully, well-developed techniques exist to estimate each feature’s impact in models like these.</p><p>The one we’ll use here is <a href="https://shap.readthedocs.io/en/latest/">SHAP values</a>. SHAP (SHapley Additive exPlanations) is “a game theoretic approach to explain the output of any machine learning model”. We will supplement this analysis with PDP (partial dependency plots) to see how exactly different features alter predictions when we hold other inputs constant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SF5bYkI59xGcPa_HD_hrIQ.png" /></figure><p>Not surprisingly, the <strong>PIE</strong> (Player Impact Estimator) score has the greatest average impact on predictions. <strong>Points</strong> come in at a close second, which just goes to show how important scoring is in attaining “star” status in the NBA.</p><p>But the really interesting thing here is the <strong>Team Conference Rank</strong> feature. To NBA fans, its no secret that a player’s team success plays a significant role in how they are perceived, especially when comparing them to other players. If Player X and Player Y both put up identical stats but X’s team is 1st in the conference and Y’s is 13th, it would be difficult to justify the case that Y is the better player. Obviously this is a broad generalization, but the point still stands.</p><p>The fascinating part (at least to me) is the sheer weight that this feature carries on determining All-Star status. Maybe we shouldn’t condemn star players on underachieving teams the way we do when they voice their wishes to play for a better team. Loyalty to a fanbase is important, but should that really take precedence over a player striving for the recognition they undeniably deserve?</p><p>This finding (among others) is further bolstered in the SHAP detail and the partial dependency plots.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q_p0XzF4gKdTysa3VDZf7A.png" /></figure><p>Each player in the testing dataset adds one dot to every row in this plot. The color of the dot denotes whether that player’s attribute was high or low, and the lateral position is how impactful this attribute value was on the predicted probability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*HS0ziGogEHdqzpt7W4Dz4A.png" /></figure><p>The increase in model impact between a PIE score of 10 and 13 is very significant. This range serves as a “hump”, so to speak, in becoming an All-Star.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*g1ARZvqIBQa0Igdg2YXX6Q.png" /></figure><p>All-Star selection probability tapers off pretty quickly as Team Conference Rank gets worse and worse.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Xne9z5h7mCvu7178PWo1Bg.png" /><figcaption>Color represents model impact based off the interaction of PIE and Team Conference Rank.</figcaption></figure><p>The plot above is really telling. Controlling for all other features, the model will handle the following two players roughly the same way:</p><ul><li>a player with a PIE of 20 (e.g. Luka Doncic) and a team rank of 15</li><li>a player with a PIE of 11 (e.g. Devonte Graham) and a team rank of 1</li></ul><p>Of course, these two features aren’t completely independent; putting up a higher PIE should help your team climb the standings, and I think this is the logic that voters and journalists subconsciously cling to when overlooking players on bad teams — “if they truly were a star, their team wouldn’t be in 15th.” I think this thesis is correct, but it has the potential to be over-applied. A lot of snubs in recent memory fall into this category, a prime example being Devin Booker the past few years. (Yes, he participated in the game this year, but wasn’t chosen initially — only as an injury replacement.)</p><p>Going back to our model, this is why Trae Young was ranked so unfavorably in our Eastern Conference predictions. The Atlanta Hawks were ranked dead last in the East, and historically bottom feeders like these very seldom yield All-Stars.</p><p>Trae Young, though, is a very unique case that the model hasn’t accounted for. The Hawks were plagued with injuries in the first half of the season, and John Collins’ 25-game suspension for PED usage didn’t help either. Add all this onto the underlying fact that the Hawks are a young, inexperienced team, and an overwhelming slew of losses seems all but inevitable. Swap out Trae Young with pretty much any other non S-tier star in the league and it’s unlikely they would do much better.</p><p>Trae Young’s All-Star selection this year was met with near-unanimous approval, but for reasons stated above, this is why the model missed him.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*W8Gxutxkq2PIQVi--Pd6Ew.png" /><figcaption>If a player misses extended time in the season, their prospects of getting picked start to decline.</figcaption></figure><h4>Player-specific SHAP force plots</h4><p>We can extend SHAP analysis even further, and investigate model decisions for specific players. By using force plots, we can generate <em>personalized report cards </em>for each player in the league, outlining which aspects of their season either helped or hindered their projected All-Star probability.</p><p>These are most insightful for edge cases — players near the All-Star border where it’s possible to make a case for them either way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5gG-sZFdhYLsUAAYcQXSnw.png" /><figcaption>The Phoenix Suns being 11th in the conference had a significant detrimental impact on Devin Booker’s model output.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5WqREKfKFyw0kMJ5-Uz5ug.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SQjZ9ho-eYjW0fgXHH-UTg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u0G0C6vq6DqPt8G1kIXdKQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xw-QTQfK158NojSKvF6RfQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TktgocAcy0ta5OM3xdvaSg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dHFViWO3usmw_PoxlUhiMg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NRMzJyaw6uWQYsR6IqUAPA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GQIva1DyYbXiBl7vdDxE9g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5KK2zU3oNh743XFlsMh8FA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u9voXozcujsB3ngKZ_igTQ.png" /></figure><h4>Exploring Chris Paul’s unique situation</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KJhHt_XhFs3qDcbOQnzsig.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H4MZNNhB8xaZRlUokeSKGg.png" /></figure><p>There’s no denying that Chris Paul is a great player. While he’s nearing the twilight years of his career, he still consistently adds significant value to any team he suits up for. Many analysts had Paul on their shortlist for the Western Conference All-Star reserves, and <em>they were right </em>— he got a well deserved spot. But his performance and value historically struggle to cross over to the stat sheet — so, on paper, his numbers usually aren’t exceedingly impressive. Since “numbers” are pretty much all that our model sees, it’s not a huge surprise that his selection probability is quite low.</p><p>Furthermore, there was a compelling narrative surrounding Chris Paul this season. When he was traded from the Houston Rockets to the OKC Thunder in exchange for Russell Westbrook in July, everybody instantly wrote off the Thunder’s season. It was expected that the Thunder were entering an era of rebuilding and retooling and would tank for the next few years.</p><p>But that didn’t happen. Instead, Chris Paul led the Thunder to being one of the better teams in the notoriously unforgiving Western Conference and was justly met with widespread praise. Sure, the entire team and organization deserves credit for this accomplishment, but Paul definitely played chief commander in flipping the script for the Thunder’s season.</p><p>The predictive model we developed here will obviously fail to capture underdog narratives like this one. While the model is generally accurate, it’s important to be aware of its shortcomings and identify where things can go wrong.</p><h3>In conclusion…</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UO5qSiDWXPEhB4nUDUH2PQ.png" /></figure><p>This entire project stemmed from mere curiosity. As a longtime NBA fan, the announcement of the All-Star rosters is something I look forward to every season. But throughout the years, some of the picks (and non-picks) have left me scratching my head and wanting answers. This curiosity ultimately reached a breaking point and that’s when I decided to frame this into a data science problem and shed some light on the situation.</p><p>This project served as a great learning experience, personally, particularly when it came to model interpretation and explainability. Applying techniques to break down the “black box” model into something more meaningful and understandable was a very worthwhile endeavor and really enriched the insights it provided. Scraping data from the dynamic webpages also served as an interesting challenge.</p><p>Thank you for taking the time to read this article and follow along as I took this problem from end-to-end. If you have any questions about this project, or suggestions for potential improvements, please let me know in the comment section — I’m always eager to broaden my data science skill set and explore new ideas.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a66e6b534998" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/using-machine-learning-to-predict-nba-all-stars-part-2-modelling-a66e6b534998">Using machine learning to predict NBA All-Stars, Part 2: Modelling</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using machine learning to predict NBA All-Stars, Part 1: Data collection]]></title>
            <link>https://medium.com/data-science/using-machine-learning-to-predict-nba-all-stars-part-1-data-collection-9fb94d386530?source=rss-cdf6f519eef5------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fb94d386530</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-mining]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[nba]]></category>
            <dc:creator><![CDATA[Cameron Porteous]]></dc:creator>
            <pubDate>Thu, 18 Jun 2020 23:03:10 GMT</pubDate>
            <atom:updated>2020-09-17T16:15:51.567Z</atom:updated>
            <content:encoded><![CDATA[<h4>An end-to-end deep dive to quantitatively investigate NBA All-Star selection.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yv42QOC6LLb8D0q3PUy-Dw.png" /><figcaption>Photo taken by Abhishek Chandra (Unsplash)</figcaption></figure><p>What exactly goes into being an NBA All-Star? As a longtime basketball fan, this was a fun and rewarding problem to dive into and explore.</p><p>This article will cover various data scraping techniques I used to construct the historical dataset needed to tackle this problem.</p><h4>Link to see modelling, predictions, and interpretation!</h4><p><strong><em>If you want to cut to the chase and skip the data collection process, head over to </em></strong><a href="https://towardsdatascience.com/using-machine-learning-to-predict-nba-all-stars-part-2-modelling-a66e6b534998?source=friends_link&amp;sk=98afe5974104d088d4d3c99e0d305a38"><strong><em>Part 2: Modelling</em></strong></a><strong><em> to see the predictions!</em></strong></p><h4>Link to the repo!</h4><p><strong><em>Click </em></strong><a href="https://github.com/cjporteo/ml-NBA-asg-predictor"><strong><em>here</em></strong></a><strong><em> to see the full repo (with readme) on my Github!</em></strong></p><h3>Context and motivation</h3><p>The NBA All-Star game is an annual exhibition game that showcases 24 of the league’s premier players. Glitzy and glamorous, the event is the marquee highlight of All-Star Weekend, with A-list celebrities often coming out in droves to watch the event.</p><p>Seeing as the league consists of the top 450 players on the planet, being selected among them as an All-Star is a momentous accolade — an ambitious goal that many players set for themselves even before being drafted. When we evaluate a player’s legacy at the end of their career, we often bring their All-Star selections into the conversation as a way to benchmark their greatness.</p><p>Considering this, in addition to <a href="https://en.wikipedia.org/wiki/NBA_salary_cap#%22Derrick_Rose%22_Rule">potential salary bonus implications,</a> one would hope that the selection system is fair, unbiased, and rooted in some sort of objectivity. Sadly, however, that isn’t quite the case.</p><p>Starters are determined by a mix of fan, player, and media voting, while head coaches select the reserves. This subjective process does its best to select candidates with a track record of elite individual performance in the current season, but it’s not without its flaws. Things like team record and social media presence leak into this decision process, and there is an inevitable outcry following the roster announcements each year from fans complaining that their favorite player has been wrongfully snubbed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*YTMljZ3uZW54H-qVKUuphA.png" /></figure><p>At its core, we might expect selected players to simply be the consensus<em> “24 best players in the league” … </em>but how exactly do we define<em> “best”</em>? Obviously, game stats like points and rebounds are important, but the playing field isn’t always level. To any longtime NBA fan, it’s no secret that team rank plays a big role in how we evaluate players individually; it is <strong>significantly</strong> harder to garner respect and acknowledgement from fans and peers if your team finds itself sitting near the bottom of the standings.</p><p>This negative bias against losing teams has kept many would-be All-Stars from being selected, such as Karl-Anthony Towns in 2016–2017, but this effect is difficult to quantitatively describe. <em>How much narrower are the goal posts for a player on a bad team versus a good one?</em> The same concern arises with injuries, too. Players who miss extended time will find it harder to get a spot, even if they put up big numbers when they actually play. But what qualifies as <em>“extended time”</em>? Where exactly is the imposed cutoff that we use to make this decision?</p><p>In an attempt to lift some of the fog and get an objective grip on what it takes to be an NBA All-Star, I went digging for the relevant data and applied machine learning techniques to concretely model this selection process.</p><h3>The blueprint</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IzByMxFsbhc4lzlq9XPCQA.png" /></figure><p>In machine learning, there is a concept known as “<strong><em>garbage in, garbage out” </em></strong>— meaning a model’s output will only be as good as the data that drives it. If we want predictions that are accurate and insightful, we must ensure we provide a dataset that is consistent, useful, and from a trusted source.</p><p>We’re going to need to scrape several seasons’ worth of historical data, but before we break out any Jupyter notebooks, we need to sit back and think about all the possible features that get taken into consideration for an All-Star selection. For a given player, what information helps us make our decision?</p><p>To start, we’ll need traditional stat averages, like points, rebounds, assists, etc. We can augment these with some advanced statistics, including usage rate, true shooting percentage, defensive win shares, and PIE. PIE stands for <a href="https://stats.nba.com/help/faq/">Player Impact Estimator</a>, <a href="http://www.nba.com/"><em>nba.com</em></a>’s version of PER (Player Efficiency Rating) that serves as a catch-all metric for player performance.</p><p>Team ranking at time of voting is also important, so we’ll use that too. For injuries and missed time, we can compare the individual’s number of games played versus the team’s and extract the appropriate percentage.</p><blockquote>It’s crucial that we only consider the first half of the season.</blockquote><p>Reputation and fan favoritism are important factors to consider, but difficult to implement here. Scraping a player’s social media presence on platforms like Instagram and Twitter is (a) a very involved data wrangling challenge and (b) impossible to reliably scale backwards due to the frenetically changing landscape of social media — plus it didn’t really exist until ~2010.</p><p>In place of this, we can use a cheap proxy for reputation: how many prior All-Star Games this player has been selected for in their career. It will struggle with young and unproven players, but it’s plausible that this feature could do an adequate job of capturing long-term popularity among fans and media.</p><p>We will aim to construct a dataset containing all of these features for every NBA player for each season since 1996, (the earliest season tracked on <a href="http://stats.nba.com/">stats.nba.com</a>). It’s crucial that we only consider the first half of the season, since that’s when the rosters get picked and everything thereafter is irrelevant. I chose a cutoff date of January 21 for each year, since this is roughly when selection decisions begin to be made.</p><p>Our output label will simply denote whether that player got selected (1) for that year’s All-Star Game or not (0). This is our ground truth. Even though the 2020 ASG has already happened and the rosters have long been determined, I’ll omit the current season (2019–20) from the labelled dataset so we can run the model later and compare predictions to the known outcome.</p><p>Once we have a model that we’re happy with, we can peer into the blackbox and quantitatively explore the inner workings of All-Star criteria and how exactly they interact with each other, hopefully putting our curiosity to rest.</p><h3>Constructing the dataset</h3><h4>Sourcing the data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oKw5OfmLGBndOF45jJLmlA.png" /></figure><p><a href="http://stats.nba.com/"><em>Stats.nba.com</em></a> will be our primary resource to satisfy our requirements. It supports functionality to query statistics over custom date ranges, which is imperative for what we want to do. Their webpages are dynamic in nature (AJAX), so scraping from them isn’t trivial — more on this later.</p><p>Data from this resource stretches back to the 1996–97 season, giving us a decent pool of training data for our model to work with.</p><p><strong><em>Note:</em></strong><em> data going back further than this cutoff point definitely exists, and while more data generally produces more accurate models, patterns between a player’s attributes and their selection decision may not be completely time invariant. NBA audiences from two different eras may place different emphases on defense, for example. So, the 1996–97 cutoff is more than admissible here.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*4_xS-9menfZgEyjplcbGMg.png" /><figcaption>The “Traditional” table for players over the first half of the 2018–2019 season. Link to table <a href="https://stats.nba.com/players/traditional/?sort=PTS&amp;dir=-1&amp;Season=2018-19&amp;SeasonType=Regular%20Season&amp;DateTo=01%2F21%2F2019">here</a>.</figcaption></figure><p>Between the <em>Traditional</em>,<em> Advanced, </em>and <em>Defense </em>tables, we can get most of what we need. Next up are team rankings and team games played (which we will need to engineer play percentage). I decided to use team conference rank instead of overall rank since, for discussion purposes, intra-conference standings are generally what people care about the most. All of this can be found in the <em>Teams</em> table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*Av8dODUEFUE2XwVPhGQcEA.png" /><figcaption>The “Teams” table for the East at the halfway mark of the 2018–2019 season. Link to table <a href="https://stats.nba.com/teams/traditional/?sort=W_PCT&amp;dir=-1&amp;Season=2018-19&amp;SeasonType=Regular%20Season&amp;Conference=East&amp;DateTo=01%2F21%2F2019">here</a>.</figcaption></figure><p>Note that in the player table, teams are listed by their shorthand symbol (e.g. CHI), but full names are used in the team table (e.g. Chicago Bulls). When we get to merging these together, we’ll need to create some sort of lookup structure to link them.</p><p>All that’s left is the player’s All-Star selection history, which we need for our output label and our “number of prior All-Star selections” feature. Luckily, this doesn’t depend on any date filter within the season — so we can use another great resource for basketball statistics: <a href="http://basketball-reference.com/">Basketball Reference</a><a href="http://www.basketball-reference.com/">.</a> These webpages are static HTML, meaning scraping from them will be more lightweight and straightforward.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*4aM0rhDBOzYnacPV6VLCMw.png" /><figcaption>The ASG roster (East) in 2015, also listing players who were selected but could not participate due to injury. Link to table <a href="https://www.basketball-reference.com/allstar/NBA_2015.html">here</a>.</figcaption></figure><p>Something of note here is that we shouldn’t neglect players who were selected as All-Stars but couldn’t participate due to injury. Even though they didn’t officially occupy a roster spot, they were chosen initially and will be treated as All-Stars throughout this analysis, along with their replacements.</p><p>League pace has increased pretty steadily over the years as the game has evolved, so we will normalize applicable game statistics by dividing by pace. Average league pace by year is something we can easily get from <a href="https://www.basketball-reference.com/leagues/NBA_stats_per_game.html">Basketball Reference</a>.</p><p>Since there was no All-Star Game in 1999 due to the lockout, we will skip over the 1998–1999 season completely when constructing our dataset.</p><h4>Scraping webpages: BeautifulSoup and Selenium</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MyWGycPmjoGdkD_e2JomsA.png" /></figure><p>Since the webpages at <a href="http://basketball-reference.com/">Basketball Reference</a> are static (meaning the content is embedded directly in its HTML) we can simply use the requests library to load the HTML document.</p><p>Then we will use<strong><em> </em></strong>BeautifulSoup to parse the document and selectively pull out the data we’re interested in. Using the pd.read_html( … ) method, we can load the data into a familiar Pandas DataFrame, and we’re off to the races.</p><p>Even though our labelled dataset is going to start with the 1996–1997 season, we are going to need All-Star roster data stretching a lot further back than this. For example, Hakeem Olajuwon was an active player in ’96, but his first All-Star selection was all the way back in ’85. We initialize our ASG scrape with the year 1970 to ensure there’s absolutely no chance we miss anybody.</p><p>The data structure we build here will be a dictionary of sets, mapping player names to a set of all the years in which they were selected as an All-Star. We pickle this dictionary (serialize it) and export it for future recall.</p><p>For those who are curious, here is the code to make it all happen:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae61edb8eae4293cd7e0159dba8a8442/href">https://medium.com/media/ae61edb8eae4293cd7e0159dba8a8442/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*Zfqe0GvY7J7l6wy-QgyTJw.png" /><figcaption>A preview of this exported data structure.</figcaption></figure><p>For the next step, we need to scrape tabular data from stats.nba.com, and this is where things get a little more complicated. These webpages have a dynamic implementation using <strong>AJAX</strong> (Asynchronous JavaScript and XML), so the displayed tables are nowhere to be found in the HTML document.</p><p>Instead, the webpage will send a request (with accompanying data filters) to an API endpoint and get back a JSON response. We would have to go through the process of identifying the relevant endpoints and then manually curate the data ourselves. This would get incredibly tedious since now we’re talking about deriving advanced statistics for every single player over a date range given nothing but a whole bunch of box scores.</p><p>So we have two real options:</p><ul><li>use a pre-built utility to do this, like <a href="https://pypi.org/project/nba-api/">nba-api,</a> reading all the documentation to learn the functionalities</li><li>use automated browser software, like Selenium WebDriver</li></ul><p>Despite being the more brute-force approach, I opted to use Selenium for the sake of time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*4Jm1US2AGGpXf16LW6sNig.png" /><figcaption>Using Selenium ChromeDriver for web scraping.</figcaption></figure><p>Selenium is a framework that allows us invoke an automated browser (WebDriver), and using the API in Python we can control this browser to navigate to the relevant webpage. After a delay period, once the response has been received from the backend, we can finally load the HTML document (containing the important data) and use BeautifulSoup and Pandas as before.</p><p>There are a few more hurdles I encountered during this scraping, but most of these issues were custodial in nature (like the Charlotte Hornets being referred to as CHH in the pre-Bobcats era and CHA afterwards) and are explained via comments.</p><p>The script will scrape <em>stats.nba.com</em> for every NBA player for each season from 1996 to today. Datasets are processed and merged appropriately, then augmented using the All-Star lookup dictionary we created before. Two datasets are exported as csv’s, one with <strong>all the labelled data</strong> spanning from the 1996–97 season to 2018–19, and the other with <strong>unlabelled data </strong>for the current season.</p><p>Being over 200 lines, the script is a little too lengthy to post here, but can be found <a href="https://github.com/cjporteo/ml-NBA-asg-predictor/blob/master/Data%20Collection/statsnba_full_scrape.py">here</a> in the repo.</p><p>Here’s a peek at the first few rows of the two datasets — with many features hidden from view:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IQJ2-KtYtmMlt1d7rH3G7A.png" /><figcaption>Labelled dataset (9595 records), with many features hidden from view in order to fit here.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*01LwIpJXf41c-FyfAU6lYA.png" /><figcaption>Unlabelled dataset for 2019–2020 (486 records).</figcaption></figure><p>Complete list of <strong>raw</strong> features:</p><ul><li>Year (calendar year that season started in)</li><li>Avg. Pace (average league pace that season)</li><li>PLAYER</li><li>TEAM</li><li>Team Conference Rank</li><li>GP (games played)</li><li>Team GP (team’s games played)</li><li>W (games won — player must have played)</li><li>PTS (points per game)</li><li>REB (rebounds per game)</li><li>AST (assists per game)</li><li>STL (steals per game)</li><li>BLK (blocks per game)</li><li>TOV (turnovers per game)</li><li>TS% (true shooting percentage)</li><li>3PM (3 pointers made per game)</li><li>DEFWS (defensive win shares)</li><li>USG% (usage rate)</li><li>PIE (player impact estimator)</li><li>Prior ASG Appearances</li><li>AS Last Year?</li></ul><p>Formal definitions of these game statistics can be found at <a href="https://stats.nba.com/help/glossary/">https://stats.nba.com/help/glossary/</a>.</p><h3>Coming Up…</h3><p>In <a href="https://towardsdatascience.com/using-machine-learning-to-predict-nba-all-stars-part-2-modelling-a66e6b534998?source=friends_link&amp;sk=98afe5974104d088d4d3c99e0d305a38">Part 2</a> of this deep dive, we will be looking at <em>modelling</em> the NBA all-star selection process using the dataset we constructed here. Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fb94d386530" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/using-machine-learning-to-predict-nba-all-stars-part-1-data-collection-9fb94d386530">Using machine learning to predict NBA All-Stars, Part 1: Data collection</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking an 82-year-old stock trading board game using Monte Carlo simulation]]></title>
            <link>https://medium.com/data-science/cracking-an-82-year-old-stock-trading-board-game-using-monte-carlo-simulation-384c26eeff6c?source=rss-cdf6f519eef5------2</link>
            <guid isPermaLink="false">https://medium.com/p/384c26eeff6c</guid>
            <category><![CDATA[board-games]]></category>
            <category><![CDATA[math]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[simulation]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Cameron Porteous]]></dc:creator>
            <pubDate>Fri, 30 Aug 2019 19:15:20 GMT</pubDate>
            <atom:updated>2021-05-07T06:58:04.629Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9MLg7G5kzGY915yyilVpFA.png" /></figure><p>Board games are fun. Stock trading is fun. Putting them together, we get the 1937 classic from Copp-Clark Publishing, <em>Stock Ticker</em>. The core gameplay is simple: buy and sell stocks from the broker in a fluctuating market and try to finish with more total assets than everyone around the table.</p><p>If you’ve never heard of <em>Stock Ticker</em>, check out eBay, your local board game café, or even your grandmother’s garage. See if you can get your hands on a copy, it’s a personal favorite of mine. Drinking a beer while watching your buddy’s <em>all-eggs-in-one-basket</em> portfolio strategy go belly up is a great way to spend a Friday night.</p><p>In-game stock movements are triggered by dice rolls, and the sheer unpredictability of this admittedly simple market model makes for an exciting and engaging game. Everyone comes up with their own unique strategies with the one common goal of amassing the most cash, and it’s fun to see these strategies pan out (or crash and burn).</p><h3>Rules of the game</h3><p>For the uninitiated, I will briefly lay out the rules of the game. Full rules can be found <a href="https://en.wikipedia.org/wiki/Stock_Ticker">here</a>.</p><ul><li>there are <em>six</em> stocks, functionally identical: Gold, Silver, Bonds, Oil, Industrial, and Grain</li><li>stocks begin at $1 per share, and you have $5000 starting capital to work with</li><li>three dice are rolled per turn to decide stock movement; Die 1 selects the stock, Die 2 decides up/down/dividend, Die 3 picks the magnitude of the movement</li><li>stock values move by ±$0.05/$0.10/$0.20 and dividends are distributed as 5%/10%/20% of number of shares</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*kdvULEilRukJBLMBGb_AwQ.jpeg" /></figure><ul><li>dividends are only paid when a stock is at <strong><em>par </em></strong>($1.00) or above</li><li>if a stock dips to $0.00, all players immediately lose their investment in that stock and it resets to $1.00</li><li>if a stock reaches $2.00, the stock splits and all shareholders double their shares and the stock resets to $1.00</li><li>transactions only occur during the trading phase at the end of seven rounds around the table; there is no buying/selling limit and no transaction fee</li><li>a time limit for the game is established ahead of time, the game ends after a trading phase occurs past the decided end time (we usually play for ~90 minutes)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1LQt3fUOBd24__D0iQeMzA.jpeg" /><figcaption>Photo credits: Scott Graham (Unsplash)</figcaption></figure><h3>Strategy</h3><p>There are a multitude of approaches to consider here. Fundamental risk management instinct tells us that splitting up our capital into as many different stocks as possible will minimize risk, a concept known as<strong> portfolio diversification.</strong></p><p>But what should the entrance and exit points be for these stocks? When do we buy? When do we sell? Which stock values carry the highest expected return after factoring in dividends? Obviously stocks at par or above are preferred since tapping into dividends will boost our return, but how far above $1.00 should we look to buy at? $1.10? $1.40?</p><blockquote>My gut feeling for this entry point was somewhere around $1.20</blockquote><p>Ideally, I wanted to pick an entry point that’s safely above par, so it’s unlikely that it will dip below $1.00 over the course of a round. I wanted to ensure a high probability that no dividends for the stock will be sacrificed in the event that they roll.</p><p>But I also didn’t want the entry point to be too high. Stock value moments determined by the dice aren’t proportional to the stock’s value — they are constant. A +$0.20 boost to share value will give a $1.60 stock <strong>12.5%</strong> ROI. But, that same +$0.20 boost would give a $1.00 stock <strong>20%</strong> ROI. The lower the price, the higher the potential ROI from stock movement.</p><p>Taking these factors into account, I wanted to strike a balance somewhere in the middle, and my gut feeling for this entry point was somewhere around $1.20. The stock could survive even a -$0.20 value hit and still pay dividends, and it’s ROI prospects still have significant upside.</p><p>However, being a curious and pedantic statistician, I wanted to put my intuition to the test. I tried Googling to see if anyone had already done something like this, but to no avail. Turns out conducting stochastic modelling for a board game from the 1930’s isn’t a very hot ticket. I was going into uncharted waters.</p><p>Back-of-the-envelope number crunching wouldn’t be good enough, though. Even though this market model is crude and elementary, there are too many stochastic factors at play to work out all the statistics by hand. Instead, I turned to <strong>Monte Carlo simulation</strong> to compute the expected payoffs and variances of different strategies.</p><h3>Simulation implementation</h3><p>This investigation was conducted in Python — full source code can be found on my GitHub <a href="https://github.com/cjporteo/stockticker-monte-carlo">here</a>.</p><p>The idea was to simulate hundreds of thousands of rounds for every single stock value that a commodity can take on at the start of a trading phase ($0.05, $0.10, … , $1.90, $1.95).</p><p>We can then take these simulation outcomes and compile them into statistics to quantify risk levels. This forms the basis of Monte Carlo simulation. Statistics of interest here are <strong>expected value</strong> and <strong>sample standard deviation</strong>. How much will this stock net me in returns, and how risky is it?</p><p>Parameters for the simulation are:</p><ul><li><strong>total value of assets</strong> at the start of the trading phase</li><li><strong>number of players</strong> at the table</li><li><strong>number of rounds</strong> around the table before the next trading phase</li></ul><p>Stock movements and dividend payments will then be simulated for <strong>players</strong>×<strong>rounds </strong>dice rolls for all possible initial values, assuming the player sinks <em>all </em>of their capital into the stock. We make this assumption to greatly simplify the simulation complexity. This will give us all the important information we need, we can apply diversification heuristics later to tune expected payoff and variance to a specific risk tolerance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*oAxXvwnpDQmdVTCCNJix4Q.jpeg" /><figcaption>Code Snippet From the Simulation</figcaption></figure><p>Simulations are serialized as <a href="https://docs.python.org/3/library/pickle.html"><em>pickles</em></a><em> </em>and stored locally to facilitate easy extension of the sim. Simulations can be done in chunks and will always be saved.</p><p>All rules of the game are fully incorporated in the model, including dividend payment, stock crash, and stock splitting.</p><p>The outcomes of this stochastic process are recorded for analysis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bMBLYosulP4qc1_AB2jzAA.jpeg" /><figcaption>Photo credits: Agence Olloweb (Unsplash)</figcaption></figure><h3>The results</h3><p>After about 12,000,000 simulations (three minutes of computation), our curiosity can be laid to rest. Cash was set as $5000 (the amount you have at the start of the game). Players were set to 4, and rounds 7.</p><p>Initial results were pretty intriguing. Going into this, I knew the $0.05 stock would have a pretty sweet expected payoff. With shares being so cheap, a little capital can go a long way. All it takes is a simple +$0.20 increase to <strong>quintuple</strong> your initial investment. However, this strategy carries a lot of baggage. One bad roll and you can kiss the entire investment goodbye. There is extreme variance with this strategy.</p><p>Here is the plot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/763/1*KmzK0J5B5ONPaeYMW7P2Eg.jpeg" /></figure><p>Variance dramatically decreases as initial stock price increases, which makes sense for reasons discussed earlier. In the &gt;$1.65 range, though, it starts to taper upwards again. Again, this aligns with what we expect. As the stock approaches $2.00, the likelihood of it splitting increases. A split brings the stock back to par, a danger zone, so to speak.</p><p>Expected payoff spikes significantly at $1.00, since the probability of collecting dividends makes a jump here.</p><p>Let’s take a closer look at what’s actually happening around the center of this plot, the high variance of the cheap stocks is skewing our view quite a bit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*pnr-WXUTKVJkNs-z9oycrQ.jpeg" /></figure><p>What this is telling us is that if we sink <em>all </em>$5000 into a $1.20 stock, our expected profit is $701.75 with a standard deviation of $1241.51. So ideally, we’re after a sweet spot between expected value and standard deviation. Anything in the $1.15 to $1.40 range is pretty decent, but if I had to pick one risk profile as ideal, I would choose the $1.30 stock.</p><p>Here’s the full numerical dump from this simulation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*zPp_t7mbvz4YFWtY-cMQgQ.jpeg" /><figcaption><strong>S</strong> = initial price, <strong>n</strong> = simulations,<strong> D</strong> = dividend yield, <strong>H</strong> = final holdings, <strong>P</strong> = D + H + (-initial cash) = profit</figcaption></figure><h3>So what’s the optimal strategy?</h3><p>Based on the results from this investigation, at the start of every trading phase, attention should be focused on any stocks that happen to be in the $1.15 to $1.40 range. If you own any stock that lies outside of this range, it’s probably advisable to liquidate it entirely and dump the capital into one of these better options, if available. Keep in mind that taking time to retool all your assets like this every single round is probably going to irritate your friends, but hey, anything to win, right?</p><blockquote>Attention should be focused on stocks that lie in the $1.15 to $1.40 range</blockquote><p>If multiple stocks lie in this sweet spot, diversification should be employed to mitigate risk for free with no consequence in expected profit. For example, at the start of the game it is a good idea to put $1000 into four of the stocks and $500 into the other two. You might not get off to many early leads this way, but you’ll be in a great position to capitalize on the state of the board at the start of the first trading phase.</p><p>If you want to boost expected profit at the cost of extra variance, sinking a small portion of your capital into cheap stocks is a great way to achieve this. For example, if the end of the game is nearing and you’re gauging that you aren’t in first place, pick up some stocks at $0.10 or $0.15 for a round and try and get lucky. Doing this in a large volume, though, is extremely unwise. Once in a blue moon, you’ll become the Warren Buffet of the table, but most times you’ll just end up with an empty wallet feeling sorry for yourself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_E0hCUu29ufmJe2wdNkRzQ.jpeg" /><figcaption>Photo credits: Brian Suman (Unsplash)</figcaption></figure><h3>In conclusion</h3><p>Was all this analysis really necessary? Were these results wildly different from what solid mathematical instinct would provide? Not really, but that’s not the point.</p><p>Statistics is all about putting hypotheses and suspicions to the test — uncovering the truth in an objective and undeniable way. There’s something so satisfying about seeing all the randomness involved with these simulations collapse into a nice, pretty pattern.</p><p>Happy board gaming!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=384c26eeff6c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/cracking-an-82-year-old-stock-trading-board-game-using-monte-carlo-simulation-384c26eeff6c">Cracking an 82-year-old stock trading board game using Monte Carlo simulation</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>