You are on page 1of 9

JOURNAL OF APPLIED ECONOMETRICS

J. Appl. Econ. 23: 515– 523 (2008)


Published online in Wiley InterScience
(www.interscience.wiley.com) DOI: 10.1002/jae.1007

MAXIMA: AN OPEN SOURCE COMPUTER ALGEBRA SYSTEM

JINHU LI AND JEFFREY S. RACINE*


Department of Economics, McMaster University, Hamilton, Ontario, Canada

1. OVERVIEW

In one way or another, economists, especially econometricians, wind up spending a good


part of their day interacting with computers. We may find ourselves writing Monte Carlo
simulations designed to study the finite-sample behaviour of statistical procedures, conducting
applied data analysis and visualization, or conducting mathematical analysis with the aid of
symbolic mathematical software. Most are aware of the renowned open source GNU compiler
collection (gcc.gnu.org), which is useful for low-level programming in, say, Fortran or C, while
the profession has embraced R, which is an open source high-level language and environment
for statistical computing and graphics; see www.R-project.org or Racine and Hyndman (2002) for
further information. However, until recently, the availability of a tested and powerful platform for
symbolic mathematics was notably absent from the open source arena.
Maxima is a computer algebra package that has a variety of potential uses for economists
and econometricians alike. It can be used to manipulate symbolic and numerical expressions
including differentiation, integration, Taylor series, Laplace transforms, ordinary differential
equations, systems of linear equations, and vectors, matrices, and tensors. Maxima produces
high-precision results by using exact fractions and arbitrarily long floating point representations,
and can plot functions and data in two and three dimensions, while also supporting gnuplot;
see www.gnuplot.info and Racine (2006) for more information regarding this powerful portable
interactive data and function plotting utility.
In this brief review, we describe the Maxima computer algebra system via a set of examples,
and also briefly discuss some additional features that make using Maxima a joy for economists
and econometricians alike. For what follows, we employ version 5.10, which was released in the
fall of 2006.

Ł Correspondence to: Jeffrey S. Racine, Department of Economics, McMaster University, Hamilton, ON L8S 4M4,
Canada. E-mail: racinej@mcmaster.ca

Copyright  2008 John Wiley & Sons, Ltd.


516 J. LI AND J. S. RACINE

2. OBTAINING AND INSTALLING


The software and related documentation can be downloaded from the Maxima home page (max-
ima.sourceforge.net), while a listserve with subscription details can be found on an open mailing
list (maxima.sourceforge.net/maximalist.html). Precompiled versions for Microsoft Windows, sev-
eral GNU/Linux distributions, and source code that can be compiled on various variants of UNIX
such as BSD is available.
One extremely useful add-on for GNU Emacs users (a popular open source editor) is imaxima.el,
which prepares a LATEX version of the Maxima buffer when ghostscript and LATEX are installed on
the system. Imaxima processes the output from Maxima with LATEX and inserts the resulting image
in the buffer; see the website link members3.jcom.home.ne.jp/imaxima for documents, downloads,
and installation instructions. For the examples that follow, we will deploy Maxima on a FreeBSD
system with imaxima.el running in an Emacs window.

3. MAXIMA EXAMPLES
3.1. Deriving Bandwidths for Kernel Density Estimates

Univariate kernel density estimation methods are now found in almost all popular statistical
programs. Bandwidth selection methods are required in order to choose the appropriate bandwidth.
One popular method is the ad hoc ‘Normal Reference Rule of Thumb’, in which a Gaussian kernel
function is employed, the variable at hand is presumed to be drawn from the normal distribution,
and the bandwidth is selected by minimizing the integrated mean squared error of the underlying
density; see Li and Racine (2007, pp. 13–14) for details. The ad hoc reference bandwidth, denoted
href , is then given by
  1/5

 



2
K zdz 

href D  2  n1/5

 


 2
z Kzdz ff00 xg2 dx 

where KÐ denotes the kernel function and fÐ the underlying density. Computing href by hand
can be a somewhat tedious calculation. However, using Maxima, the ad hoc bandwidth can be
derived as follows:

(%i1) kernel : (1/(sqrt (2*%pi))) * exp (z^2/2);


a : integrate (kernel^2, z, inf, inf), numer;
b : integrate (z^2 * kernel, z, inf, inf), numer;
density : (1/(sqrt (2*%pi) * sigma))
* exp (((xmu)/sigma)^2/2);
assume (sigma > 0);
assume (mu > 0, mu < inf);
density diff 2 : diff (density, x, 2);
c : integrate (density diff 2^2, x, inf, inf);
(a/(b * c))^(1/5), numer;
Copyright  2008 John Wiley & Sons, Ltd. J. Appl. Econ. 23: 515–523 (2008)
DOI: 10.1002/jae
SOFTWARE REVIEW 517

h opt : n% * n^1/5);
(%o10)
1.059223841048812
1
n5
This yields the popular ad hoc bandwidth formula h D 1.06n1/5 .

3.2. Constructing Higher-Order Kernel Functions


Higher-order kernel functions are often used to reduce bias in nonparametric kernel settings.
Their construction, however, can be extremely tedious; see Li and Racine (2007, pp. 33–35) for
details. Beginning with any second order kernel function, a set of moment constraints can be
constructed, yielding a polynomial which, when solved, delivers the desired higher-order kernel.
In the following example, a fourth order Gaussian kernel function is constructed using Maxima.
We first generate a polynomial for the fourth order kernel function:

ku D A2 u2 C A1 u C A0 u

where u) is the second order Gaussian kernel and A0 , A1 and A2 must satisfy moment
requirements implied by a fourth order kernel, namely

kudu D 1

and 
ul kudu D 0, l D 1, 2, 3

Using Maxima, a fourth order kernel can be derived as follows:

(%i1) gauker : (1/(sqrt (2*%pi))) * exp (Z^2/2);


int gauker : integrate (gauker, Z, inf, inf);
int Z gauker : integrate (Z * gauker, Z, inf, inf);
int Z2 gauker : integrate (Z ** 2 * gauker, Z, inf, inf);
int Z3 gauker : integrate (Z ** 3 * gauker, Z, inf, inf);
int Z4 gauker : integrate (Z ** 4 * gauker, Z, inf, inf);
eqn0 : A0 * int gauker C A1 * int Z gauker C A2
* int Z2 gauker D 1;
eqn1 : A0 * int Z gauker C A1 * int Z2 gauker C A2
* int Z3 gauker D 0;
eqn2 : A0 * int Z2 gauker C A1 * int Z3 gauker C A2
* int Z4 gauker D 0;
solve ([eqn0, eqn1, eqn2], [A0, A1, A2]);
(%o1)

3 1
A0 D , A1 D 0, A2 D 
2 2
Copyright  2008 John Wiley & Sons, Ltd. J. Appl. Econ. 23: 515–523 (2008)
DOI: 10.1002/jae
518 J. LI AND J. S. RACINE

The resulting fourth order Gaussian kernel is therefore given by

 u2
3 u2 e 2
ku D  p
2 2 2

3.3. Monte Carlo Simulations

When conducting applied data analysis or assessing the finite-sample behaviour of an estimator,
one would most likely choose a statistical platform such as R. However, Maxima, which is mainly
a tool for symbolic rather than numerical mathematics, can also perform numerical computation.
For instance, the Maxima package distrib contains a set of functions for making probability
computations. Therefore, Maxima could serve as a platform for numerical statistical methods such
as simulating random variables, calculating moments, and so forth. The following simple example
conducts a Monte Carlo experiment designed to assess the finite-sample properties of the popular
linear OLS regression estimator. We use a dataset on household expenditure and first obtain OLS
estimates of a food expenditure relationship; see shazam.econ.ubc.ca/intro/mcarlo.htm for the same
example conducted in SHAZAM. Assuming that the empirical relationship is the true model and
that the error term is normally distributed with mean 0 and variance 46.8, we then conduct a Monte
Carlo simulation for a sample of size n D 40. The sampling properties of the OLS estimator can
then be illustrated by comparing the empirical frequency distributions of the parameter estimates
to those for the original sample. Using Maxima, the Monte Carlo experiment can be expressed as
follows:

load (‘‘distrib’’);
load (‘‘numericalio.lisp’’);
/*** Import data file and read data ***/
fn : ‘‘C:/Program Files/GHJdata.txt’’;
mat : read matrix (fn)$
ones : zeromatrix (40, 1) C 1$
f : addcol (ones, transpose (matrix (random normal (0, 1, 40))))$
/*** Estimate b from the original data ***/
dep : col (mat, 1)$
indep : addcol (ones, col (mat, 2))$
b : invert (transpose (indep).indep).transpose (indep).dep;
/*** Simulation based on the assumed true model, nD40 ***/
M : 1000;
vector b1 40 : zeromatrix (M, 1)$
for i:1 thru M do
(e[i] : matrix (random normal (0, 46.853, 40)),
y[i] : indep.transpose (b) C transpose (e[i]),
simu b[i] : invert (transpose (indep).indep).transpose
(indep). y[i],
simu b1[i] : simu b[i][2, 1],
vector b1 40[i, 1] : simu b1[i])$
Copyright  2008 John Wiley & Sons, Ltd. J. Appl. Econ. 23: 515–523 (2008)
DOI: 10.1002/jae
SOFTWARE REVIEW 519

load (‘‘descriptive’’)$
histogram (vector b1 40, ‘maintitle D ‘‘Frequency Histogram of
b1, nD40’’,
’nclasses D 30, ‘relbarwidth D 1);

Obviously, unlike the statistical platform R, Maxima is not designed to be a tool for linear and
nonlinear regression, statistical tests, time-series analysis, and so on. Most of these functions have
not been integrated into Maxima, nor would one expect them to be. Therefore, the use of Maxima
as a tool for numerical manipulation is quite limited.

3.4. Solving Difference Equations

Difference equations are widely used in macroeconomics. There are various ways to solve
difference equations by hand, all of which can be tedious. Using Maxima, however, it is easy
to solve difference equations using just one step. The package solve rec has to be loaded first
to solve difference equations in Maxima.

(%i1) load (solve rec);


(%i5) eq1 : y[tC1] D a C c * y[t];
(%o5)
ytC1 D cyt C a

Given the initial value of y0 D 2, solve the difference equation by

(%i6) solve rec (eq1, y[t], y[0]D2);


(%o6)

a ct a
yt D C 2 ct 
c1 c1

The evaluation function ev() can evaluate the above expression for given values of a and c, as
in

(%i8) ev (%, a D 3, c D 2/3);


(%o8)

7 2t
yt D 9 
3t

3.5. Dynamic Programming (Value Function Iteration)

Dynamic programming is widely used to solve recursive problems in financial economics and
macroeconomics. One of the main methods for solving a dynamic programming problem involves
the so-called value function iteration. Below we present a short program for solving policy
functions for the optimal growth model (Cass–Koopmans) by way of value function iteration.
Copyright  2008 John Wiley & Sons, Ltd. J. Appl. Econ. 23: 515–523 (2008)
DOI: 10.1002/jae
520 J. LI AND J. S. RACINE

In this model, the central planner maximizes the objective function


1
ˇt lnct 
tD0

subject to the restriction


ct C ktC1 D Akt ˛

The Bellman equation can be set up as

VjC1 kt  D max flnct  C ˇVj ktC1 g


ct ,ktC1

Starting with a zero value of V0 , we can solve the policy functions to maximize V1 in the first
iteration. Then the derived policy functions ct and ktC1 are plugged into the Bellman equation to
get the value for V1 , which can be used for the second iteration. The process will be conducted for
several iterations to derive the desired policy functions. The whole process can be implemented
using the loop function in Maxima. For this example, the package lrats must first be loaded in
order to access the function lratsubst, which will be used for algebraic substitution in later
steps.

/*** Assign the initial value 0 to the value function. ***/


load (‘‘lrats’’);
V[0] : 0;
V up[0] : 0;
/*** Use a do loop to implement value function iteration ***/
for i:1 thru 2 do
(Equ : c C k1 D A * k0^a,
depends (c, k1),
c k1 : rhs (first (solve (Equ, c))),
Obj : log(c) C b * V up[i1],
Deriv eqn : diff (Equ, k1),
Deriv c : rhs (first (solve (Deriv eqn, ‘diff (c, k1)))),
Deriv Obj : diff (Obj, k1),
Deriv Obj is : 1ratsubst ([’diff(c, k1) D Deriv c, cDc k1],
Deriv Obj),
Soln: solve (Deriv Obj is D 0, k1),
if iD1 then k1 is : rhs (Soln) else k1 is : rhs (first (Soln)),
c is : ratsubst (k1 is, k1, c k1),
c[i] : c is,
k1[i] : k1 is,
V[i] : log(c[i]) C b * ev (V[i1], c D c[i], k1 D k1[i]),
V up[i] : ratsubst (k1, k0, V[i]));
/*** Display the policy functions ***/
c[1]; k1[1]; c[2]; k1[2];

The policy functions from the first and the second iteration are generated as
Copyright  2008 John Wiley & Sons, Ltd. J. Appl. Econ. 23: 515–523 (2008)
DOI: 10.1002/jae
SOFTWARE REVIEW 521

(%o5)
k0 a A

(%o6)
0

(%o7)
k0 a A
a bC1
(%o8)
a bk0 a A
a bC1

3.6. Optimal Control Problems (Real Business Cycle Models)


For many growth or economic fluctuation models, optimal control methods are the tool of choice
for model solution. Economists frequently use optimal control theory as a technique for studying
problems involving optimal decisions in a multi-period framework. Partial differentiation and total
differentiation are often used to solve constrained maximization problems. When the number of
variables in the model increases, the process becomes increasingly complicated. However, one
could use Maxima greatly to simplify the entire process. The following example uses optimal
control to solve a real business cycle (RBC) model with capital adjustment costs. The model is
given by


1
max E0 ˇt [logCt  C  log1  nt ], 1>ˇ>0
tD0
 2
 It
s.t.KtC1 D 1  υKt C It   υ Kt , 0  , 0 < υ < 1
2 Kt
Ct C It D At Kt ˛ Nt ˇ

In an optimal control process, the constrained maximization problem is first solved by the regular
Lagrangian method. The optimal rule for the control variables can be derived by taking the first
order derivatives of the Lagrangian with respect to the controls. Total differentiation with respect
to all the model variables is then conducted for further linearization to the first order conditions
(FOCs). The following are the steps needed to solve the RBC model with capital adjustment costs
in Maxima.

(%i1) load (‘‘lrats’’)$

We require the following parameters to be constant in order to distinguish them from the
multi-period variables:

(%i2) declare ([d, a, b, v, B], constant)$

Next we define the objective function and constraints:


Copyright  2008 John Wiley & Sons, Ltd. J. Appl. Econ. 23: 515–523 (2008)
DOI: 10.1002/jae
522 J. LI AND J. S. RACINE

(%i3) obj : log(c[t]) C B * log(1n[t])$


(%i4) eq1 : k[tC1]  (1d) * k[t]  v/2 * (i[t]/k[t]  d)^2
* k[t]  i[t] D 0$
(%i5) eq2 : A * k[t]^a * n[t]^b  c[t] C i[t] D 0$
(%i6) const1 t : lhs (eq1)$
(%i7) const2 t : lhs (eq2)$

Then we substitute and update the constraints:

(%i8) const1 tup1 : lratsubst (k[tC1] D k[tC2], const1 t)$


(%i9) const1 tup2 : lratsubst ([k[t] D k[tC1], i[t] D i[tC1]],
const1 tup1)$
(%i10) const2 tup2 : lratsubst ([k[t] D k[tC1], n[t] D n[tC1],
c[t] D c[tC1],
i[t] D i[tC1]], const2 t)$

The Lagrangian is defined as

(%i11) L : obj C u1[t] * const1 t C u1[tC1]^2 * const1 tup2


C u2[t] * const2 t C u2[t C 1]^2 * const2 tup2$

We then conduct partial differentiation with respect to the model’s controls. Take the first two
partial differentiations as an example:

(%i12) eq3 : diff (L, c[t]) D 0;


(%o12)
1
 u2t D 0
ct

(%i13) eq4 : diff (L, n[t]) D 0;


(%o13)
B
b kta nb1
t u2t A  D0
1  nt

Finally, we conduct total differentiation of the first-order conditions with respect to all of the
model’s variables. For instance, total differentiation of the first FOC can be derived by
(%i18) totdiff3 : diff (lhs (eq3))$
Some of the results are omitted here for the sake of brevity.

3.7. Plotting Utility Functions

The concavity or convexity of utility functions is often studied by economists. Using the powerful
plotting functions contained in Maxima, we can easily plot different types of utility functions and
visualize their properties. The properties of indifference curves can be seen from the contour of
the utility function in a 3D plot. The following command plots the CES utility function:
Copyright  2008 John Wiley & Sons, Ltd. J. Appl. Econ. 23: 515–523 (2008)
DOI: 10.1002/jae
SOFTWARE REVIEW 523

(%i1) plot3d ((x^1/3) C y^1/3))^3), [x, 1, 1000],


[y, 1, 1000], [gnuplot pm3d, true], [gnuplot preamble,
‘‘set contour’’], [gnuplot curve titles, [‘‘title ’CES
utility function, rho<0’ ‘’]]);

4. CONCLUSION
Though not a tool designed primarily for numerical analysis, Maxima is a computer algebra
package that has a variety of potential uses for economists and econometricians alike, including
numerical analysis if desired. In this review, we have presented a variety of examples which might
be of interest to economists and econometricians. Given its open source nature, features, stability,
and availability for a wide range of platforms, Maxima ought to be an appealing tool for pedagogy
and research.

ACKNOWLEDGEMENTS

We would like to thank but not implicate Robert Dodier and James MacKinnon for their valuable
input.

REFERENCES
Li Q, Racine J. 2007. Nonparametric Econometrics: Theory and Practice. Princeton University Press:
Princeton, NJ.
Racine JS. 2006. gnuplot 4.0: a portable interactive plotting utility. Journal of Applied Econometrics 21:
133–141.
Racine JS, Hyndman R. 2002. Using R to teach econometrics. Journal of Applied Econometrics 17: 175–189.

Copyright  2008 John Wiley & Sons, Ltd. J. Appl. Econ. 23: 515–523 (2008)
DOI: 10.1002/jae

You might also like