hi all how does one simulate a random walk process? i.e y(0)=0 y(t)=y(t-1)+ e(t) where e(t) is normal(0,1) say. Regards allan
Try this for size: Not a very efficient way to do it though!!! Get.Random.Walk<-function(){ length.walk<-1000 rand.walk<-rep(0,length.walk) for(i in 2:length.walk) { rand.walk[i]<-rand.walk[i-1]+rnorm(1, mean=0, sd=1) } return(rand.walk) } plot(Get.Random.Walk()) -----Original Message----- From: allan clark [mailto:allan@stats.uct.ac.za] Sent: 10 February 2004 14:48 To: Rhelp Subject: [R] R: lags hi all how does one simulate a random walk process? i.e y(0)=0 y(t)=y(t-1)+ e(t) where e(t) is normal(0,1) say. Regards allan KSS Ltd Seventh Floor St James's Buildings 79 Oxford Street Manchester M1 6SS England Company Registration Number 2800886 Tel: +44 (0) 161 228 0040 Fax: +44 (0) 161 236 6305 mailto:kssg@kssg.com http://www.kssg.com The information in this Internet email is confidential and m...{{dropped}}
How about: y<-cumsum(c(0,rnorm(100))) On Tue, 10 Feb 2004, allan clark wrote:> hi all > > how does one simulate a random walk process? > > i.e > > y(0)=0 > > y(t)=y(t-1)+ e(t) > > where e(t) is normal(0,1) say. > > Regards > allan >
> how does one simulate a random walk process? > > i.e > > y(0)=0 > > y(t)=y(t-1)+ e(t) > > where e(t) is normal(0,1) say.cumsum(c(0,rnorm(10000))) ? Claus -- ***************************************** Claus Thorn Ekstr?m <ekstrom at dina.kvl.dk> Dept of Mathematics and Physics, KVL Thorvaldsensvej 40 DK-1871 Frederiksberg C Denmark Phone:[+45] 3528 2341 Fax: [+45] 3528 2350
On Tue, 10 Feb 2004, allan clark wrote:> hi all > > how does one simulate a random walk process? > > i.e > > y(0)=0 > > y(t)=y(t-1)+ e(t) > > where e(t) is normal(0,1) say. >e<-rnorm(100) y<-cumsum(e) -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
Dear Alan, Perhaps there's a more clever solution, but the following will work and follows directly from your statement of the problem (e.g., for 100 observations): e <- rnorm(100) y <- rep(0, 101) for (t in 2:101) y[t] <- y[t-1] + e[t] y <- y[-1] I hope that this helps, John On Tue, 10 Feb 2004 16:48:22 +0200 allan clark <allan at stats.uct.ac.za> wrote:> hi all > > how does one simulate a random walk process? > > i.e > > y(0)=0 > > y(t)=y(t-1)+ e(t) > > where e(t) is normal(0,1) say. > > Regards > allan
allan clark <allan at stats.uct.ac.za> writes:> hi all > > how does one simulate a random walk process? > > i.e > > y(0)=0 > > y(t)=y(t-1)+ e(t) > > where e(t) is normal(0,1) say.E.g., c(0,cumsum(rnorm(1000))) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907