0

The situation is as follows:

I need to create a dataset of triplets where we have discrete distribution of stock prices S <- c(80,100,120,140,160), with probability P <- c(0.2, 0.3, 0.2, 0.2, 0.1), call option C <- max(S-120,0) = c(0,0,0,20,40) and liability of an option which pays 30 if in a certain region otherwise zero, namely L = I{110 \leq S \leq 150} = c(0,0,30,30,0) <- c(0,0,30,30,0). It is important to mention that if P[1] = 80, then C[1] and L[1]. This holds for i = 1,2,3,4,5. How do you create a dataset for N = 10000 simulations where each value for i corresponds to the other two values for the same i?

This is the code I had for now. Note that X_1 = S, X_2 = C and Y = L.

X_1 <- function(n) {
  sample(c(80,100,120,140,160), size = n, 
         prob = c(0.2, 0.3, 0.2, 0.2, 0.1), replace=T)
}

X_2 <- function(n) {
  sample(X_1 - 120, size = n, 
         prob = c(0.2, 0.3, 0.2, 0.2, 0.1), replace=T)
}

Y <- function(n) {
  sample(L, size = n, 
         prob = c(0.2, 0.3, 0.2, 0.2, 0.1), replace=T)
}

##Creating triplets##
df <- data.frame(S_T = X_1(10000), C_T = X_2(10000), L_T =Y(10000))
df```
Jacob
  • 1
  • 1

1 Answers1

0

I'm not sure if you want C_T to be dependent on the S_T values. If you do, I think you just want to call X_1, assign the results to an object, then use that as the argument to X_2 (or just subtract 120, which is what X_2 does).

X_1 <- function(n) {
  sample(c(80,100,120,140,160), size = n, 
         prob = c(0.2, 0.3, 0.2, 0.2, 0.1), replace=T)
}
# Call that function
S_T <- X_1(10) # for practice
C_T <- S_T - 120 # that's all you're doing in function X_2, if you want to use S_T

If you want to C_T to contain values independent of S_T, you can create function within function

X_1 <- function(n) {
  sample(c(80,100,120,140,160), size = n, 
         prob = c(0.2, 0.3, 0.2, 0.2, 0.1), replace=T)
}
X_2 <- function(n) {
  X_1(n) - 120
}
S_T <- X_1(10) # Same as above
C_T <- X_2(10) # Gives values not dependent on S_T

EDIT to address comment below: It's hard to read the comment, but it looks like you want create a function that takes the results of function X_1 and returns a result based on a condition. Use ifelse to read each element one at at time. You can create another function and then input the results of function X_1

Y <- function(X_1_func){  
  ifelse( X_1_func == 80,
          return(0),
      ifelse(X_1_func == 100,
            return(0),
            ifelse(X_1_func == 120,
                   return(30),
                    return(60) # Add a default value here or the last possible value if others are F
            )
      )
  )
}
sapply(X_1(10), Y) # Use an apply to input one element of function X_1 at a time. Assign results to L or whatever you with to call.

If this all works for you, you can accept the answer.

Brian Syzdek
  • 873
  • 6
  • 10
  • Thanks @Brian. That seems right yes! The harder point is the function for L_T. Since L_T is defined as I{110 <= S_T <=} (the indicator function where payof will be thirty if S_T will be between 110 and 150), which is in this case c(0,0,30,30,0), I thought about the following (not a very elegant way..): – Jacob Jan 21 '22 at 15:36
  • ```##Distributions## X_1 <- function(n) { sample(c(80,100,120,140,160), size = n, prob = c(0.2, 0.3, 0.2, 0.2, 0.1), replace=T) } X_2 <- function(n) { X_1(n) - 120 } Y <- function(n) { for (i in 1:n){ if (X_1(n)[i] = 80){ Y[i] = 0 } if (X_1(n)[i] = 100){ Y[i] = 0 } if (X_1(n)[i] = 120){ L[i] = 0 } if (X_1(n)[i] = 140){ L[i] = 0 } if (X_1(n)[i] = 160){ L[i] = 0 } } } ##Creating triplets## df <- data.frame(X_1(10000), X_2(10000), Y(10000)) df``` – Jacob Jan 21 '22 at 15:37
  • For the function Y: ```Y <- function(n) { for (i in 1:n){ if (X_1(n)[i] = 80){ Y[i] = 0 } if (X_1(n)[i] = 100){ Y[i] = 0 } if (X_1(n)[i] = 120){ Y[i] = 30 } if (X_1(n)[i] = 140){ Y[i] = 30 } if (X_1(n)[i] = 160){ Y[i] = 0 } } }``` – Jacob Jan 21 '22 at 15:39
  • Edited answer to address this part. – Brian Syzdek Jan 21 '22 at 16:34
  • Thanks! Sorry the code is not well readable. For sapply(X_1(10000),Y (or X_2)), I obtain the error 'C stack usage 15923120 is too close to the limit' ... – Jacob Jan 21 '22 at 17:07
  • See https://stackoverflow.com/questions/14719349/error-c-stack-usage-is-too-close-to-the-limit. Run a smaller number. – Brian Syzdek Jan 21 '22 at 20:59