Using sm.add_constant()

Course Name: Financial Time Series Analysis for Trading, Section No: 6, Unit No: 7, Unit type: Notebook

Hello, it comes to my mind why we are using here `sm.add_constant(X)` while in other notebooks we didn't used this method. I've researched a little and the answer I have is: 1. possibility of an expected non-null value, 2. measurements errors and 3. modeling of non-linear relationships. Can you explain a little about this?

Hi Daniel,



"sm.add_constant(X)" is used to add a constant term to a dataset X. This constant term is added to allow models to estimate an intercept term.



When you're fitting a linear regression model, if you don't explicitly include a constant term in your data, the model will assume that there's no intercept. However, in many cases, there's usually a non-zero intercept.



By adding a constant term using sm.add_constant(X), we are adding a column of 1s to your dataset X, which allows the model to estimate the intercept term as well.



 

And can you give me an example where is not needed to use "sm.add_constant(X)"?

Hi Daniel,



When you are working with data that naturally has an intercept at the origin, meaning that the relationship between the variables you are modeling passes through the point (0,0). In such cases, you wouldn't need to add a constant term because the intercept is inherently zero.

In the example of this notebooks means we add an intercept because we expect when BAC = 0, JPM = 0 too? Or what's the reason to expect the financial price series needs an intercept?

Hi Daniel,



The intercept is added when we feel that if the independent variable is 0, the dependent variable might have a non-zero value. When BAC = 0 and JPM is 5, it means 5 is the intercept. To make sure that the model tries estimating the intercept, we add the term sm.add_constant. The term is added in section 9 unit 6 as well. This term is mainly used for regression issues.



Thanks.