开发者

Seasonal adjustment in Python and Scipy

I am look开发者_StackOverflow社区ing to seasonally adjust monthly data, using Python. As you can see from these series: www.emconfidential.com, there is a high seasonal component to the data. I would like to adjust for this so that I can better guage if the series trend is rising or falling. Anybody know how to do this easily using scipy or other Python library?


Statsmodels can do this. They have a basic seasonal decomposition and also a wrapper to Census X13 adjustment. You could also use rpy2 to access some of R's excellent SA libraries. Here is statsmodels seasonal decomp:

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


There's no magical python library that will do seasonal adjustments for you. Applications that do this kind of thing tend to be rather large.

You'll need to work out the maths yourself and then use scipy to calculate the rest for you.


There is now 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 to be very useful, wondering what others think.


I would suggest Prophet developed by the data science team at Facebook. It has Python+R API and is used for time-series prediction although you can use it just for decomposing your series into its components (trend vs seasonality). You can easily adjust 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='-*')

Seasonal adjustment in Python and Scipy

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

Seasonal adjustment in Python and Scipy


Not sure on the programming aspect of this but I would seriously consider moving averages to solve this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜