# [R] repeat a function

Eric.Pueyo at avivainvestors.com Eric.Pueyo at avivainvestors.com
Wed Nov 1 13:31:08 CET 2017

```I want to populate the matrix prb through the function HWMProb <- function (a,j,dt) that encapsulates different functions (please see code below), using j= 0:2 for each j.

It only populates prb if I specify each function independently in the global environment and then run the loop with the iF statement, as per below.
for (j in 0:2) {
if (j==0) {
prb["0","1"] <- ProbUP(a,j,dt)
prb["0","0"] <- ProbMID(a,j,dt)
prb["0","-1"] <- ProbDWN(a,j,dt)
}
else {
if (j==jmax) {
prb[paste(j,sep = ""),"1"] <- TOPProbUP(a,j,dt)
prb[paste(j,sep = ""),"0"] <- TOPProbMID(a,j,dt)
prb[paste(j,sep = ""),"-1"] <- TOPProbDWN(a,j,dt)
prb[paste(-j,sep = ""),"1"] <- BTTMProbUP(a,-j,dt)
prb[paste(-j,sep = ""),"0"] <- BTTMProbMID(a,-j,dt)
prb[paste(-j,sep = ""),"-1"] <- BTTMProbDWN(a,-j,dt)
}
else {
prb[paste(j,sep = ""),"1"] <- ProbUP(a,j,dt)
prb[paste(j,sep = ""),"0"] <- ProbMID(a,j,dt)
prb[paste(j,sep = ""),"-1"] <- ProbDWN(a,j,dt)
prb[paste(-j,sep = ""),"1"] <- ProbUP(a,-j,dt)
prb[paste(-j,sep = ""),"0"] <- ProbMID(a,-j,dt)
prb[paste(-j,sep = ""),"-1"] <- ProbDWN(a,-j,dt)
} # Close 2nd IF
} # Close 1st IF
}

Kind regards,

Eric Pueyo
Investment Risk Analyst
Email:  Eric.Pueyo at avivainvestors.com<mailto:Eric.Pueyo at avivainvestors.com>
D: +44 (0)20 7809 8070
No. 1 Undershaft, London EC3P 3DQ
Web: www.avivainvestors.com<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.avivainvestors.com_&d=CwMFAg&c=zUO0BtkCe66yJvAZ4cAvZg&r=-34SVFvqwmZvbxD0ShuYglbuijP84lygitjFgiP1fxI&m=kRg3I7ESFxV-KaNbYHN6DPupgyEmFCiThNO6oDnpAFs&s=tQa1EuXKNfzRgiR2HgG0H_tW_X-BCpgebe5AL9A_GC8&e=>

jmax<-2
prb <-matrix(0L,nrow=5, ncol=3)
rownames(prb) <- c(seq(-2,2, by = 1))
colnames(prb) <- c(-1,0,1)
a<- 0.1
dt<-1

ExpX <-function(x,a,dt) {                              ######Defines the Expectation of X on t+1 | t
ExpX <- x*exp(-a*dt)
ExpX
}
Mfactor<-function(a,dt)  {                           #######Factor multiplicative
Mfactor<- exp(-a*dt)-1
Mfactor
}
VarX <-function(sigma,a,dt) {                         #######Defubes the Variance of X on t+1 | t
VarX <- (sigma^2/(2*a))*(1-exp(-2*a*dt))
VarX
}
DeltaX <-function(sigma,a,dt) {                       ######Defines the change of X
DeltaX<- sqrt(3*VarX(sigma,a,dt))
DeltaX<-value(DeltaX)
}

Mfactor<-function(a,dt)  {                           #######Factor multiplicative
Mfactor<- exp(-a*dt)-1
Mfactor
}

KNode<-function(sigma,x,a,j,dt) {                    ######Central Node
KNode<- round(ExpX(x,a,dt)/DeltaX(sigma,a,dt))
KNode
}

####### Probability Calculations taking into account different branches
HWMProb <- function (a,j,dt) {
######################### DESCRIPTION #####################################
ProbUP<- function( a, j, dt) 1 / 6 + ((j ^ 2 * Mfactor(a, dt) ^ 2 + j * Mfactor(a, dt)) / 2)                     ######### Probability X going up
ProbMID<- function(a, j, dt) 2 / 3 - (j ^ 2 * Mfactor(a, dt) ^ 2)                             ######## Probability X going middle
ProbDWN<-function( a, j, dt) 1 / 6 + ((j ^ 2 * Mfactor(a, dt) ^ 2 - j * Mfactor(a, dt)) / 2)                  #######  Probability X going down
TOPProbUP<- function( a, j, dt) 7 / 6 + (j ^ 2 * Mfactor(a, dt) ^ 2 + 3 * j * Mfactor(a, dt)) / 2                 ####### Top branch probability going up
TOPProbMID<- function(a, j, dt) -1 / 3 - j ^ 2 * Mfactor(a, dt) ^ 2 - 2 * j * Mfactor(a, dt)              ####### Top branch probability going MID
TOPProbDWN<- function( a, j, dt) 1 / 6 + (j ^ 2 * Mfactor(a, dt) ^ 2 + j * Mfactor(a, dt)) / 2               ####### Top branch probability going DOWN
BTTMProbUP<- function( a, j, dt) 1 / 6 + (j ^ 2 * Mfactor(a, dt) ^ 2 - j * Mfactor(a, dt)) / 2           ####### Bottom branch probability going u
BTTMProbMID<- function( a, j, dt) -1 / 3 - j ^ 2 * Mfactor(a, dt) ^ 2 + 2 * j * Mfactor(a, dt)               ####### Bottom branch probability going MID
BTTMProbDWN<- function( a, j, dt) 7 / 6 + (j ^ 2 * Mfactor(a, dt) ^ 2 - 3 * j * Mfactor(a, dt)) / 2              ####### Bottom branch probability going DOWN

if (j==0) {
prb["0","1"] <- ProbUP(a,j,dt)
prb["0","0"] <- ProbMID(a,j,dt)
prb["0","-1"] <- ProbDWN(a,j,dt)
}
else {
if (j==jmax) {
prb[paste(j,sep = ""),"1"] <- TOPProbUP(a,j,dt)
prb[paste(j,sep = ""),"0"] <- TOPProbMID(a,j,dt)
prb[paste(j,sep = ""),"-1"] <- TOPProbDWN(a,j,dt)
prb[paste(-j,sep = ""),"1"] <- BTTMProbUP(a,-j,dt)
prb[paste(-j,sep = ""),"0"] <- BTTMProbMID(a,-j,dt)
prb[paste(-j,sep = ""),"-1"] <- BTTMProbDWN(a,-j,dt)
}
else {
prb[paste(j,sep = ""),"1"] <- ProbUP(a,j,dt)
prb[paste(j,sep = ""),"0"] <- ProbMID(a,j,dt)
prb[paste(j,sep = ""),"-1"] <- ProbDWN(a,j,dt)
prb[paste(-j,sep = ""),"1"] <- ProbUP(a,-j,dt)
prb[paste(-j,sep = ""),"0"] <- ProbMID(a,-j,dt)
prb[paste(-j,sep = ""),"-1"] <- ProbDWN(a,-j,dt)
} # Close 2nd IF
} # Close 1st IF
} #Close Formula

for (j in 0:2) {
HWMProb(a,j,dt)

}

[[alternative HTML version deleted]]

```