Clear workspace, Load packages, load data

rm(list=ls())
if(!require('lpSolveAPI')){install.packages('lpSolveAPI')}
## Loading required package: lpSolveAPI
## Warning: package 'lpSolveAPI' was built under R version 3.5.3
if(!require('dplyr')){install.packages('dplyr')}
## Loading required package: dplyr
## Warning: package 'dplyr' was built under R version 3.5.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
if(!require('data.table')){install.packages('data.table')}
## Loading required package: data.table
## Warning: package 'data.table' was built under R version 3.5.3
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
setwd("C:\\Users\\yy\\Desktop")
Financial=read.csv("OPTProject_Financial.csv")
glimpse(Financial)
## Observations: 448
## Variables: 6
## $ ticker   <fct> A, AAL, AAP, AAPL, ABBV, ABC, ABT, ACN, ADBE, ADI, AD...
## $ ret_mean <dbl> 0.000421158, 0.002872721, 0.001317418, 0.002298593, 0...
## $ ret_std  <dbl> 0.015030314, 0.016122380, 0.016037914, 0.017183050, 0...
## $ Name     <fct> "Agilent Technologies Inc", "American Airlines Group"...
## $ Sector   <fct> Health Care, Industrials, Consumer Discretionary, Inf...
## $ price    <dbl> 67.00, 50.90, 115.01, 168.34, 92.01, 84.01, 60.28, 14...

Model 1

Financial$ExpectedReturn=Financial$ret_mean*Financial$price*20
Financial$ExpectedRisk=Financial$ret_std*Financial$price*20
lp=make.lp(0,448)
lp.control(lp, sense="max")
## $anti.degen
## [1] "fixedvars" "stalling" 
## 
## $basis.crash
## [1] "none"
## 
## $bb.depthlimit
## [1] -50
## 
## $bb.floorfirst
## [1] "automatic"
## 
## $bb.rule
## [1] "pseudononint" "greedy"       "dynamic"      "rcostfixing" 
## 
## $break.at.first
## [1] FALSE
## 
## $break.at.value
## [1] 1e+30
## 
## $epsilon
##       epsb       epsd      epsel     epsint epsperturb   epspivot 
##      1e-10      1e-09      1e-12      1e-07      1e-05      2e-07 
## 
## $improve
## [1] "dualfeas" "thetagap"
## 
## $infinite
## [1] 1e+30
## 
## $maxpivot
## [1] 250
## 
## $mip.gap
## absolute relative 
##    1e-11    1e-11 
## 
## $negrange
## [1] -1e+06
## 
## $obj.in.basis
## [1] TRUE
## 
## $pivoting
## [1] "devex"    "adaptive"
## 
## $presolve
## [1] "none"
## 
## $scalelimit
## [1] 5
## 
## $scaling
## [1] "geometric"   "equilibrate" "integers"   
## 
## $sense
## [1] "maximize"
## 
## $simplextype
## [1] "dual"   "primal"
## 
## $timeout
## [1] 0
## 
## $verbose
## [1] "neutral"
set.objfn(lp,Financial$ExpectedReturn)
add.constraint(lp,Financial$price, "<=", 20000)
add.constraint(lp,Financial$ExpectedRisk, "<=", 5000)
set.bounds(lp,lower=rep(0,448),upper=rep(1,448))
lp
## Model name: 
##   a linear program with 448 decision variables and 2 constraints
solve(lp)
## [1] 0
get.objective(lp)
## [1] 1098.272
get.variables(lp)
##   [1] 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 1.0000000 0.0000000
##   [8] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000
##  [15] 0.0000000 0.0000000 0.0000000 1.0000000 1.0000000 0.0000000 1.0000000
##  [22] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
##  [29] 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
##  [36] 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
##  [43] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000
##  [50] 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 0.0000000
##  [57] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 0.0000000 0.0000000
##  [64] 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 1.0000000
##  [71] 1.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000
##  [78] 1.0000000 1.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000
##  [85] 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 1.0000000
##  [92] 1.0000000 0.0000000 0.0000000 0.0000000 1.0000000 1.0000000 0.0000000
##  [99] 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000
## [106] 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [113] 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [120] 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 0.0000000
## [127] 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [134] 0.0000000 0.0000000 1.0000000 1.0000000 1.0000000 1.0000000 0.0000000
## [141] 1.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [148] 1.0000000 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 0.0000000
## [155] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [162] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [169] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [176] 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 1.0000000 0.0000000
## [183] 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000
## [190] 0.0000000 0.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000
## [197] 0.0000000 1.0000000 0.0000000 1.0000000 0.0000000 1.0000000 1.0000000
## [204] 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
## [211] 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [218] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000
## [225] 0.0000000 0.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000
## [232] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [239] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
## [246] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [253] 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [260] 1.0000000 1.0000000 0.0000000 1.0000000 1.0000000 1.0000000 0.0000000
## [267] 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000
## [274] 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 1.0000000
## [281] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [288] 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
## [295] 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 1.0000000
## [302] 1.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [309] 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 1.0000000 0.0000000
## [316] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000
## [323] 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [330] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [337] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
## [344] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [351] 1.0000000 0.0000000 0.0000000 1.0000000 1.0000000 1.0000000 0.0000000
## [358] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [365] 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [372] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [379] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 1.0000000
## [386] 1.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [393] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## [400] 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [407] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000
## [414] 0.0000000 1.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.4738738
## [421] 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
## [428] 0.0000000 1.0000000 0.0000000 0.0000000 1.0000000 1.0000000 1.0000000
## [435] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000
## [442] 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000
get.constraints(lp)
## [1] 12871.18  5000.00

Model 2

Financial$ExpectedReturn=Financial$ret_mean*Financial$price*20
Financial$ExpectedRisk=Financial$ret_std*Financial$price*20
lp=make.lp(0,448)
lp.control(lp, sense="max")
## $anti.degen
## [1] "fixedvars" "stalling" 
## 
## $basis.crash
## [1] "none"
## 
## $bb.depthlimit
## [1] -50
## 
## $bb.floorfirst
## [1] "automatic"
## 
## $bb.rule
## [1] "pseudononint" "greedy"       "dynamic"      "rcostfixing" 
## 
## $break.at.first
## [1] FALSE
## 
## $break.at.value
## [1] 1e+30
## 
## $epsilon
##       epsb       epsd      epsel     epsint epsperturb   epspivot 
##      1e-10      1e-09      1e-12      1e-07      1e-05      2e-07 
## 
## $improve
## [1] "dualfeas" "thetagap"
## 
## $infinite
## [1] 1e+30
## 
## $maxpivot
## [1] 250
## 
## $mip.gap
## absolute relative 
##    1e-11    1e-11 
## 
## $negrange
## [1] -1e+06
## 
## $obj.in.basis
## [1] TRUE
## 
## $pivoting
## [1] "devex"    "adaptive"
## 
## $presolve
## [1] "none"
## 
## $scalelimit
## [1] 5
## 
## $scaling
## [1] "geometric"   "equilibrate" "integers"   
## 
## $sense
## [1] "maximize"
## 
## $simplextype
## [1] "dual"   "primal"
## 
## $timeout
## [1] 0
## 
## $verbose
## [1] "neutral"
set.objfn(lp,Financial$ExpectedReturn)
add.constraint(lp,Financial$price, "<=", 20000)
add.constraint(lp,Financial$ExpectedRisk, "<=", 5000)
set.type(lp,columns=1:448,type='binary')
lp
## Model name: 
##   a linear program with 448 decision variables and 2 constraints
solve(lp)
## [1] 0
get.objective(lp)
## [1] 1098.165
get.variables(lp)
##   [1] 0 1 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0
##  [36] 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 0 1 1
##  [71] 1 0 0 1 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0
## [106] 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1 1 0
## [141] 1 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
## [176] 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 1 1 1 0 0 0 0 0 1
## [211] 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
## [246] 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 0 0 0 1 0 0 1 1
## [281] 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0
## [316] 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
## [351] 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
## [386] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1
## [421] 0 0 0 0 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1
get.constraints(lp)
## [1] 12803.900  4999.948

Model 3

Financial$ExpectedReturn=Financial$ret_mean*Financial$price*20
Financial$ExpectedRisk=Financial$ret_std*Financial$price*20
x=0
for(i in 1:20)
  {
    { 
      x[i]=500*i
    }
}
calculate=function(x)
{
  lp=make.lp(0,448)
  lp.control(lp, sense="max")
  set.objfn(lp,Financial$ExpectedReturn)
  add.constraint(lp,Financial$price, "<=", 20000)
  add.constraint(lp,Financial$ExpectedRisk, "<=", x)
  set.bounds(lp,lower=rep(0,448),upper=rep(1,448))
  lp
  solve(lp)
  a=get.objective(lp)
  b=get.variables(lp)
  count1=length(which(b>0))
  c=get.constraints(lp)
  return(list(count1,c[1],c[2],a))
}

df=sapply(x, FUN=calculate)
df=as.data.frame(df)
df=t(df)
df=as.data.frame(df)
z=as.data.frame(x)
df=cbind(z,df)
rownames(df)=c(1:20)
names(df)=c("riskDollarsThreshold","numberOfStocks","totalMoneySpent","expectedRiskDollars","expectedReturnDollars")
df=data.matrix(df)
write.csv(df,"C:\\Users\\yy\\Desktop\\FinanceResults.csv", row.names = FALSE)