]> Creatis software - CreaPhase.git/blob - octave_packages/econometrics-1.0.8/nls_example.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / econometrics-1.0.8 / nls_example.m
1 ## Copyright (C) 2006  Michael Creel <michael.creel@uab.es>
2 ##
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.
7 ##
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.
12 ##
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/>.
15
16 ## Example to show how to use NLS
17
18 # Generate data
19 n = 100; # how many observations?
20
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
24 theta = theta';
25
26 lambda = exp(x*theta);
27 y = randp(lambda); # generate the dependent variable
28
29 # example objective function for nls
30 function [obj_contrib, score] = nls_example_obj(theta, data, otherargs)
31         y = data(:,1);
32         x = data(:,2:columns(data));
33         lambda = exp(x*theta);
34         errors =  y - lambda;
35         obj_contrib = errors .* errors;
36         score = "na";
37 endfunction
38
39
40 #####################################
41 # define arguments for nls_estimate #
42 #####################################
43 # starting values
44 theta = zeros(3,1);
45 # data
46 data = [y, x];
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
51 control = {50,1};
52
53 ####################################
54 # do the estimation                #
55 ####################################
56 printf("\nNLS estimation example\n");
57 [theta, obj_value, convergence] = nls_estimate(theta, data, model, modelargs, control);