::Free Statistics and Forecasting Software::

v1.1.23-r2
 
  Black&White | Blue Theme | Normal Fontsize | Increase Fontsize | Decrease Fontsize | Toggle underline
Secure website (SSL) | Private
 
 


This software module is powered by R

:: Example 6.6 - Time Series Analysis and Its Applications (Edition 2: With R Examples) ::

All rights reserved. The non-commercial (academic) use of this software is free of charge. The only thing that is asked in return is to cite this software when results are used in publications.

This free online software (calculator) reproduces Example 6.6 (Newton-Raphson for Ex. 6.3) in Shumway R.H., Stoffer D.S., Time Series Analysis and Its Applications (2nd ed. with R examples). The module includes all necessary functions to reproduce their results (Kfilter.R, Ksmooth.R, and ex66.txt). In addition, some hard-coded numbers have been replaced by user-defined parameters.
Note: many combinations of parameter values will not yield satisfactory results.

Click here to edit the underlying code of this R Module.

Send output to:
Number of values (?)
Seed (?)
phi (?)
SD of state equation (?)
SD of obs. equation (?)

Click here to edit the underlying code of this R Module.





Source code of R module
1par1 <- as.numeric(par1)
2par2 <- as.numeric(par2)
3par3 <- as.numeric(par3)
4par4 <- as.numeric(par4)
5par5 <- as.numeric(par5)
6Kfilter = function(num,y,A,mu0,Sigma0,Phi,cQ,cR){
7Q=t(cQ)%*%cQ
8R=t(cR)%*%cR
9N=num+1
10nseries=ncol(as.matrix(y))
11yobs=matrix(0,N,nseries)
12yobs[2:N,]=y # yobs is y with first row=zeros
13xp=vector("list",N) # xp=x_t^{t-1}
14Pp=vector("list",N) # Pp=P_t^{t-1}
15xf=vector("list",N) # xf=x_t^t
16Pf=vector("list",N) # Pf=x_t^t
17innov=vector("list",N) # innovations
18sig=vector("list",N) # innov var-cov matrix
19like=0 # -log(likelihood)
20xf[[1]]=mu0
21Pf[[1]]=Sigma0
22for (i in 2:N){
23xp[[i]]=Phi%*%xf[[i-1]]
24Pp[[i]]=Phi%*%Pf[[i-1]]%*%t(Phi)+Q
25siginv=A[[i]]%*%Pp[[i]]%*%t(A[[i]])+R
26sig[[i]]=(t(siginv)+siginv)/2 # make sure sig is symmetric
27siginv=solve(sig[[i]]) # now siginv is sig[[i]]^{-1}
28K=Pp[[i]]%*%t(A[[i]])%*%siginv
29innov[[i]]=as.matrix(yobs[i,])-A[[i]]%*%xp[[i]]
30xf[[i]]=xp[[i]]+K%*%innov[[i]]
31Pf[[i]]=Pp[[i]]-K%*%A[[i]]%*%Pp[[i]]
32like= like + log(det(sig[[i]])) + t(innov[[i]])%*%siginv%*%innov[[i]]
33}
34like=0.5*like
35list(xp=xp,Pp=Pp,xf=xf,Pf=Pf,like=like,innov=innov,sig=sig,Kn=K)
36}
37Ksmooth = function(num,y,A,mu0,Sigma0,Phi,cQ,cR){
38kf=Kfilter(num,y,A,mu0,Sigma0,Phi,cQ,cR)
39N=num+1
40xs=vector("list",N) # xs=x_t^n
41Ps=vector("list",N) # Ps=P_t^n
42J=vector("list",N) # J=J_t
43xs[[N]]=kf$xf[[N]]
44Ps[[N]]=kf$Pf[[N]]
45for(k in N:2) {
46J[[k-1]]=(kf$Pf[[k-1]]%*%t(Phi))%*%solve(kf$Pp[[k]])
47xs[[k-1]]=kf$xf[[k-1]]+J[[k-1]]%*%(xs[[k]]-kf$xp[[k]])
48Ps[[k-1]]=kf$Pf[[k-1]]+J[[k-1]]%*%(Ps[[k]]-kf$Pp[[k]])%*%t(J[[k-1]])
49}
50list(xs=xs,Ps=Ps,J=J,xp=kf$xp,Pp=kf$Pp,xf=kf$xf,Pf=kf$Pf,like=kf$like,Kn=kf$K)
51}
52Linn=function(para){
53phi=para[1]
54sigw=para[2] # this is the standard dev
55sigv=para[3] # this is the standard dev
56mu0=0
57Sigma0=(sigw^2)/(1-phi^2)
58Sigma0[Sigma0<0] =0 # this makes sure Sigma0 is never negative
59kf = Kfilter(num,y,A,mu0,Sigma0,phi,sigw,sigv)
60return(kf$like)
61}
62set.seed(par2)
63num=par1 #100
64N=num+1
65x =arima.sim(n=N, list(ar = par3, sd=par4)) # x[1]=x_0, x[2]=x_1,...
66v = rnorm(num,0,par5)
67y=x[2:N]+v
68data=ts(y)
69u=ts.intersect(data,lag(data,-1),lag(data,-2));
70varu=var(u);
71coru=cor(u);
72phi=coru[1,3]/coru[1,2];
73q = (1-phi^2)*varu[1,2]/phi;
74r = varu[1,1] - q/(1-phi^2);
75warning = ""
76if (q < 0)
77{
78warning = "Warning: initial estimate for SD(w) could not be computed. Initial value is set to 1.
"
79q=1
80}
81if (r < 0)
82{
83warning = paste(warning,"Warning: initial estimate for SD(v) could not be computed. Initial value is set to 1.
")
84r=1
85}
86initpar=c(phi,sqrt(q),sqrt(r))
87initpar # view the initial estimates
88A=matrix(1,N,1)
89A=as.list(A,N) # A has to be a list, A[[i+1]] for y[i]. A[[1]] not used.
90est=optim(initpar, Linn, NULL, method = "BFGS", hessian = TRUE, control=list(trace=1,REPORT=1))
91est # for a summary
92stderr=sqrt(diag(solve(est$hessian)))
93cbind(est$par,stderr) # list estimates and SEs
94load(file="createtable")
95a<-table.start()
96a<-table.row.start(a)
97a<-table.element(a,"Parameter",1,TRUE)
98a<-table.element(a,"Value",1,TRUE)
99a<-table.element(a,"S.D.",1,TRUE)
100a<-table.row.end(a)
101a<-table.row.start(a)
102a<-table.element(a,"#simulated values",header=TRUE)
103a<-table.element(a,par1)
104a<-table.element(a,"-")
105a<-table.row.end(a)
106a<-table.row.start(a)
107a<-table.element(a,"random seed",header=TRUE)
108a<-table.element(a,par2)
109a<-table.element(a,"-")
110a<-table.row.end(a)
111a<-table.row.start(a)
112a<-table.element(a,"true phi",header=TRUE)
113a<-table.element(a,par3)
114a<-table.element(a,"-")
115a<-table.row.end(a)
116a<-table.row.start(a)
117a<-table.element(a,"true SD(w)",header=TRUE)
118a<-table.element(a,par4)
119a<-table.element(a,"-")
120a<-table.row.end(a)
121a<-table.row.start(a)
122a<-table.element(a,"true SD(v)",header=TRUE)
123a<-table.element(a,par5)
124a<-table.element(a,"-")
125a<-table.row.end(a)
126a<-table.row.start(a)
127a<-table.element(a,"estimated phi",header=TRUE)
128a<-table.element(a,est$par[1])
129a<-table.element(a,stderr[1])
130a<-table.row.end(a)
131a<-table.row.start(a)
132a<-table.element(a,"SD(w)",header=TRUE)
133a<-table.element(a,est$par[2])
134a<-table.element(a,stderr[2])
135a<-table.row.end(a)
136a<-table.row.start(a)
137a<-table.element(a,"SD(v)",header=TRUE)
138a<-table.element(a,est$par[3])
139a<-table.element(a,stderr[3])
140a<-table.row.end(a)
141a<-table.end(a)
142table.save(a,file="mytable1.tab")
143if (warning != "")
144{
145load(file="createtable")
146a<-table.start()
147a<-table.row.start(a)
148a<-table.element(a,"Warnings about Parameter Estimation",1,TRUE)
149a<-table.row.end(a)
150a<-table.row.start(a)
151a<-table.element(a,warning)
152a<-table.row.end(a)
153a<-table.end(a)
154table.save(a,file="mytable2.tab")
155}
Top | Output | Charts | References | History | Feedback



Cite this software as:
Wessa P., (2008), Reproduction of Example 6.6 in Time Series Analysis and Its Applications (v1.0.4) in Free Statistics Software (v1.1.23-r2), Office for Research Development and Education, URL http://www.wessa.net/rwasp_shumwaystofferex66.wasp/
The R code is based on :
Shumway R.H., Stoffer D.S., Time Series Analysis and Its Applications (2nd ed. with R examples), Springer, 2006
Shumway R.H., Stoffer D.S., Time Series Analysis and Its Applications (2nd ed. with R examples), URL http://www.stat.pitt.edu/stoffer/tsa2/#Rchapter6
Belisle, C. J. P. (1992) Convergence theorems for a class of simulated annealing algorithms on Rd., J Applied Probability, vol. 29, 885-895.
Byrd, R. H., Lu, P., Nocedal, J. and Zhu, C. (1995) A limited memory algorithm for bound constrained optimization., SIAM J. Scientific Computing, vol. 16, 1190-1208.
Fletcher, R. and Reeves, C. M. (1964) Function minimization by conjugate gradients., Computer Journal, vol. 7, 148-154.
Nash, J. C. (1990), Compact Numerical Methods for Computers., Linear Algebra and Function Minimisation., Adam Hilger.
Nelder, J. A. and Mead, R. (1965), A simplex algorithm for function minimization., Computer Journal, vol. 7, 308-313.
Nocedal, J. and Wright, S. J. (1999), Numerical Optimization., Springer.
Top | Output | Charts | References | History | Feedback
Delete history
Server DateModuleCommand
Wed, 20 Aug 2008 01:14:38 -0600Start of session-
Top | Output | Charts | References | History | Feedback

Please, send us your feedback!
Overall rating
What do you think about the response time?
Rate the facility with which you interact with our software.
Is the output presented in an understandable (clear) form?
How did you find this website/module?
Do you have any comments or suggestions for our site?
Please enter the code that is displayed in this picture: captcha

To cite Wessa.net in publications use:
Wessa, P. (2008), Free Statistics Software, Office for Research Development and Education,
version 1.1.23-r2, URL http://www.wessa.net/

© Resa R&D, and the 'Office for Research, Development, and Education' - All rights reserved. Academic license for non-commercial use only.
This website is an intellectual and physical property of Resa R&D. This includes: html content, graphical illustrations (gif files), computer software, online or electronic documentation, associated media, and printed materials. The free use of the scientific content, services, and applications in this website is granted for non commercial use only. In any case, the source (url) should always be clearly displayed. Under no circumstances are you allowed to reproduce, copy or redistribute the design, layout, or any content of this website (for commercial use) including any materials contained herein without the express written permission of Resa R&D or the Office for Research, Development, and Education.

Information provided on this web site is provided "AS IS" without warranty of any kind, either express or implied, including, without limitation, warranties of merchantability, fitness for a particular purpose, and noninfringement. We use reasonable efforts to include accurate and timely information and periodically update the information, and software without notice. However, Resa R&D makes no warranties or representations as to the accuracy or completeness of such information (or software), and it assumes no liability or responsibility for errors or omissions in the content of this web site, or any software bugs in online applications. Your use of this web site is AT YOUR OWN RISK. Under no circumstances and under no legal theory shall Resa R&D be liable to you or any other person for any direct, indirect, special, incidental, exemplary, or consequential damages arising from your access to, or use of, this web site.

Software Version : 1.1.23-r2
Algorithms & Software : Prof. dr. P. Wessa
Facilities : Resa R&D - Office for Research, Development, and Education
Server : www.wessa.net

Comments, Feedback & Errors | Privacy Policy | Statistics Resources | Wessa.net Home

 © Wessa.Net 2002-2008  
 
 

Home Page
Equation Plotter
Time Series Analysis (new)
Time Series Analysis (old)
Multiple Regression (new)
Multiple Regression (old)
Descriptive Statistics
Statistical Distributions
Hypothesis Testing
Statistics Education

Academic citations
Computations Archive
Search Computations
R Project
FAQ
About Wessa.net
Powered by Linux

Server status page
History list
Edit R Code
Google
Web wessa.net