******** 随机过程-五种基本时间序列 ********* * by 莫家伟 * software:STATA 10.0 * 2011.3.25 * 【以下代码均可以直接复制到STATA新建的.do文件中,将需要执行的语句划选, * 然后点击Do-File Editor工具栏最右边的“Do”按钮即可执行】 * 【准备知识】 * 平稳性定义 * “严平稳”:时间序列{x(t)}的联合概率分布不随时间而变,即任意n和k,x1 x2... xn * 与 x(1+k) x(2+k)... x(n+k)的联合分布相同。 * “弱平稳”:即符合以下三条 * (1)E(xt)= u , t=1,2,... * (2)Var(xt)=E(xt-u)^2=sigma, t=1,2,... * (3)Cov(xt,x(t+k))=(E(xt-u)(x(t+k)-u)=rk , t=1,2,..., k!=0 * 即协方差不随选定的时间t而变化,只与间隔k有关。 * 开始模拟数据前的基本设置: clear set obs 500 gen t=_n tsset t gen x=0 * 1、white noise(白噪声过程,最基本的随机平稳时间序列) gen noise=invnormal(uniform()) *这只能生成Std.Dev=1的white noise line noise t, title("1.白噪声过程") caption("Graphed by Daniel MJWei") * 2、random walk(随机游走过程,最基本的非随机平稳时间序列) drop noise x gen noise=invnormal(uniform()) *- 基本生成方程: x(t)= p* x(t-1) + noise gen x=0 in 1 replace x=l.x + noise if t != 1 line x t, title("2.随机游走过程p1") caption("Graphed by Daniel MJWei") *-可重复上述步骤,看图观察所谓“非平稳”、随机漂移的效果。 *-假如 p小于1呢?例如p=0.8 可以运行下述步骤查看: drop noise x gen noise=invnormal(uniform()) gen x=0 in 1 replace x=0.8*l.x + noise if t != 1 line x t, title("2.随机游走过程p1") caption("Graphed by Daniel MJWei") *- q 阶自回归过程 AR(q): x(t)= p1* x(t-1) + p2* x(t-2)+ ... + pq* x(t-q)+ noise *- 可以证明只有当特征方程 1-p1*L-p2*L^2-...-pq*L^q=0 -所有特征根L的绝对值大于1时,序列才是平稳的。下面演示一例: drop noise x gen noise=invnormal(uniform()) gen x=0 in 1 replace x=0 in 2 replace x=0 in 3 replace x=1.004*l.x - 1.05*l2.x + 1.003*l3.x + noise if t 3 line x t, title("2.q 阶自回归过程 AR(q)") caption("Graphed by Daniel MJWei") (P.S. 这个图不过重复几次随机过程,最后生成的都是这么美丽的形式,这或许就是所谓的偶然中的必然?) * 3、random walk with drift(带漂移项随机游走过程) drop noise x gen noise=invnormal(uniform()) *- 基本生成方程: x(t)=a+ x(t-1) + noise 【 a为随机性趋势(stochastic trend)】 gen x=0 in 1 replace x=0.1+ l.x + noise if t 1 line x t, title("3.带漂移项随机游走过程") caption("Graphed by Daniel MJWei") * 4、trend stationary process(趋势平稳过程) drop noise-x gen noise=invnormal(uniform()) *- 基本生成方程: x(t)=a+ b*t+ noise2; noise2(t)=p*noise2(t-1)+noise;p1 gen noise2=0 in 1 replace noise2=0.8*l.noise2 + noise if t1 gen x=1+ 0.1*t+ noise2 line x t, title("4.趋势平稳过程") caption("Graphed by Daniel MJWei") * 5、non-stationary process with deterministic trend *-(确定性趋势非平稳过程) drop noise-x gen noise=invnormal(uniform()) *- 基本生成方程: x(t)=a+ b*t+ x(t-1) + noise gen x=0 in 1 replace x=0.1+ 0.01*t+ l.x + 10*noise if t 1 line x t, title("5.确定性趋势非平稳过程") caption("Graphed by Daniel MJWei") *-检验单位根 dfuller x, trend dfuller d.x, trend ******* 到此,五种基本的时间序列介绍完毕 ********* * 参考文献 * 潘省初(2009).《计量经济学中级教程》. 清华大学出版社:p.146~148