Professional Documents
Culture Documents
1. OVERVIEW
Ł Correspondence to: Jeffrey S. Racine, Department of Economics, McMaster University, Hamilton, ON L8S 4M4,
Canada. E-mail: racinej@mcmaster.ca
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:
h opt : n% * n^1/5);
(%o10)
1.059223841048812
1
n5
This yields the popular ad hoc bandwidth formula h D 1.06n1/5 .
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
u2
3 u2 e 2
ku D p
2 2 2
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.
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.
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
7 2t
yt D 9
3t
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
1
ˇt lnct
tD0
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.
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
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.
We require the following parameters to be constant in order to distinguish them from the
multi-period variables:
We then conduct partial differentiation with respect to the model’s controls. Take the first two
partial differentiations as an example:
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.
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
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