<?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 Mark Goh on Medium]]></title>
        <description><![CDATA[Stories by Mark Goh on Medium]]></description>
        <link>https://medium.com/@gmw172?source=rss-b7e43ec833e8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IUXLmv4B3u4zh1pHhX8C9Q.jpeg</url>
            <title>Stories by Mark Goh on Medium</title>
            <link>https://medium.com/@gmw172?source=rss-b7e43ec833e8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 02:39:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gmw172/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[Demystifying Hierarchical Forecasting]]></title>
            <link>https://medium.com/@gmw172/demystifying-hierarchical-forecasting-6c0dc585ced8?source=rss-b7e43ec833e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c0dc585ced8</guid>
            <category><![CDATA[time-series-forecasting]]></category>
            <category><![CDATA[linear-algebra]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[finance]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Mark Goh]]></dc:creator>
            <pubDate>Fri, 20 Dec 2024 12:36:23 GMT</pubDate>
            <atom:updated>2024-12-20T16:44:10.612Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tEhN_bTSxA4iu7FX.jpg" /><figcaption>Fig 0. Hierarchy in nature</figcaption></figure><p>Ever wondered how to predict future costs across business units? Ever wanted to predict tomorrow’s prices across departments and regions? Ever needed to predict something across multiple levels of category, while still ensuring the total sums up perfectly?</p><p>Often, hierarchical forecasting is a topic less touched upon in time series forecasting. In this article, we can look forward to gaining an intuitive idea of hierarchical forecasting, learning its Python code, and appreciate the theory behind it.</p><h3><strong>The problem</strong></h3><p>We shall stick to a simple example throughout this article. Imagine we want to forecast net cost for the finance department. The finance department has 4 business units, namely procurement (P), financial services (F), quality (Q) and supply chain (SC). After generating forecasts separately for each component (total net cost, P, F, Q &amp; SC), we have 5 separate forecasts. We notice that our data follows some sort of hierarchy, as shown in the figure below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*aNfmZHPZmGFn-sj-M6_vxQ.png" /><figcaption>Fig 1. Hierarchy of our data</figcaption></figure><p>This means that our forecast value for each component need to adhere to a simple rule:</p><p>P + F + Q + SC = Total Net Cost</p><p>Since our forecasts for each component (Total, P, F, Q, SC) are generated independently, it is very unlikely that they will sum up nicely. This is why hierarchical reconciliation is needed — a method to reconcile these incoherent forecasts to ensure they add up properly.</p><blockquote><strong><em>In simple terms, hierarchical forecasting predicts data that can be grouped into different categories at different levels/hierarchy, ensuring they sum up nicely.</em></strong></blockquote><h3><strong>An intuitive understanding</strong></h3><p>There are 3 common methods for hierarchical reconciliation — Top-down, Bottom-up, and minT (minimum trace). First, let’s try to understand them intuitively.</p><h4>Top-down</h4><p>We retain the top level forecast (total net cost), and discard all other forecasts for the lower levels. The algorithm will then assign proportions to split the top level forecast into the respective business units (P, F, Q &amp; SC). This gives us coherent forecasts that sums up to the total net cost. However, this method is statistically biased and hence, its reconciled forecasts may be subpar.</p><h4>Bottom-up</h4><p>In contrast to top-down, the bottom-up method retains the bottom level forecasts (P, F, Q &amp; SC), and discards the top level forecast (total net cost). It then adds the bottom level forecasts up to replace the top level forecast that we discarded. Although this method is statistically unbiased, it would tend to give inaccurate forecasts at the top level, especially if we have many levels in our hierarchy, as error would be compounded for each sum we perform from the bottom to the top.</p><h4>Minimum Trace (minT)</h4><p>Theoretically, this is a more robust method than top-down and bottom-up. The minT algorithm does not discard anything, but rather, adjusts every forecast (all of total net cost, P, F, Q &amp; SC) in a way that minimizes the variance of the reconciled forecast errors, where, the forecast errors are defined as the difference between the reconciled forecast values and the actual values.</p><blockquote>In other words, minT ensures that the difference between the reconciled forecast values and the actual values are not fluctuating wildly (as stable as possible).</blockquote><h3><strong>Coding it out — some points to note</strong></h3><p>There are several libraries that implement hierarchical reconciliation in Python, such as <a href="https://unit8co.github.io/darts/generated_api/darts.dataprocessing.transformers.reconciliation.html?highlight=hierarchical">darts</a> and <a href="https://nixtlaverse.nixtla.io/hierarchicalforecast/index.html"><em>hierarhicalforecast</em></a><em>.</em> These articles — <a href="https://unit8.com/resources/hierarchical-forecast-reconciliation-with-darts/"><em>darts</em></a><em> </em>and <a href="https://forecastegy.com/posts/hierarchical-time-series-forecasting-python/"><em>hierarhicalforecast</em></a> did a great job in detailing the steps to code out a hierarchical reconciliation model, which we shall not elaborate too much here. Instead, let us focus on potential points of confusion.</p><p>Firstly, the pre-reconciled forecasts that we obtain for each component — Total Cost, P, F, Q &amp; SC can be generated using any method of your preference. These forecasts can also be generated separately from the code used to perform hierarchical reconciliation.</p><p>Next, there are certain methods — namely ‘minT wls_var’, ‘minT shrink’ and ‘minT cov’, that requires fitted values of the training data as an input.</p><p>In the context of time series, a fitted value is a forecasted value at time = t using all previous values before time = t. Usually, fitted values are one-step forecasts. For instance, if our training data spans from Jan 2013 to Dec 2023, we will obtain the fitted value for Dec 2023 by using data from Jan 2013 — Nov 2023 to predict it. The fitted value for Nov 2023 will be predicted using data from Jan 2013 — Oct 2023, and so on.</p><p>Usually, the libraries of time series models like ARIMA would come with a functionality to provide fitted values. In the case there isn’t, it would be difficult to predict obtain the fitted value of the data point of the training data since we do not have any previous data before it. Fortunately, the fitted values that are required do not need to span the whole training data set. For instance, it suffices to obtain the fitted values from Jan 2020 to Dec 2023 despite our training data spanning from Jan 2013 to Dec 2023.</p><p>Let’s look at the darts library to showcase an example of using the fitted values.</p><p>The <a href="https://unit8co.github.io/darts/_modules/darts/dataprocessing/transformers/reconciliation.html#MinTReconciliator">‘minT wls_var’ method of the darts library</a> requires fitted values as an input. It states that, ‘<em>the series</em> <em>provided to ``fit()`` contain the forecast residuals (deterministic series).’ </em>In this case, <em>‘forecast residuals’</em> refers to subtracting the fitted values from training data.</p><p>We shall now look at synthetically generated data. Similarly, our training data shall be from Jan 2013 to Dec 2023, and our test data from Jan 2024 to Dec 2024.</p><p>Let our training_data_df be the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*qUw2MD0snhcdXeGDu9jZJQ.png" /><figcaption>Fig 2. training_data_df</figcaption></figure><p>Let our fitted_values_df be the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*vQ-WQOUpOc6ZrMpCM6jSrw.png" /><figcaption>Fig 3. fitted_values_df</figcaption></figure><p>Let our unreconciled_forecasts_df be the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*TtpWHez_O6XbPxt3InJsww.png" /><figcaption>Fig 4. unreconciled_forecasts_df</figcaption></figure><p>We then proceed to subtract fitted_values_df from training_data_df to get forecast_residue_df.</p><p>We then fit on forecast_residue_df, and perform reconciliation (transform) on unreconciled_forecasts_df.</p><pre># Define hierarchy<br>hierarchy = {&quot;Total/P&quot;: [&quot;Total&quot;],<br>             &quot;Total/F&quot;: [&quot;Total&quot;],<br>             &quot;Total/Q&quot;: [&quot;Total&quot;],<br>             &quot;Total/SC&quot;: [&quot;Total&quot;]<br>}<br>reconciler = MinTReconciliator(method = &quot;wls_var&quot;)<br># Getting forecast residues<br>forecast_residue_df = training_data_df.set_index(&quot;Date&quot;) - fitted_values_df.set_index(&quot;Date&quot;)<br># Fit reconciler on forecast residues<br>reconciler = reconciler.fit(TimeSeries.from_dataframe(forecast_residue_df, hierarchy = hierarchy))<br># Reconcile the unreconciled forecasts<br>reconciled_predictions = reconciler.transform(TimeSeries.from_dataframe(unreconciled_forecasts_df, hierarchy = hierarchy))</pre><h3>Deep-diving into theory</h3><p>If you are curious about the math behind hierarchical forecasting, then this section is for you. It would be beautiful to see how linear algebra and optimization techniques underpins the applications in the industry.</p><h4>Notations</h4><p>Apologizing for the change of names, we will now call unreconciled forecasts, <strong>base forecasts. </strong>We will also refer to the level 2 forecasts (P, F, Q, SC in Fig 1) as <strong>bottom-level forecasts</strong> (not to be confused with base forecasts!).</p><p>We shall first define the notations. The base (unreconciled) forecasts, reconciled forecasts and actual values at time t can be represented by 5x1 matrices, with entries containing the respective values for each component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5MVg4ypAPvtkCLdtuPD_IQ.png" /><figcaption>Fig 5. Base forecasts, h-step ahead</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*DI03Rk-5TNpfEGavtaMqwQ.png" /><figcaption>Fig 6. Reconciled forecasts, h-step ahead</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*8FB1HAWWKxF6YUJpouBKNw.png" /><figcaption>Fig 7. Actual values</figcaption></figure><p>The bottom level actual values, which are the values of P, F, Q and SC in actual values, shall be denoted as a 4x1 matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*gpGPAzJuYRcg5sA-YkeQvA.png" /><figcaption>Fig 8. Bottom level actual values</figcaption></figure><p>Since our actual values are supposed to be coherent (sums up nicely), it satisfies this relationship:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/145/1*MnU-2SqtuYYLigpUUkXPTQ.png" /><figcaption>Fig 9.</figcaption></figure><p>The summing matrix S is the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/187/1*0to-p69DLTJGk8-yCFwTDA.png" /><figcaption>Fig 10.</figcaption></figure><p>Then, hierarchical reconciliation, which is the process of making base forecasts coherent, can be represented by this equation,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/181/1*D6CmZIr2GqYriKoQhvAWxQ.png" /><figcaption>Fig 11.</figcaption></figure><p>where P is the reconciliation matrix that maps the base forecasts into bottom-level forecasts, which would be summed up by S. We will touch upon this in the next section, where P will differ depending on the type of reconciliation method used.</p><p>We shall also define the errors.</p><p>The error of the base forecast is subtracting the base forecast from the actuals:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*A63r8AA3oAu7_rhTcC111A.png" /><figcaption>Fig 12. Base forecasts errors</figcaption></figure><p>The error of the reconciled forecast is subtracting the reconciled forecast from the actuals:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*MnU2BPEHv2YjMhGkzaXQuA.png" /><figcaption>Fig 13. Reconciled forecasts errors</figcaption></figure><h4>Reconciliation Methods</h4><p>First, we shall introduce the condition required for the reconciled forecasts to be unbaised. We say that a forecast is unbiased if it is equal to the average of the actual value, on average. Mathematically, we say that our base forecasts are unbiased if:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*riyrxP5BN5uQ6cysYrcr9A.png" /><figcaption>Fig 14.</figcaption></figure><p>We assume that our base forecasts are unbiased, and it follows that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*RZMhbLZVqM6vs5a-kyHjsA.png" /><figcaption>Fig 15.</figcaption></figure><p>Hence, in order for our reconciled forecasts to be unbiased, i.e. :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/202/1*gBO1SP-4cRhDK-rGiyILEw.png" /><figcaption>Fig 16.</figcaption></figure><p>The following condition:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/133/1*sHMB0mLRdABY3SxKUqWjRA.png" /><figcaption>Fig 17. Condition/Constraint for unbiasedness</figcaption></figure><p>must be fulfilled. Note that we cannot impose SP = I since this would not adjust our base forecasts at all.</p><h4>Top-Down Method</h4><p>This method chooses a P that retains only the top level base forecasts and discards the rest. It then splits the top level forecasts based on some proportions determined by the algorithm to get forecasts at the lower levels. In our example,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/181/1*1r4CEBou5JtQugV92uMvtg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*aDPDU3aF9OWdpHv9r7Sz1g.png" /><figcaption>Fig 18. P is the middle matrix on RHS</figcaption></figure><p>Since there is no matrix P in the top-down method that satisfies SPS = S, the top down method is always biased.</p><h4>Bottom-Up Method</h4><p>This method chooses a P that retains only the bottom-level base forecasts and discards the rest. The bottom-level base forecasts would then be summed up to get the forecasts for the rest of the levels. In our example,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*5_m2ZQNjG80WVBOeW8tu6A.png" /><figcaption>Fig 19. P is middle matrix on RHS</figcaption></figure><p>Since SPS = S is satisfied in bottom-up, it is hence an unbiased method.</p><h4>Minimum Trace (minT) method</h4><p>This method chooses a P that adjusts every base forecast in a way such that the variance of the reconciled forecasts error are minimized. We also set a constraint on P such that SPS = S, to ensure unbiasedness. In other words, we have to choose a P such that</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*LCCXQ7LhejJwUo7NTxQD9g.png" /><figcaption>Fig 20. Variance of reconciled forecasts</figcaption></figure><p>is minimized.</p><p>We shall find an expression for the above, in terms of S and P.</p><p>Since we know that</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/229/1*nVyMaDuFdmetfwvcs0xgFA.png" /><figcaption>Fig 21. Base and reconciled errors</figcaption></figure><p>we can say</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*JLpJN4K0bmKmw5FMdfLzgQ.png" /><figcaption>Fig 22.</figcaption></figure><p>since SPS = S.</p><p>Therefore,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*dNVZKg5mVzxoUh46f_Z0BA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/163/1*ianxIlBYSFxrOnY19J_a3Q.png" /><figcaption>Fig 23.</figcaption></figure><p>where W_h is the covariance matrix of <strong>base</strong> forecasts errors (which can be estimated using fitted values, explained later).</p><p>We have now got an explicit expression of the variance of the error of reconciled forecasts. However, we are still not quite done, as we have to find a matrix P that minimizes it.</p><p>Since the variances of the reconciled errors lie in the diagonals of</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/130/1*TM2qiHPI5gJH_X-ble0P-A.png" /><figcaption>Fig 24.</figcaption></figure><p>we have to find a P that minimizes the sum of the diagonals, i.e.,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/168/1*herw86r3_0RCf1ef0AYgag.png" /><figcaption>Fig 25.</figcaption></figure><p>We first start by reformulating the problem using the circular property of trace,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/355/1*OSBk9nCZo7OyebykYHvPog.png" /><figcaption>Fig 26.</figcaption></figure><p>We shall then establish some properties of S ′S.</p><p>We say S ′S is symmetric since,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*p2t2JpsBnaYIa-mMjW18Jg.png" /><figcaption>Fig 27.</figcaption></figure><p>We say S ′S is positive definite, because S can be represented in the following,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/112/1*bNz9djLSSjD_-94XWaGk7A.png" /><figcaption>Fig 28.</figcaption></figure><p>and</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/337/1*N_DWdvqKkZCld6RgZbWuXA.png" /><figcaption>Fig 29.</figcaption></figure><p>Then for all nonzero vector x,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*xtsWYi0MBEGV7_4IvqnDVA.png" /><figcaption>Fig 30.</figcaption></figure><p>Hence, S ′S is positive definite.</p><p>Similarly, PW_hP’ is symmetric because the transpose of it is equals to itself (covariance matrices (W_h) are always symmetric).</p><p>it is positive semi-definite because for all nonzero vector x,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*EVGmWU3dJmKBI3CPxLy3OA.png" /></figure><p>since covariance matrices (W_h) are always positive semi-definite.</p><p>We can then apply the Matrix Trace Inequality (Wang, Kuo, and Hau, 1986) to get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*tW3ULWhfjfgAim32wMNIgA.png" /><figcaption>Fig 31.</figcaption></figure><p>Since all the eigenvalues of S ′S are positive, we can say,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*QYs9xmc_mPtgBeBYniNfXw.png" /><figcaption>Fig 32.</figcaption></figure><p>Our optimization problem now reduces to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*5D97_DlZn1arm7F5AVFnBw.png" /><figcaption>Fig 33.</figcaption></figure><p>Note that the constraint PS = I is a stricter condition than SPS = S. The constraint PS = I is imposed for minT reconciliaiton, but not for bottom-up as it would not work. PS = I also implies SPS = S, which ensures unbiasedness in minT. Since W_h is positive semidefinite (covariance matrix is always symmetric and positive semidefinite), we can let</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/154/1*2kOzbKh9LxqQ5fgInK5CNw.png" /><figcaption>Fig 34.</figcaption></figure><p>We then obtain the well-known least norm minimization problem:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/1*iWbsfsRlc_f9erfGcPPRVg.png" /><figcaption>Fig 35.</figcaption></figure><p>The unique solution to this is the Moore-Penrose pseudoinverse of L,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/172/1*12DHnovypX5bqKipm8nx7A.png" /><figcaption>Fig 36.</figcaption></figure><p>Therefore, substituting it back,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*g-nKQNxGf2SB7LFSodUJxQ.png" /><figcaption>Fig 37.</figcaption></figure><p>The above P matrix would be used for minT reconciliation.</p><h4>Variations of minT</h4><p>The minT reconciliation method has many variations. They are simply different ways of estimating the covariance matrix of the base forecast errors, W_h (of course which we do not know the true value of). One of the variations, ‘wls_var’, was illustrated in the code snippet above Fig 4. It estimates W_h by:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/208/1*laBgpZd1LYe7aXXvA_HoQQ.png" /><figcaption>Fig 38.</figcaption></figure><p>where k &gt; 0 and</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/265/1*3CVNSYBRwTQ2Iftuolel5Q.png" /><figcaption>Fig 39.</figcaption></figure><p>This is where the fitted values of the training data comes in (Fig 3), where ˆet(1) is the <strong>base</strong> forecast residual, obtained by subtracting the <strong>one-step ahead fitted value</strong> from the <strong>actual value</strong>. This method is known as weighted least squares with variance scaling (wls_var) in literature.</p><h3>Conclusion</h3><p>It was nothing short of fascination to see how linear algebra, optimization, statistics as well as coding come together as building blocks even in the working world. Hopefully the next time you come across hierarchical data, you’d cackle and say, ha, this is no longer a mystery!</p><h4>References</h4><blockquote>Shanika L Wickramasuriya, G. A. and Hyndman, R. J. (2017). Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization.</blockquote><blockquote>Wang, SD, Kuo, TS, and Hsu, CF (1986). Trace Bounds on the Solution of the Algebraic Matrix Riccati and Lyapunov Equation. IEEE Transactions on Automatic Control AC-31, 1986.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c0dc585ced8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cyclistic Bike Share Case Study]]></title>
            <link>https://medium.com/@gmw172/google-data-analytics-professional-certificate-capstone-project-cyclistic-bike-share-case-study-8021d5d1b0b?source=rss-b7e43ec833e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/8021d5d1b0b</guid>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Mark Goh]]></dc:creator>
            <pubDate>Tue, 26 Jul 2022 17:08:14 GMT</pubDate>
            <atom:updated>2024-12-21T05:00:59.727Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xk6F2-eyveRiy3HzCSJ21Q.jpeg" /><figcaption>Bike share station (Credit: govloop.com)</figcaption></figure><h3><strong>Introduction</strong></h3><p>This project is part of the Google Data Analytics Professional Certificate. Throughout the course, I learnt about a variety of data analytics tools, such as spreadsheets, SQL, BigQuery, Tableau and R. The classic six phases of data analysis: ‘Ask’, ‘Prepare’, Process’, ‘Analyse’, ‘Share’, and ‘Act’ will form the structure of this article. In this project, I have used <strong>SQL</strong> to extract, clean and analyse data in a <strong>Postgresql</strong> database, and utilised <strong>Tableau</strong> to create visualizations. (Somehow, the data set was too large for BigQuery sandbox to handle, hence the use of Postgresql.)</p><p>Links:</p><ul><li><a href="https://github.com/linkedliszt/Cyclistic-Case-Study/blob/main/sql_code.sql">SQL code</a></li><li><a href="https://public.tableau.com/app/profile/michael.goh/viz/CyclisticCaseStudyVizFinal/Top10Stations_1">Tableau Visualisations</a></li></ul><h3>Context</h3><p>I assume the role of a junior data analyst in a fictional company called Cyclistic, a bikeshare company based in Chicago. Cyclistic features more than 5,800 bicycles and 600 docking stations. Cyclistic has two kinds of users, <strong>casual users</strong> (who only purchases single-ride/full-day passes), and users who are on <strong>annual membership</strong>. The finance analysts found out that annual members are more profitable than casual riders.</p><p>Thus, the marketing director believes that we should focus on converting casual riders into annual members, instead of pushing out a campaign which targets all new-customers.</p><h3>Ask</h3><p>The goal is to design marketing strategies aimed at converting casual riders into annual members. However, given our limited understanding on casual riders and annual members, we have to ask the right questions to figure out the most effective way. The three main questions that will guide future marketing campaigns are:</p><p><strong>1.</strong> <strong>How do annual members and casual riders use Cyclistic bikes differently?</strong></p><p>2. Why would casual riders buy Cyclistic annual memberships?</p><p>3. How can Cyclistic use digital media to influence casual riders to become members?</p><p>For this project, we will focus on the first question (the one in <strong>bold</strong>).</p><h3>Prepare</h3><p>The question we are trying to answer is: How do annual members and casual riders use Cyclistic bikes differently?</p><p>To answer this, I will be analysing historical data for the past 12 months, from June 2021 to May 2022.</p><p>These are internal data within Cyclistic, so I would deem them safe and reliable. <a href="https://divvy-tripdata.s3.amazonaws.com/index.html">Here</a> is the link to the data sets.</p><h3>Process</h3><p>Note: This is the one of the more technical phases where I will thoroughly clean my data using SQL queries. You can view my SQL code <a href="https://github.com/linkedliszt/Cyclistic-Case-Study/blob/main/sql_code.sql">here</a>.</p><h4>Combining Data</h4><p>I downloaded the past 12 months of data as CSV files, and imported them into a Postgresql data base. For ease of analysis, I combined all of them into a single dataset using the UNION function. The resulting table has 5,860,776 rows and 13 columns.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18863f00478db48e4035c5ecd2595564/href">https://medium.com/media/18863f00478db48e4035c5ecd2595564/href</a></iframe><h4>Checking for duplicates</h4><p>I ran a simple query to count the number of distinct <em>ride_id</em> values, and it turned out to be the same value as the number of rows, which was 5,860,776. Thus, we can infer that the <em>ride_id</em> column has no duplicates and it acts as a unique primary key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*Gy0-kVam2OHurZX5hylLJA.png" /></figure><h4>Deleting null values and trimming data</h4><p>Next, I deleted all rows with null values in them. Then, I trimmed each data value which are of <em>text</em> datatype using the TRIM function. This removes any spaces before and after the data value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*DshtDzs64YLF7IIvIPmyFg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*14dP4oizaNeShcgoElXZaA.png" /></figure><h4>Adding new columns</h4><p>I added 4 new columns, <em>day_of_week, ride_length, hour_of_day </em>and<em> month</em> for future analysis.</p><p>I extracted the day of the week, hour of the day and month from the <em>start_at</em> column, and calculated the length of ride by simply subtracting <em>start_at</em> from <em>ended_at</em>. I then converted <em>ride_length</em> to minutes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/1*PII7_QD6N4XJQnccCUoj9Q.png" /></figure><h4>Removing invalid data</h4><p>I found out that there were instances of negative values and unusually large values in <em>ride_length</em>, some even exceeding 5000 minutes. This was clearly unrealistic, as it was unlikely that a user would ride for such a long period of time.</p><p>Due to limited information provided, I created my own criteria: removing all rows which contain <em>ride_length</em> values of less than or equal 0 minutes (≤ 0) and more than or equal to 1440 minutes (≥24 hours).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*17PapPOd_QVLhkatR-YQ4A.png" /></figure><p>As a result, I removed a total of 1,194,617 rows, leaving 4,666,159 rows behind.</p><h3>Analyse &amp; Share</h3><p>This phase involves using SQL queries to create tables with all the variables and values you need, downloading them, and importing them into Tableau for visualisation.</p><p>I have created 5 tables in total, using the GROUP BY clause for aggregation. The first table is the most comprehensive, as shown below. It can be used to analyse the number of rides, average length of rides and the type of bikes used by casual riders and annual members.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*W_zxFxgJ8Y3AT-14A5r4yg.png" /></figure><p>The other 4 tables contain information as follows:</p><ul><li>Table I: starting stations for casual riders</li><li>Table II: starting stations for members</li><li>Table III: ending stations for casual riders</li><li>Table IV: ending stations for members</li></ul><p>These 4 tables are extracted in a similar fashion as in the figure above (view my full code <a href="https://github.com/linkedliszt/Cyclistic-Case-Study/blob/main/sql_code.sql">here</a>).</p><h4>Insights</h4><p>You can refer to my full Tableau visualisation dashboards <a href="https://public.tableau.com/app/profile/michael.goh/viz/CyclisticCaseStudyVizFinal/Top10Stations_1#1">here</a>.</p><p>Let us analyse the number of rides and average length of a ride on each day of the week.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*npmYKV_9-EmPhoG_d1eHZQ.png" /><figcaption>Fig 1. Number of rides and ride length in each day of week</figcaption></figure><p>We can clearly see that casual riders tend to ride more frequently and for longer durations during weekends. In comparison, riders on annual membership ride more during weekdays, and have a drastically shorter ride duration than casual riders.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*s0kR3TSN0E57mUrECmfxeA.png" /><figcaption>Fig 2. Number of rides and ride length in each hour of the day</figcaption></figure><p>Next, we can see that the casual riders (blue line) ride more frequently in the evening, and have shorter ride durations in the wee hours of the morning. In comparison, we can observe two spikes in the number of rides taken by annual member riders; one in the morning(~8am) and one in the evening(~5pm), corresponding to typical office working hours.</p><p>Let us now explore the starting and ending locations of both types of riders.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*1u1P1AH4jmVdCbNvQQGL3Q.png" /><figcaption>Fig 3. Top 10 start locations for casual and annual member riders</figcaption></figure><p>Fig. 3. above depicts the top 10 starting locations for both casual riders (left) and annual members(right).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*U8o5an_rMZdvGl03uOa0bQ.png" /><figcaption>Fig 4. Top 10 end locations for casual and annual member riders</figcaption></figure><p>Fig. 4. above shows the top 10 ending locations for casual riders (left) and annual members (right).</p><p>Notice how casual riders tend to ride along the coastline, while annual member riders travel mostly within the inner city?</p><p>Fig.5 below collates the top 10 most popular starting and ending stations for each user type, distilled into 4 simple bar graphs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*A9aaXrembrfVem6smUwc4w.png" /><figcaption>Fig 5. Top 10 start/end stations for each user type</figcaption></figure><p>It is worth noting that casual riders mostly prefer destinations which are coastal and close to parks, such as Millennium Park and Streeter Dr &amp; Grand Ave (which is near Ohio Street Beach). In comparison, annual members mostly travel among neighbourhood streets such as Kingsbury St.</p><p>Hence, we can likely conclude that casual riders mostly ride for leisure purposes while annual members ride to-and-fro work.</p><p>Now, we will analyse our data from a seasonal perspective.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*zmVw8x3yxybOIDW-AbG0rQ.png" /><figcaption>Fig 5. Number of rides and average ride length each month</figcaption></figure><p>We can observe that there are significantly longer and more frequent rides by casual riders during middle months like June, July and August. Similarly, the number of rides annual member riders take also peak around the middle of the year.</p><p>Thus, we can conclude that both types of riders are more active during summer.</p><p>Lastly, we can also explore the type of bikes that users prefer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*SVRJJRWz-54ccwXpMYV-xQ.png" /><figcaption>Fig. 6 Type of bikes used by riders</figcaption></figure><p>It is interesting to note that the classic bike is the most popular bike type among both casual and member riders, with its usage also peaking during the middle of the year.</p><h4>Summary of insights</h4><ol><li>Casual riders ride more for leisure while annual member riders tend to ride to travel to-and-fro their workplace.</li><li>Casual riders mostly visit parks and places near coastlines, while annual member riders travel more within the city/neighbourhood district.</li><li>Both kinds of riders are more active during summer.</li><li>Both kinds of riders prefer classic bike over the others.</li></ol><h3>Act/Recommendations</h3><ol><li>Target casual customers who are working a full time 9–5 jobs, especially those who live and work near the top 10 most popular start and end destinations</li><li>Launch physical marketing campaigns, subjected to any Covid-19 regulations, at the top few start and end destinations</li><li>Implement discounts for annual rider members who ride during peak hours (to-and-fro work)</li><li>Implement summer discounts packages for annual members only, so as to entice casual riders to convert to membership</li><li>Perhaps, increase the number of classic bikes available since both kinds of riders tend to prefer it more</li></ol><p>Here comes the end, and I hope you have found this informative!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8021d5d1b0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>