时间序列分析-linear-models-to-GARCH

重点

  • 稳态时间序列要满足三个条件:
    1. 均值不随时间变化
    2. 方差不随时间变化
    3. 协方差不随时间变化
  • 验证一个TSM的正确性的方法是验证其残差是否是白噪声
  • random walk process可以建模,但无法做预测?
  • 时间序列分析的套路是不断分解目标序列,提取趋势/周期性信息,直至残留信息是白噪声序列为止
  • 时间序列分析TSA的套路,一个尝试各种已知模型的过程,通过对残差的白噪声验证确定模型的有效性,利用AIC/BIC等方式控制模型复杂度
    这里写图片描述

  • ARCH(p)模型AR(p)是作用在方差上的模型

  • GARCH(p,q)模型是ARMA(p,q)作用在方差上的模型,而且它处理的序列本身很接近白噪声序列,但是平方后表现出自相关性

The Basics

What is a Times Series?

A time series is a series of data points indexed(or listed or graphed) in time order

Stationarity

下图来自SeanAbu.com,给出stationarity直观的解释
这里写图片描述

为什么要关注于数据的stationarity?
* 只有当数据未来的统计属性和现在的统计属性相关时,时序数据才容易被预测
* Time Series Analysis(TSA)中大部分模型都假设 convariance-stationarity(上图中的#3)所以只有当TS时stationarity时,模型预测出的统计属性,比如means,variances和correlations才是可靠的。


"For example, if the series is consistently increasing over time, the sample mean and variance will grow with the size of the sample, and they will always underestimate the mean and variance in future periods. And if the mean and variance of a series are not well-defined, then neither are its correlations with other variables." - http://people.duke.edu/~rnau/411diff.htm

实践中,金融领域的TS都不是stationary的。TSA中大部分工作是如何分辨目标序列是否是stationary的,如果不是则需要找到方法把其变成stationary的。

Serial Correlation(Autocorrelation)

本质上我们对一个序列建模就是把序列分解成三部分:趋势,周期性和随机性。随机部分称为残差和错误,是预测值和观测值之间的差异。

Why Do We Care about Serial Correlation?

Serial Correlation本质上和stationarity有关,对于模型预测结果的有效性具有重要作用。根据定义,stationary TS模型的残差是相互无关的。模型中如果没考虑到这个会导致模型系数的估测不准,增大T-statistics,增大Type-1错误。In Layman’s terms,ignoring automcorrelation means our model predictions will be bunk,and we’re likey to draw incorrect conclusions about the impack of the independent variables in our model.


译注:上述两个小节似乎把序列和模型混在一起了,建模是把序列分成趋势/周期/随机三个部分分开建模,其中随机部分就是模型的残差,残差序列不能是自相关的,必须是独立的。一个模型预测的残差是自相关的,则模型有问题

White Noise and Random Walks

我们接触到的第一个Time Series Model(TSM)是White noise(最简单的TSM)。White noise process(译注:模型)的残差是不相关,而且残差的期望是0.这种序列的不相关残差也称为independent and identically distributed(i.i.d). 如果一个TSM成功拟合到真正的过程,模型的残差应该是i.i.d,就是white noise process。TSA的一部分工作就是学习一个模型,使得模型的残差和white noise不可区分。
下面的代码来自于 Seanabu.com,可以模拟并显示white noise process。

def tsplot(y, lags=None, figsize=(10, 8), style='bmh'):
    if not isinstance(y, pd.Series):
        y = pd.Series(y)
    with plt.style.context(style):    
        fig = plt.figure(figsize=figsize)
        #mpl.rcParams['font.family'] = 'Ubuntu Mono'
        layout = (3, 2)
        ts_ax = plt.subplot2grid(layout, (0, 0), colspan=2)
        acf_ax = plt.subplot2grid(layout, (1, 0))
        pacf_ax = plt.subplot2grid(layout, (1, 1))
        qq_ax = plt.subplot2grid(layout, (2, 0))
        pp_ax = plt.subplot2grid(layout, (2, 1))

        y.plot(ax=ts_ax)
        ts_ax.set_title('Time Series Analysis Plots')
        smt.graphics.plot_acf(y, lags=lags, ax=acf_ax, alpha=0.5)
        smt.graphics.plot_pacf(y, lags=lags, ax=pacf_ax, alpha=0.5)
        sm.qqplot(y, line='s', ax=qq_ax)
        qq_ax.set_title('QQ Plot')        
        scs.probplot(y, sparams=(y.mean(), y.std()), plot=pp_ax)

        plt.tight_layout()
    return

下面的代码生成一个white noise process并显示输出。

np.random.seed(1)

# plot of discrete white noise
randser = np.random.normal(size=1000)
tsplot(randser, lags=30)

white-noise-1

上述序列表现出0附近的随机性。而且autocorrelation(ACF)和partial autocorrelation(PACF)没有明显的自相关。Keep in mind we should see approximately 5% significance in the autocorrelation plots due to pure chance as a result of sampling from the Normal distribution。在QQ和Probability Plots上,我们把测试数据和标准的正态分布做比较,如图所示,测试数据的确和一个正态白噪声标准模型匹配。

p("Random Series\n -------------\nmean: {:.3f}\nvariance: {:.3f}\nstandard deviation: {:.3f}"
.format(randser.mean(), randser.var(), randser.std()))

# Random Series
# -------------
# mean: 0.039 
# variance: 0.962
# standard deviation: 0.981

A Random Walk is defined below:

Random-walk-def

random walk是非稳态的,因为其covariance和时间相关,random walk对应的TS是无法准确预测的。

下面的代码利用numpy库从标准正态分布中采样生成一个random walk序列

# Random Walk without a drift

np.random.seed(1)
n_samples = 1000

x = w = np.random.normal(size=n_samples)
for t in range(n_samples):
    x[t] = x[t-1] + w[t]

_ = tsplot(x, lags=30)

Random-walk-1

显然这个TS是非稳态的。来验证一下random walk model是否是符合我们模拟的数据。因为random walk的定义是 xt=xt1+wt x t = x t − 1 + w t ,易得 xtxt1=wt x t − x t − 1 = w t ,即random walk 序列的一阶差分就是白噪声过程!我们用”np.diff()”在模拟数据上做验证。

# First difference of simulated Random Walk series

_ = tsplot(np.diff(x), lags=30)

diff_random-walk-1


译注:如果一个序列是random walk process,则可以用 random walk model对齐建模,但是无法准确预测??

Linear Models

Linear models又称trend models可以描述用直线可视化的时间序列,其方程如下:

yt=β0+β1t+ϵt y t = β 0 + β 1 t + ϵ t

因变量由系数 β β 和自变量时间 t t 决定。比如公司销售额每隔一段时间会增加固定的值,就是一个linear models的例子。比如下面的例子,假设ABC公司销量固定销量是-50美元( β = 0 ,即截距),每一个固定时间段内增加25美元。

# simulate linear trend
# example Firm ABC sales are -$50 by default and +$25 at every time step

w = np.random.randn(100)
y = np.empty_like(w)

b0 = -50.
b1 = 25.
for t in range(len(w)):
    y[t] = b0 + b1*t + w[t]

_ = tsplot(y, lags=lags)  

linear-models

可以看到模型的残差是相关的,而且随时间间隔lag线性降低,近似正态分布。在利用这个模型进行预测之前,需要线消除明显的自相关性。延时1时PACF出现一个峰值说明合适的模型可能是autoregressive模型


译注:上图中的第一个图“Time Series”不是一个直线,由随机噪声,只是噪声的幅度小于信号幅度。另外上图中应该也没有error/residuals出现,没有模型自然不存在残差。图中只能看到序列的自相关随时间延迟lag而降低,QQ和Probability显示和正态分布接近,但在首尾处并不严格匹配

Log-Linear Models

这个模型和Linear models相似,但是数据点形成一个指数函数分布,表示每个时间间隔内由固定的变化率。比如ABC公司销售额每段时间增加X%,如下所示:

# Simulate ABC exponential growth

# fake dates
idx = pd.date_range('2007-01-01', '2012-01-01', freq='M')

# fake sales increasing at exponential rate
sales = [np.exp( x/12 ) for x in range(1, len(idx)+1)]

# create dataframe and plot
df = pd.DataFrame(sales, columns=['Sales'], index=idx)

with plt.style.context('bmh'):
    df.plot()
    plt.title('ABC Sales')

log-linear-models

取自然对数后linear regression就可以拟合数据

# ABC log sales 

with plt.style.context('bmh'):
    pd.Series(np.log(sales), index=idx).plot()
    plt.title('ABC Log Sales')

log-log-linear-models

如前面讨论,这些模型都假设序列无关的残差,但是前面的linear model的例子已经说明这个假设是错误的。实际生活中,TS数据往往不符合稳态的假设,由此人们设计了autoregressive models。


译注:对这个两个模型出现在此处的含义不明确,这两个基于时间t建立的模型不符合稳态性,按照最开始的描述,非稳态时间序列不能用常见的平稳序列模型建模。

Autoregressive Models-AR(p)

autoregressive model中假设因变量依赖于同序列中历史中若干项,公式如下

xt=a1xt1+.
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值