Monday, February 29, 2016

Continuing Research on U.S. Gasoline and Crude Oil Prices, Part Four

A Brief Digression...

UPDATE: 29 Feb 2016

Due to some font and formatting problems (pointed out by a reader), I had to update this blog post to fix the OLS regression output tables below.  I am going to see if I can "pinch and tweak" some of the formatting to see if that will help with future posts of this kind.  -Rich

A quick note before I continue on with, and conclude, my blog series on gasoline and oil prices: I know that the spacing of my blog looks off in some spots.  Despite my efforts to make spacing uniform, things do not come out quite correctly.  I apologize.  To help get my R output spacing to be more uniform, I installed the stargazer package developed by Marek Hlavac.

I am concluding this topic with this post.  Here are links to my previous three posts in case you need/want to go back to them: [1], [2], and [3].


Modeling The Data


With much of the exploratory analysis complete, I will continue my analysis by performing several ordinary least-squares (OLS) regressions on the data.  The results are below:


1. OLS Regression of the non-differenced price data

=============================================================================================
                                               Dependent variable:                           
                    -------------------------------------------------------------------------
                                                   avgConvGas                                
                              (1)                     (2)                      (3)           
---------------------------------------------------------------------------------------------
wti                          0.052                 1.108***                                  
                            (0.085)                 (0.050)                                  
                                                                                             
brent                      0.928***                                         0.968***         
                            (0.070)                                          (0.024)         
                                                                                             
Constant                   0.244***                  0.109                  0.263***         
                            (0.062)                 (0.103)                  (0.054)         
                                                                                             
---------------------------------------------------------------------------------------------
Observations                  96                      96                       96            
R2                           0.945                   0.841                    0.944          
Adjusted R2                  0.943                   0.840                    0.944          
Residual Std. Error     0.131 (df = 93)         0.221 (df = 94)          0.131 (df = 94)     
F Statistic         793.560*** (df = 2; 93) 498.330*** (df = 1; 94) 1,597.500*** (df = 1; 94)
=============================================================================================
Note:                                                             *p<0.1; **p<0.05; ***p<0.01

Looking at the univariate regression output above, we see that the WTI crude spot price and the Brent crude spot price are statistically significant in the models where they are the sole independent variables -- see the output in the table columns (2) and (3).  In the multivariate model regression output in which they are both independent variables -- table column (1) -- the WTI crude spot price is no longer statistically significant.  Even though the coefficient for the WTI crude spot price is greater than zero in that model, at the 95% confidence level it could be zero in the model because zero falls within that range of values.


2. OLS Regression of the differenced price data



===========================================================================================
                                              Dependent variable:                          
                    -----------------------------------------------------------------------
                                                    diffGas                                
                              (1)                     (2)                     (3)          
-------------------------------------------------------------------------------------------
diffWTI                     0.394**                1.043***                                
                            (0.181)                 (0.077)                                
                                                                                           
diffBrent                  0.706***                                        1.067***        
                            (0.181)                                         (0.073)        
                                                                                           
Constant                    -0.003                  -0.002                  -0.004         
                            (0.012)                 (0.013)                 (0.012)        
                                                                                           
-------------------------------------------------------------------------------------------
Observations                  95                      95                      95           
R2                           0.710                   0.661                   0.695         
Adjusted R2                  0.703                   0.658                   0.691         
Residual Std. Error     0.116 (df = 92)         0.125 (df = 93)         0.119 (df = 93)    
F Statistic         112.357*** (df = 2; 92) 181.580*** (df = 1; 93) 211.444*** (df = 1; 93)
===========================================================================================
Note:                                                           *p<0.1; **p<0.05; ***p<0.01

In the regression output from the differenced values of the WTI and Brent crude spot price, the univariate models are both statistically significant, similar results to the non-differenced variable regression output.  However, in the multivariate model of the differenced values, WTI crude spot price is statistically significant.

The conclusion I would draw from this is that WTI crude oil spot price is less significant than Brent crude oil spot price in regards to the U.S. gasoline spot price, but that it does have a share of the significant effect on the change in U.S. gasoline spot price.


Causal Analysis


Finally, I used the grangertest() function in R in order to perform a causal analysis on both the non-differenced and the differenced variables.  This test looks for the causal relationship between the variables, like the age old question of which came first: the chicken or the egg.  In the output below, I am including the results that were found to be statistically significant.  (For the full run of Granger causality tests, please see my R script.)


1. Granger Causality Tests on the non-differenced price data


The two statistically significant results were that both WTI and Brent crude spot prices Granger cause the U.S. gasoline spot price:


Granger causality test
Model 1: avgConvGas ~ Lags(avgConvGas, 1:1) + Lags(c(wti + brent), 1:1)
Model 2: avgConvGas ~ Lags(avgConvGas, 1:1)
  Res.Df Df      F Pr(>F)  
1     92                   
2     93 -1 3.9445   0.05 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

... and that the Brent crude spot price Granger causes U.S. gasoline spot prices:

Granger causality test
Model 1: avgConvGas ~ Lags(avgConvGas, 1:1) + Lags(brent, 1:1)
Model 2: avgConvGas ~ Lags(avgConvGas, 1:1)
  Res.Df Df      F   Pr(>F)   
1     92                      
2     93 -1 10.184 0.001938 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Based on the p-values of the probability values (Pr(> F)) of the F tests in the output above shows us that the Brent crude oil spot price does Granger cause U.S. gasoline spot prices -- with a significance well within the 95% confidence level.  In the multivariate model, both WTI and Brent crude spot prices can be said to Granger cause U.S. gasoline spot prices.  However, when I combine these results with the linear regression results above, I would say that the WTI spot price is not significant in the multivariate Granger test results and that it weighs down the effects of the Brent spot price. 

2. Granger Causality Tests on the differenced price data


The statistically significant results for the differenced data was slightly different.  The multivariate model results were not significant within the 5% confidence level (but were significant within the 10% confidence level.)


The model that was statistically significant again showed that Brent crude oil spot prices Granger cause U.S. gasoline spot prices:


Granger causality test
Model 1: diffGas ~ Lags(diffGas, 1:1) + Lags(diffBrent, 1:1)
Model 2: diffGas ~ Lags(diffGas, 1:1)
  Res.Df Df      F  Pr(>F)  
1     91                    
2     92 -1 4.0147 0.04808 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Conclusion


To bring this blog series to a close, the simple conclusion that I draw from my research and analysis is that U.S. gasoline spot prices are more influenced by the Brent crude spot price than by the WTI crude spot price.  During my thesis research, the work of Dr. Phillip K. Verleger, Jr. that is listed below in the references played a large role.  In the article "The Margin, Currency, and the Price of Oil", Dr. Verleger explores the hypothesis that the Brent crude oil price represents the marginal market for oil.  I believe that my research and analysis replicates his work and results.


The interesting thing to watch in the coming months is whether or not the marginal market will move now that the U.S. has lifted its ban on the export of crude oil. 


References:


  1. "U.S. gasoline prices move with Brent rather than WTI crude oil," U.S. Energy Information Agency, November 3, 2014. http://www.eia.gov/todayinenergy/detail.cfm?id=18651.
  2. Nathan S. Balke, Stephen P. A. Brown, Mine K. Yucel, "Crude Oil and Gasoline Prices: An Assymetric Relationship?" Federal Reserve Bank of Dallas. Economic Review, First Quarter 1998. https://www.dallasfed.org/assets/documents/research/er/1998/er9801a.pdf.
  3. Clive W. J. Granger, "Investigating Causal Relations by Econometric Models and Cross-Spectral Methods," Econometrica, vol. 37, no. 3, Aug 1969, pp. 424-38. http://www.jstor.org/stable/1912791
  4. James H. Stock and Mark W. Watson, Introduction to Econometrics, 2nd ed. Boston: Pearson, 2007.Introduction to Econometrics
  5. Philip K. Verleger, Jr., "The Determinants of Official OPEC Crude Prices," The Review of Economics and Statistics, vol. LXIV, no. 2, May 1982. Retrieved December 6, 2014. http://www.jstor.org/.
  6. Philip K.Verleger, Jr., "The Margin, Currency, and the Price of Oil," NABE Business Economics, vol. 46, no. 2, 2011. http://www.nabe.com/pubs.
  7. Philip K. Verleger, Jr., "How Wall Street Controls Oil," The International Economy, Winter 2007. http://www.international-economy.com/TIE_W07_Verleger.pdf.



For the articles that are freely available, a direct link is given.  For articles that required membership access, a link to the publishing website is given.

Sunday, February 7, 2016

Using ShinyApps, Learning Python...

The past two months have been very busy, both at work and at home, so I have not had much free time to devote to a new project.

I did finish the last course in the Data Science specialization (Developing Data Products) on Coursera.  In that course, I created an application using RStudio's Shiny framework.  You can check it out on the Shiny Apps website.  I plan to use my Shiny account as a complement to this blog because it makes great interactive graphical output.  In regards to the Data Science certificate program, all that is left for me to earn the specialization certificate is the Capstone course, which starts on 7 March.

I have also started taking the courses in the Python for Everybody series (to earn that specialization certificate) as a crash course in programming with Python before I start the Machine Learning specialization certificate courses this summer.

As my schedule settles down over the course of the month, I will have time to work on some of the ideas that I have for data analysis projects.  Stay tuned!

Sunday, December 6, 2015

Continuing Research on U.S. Gasoline and Crude Oil Prices, Part Three

Continuing on with the work in my previous two posts [1] [2], I will explore the possibility that the data exhibit a unit root by using the urca package in R.  Having already established that the data exhibit significant signs of autocorrelation, checking for unit root (with and without a drift) is another step in the process of working with time series data -- and financial data falls into this category.

For each Augmented Dickey-Fuller (ADF) test looking for unit root, done using the ur.df() function within the urca package, I am including a graph of that variable as a visual reference:

1. The averaged price of U.S. Conventional Gasoline prices


> gasADFtest <- summary(ur.df(avgConvGas, 
                              type = "drift", 
                              selectlags = "BIC"))

> gasADFtest

############################################### 
# Augmented Dickey-Fuller Test Unit Root Test # 
############################################### 

Test regression drift 


Call:
lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.08395 -0.10528  0.01245  0.13314  0.31021 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.24809    0.08949   2.772  0.00675 ** 
z.lag.1     -0.10241    0.03706  -2.764  0.00692 ** 
z.diff.lag   0.39773    0.09582   4.151 7.45e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1941 on 91 degrees of freedom
Multiple R-squared:  0.1881, Adjusted R-squared:  0.1703 
F-statistic: 10.54 on 2 and 91 DF,  p-value: 7.616e-05


Value of test-statistic is: -2.7637 3.8806 

Critical values for test thestatistics: 
      1pct  5pct 10pct
tau2 -3.51 -2.89 -2.58
phi1  6.70  4.71  3.86


2. The West Texas Intermediate crude oil spot price


> wtiADFtest <- summary(ur.df(wti, 
                              type = "drift", 
                              selectlags = "BIC"))


> wtiADFtest

############################################### 
# Augmented Dickey-Fuller Test Unit Root Test # 
############################################### 

Test regression drift 


Call:
lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.47603 -0.08486  0.01266  0.10034  0.34917 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.19902    0.07044   2.825  0.00580 ** 
z.lag.1     -0.09557    0.03394  -2.816  0.00596 ** 
z.diff.lag   0.47096    0.09296   5.066 2.11e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.147 on 91 degrees of freedom
Multiple R-squared:  0.2432, Adjusted R-squared:  0.2265 
F-statistic: 14.62 on 2 and 91 DF,  p-value: 3.123e-06


Value of test-statistic is: -2.8159 4.0264 

Critical values for test statistics: 
      1pct  5pct 10pct
tau2 -3.51 -2.89 -2.58
phi1  6.70  4.71  3.86


3. The Brent crude oil spot price


brentADFtest <- summary(ur.df(brent, 
                                type = "drift", 
                                selectlags = "BIC"))

> brentADFtest

############################################### 
# Augmented Dickey-Fuller Test Unit Root Test # 
############################################### 

Test regression drift 


Call:
lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.42331 -0.07683  0.02460  0.10103  0.35226 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.14981    0.06196   2.418   0.0176 *  
z.lag.1     -0.06661    0.02777  -2.399   0.0185 *  
z.diff.lag   0.48010    0.09208   5.214 1.15e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.147 on 91 degrees of freedom
Multiple R-squared:  0.2468, Adjusted R-squared:  0.2302 
F-statistic: 14.91 on 2 and 91 DF,  p-value: 2.509e-06


Value of test-statistic is: -2.399 2.9484 

Critical values for test statistics: 
      1pct  5pct 10pct
tau2 -3.51 -2.89 -2.58
phi1  6.70  4.71  3.86


Based on reviewing the visualized graphs, I chose to use the type = "drift" option in the ur.df() function.  I do not believe that any strong signs of a trend exist in the data, but I do believe that it is appropriate to treat it for a drift term.  I also used the Bayesian information criteria in order to select the lags used with the selectlags = "BIC" option -- which selected a lag of one (1) for the ADF test.

For each variable, the ADF test shows that we can not reject the null hypothesis that the variable has a unit root.  At the bottom of each test is the ADF test statistic and the ADF test critical values.  In all cases, we can not reject the null hypothesis at the 5-percent significance level, labeled 5pct,  and only for the average gasoline price and the WTI crude price are they significant at the 10-percent (10pct) significance level.  For the purposes of my research and this blog post, I am going to assume that each variable exhibits has a unit root.  The regressions in each test shows the significance of the intercept, lagged term, and the drift component, all of which are statistically significant within the 5-percent significance level.

From here, I will continue my research using the first difference of each variable.  The plots below are for each variable, now in a first-difference form.:




These first-differenced variables make the data stationary about a mean of zero (or near to it, which can be checked by summarizing the variables with summary()) and will aid in performing an accurate causal analysis, which I will perform in my next post.  I plan to conclude this series with my next blog post and make a final update to my GitHub repository for this work as well as list the works I used in the course of my original research, when this was my thesis project.

Wednesday, November 4, 2015

Continuing Research on U.S Gasoline and Crude Oil Prices, Part Two

Exploratory Analysis, cont'd.

In my previous post, I began to explore the relationship between the spot prices of West Texas Intermediate (WTI) crude oil and Brent crude oil with the average of the spot prices of conventional gasoline in the New York  and Gulf hubs.

To continue with my analysis, the question of whether or not there was a statistically significant difference between the dollar-per-gallon price of WTI and Brent crude oil should be addressed.  The first thing I did was create a boxplot of the two price variables:


You can see that the range of the prices are about the same, and the means appear to be only slightly different.  Just to be sure, however, I calculated the confidence interval between the two means using the assumption that the variances were unequal (which they are) and then testing the hypothesis that the difference between the mean prices is zero:

Lower C.I., 95% | Upper C.I., 95% 
   -0.008423387 |     0.280491840 


t-stat, 189.208df | comparative t-stat | comparative p-value
            1.973 |           1.857953 |          0.03236876

So, you can see that the confidence intervals of the difference of the means includes zero, ranging from just more and negative (-) $0.01 to $0.28.  The hypothesis test allows me to have confidence in my decision to not reject the null hypothesis -- which appear to not be statistically significantly different from one another.

However, we know that the two prices are viewed as significantly different in the market, so further exploration is necessary.  First is to check for randomness because it is a basic assumption in statistical modelling; and, if the data are not random, than any statistical tests comparing them may produce questionable results.  For this task, I looked at the autocorrelation and partial autocorrelation of the WTI and Brent crude oil spot prices and the averaged conventional gasoline spot price:


The top row of plots are the autocorrelation plots, which suggest that there is a moderate autocorrelation between the variables and their respective lagged values.  The data do not exhibit randomness and we can infer that the current month's price has a correlation with its own lagged values.

The second row of plots are the partial autocorrelation of the variables in this analysis.  This can be used to find the appropriate lag order for the modeling to be done in the next step of my analysis.  In this case, it appears that the first and second lags are statistically significant for all of the variables (wti, brent, and avgConvGas) and only WTI data has a lagged value beyond that, and that only the third lagged value.

So, it would appear that an appropriate course of action would be to use an autoregressive model, AR(2), for all three variables and an additional model for WTI modeled as an AR(3).

----------------------------------------------------------------------------------------

I'm going to conclude this entry here.  I will continue in my analysis in my next post, which will include moving on to actually modelling the data.

EDIT (05 Nov 2015): I uploaded my R script and dataset to my GitHub account so that it is available for everyone.  I will be updating it as I go along, with the final submission sent when I conclude my work in this blog series.

Sunday, September 27, 2015

Continuing Research on U.S. Gasoline and Crude Oil Prices, Part One

Preface

The next several posts will contain a brief version of the exploratory analysis that I had performed for my final research project (thesis) while I was enrolled in a Masters program in Applied Economics.  The overall goal was to quantify the causal relationship between U.S. gasoline prices and spot crude oil prices -- both West Texas Intermediate (WTI) and Brent.

All of my data comes from the U.S. Dept. of Energy's Energy Information Agency (EIA).  A lot of the initial transformation of the data, such as picking the time frame and choosing which variables to include, was done in LibreOffice Calc.  Originally, the statistical analysis had been done in Stata12, but since migrating myself to R I have done most of my work over again.

The data and R script will be made available in my GitHub account, a link is available on the right-hand side of the page.

Introduction

Crude oil is a heavily traded commodity that garners a lot of attention.  The question that I ultimately would like to answer is: What is the relationship between crude oil and U.S. gasoline prices, and is it quantifiable in some manner?

First, I chose an eight year time period of data from the EIA's publicly available data, beginning in July 2006 and ending in June 2014.  The first part of my analysis focuses primarily on WTI and Brent crude oil spot prices and the simple average of the U.S. Conventional Gasoline prices from the New York and Gulf hubs.  (i.e.: (Conv. Gas NY + Conv. Gas Gulf) / 2)

Exploratory Analysis

Foregoing summary statistics, I will begin by visualizing the data:



From this simple line plot, it looks like the average Conventional Gasoline price tracks closer to the Brent crude oil price than the WTI price.  So this graph is followed up with the correlation between the variables, using the cor() function in R:

                 wti     brent avgConvGas
wti        1.0000000 0.9385743  0.9172266
brent      0.9385743 1.0000000  0.9718169
avgConvGas 0.9172266 0.9718169  1.0000000

From the table above you can see that the two crude oil prices are highly correlated (orange highlighting) with each other, which is not a surprise given the global nature of the crude oil market.  We also see that the two market crude oil prices are highly correlated with the gasoline prices -- again, not a surprise since gasoline is derived from crude oil via the refining process -- but U.S. Conventional Gasoline prices are more correlated with the Brent crude oil price (blue highlighting) than the WTI crude oil price (green highlighting).

...


I'm going to stop here.  I will continue my analysis in Part Two of this post.  Stay tuned!


Monday, August 17, 2015

Alcoa vs. Aluminum

For your reading pleasure, I am submitting a brief analysis of Alcoa's stock price -- as listed on the NY Stock Exchange (NYSE) -- and the London Mercantile Exchange's price for aluminum.

Before I do, I wanted to let whomever may be reading that I have updated my blog a bit with links to other blogs and sites that I frequent, as well as a link to my GitHub account.

... and, my GitHub account now has a repository for the blog that contains my R scripts and data. The README.md file explains the pertinent details.

As for my blog post, all of my data comes from Quandl.  If you haven't done so, you can sign up for an account for free. This will give you easier access to their data.

I decided to go with a year-to-date approach for my analysis.  Alcoa's stock price hasn't been all that great this year, as you can see here:

... it's lost roughly one-third of its value over the course of the year.  So, I looked up the settlement prices (the "cash" or spot price) of aluminum on the LME:

Visually, there is a noticeable correlation.  After controlling the two datasets for commonly shared dates -- U.S. markets and UK markets have some operating differences -- I was able to transform the data to make them comparable.  The correlation between the datasets is roughly 85%.

At first this may appear natural, but the relationship would make more sense if it were the opposite.  One would expect the Alcoa share price to go up as the price of aluminum goes down, since their costs would go down.  To see if the law of supply and demand were working with that assumption, I then downloaded the stock quantities of aluminum that LME has warehoused around the world.


There is a noticeable decrease in quantities of aluminum (both "primary", or pure, and alloy) being warehoused.  These quantities are closing quantities, defined as the on-hand quantity at the close of business day after deliveries in and out of inventory.  Closing quantities includes open interest quantities and cancelled interest quantities.

The next question that comes to mind is: How is this important?

Simply answered with: It may be a sign that demand for aluminum may be falling... and that may be a sign of weakness in the manufacturing sector.  Aluminum is a pretty common metal and is widely used in manufacturing, so decreasing quantities and prices may be the result of decreased demand.  

To see if this hypothesis is indeed true, I will have to do further research focusing on ore production as well as other companies with operations similar to Alcoa.

Sunday, July 26, 2015

Using RPubs To Publish Work

I need to apologize to anyone who is actually reading my blog.  I had every intention of publishing a follow up post about Greece -- comparing its 10-year bond rates with those of the other "troubled" EU countries of Spain, Italy, and Portugal -- but life got in the way a bit.

This post will be a brief explanation of my current course.  I'm currently in the Reproducible Research course in the Data Science specialization certificate offered through Coursera.

One of the assignments in this course has me working with a particularly messy dataset.  After downloading the data, cleaning it up, and performing some exploratory analysis, I published my findings to RPubs.com.

Here is a link to my report.  It's nothing fancy, but I'm proud of it. 

This was created using the knitr package that integrates nicely with RStudio.  It allows you to create markdown documents that are easily viewable in HTML, as well as publishing them to RPubs, which is maintained by the folks who created and maintain RStudio.

knitr can also create documents in .doc and .pdf format as well.  I recommend trying it if you're in a data analysis role in any compacity.

For now, I'm going to leave off here.

Update 11 December 2015: Due to RPubs documents being public, and not wanting to tempt others who are taking the Data Science specialization through Coursera to copy my work, I have removed the report from RPubs.  I apologize for the inconvenience.
Creative Commons LicenseJust A Data Geek Blog by Richard Ian Carpenter is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.