[R-SIG-Finance] simulation

Patrick Burns patrick at burns-stat.com
Sun Dec 30 10:35:36 CET 2012


Oops, not quite right:

On 30/12/2012 09:30, Patrick Burns wrote:
> Faster still will be:
>
> rets <- cumsum(rnorm(n, sd=sd))
>
> If you want to change that from returns to
> prices, then do
>
> init.price * c(0, exp(rets))

init.price * c(1, exp(rets))

or

init.price * exp(c(0, rets))

>
> Pat
>
> On 29/12/2012 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]=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[1]<-V_init_cond+Et[1]
>>> 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[2]<-c(Vt[1]-Pt[1])
>>> 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[1]<-P_init_cond+Rt[1]
>>> 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[2]<- -a*Rt[1]+a*Et[1]+e[2]
>>> 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]]
>>>
>>> _______________________________________________
>>> R-SIG-Finance at r-project.org (mailto:R-SIG-Finance at r-project.org)
>>> 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]]
>>
>>
>>
>> _______________________________________________
>> R-SIG-Finance at r-project.org 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.
>>
>

-- 
Patrick Burns
patrick at burns-stat.com
http://www.burns-stat.com
http://www.portfolioprobe.com/blog
twitter: @portfolioprobe



More information about the R-SIG-Finance mailing list