alglib 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/Manifest.txt +253 -0
- data/README.txt +33 -0
- data/Rakefile +27 -0
- data/ext/Rakefile +24 -0
- data/ext/alglib.i +24 -0
- data/ext/alglib/Makefile +157 -0
- data/ext/alglib/airyf.cpp +372 -0
- data/ext/alglib/airyf.h +81 -0
- data/ext/alglib/alglib.cpp +8558 -0
- data/ext/alglib/alglib_util.cpp +19 -0
- data/ext/alglib/alglib_util.h +14 -0
- data/ext/alglib/ap.cpp +877 -0
- data/ext/alglib/ap.english.html +364 -0
- data/ext/alglib/ap.h +666 -0
- data/ext/alglib/ap.russian.html +442 -0
- data/ext/alglib/apvt.h +754 -0
- data/ext/alglib/bdss.cpp +1500 -0
- data/ext/alglib/bdss.h +251 -0
- data/ext/alglib/bdsvd.cpp +1339 -0
- data/ext/alglib/bdsvd.h +164 -0
- data/ext/alglib/bessel.cpp +1226 -0
- data/ext/alglib/bessel.h +331 -0
- data/ext/alglib/betaf.cpp +105 -0
- data/ext/alglib/betaf.h +74 -0
- data/ext/alglib/bidiagonal.cpp +1328 -0
- data/ext/alglib/bidiagonal.h +350 -0
- data/ext/alglib/binomialdistr.cpp +247 -0
- data/ext/alglib/binomialdistr.h +153 -0
- data/ext/alglib/blas.cpp +576 -0
- data/ext/alglib/blas.h +132 -0
- data/ext/alglib/cblas.cpp +226 -0
- data/ext/alglib/cblas.h +57 -0
- data/ext/alglib/cdet.cpp +138 -0
- data/ext/alglib/cdet.h +92 -0
- data/ext/alglib/chebyshev.cpp +216 -0
- data/ext/alglib/chebyshev.h +76 -0
- data/ext/alglib/chisquaredistr.cpp +157 -0
- data/ext/alglib/chisquaredistr.h +144 -0
- data/ext/alglib/cholesky.cpp +285 -0
- data/ext/alglib/cholesky.h +86 -0
- data/ext/alglib/cinverse.cpp +298 -0
- data/ext/alglib/cinverse.h +111 -0
- data/ext/alglib/clu.cpp +337 -0
- data/ext/alglib/clu.h +120 -0
- data/ext/alglib/correlation.cpp +280 -0
- data/ext/alglib/correlation.h +77 -0
- data/ext/alglib/correlationtests.cpp +726 -0
- data/ext/alglib/correlationtests.h +134 -0
- data/ext/alglib/crcond.cpp +826 -0
- data/ext/alglib/crcond.h +148 -0
- data/ext/alglib/creflections.cpp +310 -0
- data/ext/alglib/creflections.h +165 -0
- data/ext/alglib/csolve.cpp +312 -0
- data/ext/alglib/csolve.h +99 -0
- data/ext/alglib/ctrinverse.cpp +387 -0
- data/ext/alglib/ctrinverse.h +98 -0
- data/ext/alglib/ctrlinsolve.cpp +297 -0
- data/ext/alglib/ctrlinsolve.h +81 -0
- data/ext/alglib/dawson.cpp +234 -0
- data/ext/alglib/dawson.h +74 -0
- data/ext/alglib/descriptivestatistics.cpp +436 -0
- data/ext/alglib/descriptivestatistics.h +112 -0
- data/ext/alglib/det.cpp +140 -0
- data/ext/alglib/det.h +94 -0
- data/ext/alglib/dforest.cpp +1819 -0
- data/ext/alglib/dforest.h +316 -0
- data/ext/alglib/elliptic.cpp +497 -0
- data/ext/alglib/elliptic.h +217 -0
- data/ext/alglib/estnorm.cpp +429 -0
- data/ext/alglib/estnorm.h +107 -0
- data/ext/alglib/expintegrals.cpp +422 -0
- data/ext/alglib/expintegrals.h +108 -0
- data/ext/alglib/faq.english.html +258 -0
- data/ext/alglib/faq.russian.html +272 -0
- data/ext/alglib/fdistr.cpp +202 -0
- data/ext/alglib/fdistr.h +163 -0
- data/ext/alglib/fresnel.cpp +211 -0
- data/ext/alglib/fresnel.h +91 -0
- data/ext/alglib/gammaf.cpp +338 -0
- data/ext/alglib/gammaf.h +104 -0
- data/ext/alglib/gqgengauss.cpp +235 -0
- data/ext/alglib/gqgengauss.h +92 -0
- data/ext/alglib/gqgenhermite.cpp +268 -0
- data/ext/alglib/gqgenhermite.h +63 -0
- data/ext/alglib/gqgenjacobi.cpp +297 -0
- data/ext/alglib/gqgenjacobi.h +72 -0
- data/ext/alglib/gqgenlaguerre.cpp +265 -0
- data/ext/alglib/gqgenlaguerre.h +69 -0
- data/ext/alglib/gqgenlegendre.cpp +300 -0
- data/ext/alglib/gqgenlegendre.h +62 -0
- data/ext/alglib/gqgenlobatto.cpp +305 -0
- data/ext/alglib/gqgenlobatto.h +97 -0
- data/ext/alglib/gqgenradau.cpp +232 -0
- data/ext/alglib/gqgenradau.h +95 -0
- data/ext/alglib/hbisinv.cpp +480 -0
- data/ext/alglib/hbisinv.h +183 -0
- data/ext/alglib/hblas.cpp +228 -0
- data/ext/alglib/hblas.h +64 -0
- data/ext/alglib/hcholesky.cpp +339 -0
- data/ext/alglib/hcholesky.h +91 -0
- data/ext/alglib/hermite.cpp +114 -0
- data/ext/alglib/hermite.h +49 -0
- data/ext/alglib/hessenberg.cpp +370 -0
- data/ext/alglib/hessenberg.h +152 -0
- data/ext/alglib/hevd.cpp +247 -0
- data/ext/alglib/hevd.h +107 -0
- data/ext/alglib/hsschur.cpp +1316 -0
- data/ext/alglib/hsschur.h +108 -0
- data/ext/alglib/htridiagonal.cpp +734 -0
- data/ext/alglib/htridiagonal.h +180 -0
- data/ext/alglib/ialglib.cpp +6 -0
- data/ext/alglib/ialglib.h +9 -0
- data/ext/alglib/ibetaf.cpp +960 -0
- data/ext/alglib/ibetaf.h +125 -0
- data/ext/alglib/igammaf.cpp +430 -0
- data/ext/alglib/igammaf.h +157 -0
- data/ext/alglib/inv.cpp +274 -0
- data/ext/alglib/inv.h +115 -0
- data/ext/alglib/inverseupdate.cpp +480 -0
- data/ext/alglib/inverseupdate.h +185 -0
- data/ext/alglib/jacobianelliptic.cpp +164 -0
- data/ext/alglib/jacobianelliptic.h +94 -0
- data/ext/alglib/jarquebera.cpp +2271 -0
- data/ext/alglib/jarquebera.h +80 -0
- data/ext/alglib/kmeans.cpp +356 -0
- data/ext/alglib/kmeans.h +76 -0
- data/ext/alglib/laguerre.cpp +94 -0
- data/ext/alglib/laguerre.h +48 -0
- data/ext/alglib/lbfgs.cpp +1167 -0
- data/ext/alglib/lbfgs.h +218 -0
- data/ext/alglib/lda.cpp +434 -0
- data/ext/alglib/lda.h +133 -0
- data/ext/alglib/ldlt.cpp +1130 -0
- data/ext/alglib/ldlt.h +124 -0
- data/ext/alglib/leastsquares.cpp +1252 -0
- data/ext/alglib/leastsquares.h +290 -0
- data/ext/alglib/legendre.cpp +107 -0
- data/ext/alglib/legendre.h +49 -0
- data/ext/alglib/linreg.cpp +1185 -0
- data/ext/alglib/linreg.h +380 -0
- data/ext/alglib/logit.cpp +1523 -0
- data/ext/alglib/logit.h +333 -0
- data/ext/alglib/lq.cpp +399 -0
- data/ext/alglib/lq.h +160 -0
- data/ext/alglib/lu.cpp +462 -0
- data/ext/alglib/lu.h +119 -0
- data/ext/alglib/mannwhitneyu.cpp +4490 -0
- data/ext/alglib/mannwhitneyu.h +115 -0
- data/ext/alglib/minlm.cpp +918 -0
- data/ext/alglib/minlm.h +312 -0
- data/ext/alglib/mlpbase.cpp +3375 -0
- data/ext/alglib/mlpbase.h +589 -0
- data/ext/alglib/mlpe.cpp +1369 -0
- data/ext/alglib/mlpe.h +552 -0
- data/ext/alglib/mlptrain.cpp +1056 -0
- data/ext/alglib/mlptrain.h +283 -0
- data/ext/alglib/nearunityunit.cpp +91 -0
- data/ext/alglib/nearunityunit.h +17 -0
- data/ext/alglib/normaldistr.cpp +377 -0
- data/ext/alglib/normaldistr.h +175 -0
- data/ext/alglib/nsevd.cpp +1869 -0
- data/ext/alglib/nsevd.h +140 -0
- data/ext/alglib/pca.cpp +168 -0
- data/ext/alglib/pca.h +87 -0
- data/ext/alglib/poissondistr.cpp +143 -0
- data/ext/alglib/poissondistr.h +130 -0
- data/ext/alglib/polinterpolation.cpp +685 -0
- data/ext/alglib/polinterpolation.h +206 -0
- data/ext/alglib/psif.cpp +173 -0
- data/ext/alglib/psif.h +88 -0
- data/ext/alglib/qr.cpp +414 -0
- data/ext/alglib/qr.h +168 -0
- data/ext/alglib/ratinterpolation.cpp +134 -0
- data/ext/alglib/ratinterpolation.h +72 -0
- data/ext/alglib/rcond.cpp +705 -0
- data/ext/alglib/rcond.h +140 -0
- data/ext/alglib/reflections.cpp +504 -0
- data/ext/alglib/reflections.h +165 -0
- data/ext/alglib/rotations.cpp +473 -0
- data/ext/alglib/rotations.h +128 -0
- data/ext/alglib/rsolve.cpp +221 -0
- data/ext/alglib/rsolve.h +99 -0
- data/ext/alglib/sbisinv.cpp +217 -0
- data/ext/alglib/sbisinv.h +171 -0
- data/ext/alglib/sblas.cpp +185 -0
- data/ext/alglib/sblas.h +64 -0
- data/ext/alglib/schur.cpp +156 -0
- data/ext/alglib/schur.h +102 -0
- data/ext/alglib/sdet.cpp +193 -0
- data/ext/alglib/sdet.h +101 -0
- data/ext/alglib/sevd.cpp +116 -0
- data/ext/alglib/sevd.h +99 -0
- data/ext/alglib/sinverse.cpp +672 -0
- data/ext/alglib/sinverse.h +138 -0
- data/ext/alglib/spddet.cpp +138 -0
- data/ext/alglib/spddet.h +96 -0
- data/ext/alglib/spdgevd.cpp +842 -0
- data/ext/alglib/spdgevd.h +200 -0
- data/ext/alglib/spdinverse.cpp +509 -0
- data/ext/alglib/spdinverse.h +122 -0
- data/ext/alglib/spdrcond.cpp +421 -0
- data/ext/alglib/spdrcond.h +118 -0
- data/ext/alglib/spdsolve.cpp +275 -0
- data/ext/alglib/spdsolve.h +105 -0
- data/ext/alglib/spline2d.cpp +1192 -0
- data/ext/alglib/spline2d.h +301 -0
- data/ext/alglib/spline3.cpp +1264 -0
- data/ext/alglib/spline3.h +290 -0
- data/ext/alglib/srcond.cpp +595 -0
- data/ext/alglib/srcond.h +127 -0
- data/ext/alglib/ssolve.cpp +895 -0
- data/ext/alglib/ssolve.h +139 -0
- data/ext/alglib/stdafx.h +0 -0
- data/ext/alglib/stest.cpp +131 -0
- data/ext/alglib/stest.h +94 -0
- data/ext/alglib/studenttdistr.cpp +222 -0
- data/ext/alglib/studenttdistr.h +115 -0
- data/ext/alglib/studentttests.cpp +377 -0
- data/ext/alglib/studentttests.h +178 -0
- data/ext/alglib/svd.cpp +620 -0
- data/ext/alglib/svd.h +126 -0
- data/ext/alglib/tdbisinv.cpp +2608 -0
- data/ext/alglib/tdbisinv.h +228 -0
- data/ext/alglib/tdevd.cpp +1229 -0
- data/ext/alglib/tdevd.h +115 -0
- data/ext/alglib/tridiagonal.cpp +594 -0
- data/ext/alglib/tridiagonal.h +171 -0
- data/ext/alglib/trigintegrals.cpp +490 -0
- data/ext/alglib/trigintegrals.h +131 -0
- data/ext/alglib/trinverse.cpp +345 -0
- data/ext/alglib/trinverse.h +98 -0
- data/ext/alglib/trlinsolve.cpp +926 -0
- data/ext/alglib/trlinsolve.h +73 -0
- data/ext/alglib/tsort.cpp +405 -0
- data/ext/alglib/tsort.h +54 -0
- data/ext/alglib/variancetests.cpp +245 -0
- data/ext/alglib/variancetests.h +134 -0
- data/ext/alglib/wsr.cpp +6285 -0
- data/ext/alglib/wsr.h +96 -0
- data/ext/ap.i +97 -0
- data/ext/correlation.i +24 -0
- data/ext/extconf.rb +6 -0
- data/ext/logit.i +89 -0
- data/lib/alglib.rb +71 -0
- data/lib/alglib/correlation.rb +26 -0
- data/lib/alglib/linearregression.rb +63 -0
- data/lib/alglib/logit.rb +42 -0
- data/test/test_alglib.rb +52 -0
- data/test/test_correlation.rb +44 -0
- data/test/test_correlationtest.rb +45 -0
- data/test/test_linreg.rb +35 -0
- data/test/test_logit.rb +43 -0
- data/test/test_pca.rb +27 -0
- metadata +326 -0
data/ext/alglib/linreg.h
ADDED
@@ -0,0 +1,380 @@
|
|
1
|
+
/*************************************************************************
|
2
|
+
Copyright (c) 2007-2008, Sergey Bochkanov (ALGLIB project).
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are
|
6
|
+
met:
|
7
|
+
|
8
|
+
- Redistributions of source code must retain the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
- Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer listed
|
13
|
+
in this license in the documentation and/or other materials
|
14
|
+
provided with the distribution.
|
15
|
+
|
16
|
+
- Neither the name of the copyright holders nor the names of its
|
17
|
+
contributors may be used to endorse or promote products derived from
|
18
|
+
this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*************************************************************************/
|
32
|
+
|
33
|
+
#ifndef _linreg_h
|
34
|
+
#define _linreg_h
|
35
|
+
|
36
|
+
#include "ap.h"
|
37
|
+
#include "ialglib.h"
|
38
|
+
|
39
|
+
#include "descriptivestatistics.h"
|
40
|
+
#include "gammaf.h"
|
41
|
+
#include "normaldistr.h"
|
42
|
+
#include "igammaf.h"
|
43
|
+
#include "reflections.h"
|
44
|
+
#include "bidiagonal.h"
|
45
|
+
#include "qr.h"
|
46
|
+
#include "lq.h"
|
47
|
+
#include "blas.h"
|
48
|
+
#include "rotations.h"
|
49
|
+
#include "bdsvd.h"
|
50
|
+
#include "svd.h"
|
51
|
+
|
52
|
+
|
53
|
+
struct linearmodel
|
54
|
+
{
|
55
|
+
ap::real_1d_array w;
|
56
|
+
};
|
57
|
+
struct lrreport
|
58
|
+
{
|
59
|
+
ap::real_2d_array c;
|
60
|
+
double rmserror;
|
61
|
+
double avgerror;
|
62
|
+
double avgrelerror;
|
63
|
+
double cvrmserror;
|
64
|
+
double cvavgerror;
|
65
|
+
double cvavgrelerror;
|
66
|
+
int ncvdefects;
|
67
|
+
ap::integer_1d_array cvdefects;
|
68
|
+
};
|
69
|
+
|
70
|
+
|
71
|
+
/*************************************************************************
|
72
|
+
Linear regression
|
73
|
+
|
74
|
+
Subroutine builds model:
|
75
|
+
|
76
|
+
Y = A(0)*X[0] + ... + A(N-1)*X[N-1] + A(N)
|
77
|
+
|
78
|
+
and model found in ALGLIB format, covariation matrix, training set errors
|
79
|
+
(rms, average, average relative) and leave-one-out cross-validation
|
80
|
+
estimate of the generalization error. CV estimate calculated using fast
|
81
|
+
algorithm with O(NPoints*NVars) complexity.
|
82
|
+
|
83
|
+
When covariation matrix is calculated standard deviations of function
|
84
|
+
values are assumed to be equal to RMS error on the training set.
|
85
|
+
|
86
|
+
INPUT PARAMETERS:
|
87
|
+
XY - training set, array [0..NPoints-1,0..NVars]:
|
88
|
+
* NVars columns - independent variables
|
89
|
+
* last column - dependent variable
|
90
|
+
NPoints - training set size, NPoints>NVars+1
|
91
|
+
NVars - number of independent variables
|
92
|
+
|
93
|
+
OUTPUT PARAMETERS:
|
94
|
+
Info - return code:
|
95
|
+
* -255, in case of unknown internal error
|
96
|
+
* -4, if internal SVD subroutine haven't converged
|
97
|
+
* -1, if incorrect parameters was passed (NPoints<NVars+2, NVars<1).
|
98
|
+
* 1, if subroutine successfully finished
|
99
|
+
LM - linear model in the ALGLIB format. Use subroutines of
|
100
|
+
this unit to work with the model.
|
101
|
+
AR - additional results
|
102
|
+
|
103
|
+
|
104
|
+
-- ALGLIB --
|
105
|
+
Copyright 02.08.2008 by Bochkanov Sergey
|
106
|
+
*************************************************************************/
|
107
|
+
void lrbuild(const ap::real_2d_array& xy,
|
108
|
+
int npoints,
|
109
|
+
int nvars,
|
110
|
+
int& info,
|
111
|
+
linearmodel& lm,
|
112
|
+
lrreport& ar);
|
113
|
+
|
114
|
+
|
115
|
+
/*************************************************************************
|
116
|
+
Linear regression
|
117
|
+
|
118
|
+
Variant of LRBuild which uses vector of standatd deviations (errors in
|
119
|
+
function values).
|
120
|
+
|
121
|
+
INPUT PARAMETERS:
|
122
|
+
XY - training set, array [0..NPoints-1,0..NVars]:
|
123
|
+
* NVars columns - independent variables
|
124
|
+
* last column - dependent variable
|
125
|
+
S - standard deviations (errors in function values)
|
126
|
+
array[0..NPoints-1], S[i]>0.
|
127
|
+
NPoints - training set size, NPoints>NVars+1
|
128
|
+
NVars - number of independent variables
|
129
|
+
|
130
|
+
OUTPUT PARAMETERS:
|
131
|
+
Info - return code:
|
132
|
+
* -255, in case of unknown internal error
|
133
|
+
* -4, if internal SVD subroutine haven't converged
|
134
|
+
* -1, if incorrect parameters was passed (NPoints<NVars+2, NVars<1).
|
135
|
+
* -2, if S[I]<=0
|
136
|
+
* 1, if subroutine successfully finished
|
137
|
+
LM - linear model in the ALGLIB format. Use subroutines of
|
138
|
+
this unit to work with the model.
|
139
|
+
AR - additional results
|
140
|
+
|
141
|
+
|
142
|
+
-- ALGLIB --
|
143
|
+
Copyright 02.08.2008 by Bochkanov Sergey
|
144
|
+
*************************************************************************/
|
145
|
+
void lrbuilds(const ap::real_2d_array& xy,
|
146
|
+
const ap::real_1d_array& s,
|
147
|
+
int npoints,
|
148
|
+
int nvars,
|
149
|
+
int& info,
|
150
|
+
linearmodel& lm,
|
151
|
+
lrreport& ar);
|
152
|
+
|
153
|
+
|
154
|
+
/*************************************************************************
|
155
|
+
Like LRBuildS, but builds model
|
156
|
+
|
157
|
+
Y = A(0)*X[0] + ... + A(N-1)*X[N-1]
|
158
|
+
|
159
|
+
i.e. with zero constant term.
|
160
|
+
|
161
|
+
-- ALGLIB --
|
162
|
+
Copyright 30.10.2008 by Bochkanov Sergey
|
163
|
+
*************************************************************************/
|
164
|
+
void lrbuildzs(const ap::real_2d_array& xy,
|
165
|
+
const ap::real_1d_array& s,
|
166
|
+
int npoints,
|
167
|
+
int nvars,
|
168
|
+
int& info,
|
169
|
+
linearmodel& lm,
|
170
|
+
lrreport& ar);
|
171
|
+
|
172
|
+
|
173
|
+
/*************************************************************************
|
174
|
+
Like LRBuild but builds model
|
175
|
+
|
176
|
+
Y = A(0)*X[0] + ... + A(N-1)*X[N-1]
|
177
|
+
|
178
|
+
i.e. with zero constant term.
|
179
|
+
|
180
|
+
-- ALGLIB --
|
181
|
+
Copyright 30.10.2008 by Bochkanov Sergey
|
182
|
+
*************************************************************************/
|
183
|
+
void lrbuildz(const ap::real_2d_array& xy,
|
184
|
+
int npoints,
|
185
|
+
int nvars,
|
186
|
+
int& info,
|
187
|
+
linearmodel& lm,
|
188
|
+
lrreport& ar);
|
189
|
+
|
190
|
+
|
191
|
+
/*************************************************************************
|
192
|
+
Unpacks coefficients of linear model.
|
193
|
+
|
194
|
+
INPUT PARAMETERS:
|
195
|
+
LM - linear model in ALGLIB format
|
196
|
+
|
197
|
+
OUTPUT PARAMETERS:
|
198
|
+
V - coefficients, array[0..NVars]
|
199
|
+
NVars - number of independent variables (one less than number
|
200
|
+
of coefficients)
|
201
|
+
|
202
|
+
-- ALGLIB --
|
203
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
204
|
+
*************************************************************************/
|
205
|
+
void lrunpack(const linearmodel& lm, ap::real_1d_array& v, int& nvars);
|
206
|
+
|
207
|
+
|
208
|
+
/*************************************************************************
|
209
|
+
"Packs" coefficients and creates linear model in ALGLIB format (LRUnpack
|
210
|
+
reversed).
|
211
|
+
|
212
|
+
INPUT PARAMETERS:
|
213
|
+
V - coefficients, array[0..NVars]
|
214
|
+
NVars - number of independent variables
|
215
|
+
|
216
|
+
OUTPUT PAREMETERS:
|
217
|
+
LM - linear model.
|
218
|
+
|
219
|
+
-- ALGLIB --
|
220
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
221
|
+
*************************************************************************/
|
222
|
+
void lrpack(const ap::real_1d_array& v, int nvars, linearmodel& lm);
|
223
|
+
|
224
|
+
|
225
|
+
/*************************************************************************
|
226
|
+
Procesing
|
227
|
+
|
228
|
+
INPUT PARAMETERS:
|
229
|
+
LM - linear model
|
230
|
+
X - input vector, array[0..NVars-1].
|
231
|
+
|
232
|
+
Result:
|
233
|
+
value of linear model regression estimate
|
234
|
+
|
235
|
+
-- ALGLIB --
|
236
|
+
Copyright 03.09.2008 by Bochkanov Sergey
|
237
|
+
*************************************************************************/
|
238
|
+
double lrprocess(const linearmodel& lm, const ap::real_1d_array& x);
|
239
|
+
|
240
|
+
|
241
|
+
/*************************************************************************
|
242
|
+
RMS error on the test set
|
243
|
+
|
244
|
+
INPUT PARAMETERS:
|
245
|
+
LM - linear model
|
246
|
+
XY - test set
|
247
|
+
NPoints - test set size
|
248
|
+
|
249
|
+
RESULT:
|
250
|
+
root mean square error.
|
251
|
+
|
252
|
+
-- ALGLIB --
|
253
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
254
|
+
*************************************************************************/
|
255
|
+
double lrrmserror(const linearmodel& lm,
|
256
|
+
const ap::real_2d_array& xy,
|
257
|
+
int npoints);
|
258
|
+
|
259
|
+
|
260
|
+
/*************************************************************************
|
261
|
+
Average error on the test set
|
262
|
+
|
263
|
+
INPUT PARAMETERS:
|
264
|
+
LM - linear model
|
265
|
+
XY - test set
|
266
|
+
NPoints - test set size
|
267
|
+
|
268
|
+
RESULT:
|
269
|
+
average error.
|
270
|
+
|
271
|
+
-- ALGLIB --
|
272
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
273
|
+
*************************************************************************/
|
274
|
+
double lravgerror(const linearmodel& lm,
|
275
|
+
const ap::real_2d_array& xy,
|
276
|
+
int npoints);
|
277
|
+
|
278
|
+
|
279
|
+
/*************************************************************************
|
280
|
+
RMS error on the test set
|
281
|
+
|
282
|
+
INPUT PARAMETERS:
|
283
|
+
LM - linear model
|
284
|
+
XY - test set
|
285
|
+
NPoints - test set size
|
286
|
+
|
287
|
+
RESULT:
|
288
|
+
average relative error.
|
289
|
+
|
290
|
+
-- ALGLIB --
|
291
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
292
|
+
*************************************************************************/
|
293
|
+
double lravgrelerror(const linearmodel& lm,
|
294
|
+
const ap::real_2d_array& xy,
|
295
|
+
int npoints);
|
296
|
+
|
297
|
+
|
298
|
+
/*************************************************************************
|
299
|
+
Copying of LinearModel strucure
|
300
|
+
|
301
|
+
INPUT PARAMETERS:
|
302
|
+
LM1 - original
|
303
|
+
|
304
|
+
OUTPUT PARAMETERS:
|
305
|
+
LM2 - copy
|
306
|
+
|
307
|
+
-- ALGLIB --
|
308
|
+
Copyright 15.03.2009 by Bochkanov Sergey
|
309
|
+
*************************************************************************/
|
310
|
+
void lrcopy(const linearmodel& lm1, linearmodel& lm2);
|
311
|
+
|
312
|
+
|
313
|
+
/*************************************************************************
|
314
|
+
Serialization of LinearModel strucure
|
315
|
+
|
316
|
+
INPUT PARAMETERS:
|
317
|
+
LM - original
|
318
|
+
|
319
|
+
OUTPUT PARAMETERS:
|
320
|
+
RA - array of real numbers which stores model,
|
321
|
+
array[0..RLen-1]
|
322
|
+
RLen - RA lenght
|
323
|
+
|
324
|
+
-- ALGLIB --
|
325
|
+
Copyright 15.03.2009 by Bochkanov Sergey
|
326
|
+
*************************************************************************/
|
327
|
+
void lrserialize(const linearmodel& lm, ap::real_1d_array& ra, int& rlen);
|
328
|
+
|
329
|
+
|
330
|
+
/*************************************************************************
|
331
|
+
Unserialization of DecisionForest strucure
|
332
|
+
|
333
|
+
INPUT PARAMETERS:
|
334
|
+
RA - real array which stores decision forest
|
335
|
+
|
336
|
+
OUTPUT PARAMETERS:
|
337
|
+
LM - unserialized structure
|
338
|
+
|
339
|
+
-- ALGLIB --
|
340
|
+
Copyright 15.03.2009 by Bochkanov Sergey
|
341
|
+
*************************************************************************/
|
342
|
+
void lrunserialize(const ap::real_1d_array& ra, linearmodel& lm);
|
343
|
+
|
344
|
+
|
345
|
+
/*************************************************************************
|
346
|
+
Obsolete subroutine, use LRBuildS
|
347
|
+
|
348
|
+
-- ALGLIB --
|
349
|
+
Copyright 26.04.2008 by Bochkanov Sergey
|
350
|
+
|
351
|
+
References:
|
352
|
+
1. Numerical Recipes in C, "15.2 Fitting Data to a Straight Line"
|
353
|
+
*************************************************************************/
|
354
|
+
void lrlines(const ap::real_2d_array& xy,
|
355
|
+
const ap::real_1d_array& s,
|
356
|
+
int n,
|
357
|
+
int& info,
|
358
|
+
double& a,
|
359
|
+
double& b,
|
360
|
+
double& vara,
|
361
|
+
double& varb,
|
362
|
+
double& covab,
|
363
|
+
double& corrab,
|
364
|
+
double& p);
|
365
|
+
|
366
|
+
|
367
|
+
/*************************************************************************
|
368
|
+
Obsolete subroutine, use LRBuild
|
369
|
+
|
370
|
+
-- ALGLIB --
|
371
|
+
Copyright 02.08.2008 by Bochkanov Sergey
|
372
|
+
*************************************************************************/
|
373
|
+
void lrline(const ap::real_2d_array& xy,
|
374
|
+
int n,
|
375
|
+
int& info,
|
376
|
+
double& a,
|
377
|
+
double& b);
|
378
|
+
|
379
|
+
|
380
|
+
#endif
|
@@ -0,0 +1,1523 @@
|
|
1
|
+
/*************************************************************************
|
2
|
+
Copyright (c) 2008, Sergey Bochkanov (ALGLIB project).
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are
|
6
|
+
met:
|
7
|
+
|
8
|
+
- Redistributions of source code must retain the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
- Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer listed
|
13
|
+
in this license in the documentation and/or other materials
|
14
|
+
provided with the distribution.
|
15
|
+
|
16
|
+
- Neither the name of the copyright holders nor the names of its
|
17
|
+
contributors may be used to endorse or promote products derived from
|
18
|
+
this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*************************************************************************/
|
32
|
+
|
33
|
+
#include <stdafx.h>
|
34
|
+
#include "logit.h"
|
35
|
+
|
36
|
+
static const double xtol = 100*ap::machineepsilon;
|
37
|
+
static const double ftol = 0.0001;
|
38
|
+
static const double gtol = 0.3;
|
39
|
+
static const int maxfev = 20;
|
40
|
+
static const double stpmin = 1.0E-2;
|
41
|
+
static const double stpmax = 1.0E5;
|
42
|
+
static const int logitvnum = 6;
|
43
|
+
|
44
|
+
static void mnliexp(ap::real_1d_array& w, const ap::real_1d_array& x);
|
45
|
+
static void mnlallerrors(logitmodel& lm,
|
46
|
+
const ap::real_2d_array& xy,
|
47
|
+
int npoints,
|
48
|
+
double& relcls,
|
49
|
+
double& avgce,
|
50
|
+
double& rms,
|
51
|
+
double& avg,
|
52
|
+
double& avgrel);
|
53
|
+
static void mnlmcsrch(const int& n,
|
54
|
+
ap::real_1d_array& x,
|
55
|
+
double& f,
|
56
|
+
ap::real_1d_array& g,
|
57
|
+
const ap::real_1d_array& s,
|
58
|
+
double& stp,
|
59
|
+
int& info,
|
60
|
+
int& nfev,
|
61
|
+
ap::real_1d_array& wa,
|
62
|
+
logitmcstate& state,
|
63
|
+
int& stage);
|
64
|
+
static void mnlmcstep(double& stx,
|
65
|
+
double& fx,
|
66
|
+
double& dx,
|
67
|
+
double& sty,
|
68
|
+
double& fy,
|
69
|
+
double& dy,
|
70
|
+
double& stp,
|
71
|
+
const double& fp,
|
72
|
+
const double& dp,
|
73
|
+
bool& brackt,
|
74
|
+
const double& stmin,
|
75
|
+
const double& stmax,
|
76
|
+
int& info);
|
77
|
+
|
78
|
+
/*************************************************************************
|
79
|
+
This subroutine trains logit model.
|
80
|
+
|
81
|
+
INPUT PARAMETERS:
|
82
|
+
XY - training set, array[0..NPoints-1,0..NVars]
|
83
|
+
First NVars columns store values of independent
|
84
|
+
variables, next column stores number of class (from 0
|
85
|
+
to NClasses-1) which dataset element belongs to. Fractional
|
86
|
+
values are rounded to nearest integer.
|
87
|
+
NPoints - training set size, NPoints>=1
|
88
|
+
NVars - number of independent variables, NVars>=1
|
89
|
+
NClasses - number of classes, NClasses>=2
|
90
|
+
|
91
|
+
OUTPUT PARAMETERS:
|
92
|
+
Info - return code:
|
93
|
+
* -2, if there is a point with class number
|
94
|
+
outside of [0..NClasses-1].
|
95
|
+
* -1, if incorrect parameters was passed
|
96
|
+
(NPoints<NVars+2, NVars<1, NClasses<2).
|
97
|
+
* 1, if task has been solved
|
98
|
+
LM - model built
|
99
|
+
Rep - training report
|
100
|
+
|
101
|
+
-- ALGLIB --
|
102
|
+
Copyright 10.09.2008 by Bochkanov Sergey
|
103
|
+
*************************************************************************/
|
104
|
+
void mnltrainh(const ap::real_2d_array& xy,
|
105
|
+
int npoints,
|
106
|
+
int nvars,
|
107
|
+
int nclasses,
|
108
|
+
int& info,
|
109
|
+
logitmodel& lm,
|
110
|
+
mnlreport& rep)
|
111
|
+
{
|
112
|
+
int i;
|
113
|
+
int j;
|
114
|
+
int k;
|
115
|
+
int m;
|
116
|
+
int n;
|
117
|
+
int ssize;
|
118
|
+
bool allsame;
|
119
|
+
int offs;
|
120
|
+
double threshold;
|
121
|
+
double wminstep;
|
122
|
+
double decay;
|
123
|
+
int wdim;
|
124
|
+
int expoffs;
|
125
|
+
double v;
|
126
|
+
double s;
|
127
|
+
multilayerperceptron network;
|
128
|
+
int nin;
|
129
|
+
int nout;
|
130
|
+
int wcount;
|
131
|
+
double e;
|
132
|
+
ap::real_1d_array g;
|
133
|
+
ap::real_2d_array h;
|
134
|
+
bool spd;
|
135
|
+
int cvcnt;
|
136
|
+
ap::real_1d_array x;
|
137
|
+
ap::real_1d_array y;
|
138
|
+
ap::real_1d_array wbase;
|
139
|
+
double wstep;
|
140
|
+
ap::real_1d_array wdir;
|
141
|
+
ap::real_1d_array work;
|
142
|
+
int mcstage;
|
143
|
+
logitmcstate mcstate;
|
144
|
+
int mcinfo;
|
145
|
+
int mcnfev;
|
146
|
+
|
147
|
+
threshold = 1000*ap::machineepsilon;
|
148
|
+
wminstep = 0.001;
|
149
|
+
decay = 0.001;
|
150
|
+
|
151
|
+
//
|
152
|
+
// Test for inputs
|
153
|
+
//
|
154
|
+
if( npoints<nvars+2||nvars<1||nclasses<2 )
|
155
|
+
{
|
156
|
+
info = -1;
|
157
|
+
return;
|
158
|
+
}
|
159
|
+
for(i = 0; i <= npoints-1; i++)
|
160
|
+
{
|
161
|
+
if( ap::round(xy(i,nvars))<0||ap::round(xy(i,nvars))>=nclasses )
|
162
|
+
{
|
163
|
+
info = -2;
|
164
|
+
return;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
info = 1;
|
168
|
+
|
169
|
+
//
|
170
|
+
// Initialize data
|
171
|
+
//
|
172
|
+
rep.ngrad = 0;
|
173
|
+
rep.nhess = 0;
|
174
|
+
|
175
|
+
//
|
176
|
+
// Allocate array
|
177
|
+
//
|
178
|
+
wdim = (nvars+1)*(nclasses-1);
|
179
|
+
offs = 5;
|
180
|
+
expoffs = offs+wdim;
|
181
|
+
ssize = 5+(nvars+1)*(nclasses-1)+nclasses;
|
182
|
+
lm.w.setbounds(0, ssize-1);
|
183
|
+
lm.w(0) = ssize;
|
184
|
+
lm.w(1) = logitvnum;
|
185
|
+
lm.w(2) = nvars;
|
186
|
+
lm.w(3) = nclasses;
|
187
|
+
lm.w(4) = offs;
|
188
|
+
|
189
|
+
//
|
190
|
+
// Degenerate case: all outputs are equal
|
191
|
+
//
|
192
|
+
allsame = true;
|
193
|
+
for(i = 1; i <= npoints-1; i++)
|
194
|
+
{
|
195
|
+
if( ap::round(xy(i,nvars))!=ap::round(xy(i-1,nvars)) )
|
196
|
+
{
|
197
|
+
allsame = false;
|
198
|
+
}
|
199
|
+
}
|
200
|
+
if( allsame )
|
201
|
+
{
|
202
|
+
for(i = 0; i <= (nvars+1)*(nclasses-1)-1; i++)
|
203
|
+
{
|
204
|
+
lm.w(offs+i) = 0;
|
205
|
+
}
|
206
|
+
v = -2*log(ap::minrealnumber);
|
207
|
+
k = ap::round(xy(0,nvars));
|
208
|
+
if( k==nclasses-1 )
|
209
|
+
{
|
210
|
+
for(i = 0; i <= nclasses-2; i++)
|
211
|
+
{
|
212
|
+
lm.w(offs+i*(nvars+1)+nvars) = -v;
|
213
|
+
}
|
214
|
+
}
|
215
|
+
else
|
216
|
+
{
|
217
|
+
for(i = 0; i <= nclasses-2; i++)
|
218
|
+
{
|
219
|
+
if( i==k )
|
220
|
+
{
|
221
|
+
lm.w(offs+i*(nvars+1)+nvars) = +v;
|
222
|
+
}
|
223
|
+
else
|
224
|
+
{
|
225
|
+
lm.w(offs+i*(nvars+1)+nvars) = 0;
|
226
|
+
}
|
227
|
+
}
|
228
|
+
}
|
229
|
+
return;
|
230
|
+
}
|
231
|
+
|
232
|
+
//
|
233
|
+
// General case.
|
234
|
+
// Prepare task and network. Allocate space.
|
235
|
+
//
|
236
|
+
mlpcreatec0(nvars, nclasses, network);
|
237
|
+
mlpinitpreprocessor(network, xy, npoints);
|
238
|
+
mlpproperties(network, nin, nout, wcount);
|
239
|
+
for(i = 0; i <= wcount-1; i++)
|
240
|
+
{
|
241
|
+
network.weights(i) = (2*ap::randomreal()-1)/nvars;
|
242
|
+
}
|
243
|
+
g.setbounds(0, wcount-1);
|
244
|
+
h.setbounds(0, wcount-1, 0, wcount-1);
|
245
|
+
wbase.setbounds(0, wcount-1);
|
246
|
+
wdir.setbounds(0, wcount-1);
|
247
|
+
work.setbounds(0, wcount-1);
|
248
|
+
|
249
|
+
//
|
250
|
+
// First stage: optimize in gradient direction.
|
251
|
+
//
|
252
|
+
for(k = 0; k <= wcount/3+10; k++)
|
253
|
+
{
|
254
|
+
|
255
|
+
//
|
256
|
+
// Calculate gradient in starting point
|
257
|
+
//
|
258
|
+
mlpgradnbatch(network, xy, npoints, e, g);
|
259
|
+
v = ap::vdotproduct(&network.weights(0), &network.weights(0), ap::vlen(0,wcount-1));
|
260
|
+
e = e+0.5*decay*v;
|
261
|
+
ap::vadd(&g(0), &network.weights(0), ap::vlen(0,wcount-1), decay);
|
262
|
+
rep.ngrad = rep.ngrad+1;
|
263
|
+
|
264
|
+
//
|
265
|
+
// Setup optimization scheme
|
266
|
+
//
|
267
|
+
ap::vmoveneg(&wdir(0), &g(0), ap::vlen(0,wcount-1));
|
268
|
+
v = ap::vdotproduct(&wdir(0), &wdir(0), ap::vlen(0,wcount-1));
|
269
|
+
wstep = sqrt(v);
|
270
|
+
v = 1/sqrt(v);
|
271
|
+
ap::vmul(&wdir(0), ap::vlen(0,wcount-1), v);
|
272
|
+
mcstage = 0;
|
273
|
+
mnlmcsrch(wcount, network.weights, e, g, wdir, wstep, mcinfo, mcnfev, work, mcstate, mcstage);
|
274
|
+
while(mcstage!=0)
|
275
|
+
{
|
276
|
+
mlpgradnbatch(network, xy, npoints, e, g);
|
277
|
+
v = ap::vdotproduct(&network.weights(0), &network.weights(0), ap::vlen(0,wcount-1));
|
278
|
+
e = e+0.5*decay*v;
|
279
|
+
ap::vadd(&g(0), &network.weights(0), ap::vlen(0,wcount-1), decay);
|
280
|
+
rep.ngrad = rep.ngrad+1;
|
281
|
+
mnlmcsrch(wcount, network.weights, e, g, wdir, wstep, mcinfo, mcnfev, work, mcstate, mcstage);
|
282
|
+
}
|
283
|
+
}
|
284
|
+
|
285
|
+
//
|
286
|
+
// Second stage: use Hessian when we are close to the minimum
|
287
|
+
//
|
288
|
+
while(true)
|
289
|
+
{
|
290
|
+
|
291
|
+
//
|
292
|
+
// Calculate and update E/G/H
|
293
|
+
//
|
294
|
+
mlphessiannbatch(network, xy, npoints, e, g, h);
|
295
|
+
v = ap::vdotproduct(&network.weights(0), &network.weights(0), ap::vlen(0,wcount-1));
|
296
|
+
e = e+0.5*decay*v;
|
297
|
+
ap::vadd(&g(0), &network.weights(0), ap::vlen(0,wcount-1), decay);
|
298
|
+
for(k = 0; k <= wcount-1; k++)
|
299
|
+
{
|
300
|
+
h(k,k) = h(k,k)+decay;
|
301
|
+
}
|
302
|
+
rep.nhess = rep.nhess+1;
|
303
|
+
|
304
|
+
//
|
305
|
+
// Select step direction
|
306
|
+
// NOTE: it is important to use lower-triangle Cholesky
|
307
|
+
// factorization since it is much faster than higher-triangle version.
|
308
|
+
//
|
309
|
+
spd = spdmatrixcholesky(h, wcount, false);
|
310
|
+
if( spd )
|
311
|
+
{
|
312
|
+
spd = spdmatrixcholeskysolve(h, g, wcount, false, wdir);
|
313
|
+
}
|
314
|
+
if( spd )
|
315
|
+
{
|
316
|
+
|
317
|
+
//
|
318
|
+
// H is positive definite.
|
319
|
+
// Step in Newton direction.
|
320
|
+
//
|
321
|
+
ap::vmul(&wdir(0), ap::vlen(0,wcount-1), -1);
|
322
|
+
spd = true;
|
323
|
+
}
|
324
|
+
else
|
325
|
+
{
|
326
|
+
|
327
|
+
//
|
328
|
+
// H is indefinite.
|
329
|
+
// Step in gradient direction.
|
330
|
+
//
|
331
|
+
ap::vmoveneg(&wdir(0), &g(0), ap::vlen(0,wcount-1));
|
332
|
+
spd = false;
|
333
|
+
}
|
334
|
+
|
335
|
+
//
|
336
|
+
// Optimize in WDir direction
|
337
|
+
//
|
338
|
+
v = ap::vdotproduct(&wdir(0), &wdir(0), ap::vlen(0,wcount-1));
|
339
|
+
wstep = sqrt(v);
|
340
|
+
v = 1/sqrt(v);
|
341
|
+
ap::vmul(&wdir(0), ap::vlen(0,wcount-1), v);
|
342
|
+
mcstage = 0;
|
343
|
+
mnlmcsrch(wcount, network.weights, e, g, wdir, wstep, mcinfo, mcnfev, work, mcstate, mcstage);
|
344
|
+
while(mcstage!=0)
|
345
|
+
{
|
346
|
+
mlpgradnbatch(network, xy, npoints, e, g);
|
347
|
+
v = ap::vdotproduct(&network.weights(0), &network.weights(0), ap::vlen(0,wcount-1));
|
348
|
+
e = e+0.5*decay*v;
|
349
|
+
ap::vadd(&g(0), &network.weights(0), ap::vlen(0,wcount-1), decay);
|
350
|
+
rep.ngrad = rep.ngrad+1;
|
351
|
+
mnlmcsrch(wcount, network.weights, e, g, wdir, wstep, mcinfo, mcnfev, work, mcstate, mcstage);
|
352
|
+
}
|
353
|
+
if( spd&&(mcinfo==2||mcinfo==4||mcinfo==6) )
|
354
|
+
{
|
355
|
+
break;
|
356
|
+
}
|
357
|
+
}
|
358
|
+
|
359
|
+
//
|
360
|
+
// Convert from NN format to MNL format
|
361
|
+
//
|
362
|
+
ap::vmove(&lm.w(offs), &network.weights(0), ap::vlen(offs,offs+wcount-1));
|
363
|
+
for(k = 0; k <= nvars-1; k++)
|
364
|
+
{
|
365
|
+
for(i = 0; i <= nclasses-2; i++)
|
366
|
+
{
|
367
|
+
s = network.columnsigmas(k);
|
368
|
+
if( s==0 )
|
369
|
+
{
|
370
|
+
s = 1;
|
371
|
+
}
|
372
|
+
j = offs+(nvars+1)*i;
|
373
|
+
v = lm.w(j+k);
|
374
|
+
lm.w(j+k) = v/s;
|
375
|
+
lm.w(j+nvars) = lm.w(j+nvars)+v*network.columnmeans(k)/s;
|
376
|
+
}
|
377
|
+
}
|
378
|
+
for(k = 0; k <= nclasses-2; k++)
|
379
|
+
{
|
380
|
+
lm.w(offs+(nvars+1)*k+nvars) = -lm.w(offs+(nvars+1)*k+nvars);
|
381
|
+
}
|
382
|
+
}
|
383
|
+
|
384
|
+
|
385
|
+
/*************************************************************************
|
386
|
+
Procesing
|
387
|
+
|
388
|
+
INPUT PARAMETERS:
|
389
|
+
LM - logit model, passed by non-constant reference
|
390
|
+
(some fields of structure are used as temporaries
|
391
|
+
when calculating model output).
|
392
|
+
X - input vector, array[0..NVars-1].
|
393
|
+
|
394
|
+
OUTPUT PARAMETERS:
|
395
|
+
Y - result, array[0..NClasses-1]
|
396
|
+
Vector of posterior probabilities for classification task.
|
397
|
+
Subroutine does not allocate memory for this vector, it is
|
398
|
+
responsibility of a caller to allocate it. Array must be
|
399
|
+
at least [0..NClasses-1].
|
400
|
+
|
401
|
+
-- ALGLIB --
|
402
|
+
Copyright 10.09.2008 by Bochkanov Sergey
|
403
|
+
*************************************************************************/
|
404
|
+
void mnlprocess(logitmodel& lm,
|
405
|
+
const ap::real_1d_array& x,
|
406
|
+
ap::real_1d_array& y)
|
407
|
+
{
|
408
|
+
int nvars;
|
409
|
+
int nclasses;
|
410
|
+
int offs;
|
411
|
+
int i;
|
412
|
+
int i1;
|
413
|
+
double v;
|
414
|
+
double mx;
|
415
|
+
double s;
|
416
|
+
|
417
|
+
ap::ap_error::make_assertion(lm.w(1)==logitvnum, "MNLProcess: unexpected model version");
|
418
|
+
nvars = ap::round(lm.w(2));
|
419
|
+
nclasses = ap::round(lm.w(3));
|
420
|
+
offs = ap::round(lm.w(4));
|
421
|
+
mnliexp(lm.w, x);
|
422
|
+
s = 0;
|
423
|
+
i1 = offs+(nvars+1)*(nclasses-1);
|
424
|
+
for(i = i1; i <= i1+nclasses-1; i++)
|
425
|
+
{
|
426
|
+
s = s+lm.w(i);
|
427
|
+
}
|
428
|
+
for(i = 0; i <= nclasses-1; i++)
|
429
|
+
{
|
430
|
+
y(i) = lm.w(i1+i)/s;
|
431
|
+
}
|
432
|
+
}
|
433
|
+
|
434
|
+
|
435
|
+
/*************************************************************************
|
436
|
+
Unpacks coefficients of logit model. Logit model have form:
|
437
|
+
|
438
|
+
P(class=i) = S(i) / (S(0) + S(1) + ... +S(M-1))
|
439
|
+
S(i) = Exp(A[i,0]*X[0] + ... + A[i,N-1]*X[N-1] + A[i,N]), when i<M-1
|
440
|
+
S(M-1) = 1
|
441
|
+
|
442
|
+
INPUT PARAMETERS:
|
443
|
+
LM - logit model in ALGLIB format
|
444
|
+
|
445
|
+
OUTPUT PARAMETERS:
|
446
|
+
V - coefficients, array[0..NClasses-2,0..NVars]
|
447
|
+
NVars - number of independent variables
|
448
|
+
NClasses - number of classes
|
449
|
+
|
450
|
+
-- ALGLIB --
|
451
|
+
Copyright 10.09.2008 by Bochkanov Sergey
|
452
|
+
*************************************************************************/
|
453
|
+
void mnlunpack(const logitmodel& lm,
|
454
|
+
ap::real_2d_array& a,
|
455
|
+
int& nvars,
|
456
|
+
int& nclasses)
|
457
|
+
{
|
458
|
+
int offs;
|
459
|
+
int i;
|
460
|
+
|
461
|
+
ap::ap_error::make_assertion(lm.w(1)==logitvnum, "MNLUnpack: unexpected model version");
|
462
|
+
nvars = ap::round(lm.w(2));
|
463
|
+
nclasses = ap::round(lm.w(3));
|
464
|
+
offs = ap::round(lm.w(4));
|
465
|
+
a.setbounds(0, nclasses-2, 0, nvars);
|
466
|
+
for(i = 0; i <= nclasses-2; i++)
|
467
|
+
{
|
468
|
+
ap::vmove(&a(i, 0), &lm.w(offs+i*(nvars+1)), ap::vlen(0,nvars));
|
469
|
+
}
|
470
|
+
}
|
471
|
+
|
472
|
+
|
473
|
+
/*************************************************************************
|
474
|
+
"Packs" coefficients and creates logit model in ALGLIB format (MNLUnpack
|
475
|
+
reversed).
|
476
|
+
|
477
|
+
INPUT PARAMETERS:
|
478
|
+
A - model (see MNLUnpack)
|
479
|
+
NVars - number of independent variables
|
480
|
+
NClasses - number of classes
|
481
|
+
|
482
|
+
OUTPUT PARAMETERS:
|
483
|
+
LM - logit model.
|
484
|
+
|
485
|
+
-- ALGLIB --
|
486
|
+
Copyright 10.09.2008 by Bochkanov Sergey
|
487
|
+
*************************************************************************/
|
488
|
+
void mnlpack(const ap::real_2d_array& a,
|
489
|
+
int nvars,
|
490
|
+
int nclasses,
|
491
|
+
logitmodel& lm)
|
492
|
+
{
|
493
|
+
int offs;
|
494
|
+
int i;
|
495
|
+
int wdim;
|
496
|
+
int ssize;
|
497
|
+
|
498
|
+
wdim = (nvars+1)*(nclasses-1);
|
499
|
+
offs = 5;
|
500
|
+
ssize = 5+(nvars+1)*(nclasses-1)+nclasses;
|
501
|
+
lm.w.setbounds(0, ssize-1);
|
502
|
+
lm.w(0) = ssize;
|
503
|
+
lm.w(1) = logitvnum;
|
504
|
+
lm.w(2) = nvars;
|
505
|
+
lm.w(3) = nclasses;
|
506
|
+
lm.w(4) = offs;
|
507
|
+
for(i = 0; i <= nclasses-2; i++)
|
508
|
+
{
|
509
|
+
ap::vmove(&lm.w(offs+i*(nvars+1)), &a(i, 0), ap::vlen(offs+i*(nvars+1),offs+i*(nvars+1)+nvars));
|
510
|
+
}
|
511
|
+
}
|
512
|
+
|
513
|
+
|
514
|
+
/*************************************************************************
|
515
|
+
Copying of LogitModel strucure
|
516
|
+
|
517
|
+
INPUT PARAMETERS:
|
518
|
+
LM1 - original
|
519
|
+
|
520
|
+
OUTPUT PARAMETERS:
|
521
|
+
LM2 - copy
|
522
|
+
|
523
|
+
-- ALGLIB --
|
524
|
+
Copyright 15.03.2009 by Bochkanov Sergey
|
525
|
+
*************************************************************************/
|
526
|
+
void mnlcopy(const logitmodel& lm1, logitmodel& lm2)
|
527
|
+
{
|
528
|
+
int k;
|
529
|
+
|
530
|
+
k = ap::round(lm1.w(0));
|
531
|
+
lm2.w.setbounds(0, k-1);
|
532
|
+
ap::vmove(&lm2.w(0), &lm1.w(0), ap::vlen(0,k-1));
|
533
|
+
}
|
534
|
+
|
535
|
+
|
536
|
+
/*************************************************************************
|
537
|
+
Serialization of LogitModel strucure
|
538
|
+
|
539
|
+
INPUT PARAMETERS:
|
540
|
+
LM - original
|
541
|
+
|
542
|
+
OUTPUT PARAMETERS:
|
543
|
+
RA - array of real numbers which stores model,
|
544
|
+
array[0..RLen-1]
|
545
|
+
RLen - RA lenght
|
546
|
+
|
547
|
+
-- ALGLIB --
|
548
|
+
Copyright 15.03.2009 by Bochkanov Sergey
|
549
|
+
*************************************************************************/
|
550
|
+
void mnlserialize(const logitmodel& lm, ap::real_1d_array& ra, int& rlen)
|
551
|
+
{
|
552
|
+
|
553
|
+
rlen = ap::round(lm.w(0))+1;
|
554
|
+
ra.setbounds(0, rlen-1);
|
555
|
+
ra(0) = logitvnum;
|
556
|
+
ap::vmove(&ra(1), &lm.w(0), ap::vlen(1,rlen-1));
|
557
|
+
}
|
558
|
+
|
559
|
+
|
560
|
+
/*************************************************************************
|
561
|
+
Unserialization of LogitModel strucure
|
562
|
+
|
563
|
+
INPUT PARAMETERS:
|
564
|
+
RA - real array which stores model
|
565
|
+
|
566
|
+
OUTPUT PARAMETERS:
|
567
|
+
LM - restored model
|
568
|
+
|
569
|
+
-- ALGLIB --
|
570
|
+
Copyright 15.03.2009 by Bochkanov Sergey
|
571
|
+
*************************************************************************/
|
572
|
+
void mnlunserialize(const ap::real_1d_array& ra, logitmodel& lm)
|
573
|
+
{
|
574
|
+
|
575
|
+
ap::ap_error::make_assertion(ap::round(ra(0))==logitvnum, "MNLUnserialize: incorrect array!");
|
576
|
+
lm.w.setbounds(0, ap::round(ra(1))-1);
|
577
|
+
ap::vmove(&lm.w(0), &ra(1), ap::vlen(0,ap::round(ra(1))-1));
|
578
|
+
}
|
579
|
+
|
580
|
+
|
581
|
+
/*************************************************************************
|
582
|
+
Average cross-entropy (in bits per element) on the test set
|
583
|
+
|
584
|
+
INPUT PARAMETERS:
|
585
|
+
LM - logit model
|
586
|
+
XY - test set
|
587
|
+
NPoints - test set size
|
588
|
+
|
589
|
+
RESULT:
|
590
|
+
CrossEntropy/(NPoints*ln(2)).
|
591
|
+
|
592
|
+
-- ALGLIB --
|
593
|
+
Copyright 10.09.2008 by Bochkanov Sergey
|
594
|
+
*************************************************************************/
|
595
|
+
double mnlavgce(logitmodel& lm, const ap::real_2d_array& xy, int npoints)
|
596
|
+
{
|
597
|
+
double result;
|
598
|
+
int nvars;
|
599
|
+
int nclasses;
|
600
|
+
int i;
|
601
|
+
int j;
|
602
|
+
ap::real_1d_array workx;
|
603
|
+
ap::real_1d_array worky;
|
604
|
+
int nmax;
|
605
|
+
|
606
|
+
ap::ap_error::make_assertion(lm.w(1)==logitvnum, "MNLClsError: unexpected model version");
|
607
|
+
nvars = ap::round(lm.w(2));
|
608
|
+
nclasses = ap::round(lm.w(3));
|
609
|
+
workx.setbounds(0, nvars-1);
|
610
|
+
worky.setbounds(0, nclasses-1);
|
611
|
+
result = 0;
|
612
|
+
for(i = 0; i <= npoints-1; i++)
|
613
|
+
{
|
614
|
+
ap::ap_error::make_assertion(ap::round(xy(i,nvars))>=0&&ap::round(xy(i,nvars))<nclasses, "MNLAvgCE: incorrect class number!");
|
615
|
+
|
616
|
+
//
|
617
|
+
// Process
|
618
|
+
//
|
619
|
+
ap::vmove(&workx(0), &xy(i, 0), ap::vlen(0,nvars-1));
|
620
|
+
mnlprocess(lm, workx, worky);
|
621
|
+
if( worky(ap::round(xy(i,nvars)))>0 )
|
622
|
+
{
|
623
|
+
result = result-log(worky(ap::round(xy(i,nvars))));
|
624
|
+
}
|
625
|
+
else
|
626
|
+
{
|
627
|
+
result = result-log(ap::minrealnumber);
|
628
|
+
}
|
629
|
+
}
|
630
|
+
result = result/(npoints*log(double(2)));
|
631
|
+
return result;
|
632
|
+
}
|
633
|
+
|
634
|
+
|
635
|
+
/*************************************************************************
|
636
|
+
Relative classification error on the test set
|
637
|
+
|
638
|
+
INPUT PARAMETERS:
|
639
|
+
LM - logit model
|
640
|
+
XY - test set
|
641
|
+
NPoints - test set size
|
642
|
+
|
643
|
+
RESULT:
|
644
|
+
percent of incorrectly classified cases.
|
645
|
+
|
646
|
+
-- ALGLIB --
|
647
|
+
Copyright 10.09.2008 by Bochkanov Sergey
|
648
|
+
*************************************************************************/
|
649
|
+
double mnlrelclserror(logitmodel& lm,
|
650
|
+
const ap::real_2d_array& xy,
|
651
|
+
int npoints)
|
652
|
+
{
|
653
|
+
double result;
|
654
|
+
|
655
|
+
result = double(mnlclserror(lm, xy, npoints))/double(npoints);
|
656
|
+
return result;
|
657
|
+
}
|
658
|
+
|
659
|
+
|
660
|
+
/*************************************************************************
|
661
|
+
RMS error on the test set
|
662
|
+
|
663
|
+
INPUT PARAMETERS:
|
664
|
+
LM - logit model
|
665
|
+
XY - test set
|
666
|
+
NPoints - test set size
|
667
|
+
|
668
|
+
RESULT:
|
669
|
+
root mean square error (error when estimating posterior probabilities).
|
670
|
+
|
671
|
+
-- ALGLIB --
|
672
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
673
|
+
*************************************************************************/
|
674
|
+
double mnlrmserror(logitmodel& lm, const ap::real_2d_array& xy, int npoints)
|
675
|
+
{
|
676
|
+
double result;
|
677
|
+
double relcls;
|
678
|
+
double avgce;
|
679
|
+
double rms;
|
680
|
+
double avg;
|
681
|
+
double avgrel;
|
682
|
+
|
683
|
+
ap::ap_error::make_assertion(ap::round(lm.w(1))==logitvnum, "MNLRMSError: Incorrect MNL version!");
|
684
|
+
mnlallerrors(lm, xy, npoints, relcls, avgce, rms, avg, avgrel);
|
685
|
+
result = rms;
|
686
|
+
return result;
|
687
|
+
}
|
688
|
+
|
689
|
+
|
690
|
+
/*************************************************************************
|
691
|
+
Average error on the test set
|
692
|
+
|
693
|
+
INPUT PARAMETERS:
|
694
|
+
LM - logit model
|
695
|
+
XY - test set
|
696
|
+
NPoints - test set size
|
697
|
+
|
698
|
+
RESULT:
|
699
|
+
average error (error when estimating posterior probabilities).
|
700
|
+
|
701
|
+
-- ALGLIB --
|
702
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
703
|
+
*************************************************************************/
|
704
|
+
double mnlavgerror(logitmodel& lm, const ap::real_2d_array& xy, int npoints)
|
705
|
+
{
|
706
|
+
double result;
|
707
|
+
double relcls;
|
708
|
+
double avgce;
|
709
|
+
double rms;
|
710
|
+
double avg;
|
711
|
+
double avgrel;
|
712
|
+
|
713
|
+
ap::ap_error::make_assertion(ap::round(lm.w(1))==logitvnum, "MNLRMSError: Incorrect MNL version!");
|
714
|
+
mnlallerrors(lm, xy, npoints, relcls, avgce, rms, avg, avgrel);
|
715
|
+
result = avg;
|
716
|
+
return result;
|
717
|
+
}
|
718
|
+
|
719
|
+
|
720
|
+
/*************************************************************************
|
721
|
+
Average relative error on the test set
|
722
|
+
|
723
|
+
INPUT PARAMETERS:
|
724
|
+
LM - logit model
|
725
|
+
XY - test set
|
726
|
+
NPoints - test set size
|
727
|
+
|
728
|
+
RESULT:
|
729
|
+
average relative error (error when estimating posterior probabilities).
|
730
|
+
|
731
|
+
-- ALGLIB --
|
732
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
733
|
+
*************************************************************************/
|
734
|
+
double mnlavgrelerror(logitmodel& lm, const ap::real_2d_array& xy, int ssize)
|
735
|
+
{
|
736
|
+
double result;
|
737
|
+
double relcls;
|
738
|
+
double avgce;
|
739
|
+
double rms;
|
740
|
+
double avg;
|
741
|
+
double avgrel;
|
742
|
+
|
743
|
+
ap::ap_error::make_assertion(ap::round(lm.w(1))==logitvnum, "MNLRMSError: Incorrect MNL version!");
|
744
|
+
mnlallerrors(lm, xy, ssize, relcls, avgce, rms, avg, avgrel);
|
745
|
+
result = avgrel;
|
746
|
+
return result;
|
747
|
+
}
|
748
|
+
|
749
|
+
|
750
|
+
/*************************************************************************
|
751
|
+
Classification error on test set = MNLRelClsError*NPoints
|
752
|
+
|
753
|
+
-- ALGLIB --
|
754
|
+
Copyright 10.09.2008 by Bochkanov Sergey
|
755
|
+
*************************************************************************/
|
756
|
+
int mnlclserror(logitmodel& lm, const ap::real_2d_array& xy, int npoints)
|
757
|
+
{
|
758
|
+
int result;
|
759
|
+
int nvars;
|
760
|
+
int nclasses;
|
761
|
+
int i;
|
762
|
+
int j;
|
763
|
+
ap::real_1d_array workx;
|
764
|
+
ap::real_1d_array worky;
|
765
|
+
int nmax;
|
766
|
+
|
767
|
+
ap::ap_error::make_assertion(lm.w(1)==logitvnum, "MNLClsError: unexpected model version");
|
768
|
+
nvars = ap::round(lm.w(2));
|
769
|
+
nclasses = ap::round(lm.w(3));
|
770
|
+
workx.setbounds(0, nvars-1);
|
771
|
+
worky.setbounds(0, nclasses-1);
|
772
|
+
result = 0;
|
773
|
+
for(i = 0; i <= npoints-1; i++)
|
774
|
+
{
|
775
|
+
|
776
|
+
//
|
777
|
+
// Process
|
778
|
+
//
|
779
|
+
ap::vmove(&workx(0), &xy(i, 0), ap::vlen(0,nvars-1));
|
780
|
+
mnlprocess(lm, workx, worky);
|
781
|
+
|
782
|
+
//
|
783
|
+
// Logit version of the answer
|
784
|
+
//
|
785
|
+
nmax = 0;
|
786
|
+
for(j = 0; j <= nclasses-1; j++)
|
787
|
+
{
|
788
|
+
if( worky(j)>worky(nmax) )
|
789
|
+
{
|
790
|
+
nmax = j;
|
791
|
+
}
|
792
|
+
}
|
793
|
+
|
794
|
+
//
|
795
|
+
// compare
|
796
|
+
//
|
797
|
+
if( nmax!=ap::round(xy(i,nvars)) )
|
798
|
+
{
|
799
|
+
result = result+1;
|
800
|
+
}
|
801
|
+
}
|
802
|
+
return result;
|
803
|
+
}
|
804
|
+
|
805
|
+
|
806
|
+
/*************************************************************************
|
807
|
+
Internal subroutine. Places exponents of the anti-overflow shifted
|
808
|
+
internal linear outputs into the service part of the W array.
|
809
|
+
*************************************************************************/
|
810
|
+
static void mnliexp(ap::real_1d_array& w, const ap::real_1d_array& x)
|
811
|
+
{
|
812
|
+
int nvars;
|
813
|
+
int nclasses;
|
814
|
+
int offs;
|
815
|
+
int i;
|
816
|
+
int i1;
|
817
|
+
double v;
|
818
|
+
double mx;
|
819
|
+
|
820
|
+
ap::ap_error::make_assertion(w(1)==logitvnum, "LOGIT: unexpected model version");
|
821
|
+
nvars = ap::round(w(2));
|
822
|
+
nclasses = ap::round(w(3));
|
823
|
+
offs = ap::round(w(4));
|
824
|
+
i1 = offs+(nvars+1)*(nclasses-1);
|
825
|
+
for(i = 0; i <= nclasses-2; i++)
|
826
|
+
{
|
827
|
+
v = ap::vdotproduct(&w(offs+i*(nvars+1)), &x(0), ap::vlen(offs+i*(nvars+1),offs+i*(nvars+1)+nvars-1));
|
828
|
+
w(i1+i) = v+w(offs+i*(nvars+1)+nvars);
|
829
|
+
}
|
830
|
+
w(i1+nclasses-1) = 0;
|
831
|
+
mx = 0;
|
832
|
+
for(i = i1; i <= i1+nclasses-1; i++)
|
833
|
+
{
|
834
|
+
mx = ap::maxreal(mx, w(i));
|
835
|
+
}
|
836
|
+
for(i = i1; i <= i1+nclasses-1; i++)
|
837
|
+
{
|
838
|
+
w(i) = exp(w(i)-mx);
|
839
|
+
}
|
840
|
+
}
|
841
|
+
|
842
|
+
|
843
|
+
/*************************************************************************
|
844
|
+
Calculation of all types of errors
|
845
|
+
|
846
|
+
-- ALGLIB --
|
847
|
+
Copyright 30.08.2008 by Bochkanov Sergey
|
848
|
+
*************************************************************************/
|
849
|
+
static void mnlallerrors(logitmodel& lm,
|
850
|
+
const ap::real_2d_array& xy,
|
851
|
+
int npoints,
|
852
|
+
double& relcls,
|
853
|
+
double& avgce,
|
854
|
+
double& rms,
|
855
|
+
double& avg,
|
856
|
+
double& avgrel)
|
857
|
+
{
|
858
|
+
int nvars;
|
859
|
+
int nclasses;
|
860
|
+
int i;
|
861
|
+
ap::real_1d_array buf;
|
862
|
+
ap::real_1d_array workx;
|
863
|
+
ap::real_1d_array y;
|
864
|
+
ap::real_1d_array dy;
|
865
|
+
|
866
|
+
ap::ap_error::make_assertion(ap::round(lm.w(1))==logitvnum, "MNL unit: Incorrect MNL version!");
|
867
|
+
nvars = ap::round(lm.w(2));
|
868
|
+
nclasses = ap::round(lm.w(3));
|
869
|
+
workx.setbounds(0, nvars-1);
|
870
|
+
y.setbounds(0, nclasses-1);
|
871
|
+
dy.setbounds(0, 0);
|
872
|
+
dserrallocate(nclasses, buf);
|
873
|
+
for(i = 0; i <= npoints-1; i++)
|
874
|
+
{
|
875
|
+
ap::vmove(&workx(0), &xy(i, 0), ap::vlen(0,nvars-1));
|
876
|
+
mnlprocess(lm, workx, y);
|
877
|
+
dy(0) = xy(i,nvars);
|
878
|
+
dserraccumulate(buf, y, dy);
|
879
|
+
}
|
880
|
+
dserrfinish(buf);
|
881
|
+
relcls = buf(0);
|
882
|
+
avgce = buf(1);
|
883
|
+
rms = buf(2);
|
884
|
+
avg = buf(3);
|
885
|
+
avgrel = buf(4);
|
886
|
+
}
|
887
|
+
|
888
|
+
|
889
|
+
/*************************************************************************
|
890
|
+
THE PURPOSE OF MCSRCH IS TO FIND A STEP WHICH SATISFIES A SUFFICIENT
|
891
|
+
DECREASE CONDITION AND A CURVATURE CONDITION.
|
892
|
+
|
893
|
+
AT EACH STAGE THE SUBROUTINE UPDATES AN INTERVAL OF UNCERTAINTY WITH
|
894
|
+
ENDPOINTS STX AND STY. THE INTERVAL OF UNCERTAINTY IS INITIALLY CHOSEN
|
895
|
+
SO THAT IT CONTAINS A MINIMIZER OF THE MODIFIED FUNCTION
|
896
|
+
|
897
|
+
F(X+STP*S) - F(X) - FTOL*STP*(GRADF(X)'S).
|
898
|
+
|
899
|
+
IF A STEP IS OBTAINED FOR WHICH THE MODIFIED FUNCTION HAS A NONPOSITIVE
|
900
|
+
FUNCTION VALUE AND NONNEGATIVE DERIVATIVE, THEN THE INTERVAL OF
|
901
|
+
UNCERTAINTY IS CHOSEN SO THAT IT CONTAINS A MINIMIZER OF F(X+STP*S).
|
902
|
+
|
903
|
+
THE ALGORITHM IS DESIGNED TO FIND A STEP WHICH SATISFIES THE SUFFICIENT
|
904
|
+
DECREASE CONDITION
|
905
|
+
|
906
|
+
F(X+STP*S) .LE. F(X) + FTOL*STP*(GRADF(X)'S),
|
907
|
+
|
908
|
+
AND THE CURVATURE CONDITION
|
909
|
+
|
910
|
+
ABS(GRADF(X+STP*S)'S)) .LE. GTOL*ABS(GRADF(X)'S).
|
911
|
+
|
912
|
+
IF FTOL IS LESS THAN GTOL AND IF, FOR EXAMPLE, THE FUNCTION IS BOUNDED
|
913
|
+
BELOW, THEN THERE IS ALWAYS A STEP WHICH SATISFIES BOTH CONDITIONS.
|
914
|
+
IF NO STEP CAN BE FOUND WHICH SATISFIES BOTH CONDITIONS, THEN THE
|
915
|
+
ALGORITHM USUALLY STOPS WHEN ROUNDING ERRORS PREVENT FURTHER PROGRESS.
|
916
|
+
IN THIS CASE STP ONLY SATISFIES THE SUFFICIENT DECREASE CONDITION.
|
917
|
+
|
918
|
+
PARAMETERS DESCRIPRION
|
919
|
+
|
920
|
+
N IS A POSITIVE INTEGER INPUT VARIABLE SET TO THE NUMBER OF VARIABLES.
|
921
|
+
|
922
|
+
X IS AN ARRAY OF LENGTH N. ON INPUT IT MUST CONTAIN THE BASE POINT FOR
|
923
|
+
THE LINE SEARCH. ON OUTPUT IT CONTAINS X+STP*S.
|
924
|
+
|
925
|
+
F IS A VARIABLE. ON INPUT IT MUST CONTAIN THE VALUE OF F AT X. ON OUTPUT
|
926
|
+
IT CONTAINS THE VALUE OF F AT X + STP*S.
|
927
|
+
|
928
|
+
G IS AN ARRAY OF LENGTH N. ON INPUT IT MUST CONTAIN THE GRADIENT OF F AT X.
|
929
|
+
ON OUTPUT IT CONTAINS THE GRADIENT OF F AT X + STP*S.
|
930
|
+
|
931
|
+
S IS AN INPUT ARRAY OF LENGTH N WHICH SPECIFIES THE SEARCH DIRECTION.
|
932
|
+
|
933
|
+
STP IS A NONNEGATIVE VARIABLE. ON INPUT STP CONTAINS AN INITIAL ESTIMATE
|
934
|
+
OF A SATISFACTORY STEP. ON OUTPUT STP CONTAINS THE FINAL ESTIMATE.
|
935
|
+
|
936
|
+
FTOL AND GTOL ARE NONNEGATIVE INPUT VARIABLES. TERMINATION OCCURS WHEN THE
|
937
|
+
SUFFICIENT DECREASE CONDITION AND THE DIRECTIONAL DERIVATIVE CONDITION ARE
|
938
|
+
SATISFIED.
|
939
|
+
|
940
|
+
XTOL IS A NONNEGATIVE INPUT VARIABLE. TERMINATION OCCURS WHEN THE RELATIVE
|
941
|
+
WIDTH OF THE INTERVAL OF UNCERTAINTY IS AT MOST XTOL.
|
942
|
+
|
943
|
+
STPMIN AND STPMAX ARE NONNEGATIVE INPUT VARIABLES WHICH SPECIFY LOWER AND
|
944
|
+
UPPER BOUNDS FOR THE STEP.
|
945
|
+
|
946
|
+
MAXFEV IS A POSITIVE INTEGER INPUT VARIABLE. TERMINATION OCCURS WHEN THE
|
947
|
+
NUMBER OF CALLS TO FCN IS AT LEAST MAXFEV BY THE END OF AN ITERATION.
|
948
|
+
|
949
|
+
INFO IS AN INTEGER OUTPUT VARIABLE SET AS FOLLOWS:
|
950
|
+
INFO = 0 IMPROPER INPUT PARAMETERS.
|
951
|
+
|
952
|
+
INFO = 1 THE SUFFICIENT DECREASE CONDITION AND THE
|
953
|
+
DIRECTIONAL DERIVATIVE CONDITION HOLD.
|
954
|
+
|
955
|
+
INFO = 2 RELATIVE WIDTH OF THE INTERVAL OF UNCERTAINTY
|
956
|
+
IS AT MOST XTOL.
|
957
|
+
|
958
|
+
INFO = 3 NUMBER OF CALLS TO FCN HAS REACHED MAXFEV.
|
959
|
+
|
960
|
+
INFO = 4 THE STEP IS AT THE LOWER BOUND STPMIN.
|
961
|
+
|
962
|
+
INFO = 5 THE STEP IS AT THE UPPER BOUND STPMAX.
|
963
|
+
|
964
|
+
INFO = 6 ROUNDING ERRORS PREVENT FURTHER PROGRESS.
|
965
|
+
THERE MAY NOT BE A STEP WHICH SATISFIES THE
|
966
|
+
SUFFICIENT DECREASE AND CURVATURE CONDITIONS.
|
967
|
+
TOLERANCES MAY BE TOO SMALL.
|
968
|
+
|
969
|
+
NFEV IS AN INTEGER OUTPUT VARIABLE SET TO THE NUMBER OF CALLS TO FCN.
|
970
|
+
|
971
|
+
WA IS A WORK ARRAY OF LENGTH N.
|
972
|
+
|
973
|
+
ARGONNE NATIONAL LABORATORY. MINPACK PROJECT. JUNE 1983
|
974
|
+
JORGE J. MORE', DAVID J. THUENTE
|
975
|
+
*************************************************************************/
|
976
|
+
static void mnlmcsrch(const int& n,
|
977
|
+
ap::real_1d_array& x,
|
978
|
+
double& f,
|
979
|
+
ap::real_1d_array& g,
|
980
|
+
const ap::real_1d_array& s,
|
981
|
+
double& stp,
|
982
|
+
int& info,
|
983
|
+
int& nfev,
|
984
|
+
ap::real_1d_array& wa,
|
985
|
+
logitmcstate& state,
|
986
|
+
int& stage)
|
987
|
+
{
|
988
|
+
double v;
|
989
|
+
double p5;
|
990
|
+
double p66;
|
991
|
+
double zero;
|
992
|
+
|
993
|
+
|
994
|
+
//
|
995
|
+
// init
|
996
|
+
//
|
997
|
+
p5 = 0.5;
|
998
|
+
p66 = 0.66;
|
999
|
+
state.xtrapf = 4.0;
|
1000
|
+
zero = 0;
|
1001
|
+
|
1002
|
+
//
|
1003
|
+
// Main cycle
|
1004
|
+
//
|
1005
|
+
while(true)
|
1006
|
+
{
|
1007
|
+
if( stage==0 )
|
1008
|
+
{
|
1009
|
+
|
1010
|
+
//
|
1011
|
+
// NEXT
|
1012
|
+
//
|
1013
|
+
stage = 2;
|
1014
|
+
continue;
|
1015
|
+
}
|
1016
|
+
if( stage==2 )
|
1017
|
+
{
|
1018
|
+
state.infoc = 1;
|
1019
|
+
info = 0;
|
1020
|
+
|
1021
|
+
//
|
1022
|
+
// CHECK THE INPUT PARAMETERS FOR ERRORS.
|
1023
|
+
//
|
1024
|
+
if( n<=0||stp<=0||ftol<0||gtol<zero||xtol<zero||stpmin<zero||stpmax<stpmin||maxfev<=0 )
|
1025
|
+
{
|
1026
|
+
stage = 0;
|
1027
|
+
return;
|
1028
|
+
}
|
1029
|
+
|
1030
|
+
//
|
1031
|
+
// COMPUTE THE INITIAL GRADIENT IN THE SEARCH DIRECTION
|
1032
|
+
// AND CHECK THAT S IS A DESCENT DIRECTION.
|
1033
|
+
//
|
1034
|
+
v = ap::vdotproduct(&g(0), &s(0), ap::vlen(0,n-1));
|
1035
|
+
state.dginit = v;
|
1036
|
+
if( state.dginit>=0 )
|
1037
|
+
{
|
1038
|
+
stage = 0;
|
1039
|
+
return;
|
1040
|
+
}
|
1041
|
+
|
1042
|
+
//
|
1043
|
+
// INITIALIZE LOCAL VARIABLES.
|
1044
|
+
//
|
1045
|
+
state.brackt = false;
|
1046
|
+
state.stage1 = true;
|
1047
|
+
nfev = 0;
|
1048
|
+
state.finit = f;
|
1049
|
+
state.dgtest = ftol*state.dginit;
|
1050
|
+
state.width = stpmax-stpmin;
|
1051
|
+
state.width1 = state.width/p5;
|
1052
|
+
ap::vmove(&wa(0), &x(0), ap::vlen(0,n-1));
|
1053
|
+
|
1054
|
+
//
|
1055
|
+
// THE VARIABLES STX, FX, DGX CONTAIN THE VALUES OF THE STEP,
|
1056
|
+
// FUNCTION, AND DIRECTIONAL DERIVATIVE AT THE BEST STEP.
|
1057
|
+
// THE VARIABLES STY, FY, DGY CONTAIN THE VALUE OF THE STEP,
|
1058
|
+
// FUNCTION, AND DERIVATIVE AT THE OTHER ENDPOINT OF
|
1059
|
+
// THE INTERVAL OF UNCERTAINTY.
|
1060
|
+
// THE VARIABLES STP, F, DG CONTAIN THE VALUES OF THE STEP,
|
1061
|
+
// FUNCTION, AND DERIVATIVE AT THE CURRENT STEP.
|
1062
|
+
//
|
1063
|
+
state.stx = 0;
|
1064
|
+
state.fx = state.finit;
|
1065
|
+
state.dgx = state.dginit;
|
1066
|
+
state.sty = 0;
|
1067
|
+
state.fy = state.finit;
|
1068
|
+
state.dgy = state.dginit;
|
1069
|
+
|
1070
|
+
//
|
1071
|
+
// NEXT
|
1072
|
+
//
|
1073
|
+
stage = 3;
|
1074
|
+
continue;
|
1075
|
+
}
|
1076
|
+
if( stage==3 )
|
1077
|
+
{
|
1078
|
+
|
1079
|
+
//
|
1080
|
+
// START OF ITERATION.
|
1081
|
+
//
|
1082
|
+
// SET THE MINIMUM AND MAXIMUM STEPS TO CORRESPOND
|
1083
|
+
// TO THE PRESENT INTERVAL OF UNCERTAINTY.
|
1084
|
+
//
|
1085
|
+
if( state.brackt )
|
1086
|
+
{
|
1087
|
+
if( state.stx<state.sty )
|
1088
|
+
{
|
1089
|
+
state.stmin = state.stx;
|
1090
|
+
state.stmax = state.sty;
|
1091
|
+
}
|
1092
|
+
else
|
1093
|
+
{
|
1094
|
+
state.stmin = state.sty;
|
1095
|
+
state.stmax = state.stx;
|
1096
|
+
}
|
1097
|
+
}
|
1098
|
+
else
|
1099
|
+
{
|
1100
|
+
state.stmin = state.stx;
|
1101
|
+
state.stmax = stp+state.xtrapf*(stp-state.stx);
|
1102
|
+
}
|
1103
|
+
|
1104
|
+
//
|
1105
|
+
// FORCE THE STEP TO BE WITHIN THE BOUNDS STPMAX AND STPMIN.
|
1106
|
+
//
|
1107
|
+
if( stp>stpmax )
|
1108
|
+
{
|
1109
|
+
stp = stpmax;
|
1110
|
+
}
|
1111
|
+
if( stp<stpmin )
|
1112
|
+
{
|
1113
|
+
stp = stpmin;
|
1114
|
+
}
|
1115
|
+
|
1116
|
+
//
|
1117
|
+
// IF AN UNUSUAL TERMINATION IS TO OCCUR THEN LET
|
1118
|
+
// STP BE THE LOWEST POINT OBTAINED SO FAR.
|
1119
|
+
//
|
1120
|
+
if( state.brackt&&(stp<=state.stmin||stp>=state.stmax)||nfev>=maxfev-1||state.infoc==0||state.brackt&&state.stmax-state.stmin<=xtol*state.stmax )
|
1121
|
+
{
|
1122
|
+
stp = state.stx;
|
1123
|
+
}
|
1124
|
+
|
1125
|
+
//
|
1126
|
+
// EVALUATE THE FUNCTION AND GRADIENT AT STP
|
1127
|
+
// AND COMPUTE THE DIRECTIONAL DERIVATIVE.
|
1128
|
+
//
|
1129
|
+
ap::vmove(&x(0), &wa(0), ap::vlen(0,n-1));
|
1130
|
+
ap::vadd(&x(0), &s(0), ap::vlen(0,n-1), stp);
|
1131
|
+
|
1132
|
+
//
|
1133
|
+
// NEXT
|
1134
|
+
//
|
1135
|
+
stage = 4;
|
1136
|
+
return;
|
1137
|
+
}
|
1138
|
+
if( stage==4 )
|
1139
|
+
{
|
1140
|
+
info = 0;
|
1141
|
+
nfev = nfev+1;
|
1142
|
+
v = ap::vdotproduct(&g(0), &s(0), ap::vlen(0,n-1));
|
1143
|
+
state.dg = v;
|
1144
|
+
state.ftest1 = state.finit+stp*state.dgtest;
|
1145
|
+
|
1146
|
+
//
|
1147
|
+
// TEST FOR CONVERGENCE.
|
1148
|
+
//
|
1149
|
+
if( state.brackt&&(stp<=state.stmin||stp>=state.stmax)||state.infoc==0 )
|
1150
|
+
{
|
1151
|
+
info = 6;
|
1152
|
+
}
|
1153
|
+
if( stp==stpmax&&f<=state.ftest1&&state.dg<=state.dgtest )
|
1154
|
+
{
|
1155
|
+
info = 5;
|
1156
|
+
}
|
1157
|
+
if( stp==stpmin&&(f>state.ftest1||state.dg>=state.dgtest) )
|
1158
|
+
{
|
1159
|
+
info = 4;
|
1160
|
+
}
|
1161
|
+
if( nfev>=maxfev )
|
1162
|
+
{
|
1163
|
+
info = 3;
|
1164
|
+
}
|
1165
|
+
if( state.brackt&&state.stmax-state.stmin<=xtol*state.stmax )
|
1166
|
+
{
|
1167
|
+
info = 2;
|
1168
|
+
}
|
1169
|
+
if( f<=state.ftest1&&fabs(state.dg)<=-gtol*state.dginit )
|
1170
|
+
{
|
1171
|
+
info = 1;
|
1172
|
+
}
|
1173
|
+
|
1174
|
+
//
|
1175
|
+
// CHECK FOR TERMINATION.
|
1176
|
+
//
|
1177
|
+
if( info!=0 )
|
1178
|
+
{
|
1179
|
+
stage = 0;
|
1180
|
+
return;
|
1181
|
+
}
|
1182
|
+
|
1183
|
+
//
|
1184
|
+
// IN THE FIRST STAGE WE SEEK A STEP FOR WHICH THE MODIFIED
|
1185
|
+
// FUNCTION HAS A NONPOSITIVE VALUE AND NONNEGATIVE DERIVATIVE.
|
1186
|
+
//
|
1187
|
+
if( state.stage1&&f<=state.ftest1&&state.dg>=ap::minreal(ftol, gtol)*state.dginit )
|
1188
|
+
{
|
1189
|
+
state.stage1 = false;
|
1190
|
+
}
|
1191
|
+
|
1192
|
+
//
|
1193
|
+
// A MODIFIED FUNCTION IS USED TO PREDICT THE STEP ONLY IF
|
1194
|
+
// WE HAVE NOT OBTAINED A STEP FOR WHICH THE MODIFIED
|
1195
|
+
// FUNCTION HAS A NONPOSITIVE FUNCTION VALUE AND NONNEGATIVE
|
1196
|
+
// DERIVATIVE, AND IF A LOWER FUNCTION VALUE HAS BEEN
|
1197
|
+
// OBTAINED BUT THE DECREASE IS NOT SUFFICIENT.
|
1198
|
+
//
|
1199
|
+
if( state.stage1&&f<=state.fx&&f>state.ftest1 )
|
1200
|
+
{
|
1201
|
+
|
1202
|
+
//
|
1203
|
+
// DEFINE THE MODIFIED FUNCTION AND DERIVATIVE VALUES.
|
1204
|
+
//
|
1205
|
+
state.fm = f-stp*state.dgtest;
|
1206
|
+
state.fxm = state.fx-state.stx*state.dgtest;
|
1207
|
+
state.fym = state.fy-state.sty*state.dgtest;
|
1208
|
+
state.dgm = state.dg-state.dgtest;
|
1209
|
+
state.dgxm = state.dgx-state.dgtest;
|
1210
|
+
state.dgym = state.dgy-state.dgtest;
|
1211
|
+
|
1212
|
+
//
|
1213
|
+
// CALL CSTEP TO UPDATE THE INTERVAL OF UNCERTAINTY
|
1214
|
+
// AND TO COMPUTE THE NEW STEP.
|
1215
|
+
//
|
1216
|
+
mnlmcstep(state.stx, state.fxm, state.dgxm, state.sty, state.fym, state.dgym, stp, state.fm, state.dgm, state.brackt, state.stmin, state.stmax, state.infoc);
|
1217
|
+
|
1218
|
+
//
|
1219
|
+
// RESET THE FUNCTION AND GRADIENT VALUES FOR F.
|
1220
|
+
//
|
1221
|
+
state.fx = state.fxm+state.stx*state.dgtest;
|
1222
|
+
state.fy = state.fym+state.sty*state.dgtest;
|
1223
|
+
state.dgx = state.dgxm+state.dgtest;
|
1224
|
+
state.dgy = state.dgym+state.dgtest;
|
1225
|
+
}
|
1226
|
+
else
|
1227
|
+
{
|
1228
|
+
|
1229
|
+
//
|
1230
|
+
// CALL MCSTEP TO UPDATE THE INTERVAL OF UNCERTAINTY
|
1231
|
+
// AND TO COMPUTE THE NEW STEP.
|
1232
|
+
//
|
1233
|
+
mnlmcstep(state.stx, state.fx, state.dgx, state.sty, state.fy, state.dgy, stp, f, state.dg, state.brackt, state.stmin, state.stmax, state.infoc);
|
1234
|
+
}
|
1235
|
+
|
1236
|
+
//
|
1237
|
+
// FORCE A SUFFICIENT DECREASE IN THE SIZE OF THE
|
1238
|
+
// INTERVAL OF UNCERTAINTY.
|
1239
|
+
//
|
1240
|
+
if( state.brackt )
|
1241
|
+
{
|
1242
|
+
if( fabs(state.sty-state.stx)>=p66*state.width1 )
|
1243
|
+
{
|
1244
|
+
stp = state.stx+p5*(state.sty-state.stx);
|
1245
|
+
}
|
1246
|
+
state.width1 = state.width;
|
1247
|
+
state.width = fabs(state.sty-state.stx);
|
1248
|
+
}
|
1249
|
+
|
1250
|
+
//
|
1251
|
+
// NEXT.
|
1252
|
+
//
|
1253
|
+
stage = 3;
|
1254
|
+
continue;
|
1255
|
+
}
|
1256
|
+
}
|
1257
|
+
}
|
1258
|
+
|
1259
|
+
|
1260
|
+
static void mnlmcstep(double& stx,
|
1261
|
+
double& fx,
|
1262
|
+
double& dx,
|
1263
|
+
double& sty,
|
1264
|
+
double& fy,
|
1265
|
+
double& dy,
|
1266
|
+
double& stp,
|
1267
|
+
const double& fp,
|
1268
|
+
const double& dp,
|
1269
|
+
bool& brackt,
|
1270
|
+
const double& stmin,
|
1271
|
+
const double& stmax,
|
1272
|
+
int& info)
|
1273
|
+
{
|
1274
|
+
bool bound;
|
1275
|
+
double gamma;
|
1276
|
+
double p;
|
1277
|
+
double q;
|
1278
|
+
double r;
|
1279
|
+
double s;
|
1280
|
+
double sgnd;
|
1281
|
+
double stpc;
|
1282
|
+
double stpf;
|
1283
|
+
double stpq;
|
1284
|
+
double theta;
|
1285
|
+
|
1286
|
+
info = 0;
|
1287
|
+
|
1288
|
+
//
|
1289
|
+
// CHECK THE INPUT PARAMETERS FOR ERRORS.
|
1290
|
+
//
|
1291
|
+
if( brackt&&(stp<=ap::minreal(stx, sty)||stp>=ap::maxreal(stx, sty))||dx*(stp-stx)>=0||stmax<stmin )
|
1292
|
+
{
|
1293
|
+
return;
|
1294
|
+
}
|
1295
|
+
|
1296
|
+
//
|
1297
|
+
// DETERMINE IF THE DERIVATIVES HAVE OPPOSITE SIGN.
|
1298
|
+
//
|
1299
|
+
sgnd = dp*(dx/fabs(dx));
|
1300
|
+
|
1301
|
+
//
|
1302
|
+
// FIRST CASE. A HIGHER FUNCTION VALUE.
|
1303
|
+
// THE MINIMUM IS BRACKETED. IF THE CUBIC STEP IS CLOSER
|
1304
|
+
// TO STX THAN THE QUADRATIC STEP, THE CUBIC STEP IS TAKEN,
|
1305
|
+
// ELSE THE AVERAGE OF THE CUBIC AND QUADRATIC STEPS IS TAKEN.
|
1306
|
+
//
|
1307
|
+
if( fp>fx )
|
1308
|
+
{
|
1309
|
+
info = 1;
|
1310
|
+
bound = true;
|
1311
|
+
theta = 3*(fx-fp)/(stp-stx)+dx+dp;
|
1312
|
+
s = ap::maxreal(fabs(theta), ap::maxreal(fabs(dx), fabs(dp)));
|
1313
|
+
gamma = s*sqrt(ap::sqr(theta/s)-dx/s*(dp/s));
|
1314
|
+
if( stp<stx )
|
1315
|
+
{
|
1316
|
+
gamma = -gamma;
|
1317
|
+
}
|
1318
|
+
p = gamma-dx+theta;
|
1319
|
+
q = gamma-dx+gamma+dp;
|
1320
|
+
r = p/q;
|
1321
|
+
stpc = stx+r*(stp-stx);
|
1322
|
+
stpq = stx+dx/((fx-fp)/(stp-stx)+dx)/2*(stp-stx);
|
1323
|
+
if( fabs(stpc-stx)<fabs(stpq-stx) )
|
1324
|
+
{
|
1325
|
+
stpf = stpc;
|
1326
|
+
}
|
1327
|
+
else
|
1328
|
+
{
|
1329
|
+
stpf = stpc+(stpq-stpc)/2;
|
1330
|
+
}
|
1331
|
+
brackt = true;
|
1332
|
+
}
|
1333
|
+
else
|
1334
|
+
{
|
1335
|
+
if( sgnd<0 )
|
1336
|
+
{
|
1337
|
+
|
1338
|
+
//
|
1339
|
+
// SECOND CASE. A LOWER FUNCTION VALUE AND DERIVATIVES OF
|
1340
|
+
// OPPOSITE SIGN. THE MINIMUM IS BRACKETED. IF THE CUBIC
|
1341
|
+
// STEP IS CLOSER TO STX THAN THE QUADRATIC (SECANT) STEP,
|
1342
|
+
// THE CUBIC STEP IS TAKEN, ELSE THE QUADRATIC STEP IS TAKEN.
|
1343
|
+
//
|
1344
|
+
info = 2;
|
1345
|
+
bound = false;
|
1346
|
+
theta = 3*(fx-fp)/(stp-stx)+dx+dp;
|
1347
|
+
s = ap::maxreal(fabs(theta), ap::maxreal(fabs(dx), fabs(dp)));
|
1348
|
+
gamma = s*sqrt(ap::sqr(theta/s)-dx/s*(dp/s));
|
1349
|
+
if( stp>stx )
|
1350
|
+
{
|
1351
|
+
gamma = -gamma;
|
1352
|
+
}
|
1353
|
+
p = gamma-dp+theta;
|
1354
|
+
q = gamma-dp+gamma+dx;
|
1355
|
+
r = p/q;
|
1356
|
+
stpc = stp+r*(stx-stp);
|
1357
|
+
stpq = stp+dp/(dp-dx)*(stx-stp);
|
1358
|
+
if( fabs(stpc-stp)>fabs(stpq-stp) )
|
1359
|
+
{
|
1360
|
+
stpf = stpc;
|
1361
|
+
}
|
1362
|
+
else
|
1363
|
+
{
|
1364
|
+
stpf = stpq;
|
1365
|
+
}
|
1366
|
+
brackt = true;
|
1367
|
+
}
|
1368
|
+
else
|
1369
|
+
{
|
1370
|
+
if( fabs(dp)<fabs(dx) )
|
1371
|
+
{
|
1372
|
+
|
1373
|
+
//
|
1374
|
+
// THIRD CASE. A LOWER FUNCTION VALUE, DERIVATIVES OF THE
|
1375
|
+
// SAME SIGN, AND THE MAGNITUDE OF THE DERIVATIVE DECREASES.
|
1376
|
+
// THE CUBIC STEP IS ONLY USED IF THE CUBIC TENDS TO INFINITY
|
1377
|
+
// IN THE DIRECTION OF THE STEP OR IF THE MINIMUM OF THE CUBIC
|
1378
|
+
// IS BEYOND STP. OTHERWISE THE CUBIC STEP IS DEFINED TO BE
|
1379
|
+
// EITHER STPMIN OR STPMAX. THE QUADRATIC (SECANT) STEP IS ALSO
|
1380
|
+
// COMPUTED AND IF THE MINIMUM IS BRACKETED THEN THE THE STEP
|
1381
|
+
// CLOSEST TO STX IS TAKEN, ELSE THE STEP FARTHEST AWAY IS TAKEN.
|
1382
|
+
//
|
1383
|
+
info = 3;
|
1384
|
+
bound = true;
|
1385
|
+
theta = 3*(fx-fp)/(stp-stx)+dx+dp;
|
1386
|
+
s = ap::maxreal(fabs(theta), ap::maxreal(fabs(dx), fabs(dp)));
|
1387
|
+
|
1388
|
+
//
|
1389
|
+
// THE CASE GAMMA = 0 ONLY ARISES IF THE CUBIC DOES NOT TEND
|
1390
|
+
// TO INFINITY IN THE DIRECTION OF THE STEP.
|
1391
|
+
//
|
1392
|
+
gamma = s*sqrt(ap::maxreal(double(0), ap::sqr(theta/s)-dx/s*(dp/s)));
|
1393
|
+
if( stp>stx )
|
1394
|
+
{
|
1395
|
+
gamma = -gamma;
|
1396
|
+
}
|
1397
|
+
p = gamma-dp+theta;
|
1398
|
+
q = gamma+(dx-dp)+gamma;
|
1399
|
+
r = p/q;
|
1400
|
+
if( r<0&&gamma!=0 )
|
1401
|
+
{
|
1402
|
+
stpc = stp+r*(stx-stp);
|
1403
|
+
}
|
1404
|
+
else
|
1405
|
+
{
|
1406
|
+
if( stp>stx )
|
1407
|
+
{
|
1408
|
+
stpc = stmax;
|
1409
|
+
}
|
1410
|
+
else
|
1411
|
+
{
|
1412
|
+
stpc = stmin;
|
1413
|
+
}
|
1414
|
+
}
|
1415
|
+
stpq = stp+dp/(dp-dx)*(stx-stp);
|
1416
|
+
if( brackt )
|
1417
|
+
{
|
1418
|
+
if( fabs(stp-stpc)<fabs(stp-stpq) )
|
1419
|
+
{
|
1420
|
+
stpf = stpc;
|
1421
|
+
}
|
1422
|
+
else
|
1423
|
+
{
|
1424
|
+
stpf = stpq;
|
1425
|
+
}
|
1426
|
+
}
|
1427
|
+
else
|
1428
|
+
{
|
1429
|
+
if( fabs(stp-stpc)>fabs(stp-stpq) )
|
1430
|
+
{
|
1431
|
+
stpf = stpc;
|
1432
|
+
}
|
1433
|
+
else
|
1434
|
+
{
|
1435
|
+
stpf = stpq;
|
1436
|
+
}
|
1437
|
+
}
|
1438
|
+
}
|
1439
|
+
else
|
1440
|
+
{
|
1441
|
+
|
1442
|
+
//
|
1443
|
+
// FOURTH CASE. A LOWER FUNCTION VALUE, DERIVATIVES OF THE
|
1444
|
+
// SAME SIGN, AND THE MAGNITUDE OF THE DERIVATIVE DOES
|
1445
|
+
// NOT DECREASE. IF THE MINIMUM IS NOT BRACKETED, THE STEP
|
1446
|
+
// IS EITHER STPMIN OR STPMAX, ELSE THE CUBIC STEP IS TAKEN.
|
1447
|
+
//
|
1448
|
+
info = 4;
|
1449
|
+
bound = false;
|
1450
|
+
if( brackt )
|
1451
|
+
{
|
1452
|
+
theta = 3*(fp-fy)/(sty-stp)+dy+dp;
|
1453
|
+
s = ap::maxreal(fabs(theta), ap::maxreal(fabs(dy), fabs(dp)));
|
1454
|
+
gamma = s*sqrt(ap::sqr(theta/s)-dy/s*(dp/s));
|
1455
|
+
if( stp>sty )
|
1456
|
+
{
|
1457
|
+
gamma = -gamma;
|
1458
|
+
}
|
1459
|
+
p = gamma-dp+theta;
|
1460
|
+
q = gamma-dp+gamma+dy;
|
1461
|
+
r = p/q;
|
1462
|
+
stpc = stp+r*(sty-stp);
|
1463
|
+
stpf = stpc;
|
1464
|
+
}
|
1465
|
+
else
|
1466
|
+
{
|
1467
|
+
if( stp>stx )
|
1468
|
+
{
|
1469
|
+
stpf = stmax;
|
1470
|
+
}
|
1471
|
+
else
|
1472
|
+
{
|
1473
|
+
stpf = stmin;
|
1474
|
+
}
|
1475
|
+
}
|
1476
|
+
}
|
1477
|
+
}
|
1478
|
+
}
|
1479
|
+
|
1480
|
+
//
|
1481
|
+
// UPDATE THE INTERVAL OF UNCERTAINTY. THIS UPDATE DOES NOT
|
1482
|
+
// DEPEND ON THE NEW STEP OR THE CASE ANALYSIS ABOVE.
|
1483
|
+
//
|
1484
|
+
if( fp>fx )
|
1485
|
+
{
|
1486
|
+
sty = stp;
|
1487
|
+
fy = fp;
|
1488
|
+
dy = dp;
|
1489
|
+
}
|
1490
|
+
else
|
1491
|
+
{
|
1492
|
+
if( sgnd<0.0 )
|
1493
|
+
{
|
1494
|
+
sty = stx;
|
1495
|
+
fy = fx;
|
1496
|
+
dy = dx;
|
1497
|
+
}
|
1498
|
+
stx = stp;
|
1499
|
+
fx = fp;
|
1500
|
+
dx = dp;
|
1501
|
+
}
|
1502
|
+
|
1503
|
+
//
|
1504
|
+
// COMPUTE THE NEW STEP AND SAFEGUARD IT.
|
1505
|
+
//
|
1506
|
+
stpf = ap::minreal(stmax, stpf);
|
1507
|
+
stpf = ap::maxreal(stmin, stpf);
|
1508
|
+
stp = stpf;
|
1509
|
+
if( brackt&&bound )
|
1510
|
+
{
|
1511
|
+
if( sty>stx )
|
1512
|
+
{
|
1513
|
+
stp = ap::minreal(stx+0.66*(sty-stx), stp);
|
1514
|
+
}
|
1515
|
+
else
|
1516
|
+
{
|
1517
|
+
stp = ap::maxreal(stx+0.66*(sty-stx), stp);
|
1518
|
+
}
|
1519
|
+
}
|
1520
|
+
}
|
1521
|
+
|
1522
|
+
|
1523
|
+
|