<?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 Vasantha Kumar V on Medium]]></title>
        <description><![CDATA[Stories by Vasantha Kumar V on Medium]]></description>
        <link>https://medium.com/@vasanth.analyst.freelancer?source=rss-13308d98a003------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*S6UB1yKEYwp3KfXKNzk_Mg.jpeg</url>
            <title>Stories by Vasantha Kumar V on Medium</title>
            <link>https://medium.com/@vasanth.analyst.freelancer?source=rss-13308d98a003------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 15:41:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vasanth.analyst.freelancer/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[From Theory to Practice: Deepening Regression Knowledge (Part-2)]]></title>
            <link>https://medium.com/@vasanth.analyst.freelancer/from-theory-to-practice-deepening-regression-knowledge-part-2-326aa96460d8?source=rss-13308d98a003------2</link>
            <guid isPermaLink="false">https://medium.com/p/326aa96460d8</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Vasantha Kumar V]]></dc:creator>
            <pubDate>Sat, 15 Jul 2023 09:30:46 GMT</pubDate>
            <atom:updated>2023-07-15T15:11:54.330Z</atom:updated>
            <content:encoded><![CDATA[<p>If you haven’t read Part-1 about working of regression, you can read it from <a href="https://medium.com/@vasanth.analyst.freelancer/theoretical-understanding-of-regression-part-1-109c0b2a1955">here</a>.</p><p><strong>Table of Contents :</strong></p><ol><li>Assumptions of Linear Regression</li><li>Error metrics</li></ol><h4><strong>1) Assumptions of Linear Regression :</strong></h4><p>Inherently, Linear Regression makes set of assumptions before kickstarting. It’s not like LR won’t work if these assumptions are violated. It’s just that the results would be inaccurate. Now, let’s see those assumptions in detail :</p><h4>a) Linearity :</h4><p>Yes, Just like its name, LR would expect the relationship between Target and Input variables to be linear.</p><p>What if it gets violated ?</p><ol><li>Accuracy (R2) would be pretty low</li><li>Coefficients are un-reliable</li><li>Summary stats (Confidence Interval and p-value) would also be un-reliable</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*fCRPnIlWZS7BeMKF5jyY7g.gif" /><figcaption>Fig 1 : LR fitting on random data point</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*JS3Kp0uG1_Tubwgy4veaoA.gif" /><figcaption>Fig 2 : LR fitting on Linear data point</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*OF1S4Eqy6ddXvumppcYD6A.gif" /><figcaption>Fig 3 : LR fitting on Polynomial data point</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*T5RjVTuKKxoceYeDOhcfiw.gif" /><figcaption>Fig 4 : LR fitting on sinusoidal data point</figcaption></figure><p>How to validate this in data ?</p><ol><li>Using Vizual plots (Scatter) : Similar to above plots, we can visualize, and try to observe patter. This method would become more complex, as number of variables increase, as we have to go through every plot and keep reminder of it</li><li>Correlation Coefficient : Correlation of Non-linear data point would be less/poor than linear data point. This method won’t hold good for one-sided non-linear exponential functions like above graph. While this can be a starting point, but cannot be a sole decider</li><li>Residual Plot : For a perfect linear relation data, fitted regression residual plot against predicted value should not show any pattern. It should be random, If its showing any non-linear patters, it means the assumption is violated</li><li>Test Statistics : There are few hypothesis test that exist to understand linearity assumptions. Commonly used tests are Rainbow test and Ramsey RESET (Regression Specification Error Test)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*unGdtM5Xk4jJlNvbcFMN5Q.gif" /><figcaption>Fig 5 : Error adjustment as per LR fitting over Random data</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8bPGjGpsDgf7JCpXd19uiw.gif" /><figcaption>Fig 6 : Error adjustment as per LR fitting over Linear data</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xZYPr0R-EzhGlukB5YmHfg.gif" /><figcaption>Fig 7 : Error adjustment as per LR fitting over Polynomial data</figcaption></figure><p>How to fix it ?</p><ol><li>Transformation : We can use mathematical transformation like Log, Square root, Inverse, etc depending on Non-linear that exist in the data. There are some automated tools that will kinda solve this by reducing variance in data by transforming it into normal distribution like Box-Cox, Yeo-Johnson Transformation, etc</li><li>Non-linear models : using Non-linear models, underlying pattern in data can by captured better comparing linear models. We can try polynomial regressions</li><li>Adding Interaction terms : There would be few terms, where instead of adding it as individual terms, adding it as interaction terms would convert relationship into linear and also solve multi-collinear problem</li></ol><h4>b) Independence of error :</h4><p>There should not exist any auto-correlation between errors, which means, there should not be any un-explained variance in error because of dependencies in target variable.</p><p>What if it gets violated ?</p><ol><li>If we couldn’t explain it through IDV, it’ll be evident in error. Poor model accuracy</li><li>Coefficients and summary stats are un-reliable</li></ol><p>How to validate this in data ?</p><ol><li>Plots : By plotting error against corresponding time or order, if there happens any correlation between error, or any seasonal patterns, then it means we’ve violated the rule</li><li>ACF : Using Auto Correlation function, one can identify dependencies on error</li><li>Hypothesis test : Durbin-Watson test can be used to identify auto-correlation that exist in error based on Null and Alternate hypothesis assumption</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*93PtVNrCZz4QApWiN9JaJg.gif" /><figcaption>Fig 8 : Clear Dependency of Error based on Sinusoidal relation</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8lKJ-GKFqyCWtaIVbSoBTg.gif" /><figcaption>Fig 9 : PACF of Residuals from Sinusoidal function</figcaption></figure><p>It’s clearly evident from above graph that, there exist highly auto-correlated errors, which we need to fix</p><p>How to fix it ?</p><ol><li>Adding Lagged variables : Auto-correlation in errors would typically happen in Time series data only, so adding lagged value of DV/IDV would help prevent this</li><li>Adding AR terms : Adding Auto-Regressive terms like Decomposed MA, etc as IDV would help fix the auto-correlation in errors</li><li>Using TS model : Using more appropriate Time series models like ARIMA can handle it better than Regressor model, so using optimum model would solve this’</li><li>Transformation : Sometime, doing First-Order differentiation or Log transform as well would reduce auto-correlation in errors</li></ol><h4>c) No Heteroscedasticity :</h4><p>There should be only Homoscedasticity relationship between IDV and Residual, which means, the error should vary equally across different values of IDV</p><p>What if it gets violated ?</p><ol><li>Model is unstable, as diff variance at diff level would make it harder to converge</li><li>Unreliable estimates and summary stats</li><li>Poor accuracy</li></ol><p>How to validate this in data ?</p><ol><li>Plots : Plotting Scatter between Residual and IDV and observing non-linear pattern at different value of x</li><li>Variance ratio : Finding Variance of Error ratio over different value of x. A value significantly deviating from 1 means heteroscedasticity</li><li>Hypothesis test : White test can be used to find heteroscedasticity in data. It works by regressing square of residuals against Higher Order IDV and validating its test statistics</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PI9BI4bBUqAvBlwW3eTHyA.gif" /><figcaption>Fig 10 : Error pattern with regression on Heteroscedasticity data</figcaption></figure><p>How to fix it ?</p><ol><li>Transformation : Applying Suitable Transformation based on type of Variance change would reduce heteroscedasticity</li><li>WLS methods : Using Weighted Least Square method instead of OLS would assign different weight based on error variance and model accordingly</li><li>Adding Explanatory variable : Adding necessary explanatory variable about heteroscedasticity would capture the effects properly</li><li>Splitting data : Splitting data into multiple groups based on error variance and fitting one regressor per group would capture the pattern well</li></ol><h4>d) No Multi-Collinearity :</h4><p>One another assumption in LR is that the IDV should not be correlated to each other</p><p>What if it gets violated ?</p><ol><li>Model is unstable, as diff variance at diff level would make it harder to converge</li><li>Unreliable estimates and summary stats</li><li>Poor accuracy</li></ol><p>How to validate this in data ?</p><ol><li>Plots : By plotting scatter between IDVs, we can identify correlated variables</li><li>Correlation Matrix : Creating N X N correlation matrix where N represents number of features. Values closer to +1 or -1 represents highly correlated variables.</li><li>VIF : Variance Inflation Factor can be used to find Multicollinear variables. It works by regressing each IDV against each other IDV and computes VIF with formula as 1/(1-R²). Higher value of VIF (Typically &gt; 5 or 10, represents Multicollinear)</li></ol><p>How to fix it ?</p><ol><li>Remove variables : We can remove one or more of Correlated variables</li><li>Combine variables : Instead of simply removing, we can combine all those features and create new based any any mathematical form like addition, multiplication, etc based on method which preserve most variance</li><li>Regularization : By using either L1 or L2 regularization, one can choose to suppress effect of multi-collinear variables</li><li>DRA : By using suitable dimensional reduction algos like PCA, LDA, etc, one can reduce number of collinear variables by preserving maximum variance</li></ol><h4>e) Normality of Errors :</h4><p>Regression assumes the distribution of error to be normal to compute most of its summary stats</p><p>What if it gets violated ?</p><ol><li>Unreliable estimates and summary stats like Confidence Interval, p-value</li></ol><p>How to validate this in data ?</p><ol><li>Plots : Plotting Histogram of Error would help understand distribution</li><li>Q-Q Plot : Plotting Theoretical Quantile against Quantiles of values from data would tell us the distribution of data</li><li>Hypothesis test : Shapiro Wilk test can be used to find normality of data. It works by comparing SSE against Shapiro coefficients with either of normality of data</li></ol><p>How to fix it ?</p><ol><li>Transformation : Applying suitable transformation based on type of distribution can help data to bring to normal distribution</li><li>Non-Normal regressors : By using regressors that don’t have normal assumption, proper pattern in underlying data can be learned. Commonly used non-normal regressors are : M-estimators, GLM based regressors</li><li>Bootstrap based inference : By using bootstrapping technique, one can get robust estimate statistics, even when distribution is not normal</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJAkdt7NGTHbAOCrTzDRUw.gif" /><figcaption>Fig 11 : Right skewed error example</figcaption></figure><h4>2) Error Metrics :</h4><p>Regression uses error metrics inherently in two areas : 1) While iterating through model and 2) While creating summary statistics and reporting</p><p>There are 5 commonly used error metrics and using each metric at different places would make regressor react differently while tuning. Let’s go through one by one.</p><h4>a) MSE :</h4><p>Mean Squared error allocates more weight to larger error, which will make it a better fit model provided no outliers</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*DRf_fhq12qSjj60l43WK0A.png" /><figcaption>Fig 12 : Mean Squared Error Formula</figcaption></figure><p>Where it can’t be used ?</p><ol><li>Since we are squaring the residual, if we got an Outlier and using MSE would allocate more weight to that and tilt the line towards it. In this case, using MSE wouldn’t be ideal</li><li>Since, we are squaring it. Interpreting Error would be harder as it is in different scale comparing Target variable</li></ol><h4>b) RMSE :</h4><p>This is just the rooted version of MSE. Since it is in the same scale as residual (without any squaring), interpreting this number would be straightforward, That is when RMSE can be used. Otherwise, it also has the same limitation to outlier as MSE</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*FevJqHEsrJV34q_OfJiQzg.png" /><figcaption>Fig 13 : Root mean Squared error formula</figcaption></figure><h4>c) MAE :</h4><p>MAE is less prone to outlier comparing MSE, as we are giving equal weight to all error terms</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*nejglhavg6OgXPjp5HrfGg.png" /><figcaption>Fig 14 : Mean absolute error formula</figcaption></figure><p>Where it can’t be used ?</p><ol><li>Places where penalizing higher error should be priority, MAE can’t be used, as its using equal weight across all</li></ol><h4>d) R-Squared :</h4><p>R-Squared is more of an inference metric which explains how good the regressor can explain the variance in the data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*2VfzTsEdFEIh9q6rbfwO4Q.png" /><figcaption>Fig 15 : R-Squared formula</figcaption></figure><p>Limitations :</p><ol><li>In real world, where we’d be having lot of IDVs to explain DV, adding more un-correlated variables would make the model prone to Overfitting. But as per R-Squared, as we increase the number of features, R-Squared can either remain the same or increase, which makes it harder for problems with more features</li></ol><h4>e) Adjusted R-Squared :</h4><p>Adjusted R-Squared was introduced to avoid the limitations in R-Squared with below formula :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*2oTWYMMQ7O63bR3WtKSV6Q.png" /><figcaption>Fig 16 : Adjusted R-squared formula</figcaption></figure><p>Let’s say we got R-Squared of 0.6 with 900 data points and 35 features, and lets assume we got 0.6 R-Squared with 37 features, adjusted R-Squared would change from 0.583 to 0.582</p><p>Let’s visualize how error metrics are reacting to different variance of data points :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*2LV7pKEIoZtxYhEcgbWD3g.gif" /><figcaption>Fig 17 : Error metrics reaction to Linear data</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*OY4m9nIpagE030kZVjEaLg.gif" /><figcaption>Fig 18 : Error metric reaction to linear data with Outliers</figcaption></figure><p>As we can observe from the above graph of Data with Outlier that MAE decreased till a point and then starts to increase, while that is not the case with MSE, which will try to tilt the line to fit the Outlier. So MAE would be better in this case.</p><p>We’ve seen Assumptions of Linear regression and error metrics. Codes used to generate the graphs can be found <a href="https://github.com/imironnman/blogs">here</a></p><p>In the next part, we’d be seeing regularization techniques in detail along with Interpreting OLS Model summary stats.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=326aa96460d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Theoretical understanding of Regression (Part-1)]]></title>
            <link>https://medium.com/@vasanth.analyst.freelancer/theoretical-understanding-of-regression-part-1-109c0b2a1955?source=rss-13308d98a003------2</link>
            <guid isPermaLink="false">https://medium.com/p/109c0b2a1955</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[visualization]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Vasantha Kumar V]]></dc:creator>
            <pubDate>Thu, 13 Jul 2023 17:13:07 GMT</pubDate>
            <atom:updated>2023-07-15T09:31:08.678Z</atom:updated>
            <content:encoded><![CDATA[<h3>From Theory to Practice: Deepening Regression Knowledge (Part-1)</h3><p><strong>Table of Contents :</strong></p><ol><li>Definition</li><li>Types of Regression</li><li>Linear Regression Overview</li><li>Working and Iteration process</li><li>Sample data and Viz best fitting line</li></ol><p><strong>Definition</strong></p><p>Regression is a mathematical notion to understand relationship between variables, where the predictor variable is continuous.</p><p>Example 1: ETA Prediction on Swiggy / Zomato Orders (Against distance, traffic data)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*_orPBKvFPTF1yazBYncSXg.png" /><figcaption>Fig 1 : Basic ETA estimator</figcaption></figure><p><strong>Types of Regression :</strong></p><ol><li>Simple Linear Regression</li><li>Multinomial Linear Regression</li><li>Non-Linear Regression</li></ol><p>In this post, we’d be talking about Simple Linear regression on its math, assumptions, objective function behind it.</p><p><strong>Linear Regression :</strong></p><p>Linear Regression is one of the simplest algorithm yet with wider applications. Even after the development of more sophisticated algorithms, regression is still being sought after because of its explain ability and flexibility as per business constraints.</p><p>Lets talk about Simple Linear Regression. Just like its name, this is a simple concept, where we’d be having only one explanatory variable against target variable. It can be represented as :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*xQC-Kl5dvWSZzccYwxVkzA.png" /><figcaption>Fig 2 : SLR Estimator</figcaption></figure><p><strong>Working of Regression:</strong></p><p>Let’s now construct a random linear data points and try to understand how LR works :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*Xfq6q9LByzXFhZ4OHuIssg.png" /><figcaption>Fig 3 : Randomly generated data</figcaption></figure><p>Entire objective of regression with this data is to identify optimal value for m (slope) and c (constant), so that we’ll get minimal error. This can be done by 2 methods (Gradient Descent and Least Square (OLS, WLS) methods).</p><p>I’ll explain based on Gradient Descent on this article, We can discuss Least Square in later part of article series. Let’s first understand what Gradient Descent is :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*Vvzu-5G8ypG5Ab3Lh4Rx2w.png" /><figcaption>Fig 4: Gradient Descent definition</figcaption></figure><p><strong>Iteration Process :</strong></p><p>a) Initially Regressor will assign random value of slope and constant to the equation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*xlgg3qjGPswQXSviDkAAlA.png" /></figure><p>b) Now, it’ll predict Y based on initialized m and c with regressor equation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*Y237H9w3YwBNuNzNit2JPA.png" /></figure><p>c) Now we know the actual and predicted Y. Regressor have to find error with existing slope and constant and tune it in such a way to reduce the error. To do this process, Regressor incorporates Gradient Descent Algorithm.</p><p>Let’s define the Cost function first (I’m using MSE for simplicity, but we can use any other functions like MAE, etc) :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*Md7bi4e9kP_mftJdjRk_yw.png" /><figcaption>Fig 5 : Mean Squared Error formula</figcaption></figure><p>As I already mentioned, Gradient descent will try to minimize above Cost function by tuning parameters. Since in order to reduce the error downwards, we need to take derivative of Cost function with respect to tuning parameters to understand direction of parameter search.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*TXG_r5N-WIWmuGgIG6tQGw.png" /><figcaption>Fig 6 : Partial derivative of MSE with slope</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/337/1*8F6Jh3_zNaaothT38bL2-A.png" /><figcaption>Fig 7 : Partial derivative of MSE with constant</figcaption></figure><p>Now, let’s see formula to update m and c :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*eGlYNndeamZR6giOhKUyAg.png" /><figcaption>Fig 8 : Slope updating formula</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*umQIBl-60gfrT-nZar0jHA.png" /><figcaption>Fig 9 : Constant updating formula</figcaption></figure><p>Since we got all necessary formulas and data, let’s see how parameters are getting updated :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*wWwPCCOZZNFR2TGBVPdqNg.png" /><figcaption>Fig 10 : Slope and Constant Updating from Sample data</figcaption></figure><p>Above adjusted c and m is based on single iteration. These iterations will continue to happen until either arrival of Iteration count or Convergence of Cost function (No major fluctuation in error).</p><p>Now, based on above method, if we visualize fitting of SLR, it’d look like below gif :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*SSlx-tUoIcXIOk2Cpr44ww.gif" /><figcaption>Gif 1 : Visualizing fitting of SLR</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*JS3Kp0uG1_Tubwgy4veaoA.gif" /><figcaption>Gif 2 : LR fitting on Linear data point</figcaption></figure><p>Code used to generate above graph can be downloaded from here : <a href="https://github.com/imironnman/blogs">SLR Viz</a></p><p>So far, we had seen steps involved in SLR on fitting best fit line using Gradient Descent and MSE.</p><p>We’ll see Assumptions of Linear Regression, and what would it mean if those got violated, how to identify whether assumptions are met or not, in along with when to use which error metric in <a href="https://medium.com/@vasanth.analyst.freelancer/from-theory-to-practice-deepening-regression-knowledge-part-2-326aa96460d8">part 2</a>.</p><p>See you there!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=109c0b2a1955" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>