In the following program, this portfolio.optim function has been used instead of the custom built function shown in the previous post.

```
========================================
setwd("C:/Users/admin/Desktop/Data Analytics/QuantitativeFinance/QFLabs")
getwd()
India5 <- read.csv("India5.csv")
assets <- India5[,3:7]
returns <- log(tail(assets, -1) / head(assets, -1))
library("tseries")
## Create an optimum portfolio with expected means defined and shorts allowed
w2 <- portfolio.optim(as.matrix(returns),pm = 0.005,shorts = TRUE,riskless= FALSE)
w2$pw
sum(w2$pw)
## Create an optimum portfolio with expected means defined and shorts NOT ALLOWED
w2 <- portfolio.optim(as.matrix(returns),shorts = FALSE,riskless = FALSE)
w2$pw
sum(w2$pw)
w2$pm
# Create a function to create the frontier using portfolio.optim
frontier2 <- function(return,minRet,maxRet){
rbase <- seq(minRet,maxRet,length=100)
s <- sapply(rbase,function(x){
p2 <- portfolio.optim(as.matrix(returns),pm = x, shorts = TRUE)
p2$ps^2
})
plot(s,rbase,xlab="Variance",ylab="Return", main="w/Portfolio.Optim")
}
frontier2(returns,-0.0005,0.05)
========================================
```

We observe that with Short positions allowed ( shorts=true), the portfolio weights are the same as what was obtained in the previous program ( previous post ), and as expected they sum up to 1

> w2$pw

[1] 4.869750 5.651428 3.375111 -10.711613 -2.184675

> sum(w2$pw)

[1] 1

however when shorts are not allowed ( shorts = false ) a different portfolio is created

> w2$pw

[1] 0.22797760 0.19765858 0.03686214 0.16357332 0.37392835

> sum(w2$pw)

[1] 1

> w2$pm

[1] 0.0003971317

and they sum up to 1 as well.

However, when short positions are not allowed, it is not possible to specify the portfolio returns before hand ( as it was, in the shorts allowed case, where pm = 0.005). With this constraint, the portfolio returns has dropped to 0.000397.

The efficient frontier remains the same as before

The program given in Alphaism uses portfolio.optim and then extends it further.

## No comments:

## Post a Comment