[R-sig-Finance] MC simulations from R-SIG-Finance Digest, Vol 25, Issue 17
Patrick Burns
patrick at burns-stat.com
Sun Jun 25 20:39:14 CEST 2006
Actually 'cbind' is not a very good choice. Operations
like:
results <- cbind(results, vect)
and
vect <- c(vect, newval)
eat up a lot of memory. Instead you want to do something
like:
ans <- array(NA, c(length.result, trials))
for(i in 1:trials) {
this.result <- ...
ans[,i] <- this.result
}
S Poetry has some explanation of the fragmentation of
memory that the "bad" operations create. S Poetry is also
one place, among many, to find information on the commands
to write code such as a simulation like this.
Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
Daniele Amberti wrote:
> 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)
>}
>
>
>
More information about the R-SIG-Finance
mailing list