# Object Oriented Programming with R: An example with a Cournot duopoly

April 23, 2014
By

(This article was first published on Econometrics and Free Software, and kindly contributed to R-bloggers)

I started reading Applied Computational Economics & Finance by Mario J. Miranda and Paul L. Fackler. It is a very interesting book that I recommend to every one of my colleagues. The only issue I have with this book, is that the programming language they use is Matlab, which is proprietary. While there is a free as in freedom implementation of the Matlab language, namely Octave, I still prefer using R. In this post, I will illustrate one example the authors present in the book with R, using the package rootSolve. rootSolve implements Newtonian algorithms to find roots of functions; to specify the functions for which I want the roots, I use R's Object-Oriented Programming (OOP) capabilities to build a model that returns two functions.

### Theoretical background

The example is taken from Miranda's and Fackler's book, on page 35. The authors present a Cournot duopoly model. In a Cournot duopoly model, two firms compete against each other by quantities. Both produce a certain quantity of an homogenous good, and take the quantity produce by their rival as given.

The inverse demand of the good is :

the cost function for firm i is:

and the profit for firm i:

The optimality condition for firm i is thus:

### Implementation in R

If we want to find the optimal quantities and we need to program the optimality condition and we could also use the jacobian of the optimality condition. The jacobian is generally useful to speed up the root finding routines. This is were OOP is useful. Firt let's create a new class, called Model:

setClass(Class = "Model", slots = list(OptimCond = "function", JacobiOptimCond = "function"))


This new class has two slots, which here are functions (in general slots are properties of your class); we need the model to return the optimality condition and the jacobian of the optimality condition.

Now we can create a function which will return these two functions for certain values of the parameters, c and of the model:

my_mod <- function(eta, c) {
e = -1/eta

OptimCond <- function(q) {
return(sum(q)^e + e * sum(q)^(e - 1) * q - diag(c) %*% q)
}

JacobiOptimCond <- function(q) {
return((e * sum(q)^e) * array(1, c(2, 2)) + (e * sum(q)^(e - 1)) * diag(1,
2) + (e - 1) * e * sum(q)^(e - 2) * q * c(1, 1) - diag(c))
}

return(new("Model", OptimCond = OptimCond, JacobiOptimCond = JacobiOptimCond))

}


The function my_mod takes two parameters, eta and c and returns two functions, the optimality condition and the jacobian of the optimality condition. Both are now accessible via my_mod(eta=1.6,c = c(0.6,0.8))@OptimCond and my_mod(eta=1.6,c = c(0.6,0.8))@JacobiOptimCond respectively (and by specifying values for eta and c).

Now, we can use the rootSolve package to get the optimal values and :

library("rootSolve")

multiroot(f = my_mod(eta = 1.6, c = c(0.6, 0.8))@OptimCond, start = c(1, 1),
maxiter = 100, jacfunc = my_mod(eta = 1.6, c = c(0.6, 0.8))@JacobiOptimCond)

## $root ## [1] 0.8396 0.6888 ## ##$f.root
##            [,1]
## [1,] -2.220e-09
## [2,]  9.928e-09
##
## $iter ## [1] 4 ## ##$estim.precis
## [1] 6.074e-09


After 4 iterations, we get that and are equal to 0.84 and 0.69 respectively, which are the same values as in the book!

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...