]> Creatis software - CreaPhase.git/blob - octave_packages/econometrics-1.0.8/scale_data.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / econometrics-1.0.8 / scale_data.m
1 # Copyright (C) 2003,2004  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 # Standardizes and normalizes data matrix,
17 # primarily for use by BFGS
18
19 function [zz, scalecoefs] = scale_data(z);
20
21         n = rows(z);
22         k = columns(z);
23
24         # Scale data
25         s = std(z)';
26         test = s != 0;
27         s = s + (1 - test); # don't scale if column is a constant (avoid div by zero)
28         A = diag(1 ./ s);
29
30         # De-mean all variables except constant, if a constant is present
31         test = std(z(:,1)) != 0;
32         if test
33                 bb = zeros(n,k);
34                 b = zeros(k,1);
35         else
36                 b = -mean(z)';
37                 test = std(z)' != 0;
38                 # don't take out mean if the column is a constant, to preserve identification
39                 b = b .* test;
40                 b = A*b;
41                 bb = (diag(b) * ones(k,n))';
42         endif
43         zz = z*A + bb;
44         scalecoefs = {A,b};
45 endfunction