Wednesday, 1 February 2017

Looking at Oil with R (Part 3) - Portfolio Optimization of Oil and Gas Companies


Introduction 


In my previous post, I compared oil futures against current OPEC oil price. In this post, I want to take advantage of R to optimize a portfolio of oil and gas companies. Portfolio optimization is a method of diversifying investments to to reach a specified level of risk and return. The portfolio I am building is comprised of companies specializing in oil and gas. Specifically, those in the extraction and processing stage. 


My target portfolio will be minimizing variance - or risk. By minimizing risk, I am accepting less return on my investment. 


Goal


To identify O&G firms from NASDAQ with the least variance in stock price after January 2014. These firms would be the best choice for a low-risk portfolio focusing on petrochemicals. 


Technique 


I learned about the basics of portfolio optimization from an online thesis (Engels, 2004). The first two chapters cover the fundamentals of optimization theory. 


The names of companies came from the NASDAQ website. NASDAQ has their data available in a .csv format which I downloaded and imported into R. I filtered the data to show only those in Oil & Gas. 


NASDAQ = read.csv("companylist.csv",header=T) #import data 

NASDAQ_oil = subset(NASDAQ, grepl("Oil",as.character(Industry),ignore.case = TRUE) & grepl("Energy",as.character(Sector),
ignore.case = TRUE)) #filter data

oil_list = as.vector(as.matrix(NASDAQ_oil)[,1]) #extract ticker symbol from dataset 


Next, I got stock prices for each ticker symbol. I chose to 'train' the portfolio algorithm with data from January 2014 to present. I chose this period because it included the 2015 oil glut - meaning a more holistic view. 


library (quantmod)

library(PerformanceAnalytics)
getSymbols(oil_list,from = '2014-01-01')
return_matrix = NULL
for( symbol in oil_list)
{
  return_matrix = merge.xts(return_matrix,Return.calculate(Ad(get(paste(symbol))),method='discrete'))
}


The 'quantmod' package is useful because it extracts data from multiple sources, including Google Finance and Yahoo Finance. The getSymbol() function extracts stock prices for the array of ticker symbols. I used the 'PerformanceAnalytics' package to calculate cumulative returns matrix. The 'PerformanceAnalytics' package can create some aesthetic graphs as well. 

chart.CumReturns(return_matrix, main = "Cumulative Returns of Oil & Gas Companies on NASDAQ",ylab = "Percent Increase (%)",ylim=c(-2,2),colorset=redmono)

(Figure 1) Cumulative Returns of Various O&G Companies


Since October 2014, the performance of many O&G firms had a downward trend. As explained in my previous post, the trend came from uncontrolled overproduction from OPEC. Around July 2016, the downward trend plateaus. The plateau could come from recent news that OPEC, and other countries, would agree to cut production (Cunningham, 2017). 



 The 'quantmod' package is useful because it extracts data from multiple sources, including Google Finance and Yahoo Finance. I took this from an amazing tutorial by Kyle Balkissoon (Balkissoon, 2016). Kyle's tutorial builds on portfolio optimization in greater detail. 


The getSymbol() function extracts stock prices for the array of ticker symbols. I used the 'PerformanceAnalytics' package to calculate cumulative returns matrix. The 'PerformanceAnalytics' package can create some aesthetic graphs as well. 

The next step is to construct the actual portfolio. 


library(PortfolioAnalytics)
minSD.opt=portfolio.spec(
  assets=colnames(return_matrix))

minSD.opt=add.objective(
  portfolio=minSD.opt
  type='risk',
  name='StdDev')


minSD.opt=add.constraint(
  portfolio = minSD.opt,
  type="full_investment")

minSD.opt=add.constraint(
  portfolio = minSD.opt,
  type="long_only")

minSD.opt=add.constraint(
  portfolio = minSD.opt,
  type="box",
  min=0,max=0.2)

What is essentially happening here is that I am creating an object called minSD.opt. This object is essentially a list of user-defined constraints. My minimum variance portfolio reduces the risk to within the defined standard deviation of 0 to 0.3. The "full_investment" constraint means the sum of portfolio weights should add to 1 - meaning the whole portfolio is currently invested. I assume the "long_only" constraint means that the portfolio size does not change. Further clarification on these constraints would be appreciated. 


library(DEoptim)
OptimizedPortfolioMinVariance=optimize.portfolio(
  R=return_matrix,
  portfolio=MinimumVariancePortfolio,
  trace=TRUE)

At this point, I attempt to optimize my portfolio with the list of constraints in the MinimumVariancePortfolio object. The optimization should take no more than a minute max. 


x<-extractWeights(OptimizedPortfolioMinVariance) #extract weights of portfolio

x=x[order(-x)] #order list based on proportion 
z1<-x[x>0] #remove companies that have zero weights 
z<-as.data.frame(z1) #turn into data frame 
r<-sub('.Adjusted', '', rownames(z)) #by default, name has ".Adjusted" concatenated to it. Remove the ".adjusted" suffix 
b = (data.frame(r,as.vector(z1))) #turn list into dataframe 

barplot(b$as.vector.z1.,names.arg = r, las = 2, ylab = "Portfolio Proportion",border = NA,main = "Oil & Gas Portfolio with Minimum Variance",col = 'brown1') #get a barplot 


Finally, I extract the proportion of firms in my portfolio as a list. I clean and order the data to make it look more appealing. In the end, I create a barplot to visually illustrate the O&G firms with the lowest risk since January 2014 (Figure 2). 



(Figure 2) Weights of Minimum Variance O&G Portfolio

The top three least riskiest companies, Centennial Resource Development, Dorchester Minerals and Viper Energy Partners, comprise 49.8% of our portfolio. I was surprised that all three companies came from Texas. 

After further research, I learned that Texas is naturally rich in previously inaccessible reserves of oil and natural gas in the form of shale formations. Advances in extraction technologies as hydraulic fracking has significantly increased production (Klare, 2016). As NASDAQ has many international oil and gas companies, it is safe to assume that petrochemical companies from Texas are the least riskiest to invest in.  








Resources



Balkissoon, K. (2016). Portfolio Construction and Optimization with R. Retrieved 1 10, 2017, from http://kkb.io/2016/10/26/portfolio-optimization/

Cunningham, N. (2017, 1 19). Despite OPEC Deal Oil Prices Could Fall Sharply From Here. Retrieved 1 20, 2017, from http://oilprice.com/Energy/Energy-General/Despite-OPEC-Deal-Oil-Prices-Could-Fall-Sharply-From-Here.html

Klare, M. T. (2016, 3 18). The Future of Oil Is Here—and It Doesn’t Look Pretty. Retrieved 1 15, 2017, from https://www.thenation.com/article/the-future-of-oil-is-here-and-it-doesnt-look-pretty/