Material to accompany this lessonDownload the spreadsheet found here on Google Sheets. We will use this spreadsheet for the lesson. Download it as CSV, the most common method for storing a database that can be read by any language (Comma-Separated Values).Create a python3 development environment or use ready-made online environments such as JupyterLab online and import the database.Statsmodels in PythonStart by downloading the data as CSV, renaming it to "dados-2.csv" to make it easier to work with. We will use the pandas, numpy, statsmodels, and matplotlib packages in Python. Below you can see how to import each one:Importing packages in PythonTime as an indexNotice that we are using decimal="," and thousands="." precisely because our data is in Portuguese. Now we will use time as the index of our data table. Below is the code to set time as the index and also create a "Days" column. The "Days" column will be used in the linear regression; with it, we will check when each stock is growing over time using the slope coefficient.Transformation to use time as an indexCorrelation matrixBefore starting the regressions, let's move on to a very important analysis for investors: the correlation matrix. This matrix shows how each stock is correlated. If you want to create an investment portfolio in the future, remember to choose stocks that are negatively correlated with each other, as this reduces the portfolio's risk. Below is the code to calculate the correlation matrix:Calculating correlationsThe matrix should look like this at the end:Correlation matrix between stocksBelow is what was printed by Python regarding the correlations. Most positive correlations:Company 1Company 2CorrelationBradescoTim0.957497TimBradesco0.957497Domino's pizzaDays0.948813DaysDomino's pizza0.948813MastercardDomino's pizza0.921368Domino's pizzaMastercard0.921368SantanderAmerican Express Company0.888684American Express CompanySantander0.888684TimSantander0.888301SantanderTim0.888301Most negative correlations:Company 1Company 2CorrelationDaysSantander-0.642465SantanderDays-0.642465Ford motorsBraskem-0.668737BraskemFord motors-0.668737Ford motorsMastercard-0.708027MastercardFord motors-0.708027Domino's pizzaFord motors-0.826345Ford motorsDomino's pizza-0.826345Simple Linear RegressionNow let's actually use Statsmodels. Below is the code to perform a simple linear regression between Gold and the number of Days.Linear regression in StatsmodelsLinear regression provides several results; see the table below and try to interpret each parameter:OLS resultsBelow are our forecast and the observed real-world points:Forecast vs observedIt's not one of the best regressions, right? Let's also look at the residuals.The residuals show a trend and are not symmetricalARIMA RegressionIf our friend Linear Regression cannot forecast, we can look for other models. So let's try to run an ARIMA(1,1,1) regression between the value of Gold and Time.Python code for ARIMAARIMA resultForecast vs Observed with ARIMAEven though the forecast looks good, we must not forget to look at the residuals:ARIMA residualsTraining set vs test setEven though ARIMA had a great result, it is good to check by dividing our sample into at least two sets: a training set and a test set. We will split it in half, train our model with the training set, and check whether it also has good results on the test set.Creating the test and training sets and running a new regressionTraining ARIMA resultsFinal forecast:Test, Training, and observed valuesMultivariate RegressionIf you do not want to work with ARIMA, it is possible to perform a Multivariate Regression by choosing stocks correlated with yours to make forecasts. In the example below, I use Chevron's price and the number of Days to forecast the price of Vale do Rio Doce.Python code for multivariate regressionMultivariate Regression resultForecast and observed values from the Multivariate RegressionExercisesNow you do it:1) Import this dataset into pandas2) Create the correlation matrix3) Choose a stock and perform a linear regression between it and the days. It cannot be gold!4) Choose a stock and run an ARIMA between it and time.5) Choose a stock and train an ARIMA with half of the data, then test it with the other half.6) Challenge: perform a multivariate regression that attempts to explain the value of a stock using time and the value of another stock as a reference.
— 코멘트 0
, 반응 1
첫 댓글을 남겨보세요