[R-sig-Finance] MC simulation

Jens Wildermuth Jens.Wildermuth at gmx.de
Thu Jun 22 11:03:56 CEST 2006


Hi, 

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)
}

-- 


Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer



More information about the R-SIG-Finance mailing list