[R-sig-Finance] MC simulation

Yuri Volchik volchik2000 at list.ru
Thu Jun 22 15:15:34 CEST 2006


Jens,

you might try to look at the "merge" function.
http://spider.stat.umn.edu/R/library/base/html/merge.html



> I am pretty new to R. However, I checked the introduction to R
> paper and did not find a solution for my problem. I use a mean
> reversion process to simulate US RMBS spreads for different rating
> classes. My problem is that for each MC run I would like to generate
> a vector and attach these vectors with each other to generate a
> matrix, which contains all the simulation results. Right now I just
> see the results of every simulation run because I plot them. I tried
> something using the print(x) (as.matrix() and so forth) command but
> I didn't work out properly. I would very much appreciate any help on
> that problem. Thanks in advance. 

> Cheers 
> Jens


> #Set up variables
> AAA<-data$AAA
> AA<-data$AA
> A<-data$A
> BBB<-data$BBB

> logAAA<-log(AAA)
> logAA<-log(AA)
> logA<-log(A)
> logBBB<-log(BBB)

> diffAAA<-logAAA[1:100]-logAAA[2:101]
> diffAA<-logAA[1:100]-logAA[2:101]
> diffA<-logA[1:100]-logA[2:101]
> diffBBB<-logBBB[1:100]-logBBB[2:101]

> #Estimate coefficients by OLS
> regAAA<-lm(diffAAA ~ logAAA[2:101])

> #Parameters for O-U process
> n?<--log(1+regAAA$coefficients[2])
> averagex<--(regAAA$coefficients[1]/regAAA$coefficients[2])
> resierror<-sqrt(var(regAAA$residuals)) #check this 
> sigma<-resierror*sqrt((2*log(1+regAAA$coefficients[2]))/((1+regAAA$coefficients[2])^2-1))


> lAAA<-length(diffAAA)
> x<-logAAA[lAAA]
> t<-c(1:60)
> delta<-1
> mcsim<-40

> par(mfrow=c(4,1))

> #MC for AAA
> for ( k in 1:mcsim ){

>     rn<-rnorm(length(t), mean=0,sd=1)

>     for ( i in 1:length(t) ){
>        
> x[t+1]<-x[t]*exp(-n?*delta*t)+averagex*(1-exp(-n?*delta*t))+sigma*sqrt((1-exp(-2*n?*delta*t))/(2*n?))*rn[t]
>         }
>     vect<-c(logAAA,x[2:length(x)])
>     plot.ts(exp(vect), plot.type = c("single"),col="red",type="l")
>     lines(AAA)
> }




-- 
Best regards,
 Yuri                            mailto:volchik2000 at list.ru



More information about the R-SIG-Finance mailing list