# Ruin probability in R, how could I get values? What is wrong in this code.

Hello, I should calculate ruin probability with R. I have code, however I din not know how print values. I should get table with answers.
date x:
table1 <- matrix(c(0,1,2,3,4,5,6,7,0.6,0.02,0.16,0.02,0.1,0.05,0.02,0.03), ncol=8, byrow=TRUE)
date y:
table2 <- matrix(c(0,1,2,0.49,0.43,0.08), ncol=3, byrow=TRUE)
create function, which calculate Hx(u)
Hx <-function(u){
x <-0
y<- 1
for (y in 1:ncol(table1)) {
if((table1[1,y]) <= u) {
x <- x + table1[2,y]
}}
return(x)
}
create function which calculate Hy(u)
Hy <-function(u){
x <-0
y<- 1
for (y in 1:ncol(table2)) {
if((table2[1,y]) <= u) {
x <- x + table2[2,y]
}}
return(x)
}
create function which calculate Hx(k)
hx <-function(k){
r <-0
if (is.element(k,table1[1,])) {
r <- table1[2,match(k,table1[1,])]
}
return(r)
}
create function which calculate Hy(u)
hy <-function(k){
r <-0
if (is.element(k,table2[1,])) {
r <- table2[2,match(k,table2[1,])]
}
return(r)
}
create two functions, which are depend on each other (u and t)
memo<-new.env(hash=TRUE)
psi1 <- function(u,t) {
if (t == 1) return (1 - Hx(u))
if(exists(paste(u,t),memo,inherits=FALSE))
return(get(paste(u,t),memo))
n<-(1 - Hx(u))
for (k in 0:u){ n <- n + psi2(u+1-k,t-1)*hx(k)
assign(paste(u,t),n,envir=memo)
}
return(n)
}
memo1<-new.env(hash=TRUE)
psi2 <- function(u,t) {
if (t == 1) return (1 - Hy(u))
if(exists(paste(u,t),memo1,inherits=FALSE))
return(get(paste(u,t),memo1))
n<-(1 - Hy(u))
for (k in 0:u){ n <- n + psi1(u+1-k,t-1)*hy(k)
assign(paste(u,t),n,envir=memo1)
}
return(n)
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.