Seasonal adjustment in Python and Scipy

I am looking for an opportunity to monthly adjust monthly data using Python. As you can see from these series: www.emconfidential.com , there is a high seasonal component for the data. I would like to adapt to this so that I can better understand if the trend is growing or falling in a series. Does anyone know how to do this easily using scipy or another Python library?

+7
python scipy
source share
5 answers

There is no magic python library that will make seasonal adjustments for you. Applications that do such things are usually quite large .

You will need to work out the math and then use scipy to calculate the rest for you.

+3
source share

Statsmodels can do this. They have basic seasonal decomposition, as well as an X13 overhaul wrapper. You can also use rpy2 to access some of the R excellent SA libraries. Here are the statistical seasonal decomposition models:

import pandas as pd import statsmodels.api as sm import matplotlib.pyplot as plt pd.options.display.mpl_style = 'default' %matplotlib inline dta = sm.datasets.co2.load_pandas().data.resample("M").fillna(method="ffill") res = sm.tsa.seasonal_decompose(dta) fig = res.plot() fig.set_size_inches(10, 5) plt.tight_layout() 

http://statsmodels.sourceforge.net/0.6.0/release/version0.6.html

+3
source share

Now there is a package that seems to be exactly what you are looking for! Check out the seasonal package, here is the link. I personally found it very useful, wondering what others think.

+1
source share

I would suggest Prophet , developed by the Facebook data team. It has a Python + R API and is used to predict time series, although you can only use it to decompose your series into its components (trend versus seasonality). You can easily customize and visualize the decomposition:

 from fbprophet import Prophet import numpy as np import pandas as pd # Create series np.random.seed(0) x = np.arange(0, 10, .285) y_periodic = np.sin(x*np.pi) y_random = np.random.normal(size=len(x)) y_trend = x / 10. df = pd.DataFrame({'ds': pd.date_range('01-01-2017', periods=len(x)), 'y': y_periodic}) df.head() # has to be a DataFrame with columns "ds" and "y" df.set_index('ds').plot(style='-*') 

Noise series

 # Estimate the model m = Prophet() m.fit(df); forecast = m.predict(df) m.plot_components(forecast); 

Decomposition of trend and seasonality

+1
source share

Not sure about this aspect of programming, but I would seriously think about moving averages to solve this problem.

0
source share

All Articles