I am trying to create a linear programming model that will find the optimal mix of bonds to sell that maximizes book yield, exceed a certain market value, do not fall below a certain gain/loss amount, and are limited to the maximum amount that is actually owned of the bond.
Here is a sample of data from a bond portfolio that I am working with:
min max market_value gain_loss book_yield
0 0.192 5793070.000 -2096660.217 1.564
0 0.332 10013973.531 -257093.942 5.304
0 0.357 10741519.024 -3612018.833 1.614
0 0.118 3552909.914 -127308.087 4.218
0 0.00006 1834.918 -4.967 5.023
Here is my current code:
library(lpSolve)
library(lpSolveAPI)
library(readxl)
setwd(directory)
bond_data2 <- read_xlsx("Transaction Model2.xlsx")
View(bond_data2)
gain_loss2 <- bond_data2$`Gain/Loss`
market_value2 <- bond_data2$`Market Value`
book_yield2 <- bond_data2$`Book Yield`
max_owned2 <- bond_data2$Max
lp_model2 <- make.lp(0, ncol=length(max_owned2))
set.objfn(lp_model2, book_yield2)
for (i in seq_along(max_owned2)) {
set.bounds(lp_model2, columns = i, upper = max_owned2[i], lower = 0)
}
add.constraint(lp_model2, c(market_value2), ">=", 15000000)
add.constraint(lp_model2, c(gain_loss2), ">=", -2000000)
solve(lp_model2)
solution <- get.variables(lp_model2)
solution
print(get.objective(lp_model2))
print(get.constraints(lp_model2))
bonds_to_sell2 <- bond_data2[solution > 0, ]
print(bonds_to_sell2$CUSIP)
print.lpExtPtr(lp_model2)
Here are the results I receive:
>solution
[1] 6.952180e-310 3.799365e-321 0.000000e+00 0.000000e+00 0.000000e+00
Which clearly are unfeasible and make no sense, however, when I print the properties of the mode, they all look how I want them to:
>C1 C2 C3 C4 C5
>Minimize 1.564 5.304 1.614 4.218 5.023
>R1 5793070 10013973.530993 10741519.024 3552909.91400 1834.918000 >= 1.5e+07
>R2 -2096660.21700 -257093.94199 -3612018.83300 -127308.087000 -4.967 >= -2e+06
>Kind Std Std Std Std Std
>Type Real Real Real Real Real
>Upper 0.192439652079616 0.332653598565203 0.356821889565486 0.118023905756426 6.09540332698593e-05
>Lower 0 0 0 0 0
To simplify:
What is the best mix of bonds to sell that:
- Minimize book yield
- do not exceed a 2,000,000 loss
- Provide 15,000,000 in market value.
- maximum amount sold does not exceed what is owned (found in the max column which is that bonds ratio of the total sum of the market values)
Any help would be much appreciated.
mmagness is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.