introd u ction to the co u rse
play

Introd u ction to the Co u rse TIME SE R IE S AN ALYSIS IN P YTH - PowerPoint PPT Presentation

Introd u ction to the Co u rse TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian E x ample of Time Series : Google Trends TIME SERIES ANALYSIS IN PYTHON E x ample of Time Series :


  1. Introd u ction to the Co u rse TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian

  2. E x ample of Time Series : Google Trends TIME SERIES ANALYSIS IN PYTHON

  3. E x ample of Time Series : Climate Data TIME SERIES ANALYSIS IN PYTHON

  4. E x ample of Time Series : Q u arterl y Earnings Data TIME SERIES ANALYSIS IN PYTHON

  5. E x ample of M u ltiple Series : Nat u ral Gas and Heating Oil TIME SERIES ANALYSIS IN PYTHON

  6. Goals of Co u rse Learn abo u t time series models Fit data to a times series model Use the models to make forecasts of the f u t u re Learn ho w to u se the rele v ant statistical packages in P y thon Pro v ide concrete e x amples of ho w these models are u sed TIME SERIES ANALYSIS IN PYTHON

  7. Some Usef u l Pandas Tools Changing an inde x to datetime df.index = pd.to_datetime(df.index) Plo � ing data df.plot() Slicing data df['2012'] TIME SERIES ANALYSIS IN PYTHON

  8. Some Usef u l Pandas Tools Join t w o DataFrames df1.join(df2) Resample data ( e . g . from dail y to w eekl y) df = df.resample(rule='W', how='last') TIME SERIES ANALYSIS IN PYTHON

  9. More pandas F u nctions Comp u ting percent changes and di � erences of a time series df['col'].pct_change() df['col'].diff() pandas correlation method of Series df['ABC'].corr(df['XYZ']) pandas a u tocorrelation df['ABC'].autocorr() TIME SERIES ANALYSIS IN PYTHON

  10. Let ' s practice ! TIME SE R IE S AN ALYSIS IN P YTH ON

  11. Correlation of T w o Time Series TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian

  12. Correlation of T w o Time Series Plot of S & P 500 and JPMorgan stock TIME SERIES ANALYSIS IN PYTHON

  13. Correlation of T w o Time Series Sca � er plot of S & P 500 and JP Morgan ret u rns TIME SERIES ANALYSIS IN PYTHON

  14. More Scatter Plots Correlation = 0.9 Correlation = 0.4 Correlation = -0.9 Corelation = 1.0 TIME SERIES ANALYSIS IN PYTHON

  15. Common Mistake : Correlation of T w o Trending Series Do w Jones Ind u strial A v erage and UFO Sightings ( www. n u forc . org ) Correlation of le v els : 0.94 Correlation of percent changes : ≈ 0 TIME SERIES ANALYSIS IN PYTHON

  16. E x ample : Correlation of Large Cap and Small Cap Stocks Start w ith stock prices of SPX ( large cap ) and R 2000 ( small cap ) First step : Comp u te percentage changes of both series df['SPX_Ret'] = df['SPX_Prices'].pct_change() df['R2000_Ret'] = df['R2000_Prices'].pct_change() TIME SERIES ANALYSIS IN PYTHON

  17. E x ample : Correlation of Large Cap and Small Cap Stocks Vis u ali z e correlation w ith sca � ter plot plt.scatter(df['SPX_Ret'], df['R2000_Ret']) plt.show() TIME SERIES ANALYSIS IN PYTHON

  18. E x ample : Correlation of Large Cap and Small Cap Stocks Use pandas correlation method for Series correlation = df['SPX_Ret'].corr(df['R2000_Ret']) print("Correlation is: ", correlation) Correlation is: 0.868 TIME SERIES ANALYSIS IN PYTHON

  19. Let ' s practice ! TIME SE R IE S AN ALYSIS IN P YTH ON

  20. Simple Linear Regressions TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian

  21. What is a Regression ? Simple linear regression : y = α + βx + ϵ t t t TIME SERIES ANALYSIS IN PYTHON

  22. What is a Regression ? Ordinar y Least Sq u ares ( OLS ) TIME SERIES ANALYSIS IN PYTHON

  23. P y thon Packages to Perform Regressions Warning : the order of x and In statsmodels : y is not consistent across import statsmodels.api as sm sm.OLS(y, x).fit() packages In n u mp y: np.polyfit(x, y, deg=1) In pandas : pd.ols(y, x) In scip y: from scipy import stats stats.linregress(x, y) TIME SERIES ANALYSIS IN PYTHON

  24. E x ample : Regression of Small Cap Ret u rns on Large Cap Import the statsmodels mod u le import statsmodels.api as sm As before , comp u te percentage changes in both series df['SPX_Ret'] = df['SPX_Prices'].pct_change() df['R2000_Ret'] = df['R2000_Prices'].pct_change() Add a constant to the DataFrame for the regression intercept df = sm.add_constant(df) TIME SERIES ANALYSIS IN PYTHON

  25. Regression E x ample ( contin u ed ) Notice that the � rst ro w of ret u rns is NaN SPX_Price R2000_Price SPX_Ret R2000_Ret Date 2012-11-01 1427.589966 827.849976 NaN NaN 2012-11-02 1414.199951 814.369995 -0.009379 -0.016283 Delete the ro w of NaN df = df.dropna() R u n the regression results = sm.OLS(df['R2000_Ret'],df[['const','SPX_Ret']]).fit() print(results.summary()) TIME SERIES ANALYSIS IN PYTHON

  26. Regression E x ample ( contin u ed ) Regression o u tp u t Intercept in results.params[0] Slope in results.params[1] TIME SERIES ANALYSIS IN PYTHON

  27. Regression E x ample ( contin u ed ) Regression o u tp u t TIME SERIES ANALYSIS IN PYTHON

  28. Relationship Bet w een R - Sq u ared and Correlation 2 2 [corr( x , y )] = R ( or R - sq u ared ) sign(corr) = sign(regression slope) In last e x ample : R - Sq u ared = 0.753 Slope is positi v e √0.753 correlation = + = 0.868 TIME SERIES ANALYSIS IN PYTHON

  29. Let ' s practice ! TIME SE R IE S AN ALYSIS IN P YTH ON

  30. A u tocorrelation TIME SE R IE S AN ALYSIS IN P YTH ON Rob Reider Adj u nct Professor , NYU - Co u rant Cons u ltant , Q u antopian

  31. What is A u tocorrelation ? Correlation of a time series w ith a lagged cop y of itself Lag - one a u tocorrelation Also called serial correlation TIME SERIES ANALYSIS IN PYTHON

  32. Interpretation of A u tocorrelation Mean Re v ersion - Negati v e a u tocorrelation TIME SERIES ANALYSIS IN PYTHON

  33. Interpretation of A u tocorrelation Moment u m , or Trend Follo w ing - Positi v e a u tocorrelation TIME SERIES ANALYSIS IN PYTHON

  34. Traders Use A u tocorrelation to Make Mone y Indi v id u al stocks Historicall y ha v e negati v e a u tocorrelation Meas u red o v er short hori z ons ( da y s ) Trading strateg y: B uy losers and sell w inners Commodities and c u rrencies Historicall y ha v e positi v e a u tocorrelation Meas u red o v er longer hori z ons ( months ) Trading strateg y: B uy w inners and sell losers TIME SERIES ANALYSIS IN PYTHON

  35. E x ample of Positi v e A u tocorrelation : E x change Rates Use dail y ¥ /$ e x change rates in DataFrame df from FRED Con v ert inde x to datetime # Convert index to datetime df.index = pd.to_datetime(df.index) # Downsample from daily to monthly data df = df.resample(rule='M', how='last') # Compute returns from prices df['Return'] = df['Price'].pct_change() # Compute autocorrelation autocorrelation = df['Return'].autocorr() print("The autocorrelation is: ",autocorrelation) The autocorrelation is: 0.0567 TIME SERIES ANALYSIS IN PYTHON

  36. Let ' s practice ! TIME SE R IE S AN ALYSIS IN P YTH ON

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend