# A Question About Writing a Function

Hi,
I have a question about writing a function for cumulative prospect theory. I have already written the basic function, but i dont know how to express the 'cumulative' part (as graph below). The function needs to consider first weather A1_payoff > A2_payoff.

CPT_V1 <- function (A1_payoff,
A2_payoff,
B1_payoff,
B2_payoff,
A1_prob,
A2_prob,
B1_prob,
B2_prob,
alpha,
epsilon,
lambda,
gamma,
xi){
u <- function(x) {
ifelse(x > 0,
(abs(x)) ^ alpha,
(sign(x)*lambda) * ((abs(x)) ^ alpha))
}
w <- function (x,y){
ifelse(x > 0,
(y^gamma) / (((y ^ gamma) + ((1-y)^gamma)) ^ (1/gamma)),
(y^xi) / (((y ^ xi) + ((1-y)^xi)) ^ (1/xi)))
}
v.A <- w(A1_payoff,A1_prob) * u(A1_payoff) + w(A2_payoff,A2_prob) * u(A2_payoff)
v.B <- w(B1_payoff,B1_prob) * u(B1_payoff) + w(B1_payoff,B1_prob) * u(B2_payoff)
v.diff <- v.A-v.B
}

Hi,
If I understand your question correctly, You need to repeat calculation until some threshold.

There's a lot of way to do this but I believe this post about recursive will inspire you.

If concept of recursion doesn't fit to you, try to search Fibonachi will help you a lot.

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