Saturday 6 December 2014

FINANCIAL DATA MODELLING WITH R- PORTFOLIO OPTIMIZATION (PART-II)


PORTFOLIO OPTIMIZATION-2


According to Modern Portfolio Theory, an investor can effectively reduce the total risk of his portfolio through mean-variance optimization (diversification). The analysis in the previous post created portfolios that had negative weights, or "short" positions. To overcome we search for options to block short positions and arrived at a couple of other websites that demonstrated portfolio optimization with more complex models.

Installing the “tseries” package
 
Function portfolio.optim in package “tseries” is called for getting efficient frontier and solve.QP from package “quandprog” is used for portfolio construction.
 
> library("tseries")
> India5 <- read.csv("India5.csv")
> assets <- India5[,3:7]
> returns <- log(tail(assets, -1) / head(assets, -1))
 
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
[1] -0.6421529  2.6189265 -0.3917414 -0.7363362  0.1513039
> sum(w2$pw)
[1] 1
 
Create an optimum portfolio with expected means defined and shorts NOT ALLOWED
 
> w2 <- portfolio.optim(as.matrix(returns),shorts = FALSE,riskless = FALSE)
> w2$pw
[1] 0.07895285 0.24635246 0.05768448 0.60260070 0.01440952
> sum(w2$pw)
[1] 1
> w2$pm
[1] 0.0003442065
 
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)
 
It is observed that with Short positions allowed (i.e. shorts=true), the portfolio weights are the same as what was obtained in the previous program ( check PORTFOLIO OPTIMIZATION-1 ), and as expected they sum up to 1.
 
> w2$pw
[1] -0.6421529  2.6189265 -0.3917414 -0.7363362  0.1513039
> sum(w2$pw)
[1] 1
 
But when shorts are not allowed that is shorts= False, we see altogether a different portfolio is created.

> w2$pw
[1] 0.07895285 0.24635246 0.05768448 0.60260070 0.01440952
> sum(w2$pw)
[1] 1
> w2$pm
[1] 0.0003442065
 
However, when short positions are not allowed, it is not possible to specify the portfolio returns beforehand ( as it was, in the shorts allowed case, where pm = 0.005). With this constraint, the portfolio returns has dropped to 0.000344.



The efficient frontier remains the same as before-

FINANCIAL DATA MODELLING WITH R- PORTFOLIO OPTIMIZATION-(PART-I)

PORTFOLIO OPTIMIZATION-1

 
This chapter illustrates how to extract data on returns on 5 Indian Companies that are listed in the NSE and use to create an optimum portfolio and draw the efficient frontier.. The following R program shows how this data for 5 Indian Companies such as Adani Power, Suzlon Energy, NTPC, CESC, GVK has been extracted from Quandl and converted into a CSV file for repeated usage.
Creating Dataset
 
> library(Quandl)
> Quandl.auth ("vFTSoFd2sWdPtJfKz-mn")
> FADANI1314<- Quandl("GOOG/NSE_ADANIPOWER", trim_start="2013-01-01", trim_end="2014-07-30")
> FSUZ1314<- Quandl("NSE/SUZLON", trim_start="2013-01-01", trim_end="2014-07-30")
> FNTPC1314<- Quandl("NSE/NTPC", trim_start="2013-01-01", trim_end="2014-07-30")
> FCESC1314<- Quandl("NSE/CESC", trim_start="2013-01-01", trim_end="2014-07-30")
> FGVK1314<- Quandl("NSE/GVKPIL", trim_start="2013-01-01", trim_end="2014-07-30")
> library("plyr", lib.loc="~/R/win-library/3.1")
> CADANI <- FADANI1314[,c('Date','Close')]
> CCESC <- FCESC1314[,c('Date','Close')]
> CGVK <- FGVK1314[,c('Date','Close')]
> CNTPC <- FNTPC1314[,c('Date','Close')]
> CSUZ <- FSUZ1314[,c('Date','Close')]
> Stocks <- merge(CADANI,CCESC,by.x='Date',by.y='Date')
> Stocks <- rename(Stocks,c("Close.x"="Adani","Close.y"="CESC"))
> Stocks <- merge(Stocks,CGVK,by.x='Date',by.y='Date')
> Stocks <- rename(Stocks,c("Close"="GVK"))
> Stocks <- merge(Stocks,CNTPC,by.x='Date',by.y='Date')
> Stocks <- rename(Stocks,c("Close"="NTPC"))
> Stocks <- merge(Stocks,CSUZ,by.x='Date',by.y='Date')
> Stocks <- rename(Stocks,c("Close"="SUZ"))
> write.csv(Stocks,"India5.csv")
 
 

The main concept of portfolio optimization (which won theNobel Prize for Harry Markowitz in 1990) is based on the correlation between investment products, we can reduce the risk (which in this case is measured by variance) of the portfolio and still get the desired expected return.
> India5 <- read.csv("India5.csv")
> View(India5)
> assets <- India5[,3:7]
> View(assets)
 
Calculate the Returns
 
> returns <- log(tail(assets, -1) / head(assets, -1))
 
To Calculate the Optimum Portfolio Weights
 
 
> OptWeights <- function(return, mu = 0.005) {
+     Q <- rbind(cov(return), rep(1, ncol(assets)), colMeans(return))
+     Q <- cbind(Q, rbind(t(tail(Q, 2)), matrix(0, 2, 2)))
+     b <- c(rep(0, ncol(assets)), 1, mu)
+     head(solve(Q, b),-2)
+ }
 
Call the function with data and note that the weights add up to 1
 
> OptWeights(returns)
     Adani       CESC        GVK       NTPC        SUZ 
-0.6421529  2.6189265 -0.3917414 -0.7363362  0.1513039 
> sum(OptWeights(returns))
[1] 1
 
Defining a function to create the Graph of Efficient Frontier
 
> frontier <- function(return,minRet,maxRet){
+     Q <- cov(return)
+     n <- ncol(assets)
+     r <- colMeans(return)
+     Q1 <- rbind(Q,rep(1,n),r)
+     Q1 <- cbind(Q1,rbind(t(tail(Q1,2)),matrix(0,2,2)))
+     rbase <- seq(minRet,maxRet,length=100)
+     s <- sapply(rbase,function(x){
+         y <- head(solve(Q1,c(rep(0,n),1,x)),n)
+         y %*% Q %*% y
+     })
+     plot(s,rbase,xlab="Variance",ylab="Return", main = "Custom")
+ }
 
Plot the Efficient Frontier between two values of Return
> frontier(returns,-0.0005,0.05)
 

 
On the variance-return plane, the desired return-minimum variance curve is called Portfolio Frontier.
However we note that some of the weights are negative, which means that the portfolio allows "short" positions. It is possible to bar short positions.

Monday 22 September 2014

RDBMS-DW ASSIGNMENT

PART-III

CHAPTER-10

10.1: Adding new columns to the customer dimension



10.2: Adding the order_quantity column



10.3: Revised daily DW regular population





10.4: Adding the order_quantity column to the sales_order table




10.4: Adding the order_quantity column to the sales_order table



10.5: Adding nine sales orders with order quantities




Sales_Order table






CHAPTER-11


11.1: Promotion indicator
11.2: Populating the promotion indicator
11.3: Creating the promotion staging table



CHAPTER-12




12.1: Implementing the month roll-up dimension


12.2: The revised date pre-population script
Query the month_dim to confirm correct population

12.3: PA customers



12.4: Non-PA customers



12.5: The revised daily DW regular population 



12.6: Adding two customers 



CHAPTER-13


13.1: Adding the request_delivery_date_sk column


13.2: The revised daily DW regular population 



13.3: Adding the request_delivery_date column to the sales_order table 





13.4: Adding three sales orders with request delivery dates



13.5: Daily sales summary




13.6: Creating date views 
13.7: Database view role playing




CHAPTER-14


14.1: Creating monthly_sales_order_fact


14.2: Populating month_end_sales_order_fact



14.3: Modifying the sales order table



14.7: Adding two sales orders

14.8: Adding three sales orders with Allocate and/or Packing dates


 14.9: Sales orders with Allocate and/or Packing dates