Linear Programming Example

BostonBSD

Active Member

Reaction score: 55
Messages: 119

This is way too neat for me not to post this.

I just found the R scripting language with the lpsolve library.

We want to maximize profit on a portfolio.
LP-Example.jpg

So we set up an objective function and constraints:
Code:
MAX = 0.055A + 0.049B + 0.1C + 0.085D + 0.1E

A+B+C+D+E <= 100,000
A+B <= 20,000
C+D <= 50,000
E <= 60,000
A >= 5,000

Transformed into matrix format constraints (non-negative constraints are assumed):
1,1,1,1,1 <= 100000
1,1,0,0,0 <= 20000
0,0,1,1,0 <= 50000
0,0,0,0,1 <= 60000
1,0,0,0,0 >= 5000

Then we create an R script:

Code:
library(lpSolve)
f.obj <- c(0.055, 0.049, 0.1, 0.085, 0.1)
f.con <- matrix(c(1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0), nrow = 5, byrow = TRUE)
f.dir <- c("<=","<=","<=","<=",">=")
f.rhs <- c(100000, 20000, 50000, 60000, 5000)
lp("max", f.obj, f.con, f.dir, f.rhs)
lp("max", f.obj, f.con, f.dir, f.rhs)$solution
lp("max", f.obj, f.con, f.dir, f.rhs)$objval

Then we run the script
R -f example.r

Then we get an answer:

Code:
> library(lpSolve)
> f.obj <- c(0.055, 0.049, 0.1, 0.085, 0.1)
> f.con <- matrix(c(1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0), nrow = 5, byrow = TRUE)
> f.dir <- c("<=","<=","<=","<=",">=")
> f.rhs <- c(100000, 20000, 50000, 60000, 5000)
> lp("max", f.obj, f.con, f.dir, f.rhs)
Success: the objective function is 9775
> lp("max", f.obj, f.con, f.dir, f.rhs)$solution
[1]  5000     0 50000     0 45000
> lp("max", f.obj, f.con, f.dir, f.rhs)$objval
[1] 9775
>

So we need to invest 5000 Euros into stock A, 50000 Euros into stock C, and 45000 Euros into stock E in order to maximize profit {given the expected returns}.

Our expected maximum profit is 9775 Euros.

This is very powerful for personal investing.
 

Alain De Vos

Aspiring Daemon

Reaction score: 245
Messages: 952

R is indeed a powerfull domain specific language.
It would take much more time to write an application in C doing this same job.
 
OP
B

BostonBSD

Active Member

Reaction score: 55
Messages: 119

Yeah absolutely, bakul just posted something about maxima, which does the same thing, but appears to be more english readable.

I'll have to check that out next.
 

Crivens

Moderator
Staff member
Moderator

Reaction score: 1,514
Messages: 2,428

But first you explain where to get that kind of money to risk on the stock market. Family dads want to know.;)
 
OP
B

BostonBSD

Active Member

Reaction score: 55
Messages: 119

But first you explain where to get that kind of money to risk on the stock market. Family dads want to know.;)
Meh, cash vs bullion vs foreign currency. Inflation may go up, go to bullion, inflation may go down go to cash, if the currency is in danger go to either bullion or foreign currency [the government is in danger during deflation].

It doesn't really matter how much you have. It just helps with decision making....value has to be stored somewhere.
 

Crivens

Moderator
Staff member
Moderator

Reaction score: 1,514
Messages: 2,428

Well, I have kids. What is this value thingy you keep talking about...?
 

kpedersen

Son of Beastie

Reaction score: 1,671
Messages: 2,505

If you send them to work on the farm, how much will they earn you that you can play the stocks game with? ;)

Our pet rabbits are useless when it comes to earning us money.
 
Top