# Linear Programming Example



## BostonBSD (Apr 19, 2021)

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.




So we set up an objective function and constraints:

```
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:


```
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:


```
> 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 (Apr 19, 2021)

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


----------



## BostonBSD (Apr 19, 2021)

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.


----------



## Alain De Vos (Apr 19, 2021)

Afterwards you can do it in Julia.


----------



## BostonBSD (Apr 19, 2021)

And that looks even more readable than maxima.


----------



## Crivens (Apr 19, 2021)

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


----------



## BostonBSD (Apr 19, 2021)

Crivens said:


> 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 (Apr 19, 2021)

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


----------



## BostonBSD (Apr 19, 2021)

Well are kids an asset or a liability?


----------



## Jose (Apr 19, 2021)

BostonBSD said:


> Well are kids an asset or a liability?


Depends on the day.


----------



## kpedersen (Apr 19, 2021)

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.


----------



## Crivens (Apr 19, 2021)

Jose said:


> Depends on the day.


Yes. Some days you have moments where you want to _short_ them...
I consider them the best investment, hands down.


----------

