1 ## Copyright (C) 2006 Michael Creel <michael.creel@uab.es>
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
16 ## Example to show how to use NLS
19 n = 100; # how many observations?
21 # the explanatory variables: note that they have unequal scales
22 x = [ones(n,1) rand(n,2)];
23 theta = 1:3; # true coefficients are 1,2,3
26 lambda = exp(x*theta);
27 y = randp(lambda); # generate the dependent variable
29 # example objective function for nls
30 function [obj_contrib, score] = nls_example_obj(theta, data, otherargs)
32 x = data(:,2:columns(data));
33 lambda = exp(x*theta);
35 obj_contrib = errors .* errors;
40 #####################################
41 # define arguments for nls_estimate #
42 #####################################
47 # name of model to estimate
48 model = "nls_example_obj";
49 modelargs = {}; # none required for this obj fn.
50 # controls for bfgsmin - limit to 50 iters, and print final results
53 ####################################
55 ####################################
56 printf("\nNLS estimation example\n");
57 [theta, obj_value, convergence] = nls_estimate(theta, data, model, modelargs, control);