[This article was first published on K & L Fintech Modeling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This post explains a two-stage stochastic linear programming (SLP) in a simplified manner and implements this model using R. This exercise is for the clear understanding of SLP model and will be a solid basis for the advanced topics such as multi-stage SLP, scenario tree generation, and so on. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
For 2-stage SLP, we use the simple ALM cash flow matching problem which was dealt with the following previous post.
Stochastic Linear Programming
Stochastic programming assumes that some given parameters (i.e. interest rates, liabilities requirements) in deterministic model are random variables with known probability distributions. Of course, there are also predetermined or known parameters in SLP.
For example, in the floating rate note, the first coupon rate is predetermined prior to a pricing date but the remaining coupon rates are going to be realized at the future reset dates so that our decisions for subsequent buying, selling, and holding will be dependent on these realizations.
As a whole, SLP is classified either as two-stage SLP or multi-stage SLP. The former has two decision timings and the latter has multiple decision times. More realistic model is also multi-stage SLP but as you know, dealing with many stages all at once is much more difficult than only two-stage.
In practice and academics, multi-stage and two-stage or three-stage SLP are widely used. In some case, multi-stage SLP can be converted into a two-stage SLP. Before delving into the advanced multi-stage SLP model, it is natural that our focus centers on two-stage SLP at first.
From the following figure, we can find that 1) sky blue circle in the first stage is known and shows one realization but 2) grey circles in the second stage are not known and show possible realizations with probabilities (a set of scenario).
Figure : Illustration of two-stage stochastic programming
The first decision must be made here-and-now and cannot depend on future realizations. The second decision is the wait-and-see decision which can be made after some of the random parameters are observed. In the sense that the second decision has a characteristics of correction after seeing data, this type of SLP is called the recourse model. We interpret the recourse as a correction.
We think that this kind of reasoning is in line with the Kalman filtering process which consists of alternating repetitions of a prediction and a correction. Prediction is done without observing data and correction after seeing data.
A Generic Form of Two-stage SLP Problem with Recourse
A generic form of a two-stage SLP program with recourse has the following form.
\[\begin{align} &\max_{x} a^T x + E[\max_{y(\omega)} c(\omega)^Ty(\omega)] \\ s.t. &\\ & Ax = b \\ & B(\omega)x + C(\omega)y(\omega) = d(\omega) \\ &x \ge 0, \quad y(\omega) \ge 0 \end{align}\]
In the above formulation, the objective function consists of a decision of \(x\) and expectation of decision of \(y(\omega)\). When \(x\) is found by optimization, future scenarios are used as the form of expectation because we do not know which scenario is revealed but know the probability of each scenario, which can be summarized by the expectation.
Constraint for \(x\) is set with deterministic parameters independently of the future scenario. But constraints for \(y(\omega)\) are set with stochastic parameters and it is affected by a decision of \(x\) because resource is not infinite.
Deterministic Equivalent of Two-stage SLP Problem with Recourse
When probabilities of scenarios are discrete, \[\begin{align} E[\max_{y(\omega)} c(w)^Ty(\omega)] = \sum_{k=1}^{S} {p_k \max_{y(\omega_k)} c(\omega_k)^Ty(\omega_k) } \end{align}\]
Using a notation \(_k\) instead of the \((\omega_k)\), the two-stage SLP problem has the following discretized form.
\[\begin{align} &\max_{x} a^T x + \sum_{k=1}^{S} {p_k \max_{y_k} c_k^Ty_k } \\ s.t. &\\ & Ax = b \\ & B_k x + C_k y_k = d_k \quad k = 1,…,S \\ &x \ge 0 \\ &y_k \ge 0 \quad k = 1,…,S \end{align}\]
For an implementation, we can expand summation into each components as follows.
Constraint for \(x\) is set with deterministic parameters independently of the future scenario. But constraints for \(y(\omega)\) are set with stochastic parameters and it is affected by a decision of \(x\) because resource is not infinite.
Introduction of Interest Rate Scenario
Stohcastic scenario is vital to SLP modeling. Scenario generation can be applied to Interest rates, stock returns, liability cash flows, and so on. For interest rate scenarios, Black-Derman-Toy (BDT) model is widely used.
For a simplified interet rate scenarios. we add two possible interest rate scenarios to the simple ALM cash flow matching problem which was dealt with the previous post.
In the next figure for scenario, interest rates for two-stage consist of the case of up and down and we assume that the interest rates of CP funding are all above the interest rates of the line of credit. This view is intended for investigating the substitution effect between the line of credit and CP funding.
Let \(X\) denote a vector of 25 decision variables, three 1st-stage decision variables (\(x_{01}, y_{01}, z_{01}\)) and the rest of 2nd-stage decision variables, and then the following representation can be obtained.
\[\begin{align} &\max_{X} 0.5\nu_1 + 0.5\nu_2 \\ s.t. &\\ &AX = b \\ \\ &X \ge 0, \quad x_{01}, x_{1i}, x_{2i} \le 100 \\ \\ X = &x_{01}, x_{12},x_{13},x_{14},x_{15},x_{22},x_{23},x_{24},x_{25}, \\ &y_{01},y_{12},y_{13},y_{22},y_{23},\\ &z_{01},z_{12},z_{13},z_{14},z_{15},z_{22},z_{23},z_{24},z_{25}, \\ &v_{1},v_{2} \end{align}\]
Here, the constraint matrix (\(A\)) has the following form. Now we can solve this problem by using R.
R code
In this post, we use ROI and ROI.plugin.lpsolve R packages to solve the above linear optimization problem locally instead of ROI.plugin.neos. The following R code implements the above simplified two-stage stochastic linear programming for the same asset-liability cash flow matching problem with the help of ROI and ROI.plugin.lpsolve.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #========================================================# # Quantitative ALM, Financial Econometrics & Derivatives # ML/DL using R, Python, Tensorflow by Sang-Heon Lee # # https://kiandlee.blogspot.com #——————————————————–# # A simple two-stage stochastic linear programming # for a simple Asset/Liability CF Matching problem #========================================================# graphics.off() # clear all graphs rm(list = ls()) # remove all files from your workspace library(ROI) library(ROI.plugin.lpsolve) # Gross Interest Rates Scenario # Xi:the line of credit, Yi:CP 90d, Zi:excess funds # 1st stage, 2nd stage Rx0 = 1+0.12/12; Rx1 = 1+0.15/12; Rx2 = 1+0.05/12 Ry0 = 1+0.08/4; Ry1 = 1+0.20/4; Ry2 = 1+0.10/4 Rz0 = 1+0.036/12;Rz1 = 1+0.05/12; Rz2 = 1+0.02/12 # decision variables (25) # 1st stage # –> x01, y01, z01 # # 2nd stage # –> x12,x13,x14,x15, y12,y13, z12,z13,z14,z15, v1 # x22,x23,x24,x25, y22,y23, z22,z23,z24,z25, v2 # x01,12,13,14,15,22,23,24,25, y01,12,13,22,23, # z01,12,13,14,15,22,23,24,25, v1,2 # Left hand side matrix v.LHS <– c( 1,0,0,0,0,0,0,0,0, 1,0,0,0,0, –1,0,0,0,0,0,0,0,0, 0,0, –Rx0,1,0,0,0,0,0,0,0, 0,1,0,0,0, Rz0,–1,0,0,0,0,0,0,0, 0,0, 0,–Rx1,1,0,0,0,0,0,0, 0,0,1,0,0, 0,Rz1,–1,0,0,0,0,0,0, 0,0, 0,0,–Rx1,1,0,0,0,0,0, –Ry0,0,0,0,0, 0,0,Rz1,–1,0,0,0,0,0, 0,0, 0,0,0,–Rx1,1,0,0,0,0, 0,–Ry1,0,0,0, 0,0,0,Rz1,–1,0,0,0,0, 0,0, 0,0,0,0,–Rx1,0,0,0,0, 0,0,–Ry1,0,0, 0,0,0,0,Rz1, 0,0,0,0,–1,0, –Rx0,0,0,0,0,1,0,0,0, 0,0,0,1,0, Rz0,0,0,0,0,–1,0,0,0, 0,0, 0,0,0,0,0,–Rx2,1,0,0, 0,0,0,0,1, 0,0,0,0,0,Rz2,–1,0,0, 0,0, 0,0,0,0,0,0,–Rx2,1,0, –Ry0,0,0,0,0, 0,0,0,0,0,0,Rz2,–1,0, 0,0, 0,0,0,0,0,0,0,–Rx2,1, 0,0,0,–Ry2,0, 0,0,0,0,0,0,0,Rz2,–1, 0,0, 0,0,0,0,0,0,0,0,–Rx2, 0,0,0,0,–Ry2, 0,0,0,0,0,0,0,0, Rz2, 0,–1 ) m.LHS <– matrix(v.LHS, nrow = 11, byrow = TRUE) # 0.5*v1+0.5*v2 is the objective function lp_obj <– L_objective(c(rep(0,23), 0.5, 0.5)) # LHS * X = RHS lp_con <– L_constraint( L = m.LHS, dir = rep(“==”, 11), rhs = c(150, 100, –200, 200, –50, –300, 100, –200, 200, –50, –300)) # Lower & Upper bounds for decision variables lp_bound <– V_bound( li=1:25, ui=1:25, lb=rep(0,25), ub=c(rep(100,9), rep(Inf,25–9))) # Set Problem lp <– OP(objective = lp_obj, constraints = lp_con, bounds = lp_bound, maximum = TRUE) # Solve Problem opt <– ROI_solve(lp, solver = “lpsolve”, control = list(epsb = 1e–20)) # Print cat(“\nResult for a simple two-stage SLP \n\n”, “Objective Function Value = “, round(opt$objval,4), “\n\n”, “1st Stage Decision Variables:\n”, ” x01, y01, z01 = “, round(opt$solution[c(1,10,15)],4), “\n\n”, “2nd Stage Decision Variables:\n”, ” x12,13,14,15,22,23,24,25 = \n”, ” “, round(opt$solution[2:9],4), “\n”, ” y12,13,22,23 = \n”, ” “, round(opt$solution[11:14],4), “\n”, ” z12,13,14,15,22,23,24,25 = \n”, ” “, round(opt$solution[16:23],4), “\n”, ” v1,2 = \n”, ” “, round(opt$solution[24:25],4)) | cs |
Comparing this result with the previous one, we can find that ALM manager try to use more funding of the line of credit than the previous deterministic case because future funding cost for the line of credit is relatively cheaper than that of CP issuance. Since we use the equality constraints, the first decision is not changed but the latter decisions are affected by the future interest rate scenarios.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Result for a simple two–stage SLP Objective Function Value = 89.9009 1st Stage Decision Variables x01,y01,z01 = 0 150 0 2nd Stage Decision Variables x12,13,14,15,22,23,24,25 = 53.5714 0 100 100 51.626 0 100 100 y12,13,22,23 = 46.4286 106.1913 48.374 104.4202 z12,13,14,15,22,23,24,25 = 0 251.9502 0 0 0 252.579 0 0 v1,2 = 87.2492 92.5527 | cs |
Concluding Remarks
In this post, we implements the two-stage SLP for the simple ALM cash flow matching model. However, we need to be cautious about this implementation. Our problem is so simple that we can use naïve LP programming. But more realistic SLP models have highly dimensional scenario structures so that it can not be solved this naïve method. For solving the realistic two-stage or multi-stage SLP, many advanced numerical techniques are presented and commercial solvers such as GAMS, CPLEX are used.
Keep in mind that our simplified example is for the clear understanding of two-stage stochastic linear programming as a building block for the next advanced model.\(\blacksquare\)
To leave a comment for the author, please follow the link and comment on their blog: K & L Fintech Modeling.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.