当前位置: 首页 > 工具软件 > statsmodels > 使用案例 >

Python 第三方模块 统计1 statsmodels模块2 时间序列

刁丰羽
2023-12-01

三.时间序列研究(Time-Series Study)
(1)导入:

#通常导入为tsa:
import statsmodels.tsa.api as tsa
#注意:
①这类接口推荐用于交互式使用
②这些类/函数实际上是定义在其他地方的,sm只是提供了1个接口

(2)统计与测试(Statistics and Tests):

求"自相关函数"(Autocorrelation Function):tsa.acf(<x>[,adjusted=False,nlags=None,qstat=False,fft=None,alpha=None,missing='none'])
  #实际上是statsmodels.tsa.stattools.acf()
估计"自协方差"(Autocovariance):tsa.acovf(<x>[,adjusted=False,demean=True,fft=None,missing='none',nlag=None])
  #实际上是statsmodels.tsa.stattools.acovf()
"迪基-福勒单位根检验"(Augmented Dickey-Fuller Unit Root Test):tsa.adfuller(<x>[,maxlag=None,regression='c',autolag='AIC',store=False,regresults=False])
  #实际上是statsmodels.tsa.stattools.adfuller()
时间序列独立性的"BDS检验统计"(BDS Test Statistic):tsa.bds(<x>[,max_dim=2,epsilon=None,distance=1.5])
  #实际上是statsmodels.tsa.stattools.bds()
"互相关函数"(Cross-Correlation Function):tsa.ccf(<x>,<y>[,adjusted=True])
  #实际上是statsmodels.tsa.stattools.ccf()
求2个序列间的"互协方差"(Cross-Covariance):tsa.ccovf(<x>,<y>[,adjusted=True,demean=True])
  #实际上是statsmodels.tsa.stattools.ccovf()
对"一元方程"(Univariate Equation)的"无协整性"(No-Cointegration)的测试:tsa.coint(<y0>,<y1>[,trend='c',method='aeg',maxlag=None,autolag='aic',return_results=None])
  #实际上是statsmodels.tsa.stattools.coint()
"KPSS检验"(Kwiatkowski-Phillips-Schmidt-Shin Test):tsa.kpss(<x>[,regression='c',nlags=None,store=False])
  #实际上是statsmodels.tsa.stattools.kpss()
"偏自相关估计"(Partial Autocorrelation Estimate):tsa.pacf(<x>[,nlags=None,method='ywadjusted',alpha=None])
  #实际上是statsmodels.tsa.stattools.pacf()
基于OLS的偏自相关:tsa.pacf_ols(<x>[,nlags=None,efficient=True,adjusted=False])
  #实际上是statsmodels.tsa.stattools.pacf_ols()
基于"非递归尤尔—沃克方程"(Non-Recursive Yule_Walker)的偏自相关估计:tsa.pacf_yw(<x>[,nlags=None,method='adjusted'])
  #实际上是statsmodels.tsa.stattools.pacf_yw()
求"LBQ统计量"(Ljung-Box Q Statistic):tsa.q_stat(<x>,<nobs>[,type=None])
  #statsmodels.tsa.stattools.q_stat()

(3)单变量时间序列分析(Univariate Time-Series Analysis):

"自回归模型"(Autoregressive Model;AR Model):class tsa.AutoReg(<endog>,<lags>[,trend='c',seasonal=False,exog=None,hold_back=None,period=None,missing='none',deterministic=None,old_names=None])
  #实际上是class statsmodels.tsa.ar_model.AutoReg
"自回归差分整合滑动平均模型"(Autoregressive Integrated Moving Average Model;ARIMA Model):class tsa.ARIMA(<endog>[,exog=None,order=(0,0,0),seasonal_order=(0,0,0,0),trend=None,enforce_stationarity=True,enforce_invertibility=True,concentrate_scale=False,trend_offset=1,dates=None,freq=None,missing='none',validate_specification=True])
  #实际上是class statsmodels.tsa.arima.model.ARIMA
带有"外生回归因子"(Exogenous Regressors)的"季节性自回归差分整合滑动平均模型"(Seasonal AutoRegressive Integrated Moving Average Model;SARIMA Model):class tsa.SARIMAX(<endog>[,exog=None,order=(0,0,0),seasonal_order=(0,0,0,0),trend=None,measurement_error=False,time_varying_regression=False,mle_regression=True,simple_differencing=False,enforce_stationarity=True,enforce_invertibility=True,hamilton_representation=False,concentrate_scale=False,trend_offset=1,use_exact_diffuse=False,dates=None,freq=None,missing='none',validate_specification=True,**kwargs])
  #实际上是class statsmodels.tsa.statespace.sarimax.SARIMAX
计算大量ARMA模型的"信息准则"(Information Criteria):tsa.arma_order_select_ic(<y>[,max_ar=4,max_ma=2,ic='bic',trend='c',model_kw=None,fit_kw=None])
  #实际上是statsmodels.tsa.stattools.arma_order_select_ic()
来自ARMA模型的"模拟数据"(Simulate Data):tsa.arma_generate_sample(<ar>,<ma>[,nsample,scale=1,distrvs=None,axis=0,burnin=0])
  #实际上是statsmodels.tsa.arima_process.arma_generate_sample()
指定"拉格朗日多项式"(Lagrange-Polynomial)的ARMA过程的理论性质:class tsa.ArmaProcess([ar=None,ma=None,nobs=100])
  #实际上是class statsmodels.tsa.arima_process.ArmaProcess

(4)指数平滑法(Exponential Smoothing):

"霍尔特-温特指数平滑法"(Holt Winter's Exponential Smoothing):class tsa.ExponentialSmoothing(<endog>[,trend=None,damped_trend=False,seasonal=None,seasonal_periods=None,initialization_method=None,initial_level=None,initial_trend=None,initial_seasonal=None,use_boxcox=None,bounds=None,dates=None,freq=None,missing="none")
  #实际上是class statsmodels.tsa.holtwinters.ExponentialSmoothing
"霍尔特指数平滑法"(Holt's Exponential Smoothing):class tsa.Holt(<endog>[,exponential=False,damped_trend=False,initialization_method=None,initial_level=None,initial_trend=None])
  #实际上是class statsmodels.tsa.holtwinters.Holt
"简单指数平滑法"(Simple Exponential Smoothing):class tsa.SimpleExpSmoothing(<endog>[,initialization_method=None,initial_level=None])
  #实际上是class statsmodels.tsa.holtwinters.SimpleExpSmoothing
"线性指数平滑模型"(Linear Exponential Smoothing Models):class tsa.ExponentialSmoothing(<endog>[,trend=False,damped_trend=False,seasonal=None,initialization_method='estimated',initial_level=None,initial_trend=None,initial_seasonal=None,bounds=None,concentrate_scale=True,dates=None,freq=None,missing='none'])
  #实际上是class statsmodels.tsa.statespace.exponential_smoothing.ExponentialSmoothing
"ETS模型"(ETS models):class tsa.ETSModel(<endog>[,error='add',trend=None,damped_trend=False,seasonal=None,seasonal_periods=None,initialization_method='estimated',initial_level=None,initial_trend=None,initial_seasonal=None,bounds=None,dates=None,freq=None,missing='none'])
  #实际上是class statsmodels.tsa.exponential_smoothing.ets.ETSModel

(5)多元时间序列模型(Multivariate Time Series Models):

"动态因子模型"(Dynamic factor model):class tsa.DynamicFactor(<endog>,<k_factors>,<factor_order>[,exog=None,error_order=0,error_var=False,error_cov_type='diagonal',enforce_stationarity=True,**kwargs])
  #实际上是class statsmodels.tsa.statespace.dynamic_factor.DynamicFactor
基于"最大期望算法"(Expectation-Maximization Algorithm;EM Algorithm)的动态因子模型:class tsa.DynamicFactorMQ(<endog>[,k_endog_monthly=None,factors=1,factor_orders=1,factor_multiplicities=None,idiosyncratic_ar1=True,standardize=True,endog_quarterly=None,init_t0=False,obs_cov_diag=False,**kwargs])
  #实际上是class statsmodels.tsa.statespace.dynamic_factor_mq.DynamicFactorMQ
拟合VAR(p)过程并选择"滞后阶数"(Lag Order):class tsa.VAR(<endog>[,exog=None,dates=None,freq=None,missing='none'])
  #实际上是class statsmodels.tsa.vector_ar.var_model.VAR
带有外生回归因子的"向量自回归滑动平均模型"(Vector Autoregressive Moving Average Model):class tsa.VARMAX(<endog>[,exog=None,order=(1,0),trend='c',error_cov_type='unstructured',measurement_error=False,enforce_stationarity=True,enforce_invertibility=True,trend_offset=1,**kwargs])
  #实际上是class statsmodels.tsa.statespace.varmax.VARMAX
拟合VAR过程并估计A与B的"Structural Components":class tsa.SVAR(<endog>,<svar_type>[,dates=None,freq=None,A=None,B=None,missing='none'])
  #实际上是class statsmodels.tsa.vector_ar.svar_model.SVAR
"向量误差修正模型"(Vector Error Correction Model;VECM):class tsa.VECM(<endog>[,exog=None,exog_coint=None,dates=None,freq=None,missing='none',k_ar_diff=1,coint_rank=1,deterministic='nc',seasons=0,first_season=0])
  #实际上是class statsmodels.tsa.vector_ar.vecm.VECM
"一元未观测分量时间序列模型"(Univariate Unobserved Components Time Series Model):class tsaNone,exog=None,irregular=False,stochastic_level=False,stochastic_trend=False,stochastic_seasonal=True,stochastic_freq_seasonal=None,stochastic_cycle=False,damped_cycle=False,cycle_period_bounds=None,mle_regression=True,use_exact_diffuse=False,**kwargs])
  #实际上是class statsmodels.tsa.statespace.structural.UnobservedComponents

(6)过滤与分解(Filters and Decompositions):

基于滑动平均的"季节分解"(Seasonal Decomposition):tsa.seasonal_decompose(<x>[,model='additive',filt=None,period=None,two_sided=True,extrapolate_trend=0])
  #实际上是statsmodels.tsa.seasonal.seasonal_decompose()
"基于LOESS的季节-趋势分解"(Season-Trend Decomposition using LOESS;STL):class tsa.STL(<endog>[,period=None,seasonal=7,trend=None,low_pass=None,seasonal_deg=0,trend_deg=0,low_pass_deg=0,robust=False,seasonal_jump=1,trend_jump=1,low_pass_jump=1])
  #实际上是class statsmodels.tsa.seasonal.STL
"BK带通滤波器"(Baxter-King Bandpass Filter):tsa.bkfilter(<x>[,low=6,high=32,K=12])
  #实际上是statsmodels.tsa.filters.bk_filter.bkfilter()
"CF不对称随机游走滤波器"(Christiano Fitzgerald Asymmetric,Random Walk Filter):tsa.cffilter(<x>[,low=6,high=32,drift=True])
  #实际上是statsmodels.tsa.filters.cf_filter.cffilter()
"HP滤波器"(Hodrick-Prescott Filter.):tsa.hpfilter(<x>[,lamb=1600])
  #实际上是statsmodels.tsa.filters.hp_filter.hpfilter()

(7)马尔可夫区制转换模型(Markov Regime Switching Models):

"马尔可夫转换回归模型"(Markov Switching Regression Model):class tsa.MarkovAutoregression(<endog>,<k_regimes>,<order>[,trend='c',exog=None,exog_tvtp=None,switching_ar=True,switching_trend=True,switching_exog=False,switching_variance=False,dates=None,freq=None,missing='none'])
  #实际上是class statsmodels.tsa.regime_switching.markov_autoregression.MarkovAutoregression
"1阶K-区制马尔可夫转换模型"(First-Order K-Regime Markov Switching Regression Model):class tsa.MarkovRegression(<endog>,<k_regimes>[,trend='c',exog=None,order=0,exog_tvtp=None,switching_trend=True,switching_exog=True,switching_variance=False,dates=None,freq=None,missing='none'])
  #实际上是class statsmodels.tsa.regime_switching.markov_regression.MarkovRegression

(8)预测(Forecasting):

Model-based forecasting using STL to remove seasonality:class tsa.STLForecast(<endog>,<model>[,model_kwargs=None,period=None,seasonal=7,trend=None,low_pass=None,seasonal_deg=1,trend_deg=1,low_pass_deg=1,robust=False,seasonal_jump=1,trend_jump=1,low_pass_jump=1])
  #实际上是class statsmodels.tsa.forecasting.stl.STLForecast
The Theta forecasting model of Assimakopoulos and Nikolopoulos (2000):class tsa.ThetaModel(<endog>[,period=None,deseasonalize=True,use_test=True,method='auto',difference=False)
  #实际上是class statsmodels.tsa.forecasting.theta.ThetaModel

(9)时间序列工具(Time-Series Tools):

Returns an array with lags included given an array:tsa.add_lag(<x>[,col=None,lags=1,drop=False,insert=True])
  #实际上是statsmodels.tsa.tsatools.add_lag()
Add a trend and/or constant to an array:tsa.add_trend(<x>[,trend='c',prepend=False,has_constant='skip'])
  #实际上是statsmodels.tsa.tsatools.add_trend()
Detrend an array with a trend of given order along axis 0 or 1:tsa.detrend(<x>[,order=1,axis=0])
  #实际上是statsmodels.tsa.tsatools.detrend()
Create 2d array of lags:tsa.lagmat(<x>,<maxlag>[,trim='forward',original='ex',use_pandas=False])
  #实际上是statsmodels.tsa.tsatools.lagmat()
Generate lagmatrix for 2d array, columns arranged by variables:tsa.lagmat2ds(<x>[,maxlag0,maxlagex=None,dropex=0,trim='forward',use_pandas=False])
  #实际上是statsmodels.tsa.tsatools.lagmat2ds()
Container class for deterministic terms:class tsa.DeterministicProcess(<index>[,period=None,constant=False,order=0,seasonal=False,fourier=0,additional_terms=(),drop=False])
  #实际上是class statsmodels.tsa.deterministic.DeterministicProcess

(10)X12/X13接口(X12/X13 Interface):

Perform x13-arima analysis for monthly or quarterly data:tsa.x13_arima_analysis(<endog>[,maxorder=(2,1),maxdiff=(2,1),diff=None,exog=None,log=None,outlier=True,trading=False,forecast_periods=None,retspec=False,speconly=False,start=None,freq=None,print_stdout=False,x12path=None,prefer_x13=True])
  #实际上是statsmodels.tsa.x13.x13_arima_analysis()
Perform automatic seasonal ARIMA order identification using x12/x13 ARIMA:tsa.x13_arima_select_order(<endog>[,maxorder=(2,1),maxdiff=(2,1),diff=None,exog=None,log=None,outlier=True,trading=False,forecast_periods=None,start=None,freq=None,print_stdout=False,x12path=None,prefer_x13=True])
  #实际上是statsmodels.tsa.x13.x13_arima_select_order()

5.基于公式的接口(Formula Interface)

A convenience interface for specifying models using formula strings and DataFrames. This API directly 
exposes the from_formula class method of models that support the formula API

(1)导入:

#通常导入为smf:
import statsmodels.formula.api as smf

(2)模型(Models):

 类似资料: