# simulation Classic List Threaded 5 messages Open this post in threaded view
|

## simulation

 Dear R users, suppose we have a random walk such as: v_t+1 = v_t + e_t+1 where e_t is a normal IID noise pocess with mean = m and standard deviation = sd and v_t is the fundamental value of a stock. Now suppose I want a trading strategy to be: x_t+1 = c(v_t â p_t) where c is a costant. I know, from the paper where this equations come from (Farmer and Joshi, The price dynamics of common trading strategies, 2001) that the induced price dynamics is: r_t+1 = âa*r_t + a*e_t + theta_t+1 and p_t+1 = p_t +r_t+1 where r_t = p_t â p_t-1 , e_t = v_t â v_t-1 and a = c/lambda (lambda is another constant). How can I simulate the equations I have just presented? I have good confidence with R for statistical analysis, but not for simulation therefore I apologize for my ignorance. What I came up with is the following: ##general settings c<-0.5 lambda<-0.3 a<-c/lambda n<-500 ## Eq.12 (the v_t random walk) V_init_cond<-0 Et<-ts(rnorm(n+100,mean=0,sd=1)) Vt<-Et*0 Vt<-V_init_cond+Et for(i in 2:(n+100)) { Vt[i]<-Vt[i-1]+Et[i] } Vt<-ts(Vt[(length(Vt)-n+1):length(Vt)]) plot(Vt) ## Eq.13 (the strategy) Xt_init_cond<-0 Xt<-Xt_init_cond*0 Xt<-c(Vt-Pt) for(i in 2:(n)){ Xt[i]<-c(Vt[i-1]-Pt[i-1]) } Xt<-ts(Xt[(length(Xt)-n+1):length(Xt)]) plot(Xt) ## Eq. 14 (pice dynamics) P_init_cond<-0 Pt<-Rt*0 Pt<-P_init_cond+Rt for(i in 2:(n+100)) { Pt[i]<-Pt[i-1]+Rt[i] } Pt<-ts(Pt[(length(Pt)-n+1):length(Pt)]) plot(Pt) Rt_init_cond<-0 Rt<-Rt_init_cond*0 Rt<- -a*Rt+a*Et+e for(i in 2:(n)){ Rt[i]<- -a*Rt[i-1]+a*Et[i-1]+e[i] } Rt<-ts(Rt[(length(Rt)-n+1):length(Rt)]) plot(Rt) I donât think the code above is correct, and I donât even know if this is the approach I have to take. Any suggestion is warmly appreciated. thanks, Simone Gogna         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Open this post in threaded view
|

## Re: simulation

 Well the simplest but most inefficient approach is a "for" loop. However the best ant kinda sophisticated approach is to use "Reduce". I did not analyse your problem as it would take some time for dubious knowledge, but here is a simple random walk simulation presented in both ways. Reduce(function(x,eps) {x+eps},rnorm(100,sd=2.5),accumulate=TRUE) a=numeric();a=1 for (i in 2:100){a[i]=a[i-1]+rnorm(1,sd=2.5)} If you can present me with the paper I might be able to simulate the ones you need. As I am not going to analyse something what I doubt it will have any value to me. Also for efficiency: Unit: microseconds     expr      min       lq   median        uq      max 1  sim() 1074.634 1100.702 1120.667 1172.0290 4291.588 2 sim2()  499.730   507.990   527.797  582.7205 1516.850 where sim2 is Reduce function. Kind regards,--   Dominykas Grigonis On Saturday, 29 December 2012 at 13:33, Simone Gogna wrote: > Dear R users, > suppose we have a random walk such as: >   > v_t+1 = v_t + e_t+1 >   > where e_t is a normal IID noise pocess with mean = m and standard deviation = sd and v_t is the fundamental value of a stock. >   > Now suppose I want a trading strategy to be: >   > x_t+1 = c(v_t â p_t) >   > where c is a costant. > I know, from the paper where this equations come from (Farmer and Joshi, The price dynamics of common trading strategies, 2001) that the induced price dynamics is: >   > r_t+1 = âa*r_t + a*e_t + theta_t+1 >   > and >   > p_t+1 = p_t +r_t+1 >   > where r_t = p_t â p_t-1 , e_t = v_t â v_t-1 and a = c/lambda (lambda is another constant). >   > How can I simulate the equations I have just presented? > I have good confidence with R for statistical analysis, but not for simulation therefore I apologize for my ignorance. > What I came up with is the following: >   > ##general settings > c<-0.5 > lambda<-0.3 > a<-c/lambda > n<-500   >   > ## Eq.12 (the v_t random walk) > V_init_cond<-0 > Et<-ts(rnorm(n+100,mean=0,sd=1)) > Vt<-Et*0 > Vt<-V_init_cond+Et > for(i in 2:(n+100)) { > Vt[i]<-Vt[i-1]+Et[i] > } > Vt<-ts(Vt[(length(Vt)-n+1):length(Vt)]) > plot(Vt) >   > ## Eq.13 (the strategy) > Xt_init_cond<-0 > Xt<-Xt_init_cond*0 > Xt<-c(Vt-Pt) > for(i in 2:(n)){ > Xt[i]<-c(Vt[i-1]-Pt[i-1]) > } > Xt<-ts(Xt[(length(Xt)-n+1):length(Xt)]) > plot(Xt) >   > ## Eq. 14 (pice dynamics) > P_init_cond<-0 > Pt<-Rt*0 > Pt<-P_init_cond+Rt > for(i in 2:(n+100)) { > Pt[i]<-Pt[i-1]+Rt[i] > } > Pt<-ts(Pt[(length(Pt)-n+1):length(Pt)]) > plot(Pt) > Rt_init_cond<-0 > Rt<-Rt_init_cond*0 > Rt<- -a*Rt+a*Et+e > for(i in 2:(n)){ > Rt[i]<- -a*Rt[i-1]+a*Et[i-1]+e[i] > } > Rt<-ts(Rt[(length(Rt)-n+1):length(Rt)]) > plot(Rt) >   > I donât think the code above is correct, and I donât even know if this is the approach I have to take. > Any suggestion is warmly appreciated. >   > thanks, > Simone Gogna   > [[alternative HTML version deleted]] >   > _______________________________________________ > [hidden email] (mailto:[hidden email]) mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions should go. >   >           [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Open this post in threaded view
|

## Re: simulation

 Srry, I missed half of your post. But again, I need the presented equations. Kind regards,--   Dominykas Grigonis On Saturday, 29 December 2012 at 15:12, Dominykas Grigonis wrote: > Well the simplest but most inefficient approach is a "for" loop. However the best ant kinda sophisticated approach is to use "Reduce". >   > I did not analyse your problem as it would take some time for dubious knowledge, but here is a simple random walk simulation presented in both ways. >   > Reduce(function(x,eps) {x+eps},rnorm(100,sd=2.5),accumulate=TRUE) >   > a=numeric();a=1 > for (i in 2:100){a[i]=a[i-1]+rnorm(1,sd=2.5)} >   > If you can present me with the paper I might be able to simulate the ones you need. As I am not going to analyse something what I doubt it will have any value to me. >   > Also for efficiency: > Unit: microseconds >     expr      min       lq   median        uq      max > 1  sim() 1074.634 1100.702 1120.667 1172.0290 4291.588 > 2 sim2()  499.730   507.990   527.797  582.7205 1516.850 >   >   > where sim2 is Reduce function. >   >   >   > Kind regards,--   > Dominykas Grigonis >   >   > On Saturday, 29 December 2012 at 13:33, Simone Gogna wrote: >   > > Dear R users, > > suppose we have a random walk such as: > >   > > v_t+1 = v_t + e_t+1 > >   > > where e_t is a normal IID noise pocess with mean = m and standard deviation = sd and v_t is the fundamental value of a stock. > >   > > Now suppose I want a trading strategy to be: > >   > > x_t+1 = c(v_t â p_t) > >   > > where c is a costant. > > I know, from the paper where this equations come from (Farmer and Joshi, The price dynamics of common trading strategies, 2001) that the induced price dynamics is: > >   > > r_t+1 = âa*r_t + a*e_t + theta_t+1 > >   > > and > >   > > p_t+1 = p_t +r_t+1 > >   > > where r_t = p_t â p_t-1 , e_t = v_t â v_t-1 and a = c/lambda (lambda is another constant). > >   > > How can I simulate the equations I have just presented? > > I have good confidence with R for statistical analysis, but not for simulation therefore I apologize for my ignorance. > > What I came up with is the following: > >   > > ##general settings > > c<-0.5 > > lambda<-0.3 > > a<-c/lambda > > n<-500   > >   > > ## Eq.12 (the v_t random walk) > > V_init_cond<-0 > > Et<-ts(rnorm(n+100,mean=0,sd=1)) > > Vt<-Et*0 > > Vt<-V_init_cond+Et > > for(i in 2:(n+100)) { > > Vt[i]<-Vt[i-1]+Et[i] > > } > > Vt<-ts(Vt[(length(Vt)-n+1):length(Vt)]) > > plot(Vt) > >   > > ## Eq.13 (the strategy) > > Xt_init_cond<-0 > > Xt<-Xt_init_cond*0 > > Xt<-c(Vt-Pt) > > for(i in 2:(n)){ > > Xt[i]<-c(Vt[i-1]-Pt[i-1]) > > } > > Xt<-ts(Xt[(length(Xt)-n+1):length(Xt)]) > > plot(Xt) > >   > > ## Eq. 14 (pice dynamics) > > P_init_cond<-0 > > Pt<-Rt*0 > > Pt<-P_init_cond+Rt > > for(i in 2:(n+100)) { > > Pt[i]<-Pt[i-1]+Rt[i] > > } > > Pt<-ts(Pt[(length(Pt)-n+1):length(Pt)]) > > plot(Pt) > > Rt_init_cond<-0 > > Rt<-Rt_init_cond*0 > > Rt<- -a*Rt+a*Et+e > > for(i in 2:(n)){ > > Rt[i]<- -a*Rt[i-1]+a*Et[i-1]+e[i] > > } > > Rt<-ts(Rt[(length(Rt)-n+1):length(Rt)]) > > plot(Rt) > >   > > I donât think the code above is correct, and I donât even know if this is the approach I have to take. > > Any suggestion is warmly appreciated. > >   > > thanks, > > Simone Gogna   > > [[alternative HTML version deleted]] > >   > > _______________________________________________ > > [hidden email] (mailto:[hidden email]) mailing list > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> > -- Subscriber-posting only. If you want to post, subscribe first. > > -- Also note that this is not the r-help list where general R questions should go. > >   > >   > >   >   >           [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.