[R-sig-Finance] MC simulations from R-SIG-Finance Digest, Vol 25, Issue 17

Daniele Amberti amberti at inwind.it
Thu Jun 22 12:16:28 CEST 2006


    vect<-c(logAAA,x[2:length(x)])
    plot.ts(exp(vect), plot.type = c("single"),col="red",type="l")
    lines(AAA)

istead of plot you should use cbind()
for istance:

vect <- c()
results <- cbind(results, vect)

so results will have a column for each calculated vect...

Regards
Daniele Amberti


Message: 5
Date: Thu, 22 Jun 2006 11:03:56 +0200
From: "Jens Wildermuth" <Jens.Wildermuth at gmx.de>
Subject: [R-sig-Finance] MC simulation
To: r-sig-finance at stat.math.ethz.ch
Message-ID: <20060622090356.104080 at gmx.net>
Content-Type: text/plain; charset="iso-8859-1"

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$coefficie
nts[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-ex
p(-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



------------------------------

_______________________________________________
R-SIG-Finance mailing list
R-SIG-Finance at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-finance


End of R-SIG-Finance Digest, Vol 25, Issue 17



More information about the R-SIG-Finance mailing list