alglib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/History.txt +7 -0
  2. data/Manifest.txt +253 -0
  3. data/README.txt +33 -0
  4. data/Rakefile +27 -0
  5. data/ext/Rakefile +24 -0
  6. data/ext/alglib.i +24 -0
  7. data/ext/alglib/Makefile +157 -0
  8. data/ext/alglib/airyf.cpp +372 -0
  9. data/ext/alglib/airyf.h +81 -0
  10. data/ext/alglib/alglib.cpp +8558 -0
  11. data/ext/alglib/alglib_util.cpp +19 -0
  12. data/ext/alglib/alglib_util.h +14 -0
  13. data/ext/alglib/ap.cpp +877 -0
  14. data/ext/alglib/ap.english.html +364 -0
  15. data/ext/alglib/ap.h +666 -0
  16. data/ext/alglib/ap.russian.html +442 -0
  17. data/ext/alglib/apvt.h +754 -0
  18. data/ext/alglib/bdss.cpp +1500 -0
  19. data/ext/alglib/bdss.h +251 -0
  20. data/ext/alglib/bdsvd.cpp +1339 -0
  21. data/ext/alglib/bdsvd.h +164 -0
  22. data/ext/alglib/bessel.cpp +1226 -0
  23. data/ext/alglib/bessel.h +331 -0
  24. data/ext/alglib/betaf.cpp +105 -0
  25. data/ext/alglib/betaf.h +74 -0
  26. data/ext/alglib/bidiagonal.cpp +1328 -0
  27. data/ext/alglib/bidiagonal.h +350 -0
  28. data/ext/alglib/binomialdistr.cpp +247 -0
  29. data/ext/alglib/binomialdistr.h +153 -0
  30. data/ext/alglib/blas.cpp +576 -0
  31. data/ext/alglib/blas.h +132 -0
  32. data/ext/alglib/cblas.cpp +226 -0
  33. data/ext/alglib/cblas.h +57 -0
  34. data/ext/alglib/cdet.cpp +138 -0
  35. data/ext/alglib/cdet.h +92 -0
  36. data/ext/alglib/chebyshev.cpp +216 -0
  37. data/ext/alglib/chebyshev.h +76 -0
  38. data/ext/alglib/chisquaredistr.cpp +157 -0
  39. data/ext/alglib/chisquaredistr.h +144 -0
  40. data/ext/alglib/cholesky.cpp +285 -0
  41. data/ext/alglib/cholesky.h +86 -0
  42. data/ext/alglib/cinverse.cpp +298 -0
  43. data/ext/alglib/cinverse.h +111 -0
  44. data/ext/alglib/clu.cpp +337 -0
  45. data/ext/alglib/clu.h +120 -0
  46. data/ext/alglib/correlation.cpp +280 -0
  47. data/ext/alglib/correlation.h +77 -0
  48. data/ext/alglib/correlationtests.cpp +726 -0
  49. data/ext/alglib/correlationtests.h +134 -0
  50. data/ext/alglib/crcond.cpp +826 -0
  51. data/ext/alglib/crcond.h +148 -0
  52. data/ext/alglib/creflections.cpp +310 -0
  53. data/ext/alglib/creflections.h +165 -0
  54. data/ext/alglib/csolve.cpp +312 -0
  55. data/ext/alglib/csolve.h +99 -0
  56. data/ext/alglib/ctrinverse.cpp +387 -0
  57. data/ext/alglib/ctrinverse.h +98 -0
  58. data/ext/alglib/ctrlinsolve.cpp +297 -0
  59. data/ext/alglib/ctrlinsolve.h +81 -0
  60. data/ext/alglib/dawson.cpp +234 -0
  61. data/ext/alglib/dawson.h +74 -0
  62. data/ext/alglib/descriptivestatistics.cpp +436 -0
  63. data/ext/alglib/descriptivestatistics.h +112 -0
  64. data/ext/alglib/det.cpp +140 -0
  65. data/ext/alglib/det.h +94 -0
  66. data/ext/alglib/dforest.cpp +1819 -0
  67. data/ext/alglib/dforest.h +316 -0
  68. data/ext/alglib/elliptic.cpp +497 -0
  69. data/ext/alglib/elliptic.h +217 -0
  70. data/ext/alglib/estnorm.cpp +429 -0
  71. data/ext/alglib/estnorm.h +107 -0
  72. data/ext/alglib/expintegrals.cpp +422 -0
  73. data/ext/alglib/expintegrals.h +108 -0
  74. data/ext/alglib/faq.english.html +258 -0
  75. data/ext/alglib/faq.russian.html +272 -0
  76. data/ext/alglib/fdistr.cpp +202 -0
  77. data/ext/alglib/fdistr.h +163 -0
  78. data/ext/alglib/fresnel.cpp +211 -0
  79. data/ext/alglib/fresnel.h +91 -0
  80. data/ext/alglib/gammaf.cpp +338 -0
  81. data/ext/alglib/gammaf.h +104 -0
  82. data/ext/alglib/gqgengauss.cpp +235 -0
  83. data/ext/alglib/gqgengauss.h +92 -0
  84. data/ext/alglib/gqgenhermite.cpp +268 -0
  85. data/ext/alglib/gqgenhermite.h +63 -0
  86. data/ext/alglib/gqgenjacobi.cpp +297 -0
  87. data/ext/alglib/gqgenjacobi.h +72 -0
  88. data/ext/alglib/gqgenlaguerre.cpp +265 -0
  89. data/ext/alglib/gqgenlaguerre.h +69 -0
  90. data/ext/alglib/gqgenlegendre.cpp +300 -0
  91. data/ext/alglib/gqgenlegendre.h +62 -0
  92. data/ext/alglib/gqgenlobatto.cpp +305 -0
  93. data/ext/alglib/gqgenlobatto.h +97 -0
  94. data/ext/alglib/gqgenradau.cpp +232 -0
  95. data/ext/alglib/gqgenradau.h +95 -0
  96. data/ext/alglib/hbisinv.cpp +480 -0
  97. data/ext/alglib/hbisinv.h +183 -0
  98. data/ext/alglib/hblas.cpp +228 -0
  99. data/ext/alglib/hblas.h +64 -0
  100. data/ext/alglib/hcholesky.cpp +339 -0
  101. data/ext/alglib/hcholesky.h +91 -0
  102. data/ext/alglib/hermite.cpp +114 -0
  103. data/ext/alglib/hermite.h +49 -0
  104. data/ext/alglib/hessenberg.cpp +370 -0
  105. data/ext/alglib/hessenberg.h +152 -0
  106. data/ext/alglib/hevd.cpp +247 -0
  107. data/ext/alglib/hevd.h +107 -0
  108. data/ext/alglib/hsschur.cpp +1316 -0
  109. data/ext/alglib/hsschur.h +108 -0
  110. data/ext/alglib/htridiagonal.cpp +734 -0
  111. data/ext/alglib/htridiagonal.h +180 -0
  112. data/ext/alglib/ialglib.cpp +6 -0
  113. data/ext/alglib/ialglib.h +9 -0
  114. data/ext/alglib/ibetaf.cpp +960 -0
  115. data/ext/alglib/ibetaf.h +125 -0
  116. data/ext/alglib/igammaf.cpp +430 -0
  117. data/ext/alglib/igammaf.h +157 -0
  118. data/ext/alglib/inv.cpp +274 -0
  119. data/ext/alglib/inv.h +115 -0
  120. data/ext/alglib/inverseupdate.cpp +480 -0
  121. data/ext/alglib/inverseupdate.h +185 -0
  122. data/ext/alglib/jacobianelliptic.cpp +164 -0
  123. data/ext/alglib/jacobianelliptic.h +94 -0
  124. data/ext/alglib/jarquebera.cpp +2271 -0
  125. data/ext/alglib/jarquebera.h +80 -0
  126. data/ext/alglib/kmeans.cpp +356 -0
  127. data/ext/alglib/kmeans.h +76 -0
  128. data/ext/alglib/laguerre.cpp +94 -0
  129. data/ext/alglib/laguerre.h +48 -0
  130. data/ext/alglib/lbfgs.cpp +1167 -0
  131. data/ext/alglib/lbfgs.h +218 -0
  132. data/ext/alglib/lda.cpp +434 -0
  133. data/ext/alglib/lda.h +133 -0
  134. data/ext/alglib/ldlt.cpp +1130 -0
  135. data/ext/alglib/ldlt.h +124 -0
  136. data/ext/alglib/leastsquares.cpp +1252 -0
  137. data/ext/alglib/leastsquares.h +290 -0
  138. data/ext/alglib/legendre.cpp +107 -0
  139. data/ext/alglib/legendre.h +49 -0
  140. data/ext/alglib/linreg.cpp +1185 -0
  141. data/ext/alglib/linreg.h +380 -0
  142. data/ext/alglib/logit.cpp +1523 -0
  143. data/ext/alglib/logit.h +333 -0
  144. data/ext/alglib/lq.cpp +399 -0
  145. data/ext/alglib/lq.h +160 -0
  146. data/ext/alglib/lu.cpp +462 -0
  147. data/ext/alglib/lu.h +119 -0
  148. data/ext/alglib/mannwhitneyu.cpp +4490 -0
  149. data/ext/alglib/mannwhitneyu.h +115 -0
  150. data/ext/alglib/minlm.cpp +918 -0
  151. data/ext/alglib/minlm.h +312 -0
  152. data/ext/alglib/mlpbase.cpp +3375 -0
  153. data/ext/alglib/mlpbase.h +589 -0
  154. data/ext/alglib/mlpe.cpp +1369 -0
  155. data/ext/alglib/mlpe.h +552 -0
  156. data/ext/alglib/mlptrain.cpp +1056 -0
  157. data/ext/alglib/mlptrain.h +283 -0
  158. data/ext/alglib/nearunityunit.cpp +91 -0
  159. data/ext/alglib/nearunityunit.h +17 -0
  160. data/ext/alglib/normaldistr.cpp +377 -0
  161. data/ext/alglib/normaldistr.h +175 -0
  162. data/ext/alglib/nsevd.cpp +1869 -0
  163. data/ext/alglib/nsevd.h +140 -0
  164. data/ext/alglib/pca.cpp +168 -0
  165. data/ext/alglib/pca.h +87 -0
  166. data/ext/alglib/poissondistr.cpp +143 -0
  167. data/ext/alglib/poissondistr.h +130 -0
  168. data/ext/alglib/polinterpolation.cpp +685 -0
  169. data/ext/alglib/polinterpolation.h +206 -0
  170. data/ext/alglib/psif.cpp +173 -0
  171. data/ext/alglib/psif.h +88 -0
  172. data/ext/alglib/qr.cpp +414 -0
  173. data/ext/alglib/qr.h +168 -0
  174. data/ext/alglib/ratinterpolation.cpp +134 -0
  175. data/ext/alglib/ratinterpolation.h +72 -0
  176. data/ext/alglib/rcond.cpp +705 -0
  177. data/ext/alglib/rcond.h +140 -0
  178. data/ext/alglib/reflections.cpp +504 -0
  179. data/ext/alglib/reflections.h +165 -0
  180. data/ext/alglib/rotations.cpp +473 -0
  181. data/ext/alglib/rotations.h +128 -0
  182. data/ext/alglib/rsolve.cpp +221 -0
  183. data/ext/alglib/rsolve.h +99 -0
  184. data/ext/alglib/sbisinv.cpp +217 -0
  185. data/ext/alglib/sbisinv.h +171 -0
  186. data/ext/alglib/sblas.cpp +185 -0
  187. data/ext/alglib/sblas.h +64 -0
  188. data/ext/alglib/schur.cpp +156 -0
  189. data/ext/alglib/schur.h +102 -0
  190. data/ext/alglib/sdet.cpp +193 -0
  191. data/ext/alglib/sdet.h +101 -0
  192. data/ext/alglib/sevd.cpp +116 -0
  193. data/ext/alglib/sevd.h +99 -0
  194. data/ext/alglib/sinverse.cpp +672 -0
  195. data/ext/alglib/sinverse.h +138 -0
  196. data/ext/alglib/spddet.cpp +138 -0
  197. data/ext/alglib/spddet.h +96 -0
  198. data/ext/alglib/spdgevd.cpp +842 -0
  199. data/ext/alglib/spdgevd.h +200 -0
  200. data/ext/alglib/spdinverse.cpp +509 -0
  201. data/ext/alglib/spdinverse.h +122 -0
  202. data/ext/alglib/spdrcond.cpp +421 -0
  203. data/ext/alglib/spdrcond.h +118 -0
  204. data/ext/alglib/spdsolve.cpp +275 -0
  205. data/ext/alglib/spdsolve.h +105 -0
  206. data/ext/alglib/spline2d.cpp +1192 -0
  207. data/ext/alglib/spline2d.h +301 -0
  208. data/ext/alglib/spline3.cpp +1264 -0
  209. data/ext/alglib/spline3.h +290 -0
  210. data/ext/alglib/srcond.cpp +595 -0
  211. data/ext/alglib/srcond.h +127 -0
  212. data/ext/alglib/ssolve.cpp +895 -0
  213. data/ext/alglib/ssolve.h +139 -0
  214. data/ext/alglib/stdafx.h +0 -0
  215. data/ext/alglib/stest.cpp +131 -0
  216. data/ext/alglib/stest.h +94 -0
  217. data/ext/alglib/studenttdistr.cpp +222 -0
  218. data/ext/alglib/studenttdistr.h +115 -0
  219. data/ext/alglib/studentttests.cpp +377 -0
  220. data/ext/alglib/studentttests.h +178 -0
  221. data/ext/alglib/svd.cpp +620 -0
  222. data/ext/alglib/svd.h +126 -0
  223. data/ext/alglib/tdbisinv.cpp +2608 -0
  224. data/ext/alglib/tdbisinv.h +228 -0
  225. data/ext/alglib/tdevd.cpp +1229 -0
  226. data/ext/alglib/tdevd.h +115 -0
  227. data/ext/alglib/tridiagonal.cpp +594 -0
  228. data/ext/alglib/tridiagonal.h +171 -0
  229. data/ext/alglib/trigintegrals.cpp +490 -0
  230. data/ext/alglib/trigintegrals.h +131 -0
  231. data/ext/alglib/trinverse.cpp +345 -0
  232. data/ext/alglib/trinverse.h +98 -0
  233. data/ext/alglib/trlinsolve.cpp +926 -0
  234. data/ext/alglib/trlinsolve.h +73 -0
  235. data/ext/alglib/tsort.cpp +405 -0
  236. data/ext/alglib/tsort.h +54 -0
  237. data/ext/alglib/variancetests.cpp +245 -0
  238. data/ext/alglib/variancetests.h +134 -0
  239. data/ext/alglib/wsr.cpp +6285 -0
  240. data/ext/alglib/wsr.h +96 -0
  241. data/ext/ap.i +97 -0
  242. data/ext/correlation.i +24 -0
  243. data/ext/extconf.rb +6 -0
  244. data/ext/logit.i +89 -0
  245. data/lib/alglib.rb +71 -0
  246. data/lib/alglib/correlation.rb +26 -0
  247. data/lib/alglib/linearregression.rb +63 -0
  248. data/lib/alglib/logit.rb +42 -0
  249. data/test/test_alglib.rb +52 -0
  250. data/test/test_correlation.rb +44 -0
  251. data/test/test_correlationtest.rb +45 -0
  252. data/test/test_linreg.rb +35 -0
  253. data/test/test_logit.rb +43 -0
  254. data/test/test_pca.rb +27 -0
  255. metadata +326 -0
@@ -0,0 +1,218 @@
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 _lbfgs_h
34
+ #define _lbfgs_h
35
+
36
+ #include "ap.h"
37
+ #include "ialglib.h"
38
+
39
+ struct lbfgsstate
40
+ {
41
+ int n;
42
+ int m;
43
+ double epsg;
44
+ double epsf;
45
+ double epsx;
46
+ int maxits;
47
+ int flags;
48
+ int nfev;
49
+ int mcstage;
50
+ int k;
51
+ int q;
52
+ int p;
53
+ ap::real_1d_array rho;
54
+ ap::real_2d_array y;
55
+ ap::real_2d_array s;
56
+ ap::real_1d_array theta;
57
+ ap::real_1d_array d;
58
+ double stp;
59
+ ap::real_1d_array work;
60
+ double fold;
61
+ double gammak;
62
+ ap::real_1d_array x;
63
+ double f;
64
+ ap::real_1d_array g;
65
+ bool xupdated;
66
+ ap::rcommstate rstate;
67
+ int repiterationscount;
68
+ int repnfev;
69
+ int repterminationtype;
70
+ bool brackt;
71
+ bool stage1;
72
+ int infoc;
73
+ double dg;
74
+ double dgm;
75
+ double dginit;
76
+ double dgtest;
77
+ double dgx;
78
+ double dgxm;
79
+ double dgy;
80
+ double dgym;
81
+ double finit;
82
+ double ftest1;
83
+ double fm;
84
+ double fx;
85
+ double fxm;
86
+ double fy;
87
+ double fym;
88
+ double stx;
89
+ double sty;
90
+ double stmin;
91
+ double stmax;
92
+ double width;
93
+ double width1;
94
+ double xtrapf;
95
+ };
96
+ struct lbfgsreport
97
+ {
98
+ int iterationscount;
99
+ int nfev;
100
+ int terminationtype;
101
+ };
102
+
103
+
104
+ /*************************************************************************
105
+ LIMITED MEMORY BFGS METHOD FOR LARGE SCALE OPTIMIZATION
106
+
107
+ The subroutine minimizes function F(x) of N arguments by using a quasi-
108
+ Newton method (LBFGS scheme) which is optimized to use a minimum amount
109
+ of memory.
110
+
111
+ The subroutine generates the approximation of an inverse Hessian matrix by
112
+ using information about the last M steps of the algorithm (instead of N).
113
+ It lessens a required amount of memory from a value of order N^2 to a
114
+ value of order 2*N*M.
115
+
116
+ Input parameters:
117
+ N - problem dimension. N>0
118
+ M - number of corrections in the BFGS scheme of Hessian
119
+ approximation update. Recommended value: 3<=M<=7. The smaller
120
+ value causes worse convergence, the bigger will not cause a
121
+ considerably better convergence, but will cause a fall in the
122
+ performance. M<=N.
123
+ X - initial solution approximation, array[0..N-1].
124
+ EpsG - positive number which defines a precision of search. The
125
+ subroutine finishes its work if the condition ||G|| < EpsG is
126
+ satisfied, where ||.|| means Euclidian norm, G - gradient, X -
127
+ current approximation.
128
+ EpsF - positive number which defines a precision of search. The
129
+ subroutine finishes its work if on iteration number k+1 the
130
+ condition |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1} is
131
+ satisfied.
132
+ EpsX - positive number which defines a precision of search. The
133
+ subroutine finishes its work if on iteration number k+1 the
134
+ condition |X(k+1)-X(k)| <= EpsX is fulfilled.
135
+ MaxIts- maximum number of iterations. If MaxIts=0, the number of
136
+ iterations is unlimited.
137
+ Flags - additional settings:
138
+ * Flags = 0 means no additional settings
139
+ * Flags = 1 "do not allocate memory". used when solving
140
+ a many subsequent tasks with same N/M values.
141
+ First call MUST be without this flag bit set,
142
+ subsequent calls of MinLBFGS with same LBFGSState
143
+ structure can set Flags to 1.
144
+
145
+ Output parameters:
146
+ State - structure used for reverse communication.
147
+
148
+ See also MinLBFGSIteration, MinLBFGSResults
149
+
150
+ -- ALGLIB --
151
+ Copyright 14.11.2007 by Bochkanov Sergey
152
+ *************************************************************************/
153
+ void minlbfgs(const int& n,
154
+ const int& m,
155
+ ap::real_1d_array& x,
156
+ const double& epsg,
157
+ const double& epsf,
158
+ const double& epsx,
159
+ const int& maxits,
160
+ int flags,
161
+ lbfgsstate& state);
162
+
163
+
164
+ /*************************************************************************
165
+ One L-BFGS iteration
166
+
167
+ Called after initialization with MinLBFGS.
168
+ See HTML documentation for examples.
169
+
170
+ Input parameters:
171
+ State - structure which stores algorithm state between calls and
172
+ which is used for reverse communication. Must be initialized
173
+ with MinLBFGS.
174
+
175
+ If suborutine returned False, iterative proces has converged.
176
+
177
+ If subroutine returned True, caller should calculate function value
178
+ State.F an gradient State.G[0..N-1] at State.X[0..N-1] and call
179
+ MinLBFGSIteration again.
180
+
181
+ -- ALGLIB --
182
+ Copyright 20.04.2009 by Bochkanov Sergey
183
+ *************************************************************************/
184
+ bool minlbfgsiteration(lbfgsstate& state);
185
+
186
+
187
+ /*************************************************************************
188
+ L-BFGS algorithm results
189
+
190
+ Called after MinLBFGSIteration returned False.
191
+
192
+ Input parameters:
193
+ State - algorithm state (used by MinLBFGSIteration).
194
+
195
+ Output parameters:
196
+ X - array[0..N-1], solution
197
+ Rep - optimization report:
198
+ * Rep.TerminationType completetion code:
199
+ * -2 rounding errors prevent further improvement.
200
+ X contains best point found.
201
+ * -1 incorrect parameters were specified
202
+ * 1 relative function improvement is no more than
203
+ EpsF.
204
+ * 2 relative step is no more than EpsX.
205
+ * 4 gradient norm is no more than EpsG
206
+ * 5 MaxIts steps was taken
207
+ * Rep.IterationsCount contains iterations count
208
+ * NFEV countains number of function calculations
209
+
210
+ -- ALGLIB --
211
+ Copyright 14.11.2007 by Bochkanov Sergey
212
+ *************************************************************************/
213
+ void minlbfgsresults(const lbfgsstate& state,
214
+ ap::real_1d_array& x,
215
+ lbfgsreport& rep);
216
+
217
+
218
+ #endif
@@ -0,0 +1,434 @@
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 "lda.h"
35
+
36
+ /*************************************************************************
37
+ Multiclass Fisher LDA
38
+
39
+ Subroutine finds coefficients of linear combination which optimally separates
40
+ training set on classes.
41
+
42
+ INPUT PARAMETERS:
43
+ XY - training set, array[0..NPoints-1,0..NVars].
44
+ First NVars columns store values of independent
45
+ variables, next column stores number of class (from 0
46
+ to NClasses-1) which dataset element belongs to. Fractional
47
+ values are rounded to nearest integer.
48
+ NPoints - training set size, NPoints>=0
49
+ NVars - number of independent variables, NVars>=1
50
+ NClasses - number of classes, NClasses>=2
51
+
52
+
53
+ OUTPUT PARAMETERS:
54
+ Info - return code:
55
+ * -4, if internal EVD subroutine hasn't converged
56
+ * -2, if there is a point with class number
57
+ outside of [0..NClasses-1].
58
+ * -1, if incorrect parameters was passed (NPoints<0,
59
+ NVars<1, NClasses<2)
60
+ * 1, if task has been solved
61
+ * 2, if there was a multicollinearity in training set,
62
+ but task has been solved.
63
+ W - linear combination coefficients, array[0..NVars-1]
64
+
65
+ -- ALGLIB --
66
+ Copyright 31.05.2008 by Bochkanov Sergey
67
+ *************************************************************************/
68
+ void fisherlda(const ap::real_2d_array& xy,
69
+ int npoints,
70
+ int nvars,
71
+ int nclasses,
72
+ int& info,
73
+ ap::real_1d_array& w)
74
+ {
75
+ ap::real_2d_array w2;
76
+
77
+ fisherldan(xy, npoints, nvars, nclasses, info, w2);
78
+ if( info>0 )
79
+ {
80
+ w.setbounds(0, nvars-1);
81
+ ap::vmove(w.getvector(0, nvars-1), w2.getcolumn(0, 0, nvars-1));
82
+ }
83
+ }
84
+
85
+
86
+ /*************************************************************************
87
+ N-dimensional multiclass Fisher LDA
88
+
89
+ Subroutine finds coefficients of linear combinations which optimally separates
90
+ training set on classes. It returns N-dimensional basis whose vector are sorted
91
+ by quality of training set separation (in descending order).
92
+
93
+ INPUT PARAMETERS:
94
+ XY - training set, array[0..NPoints-1,0..NVars].
95
+ First NVars columns store values of independent
96
+ variables, next column stores number of class (from 0
97
+ to NClasses-1) which dataset element belongs to. Fractional
98
+ values are rounded to nearest integer.
99
+ NPoints - training set size, NPoints>=0
100
+ NVars - number of independent variables, NVars>=1
101
+ NClasses - number of classes, NClasses>=2
102
+
103
+
104
+ OUTPUT PARAMETERS:
105
+ Info - return code:
106
+ * -4, if internal EVD subroutine hasn't converged
107
+ * -2, if there is a point with class number
108
+ outside of [0..NClasses-1].
109
+ * -1, if incorrect parameters was passed (NPoints<0,
110
+ NVars<1, NClasses<2)
111
+ * 1, if task has been solved
112
+ * 2, if there was a multicollinearity in training set,
113
+ but task has been solved.
114
+ W - basis, array[0..NVars-1,0..NVars-1]
115
+ columns of matrix stores basis vectors, sorted by
116
+ quality of training set separation (in descending order)
117
+
118
+ -- ALGLIB --
119
+ Copyright 31.05.2008 by Bochkanov Sergey
120
+ *************************************************************************/
121
+ void fisherldan(const ap::real_2d_array& xy,
122
+ int npoints,
123
+ int nvars,
124
+ int nclasses,
125
+ int& info,
126
+ ap::real_2d_array& w)
127
+ {
128
+ int i;
129
+ int j;
130
+ int k;
131
+ int m;
132
+ double v;
133
+ ap::integer_1d_array c;
134
+ ap::real_1d_array mu;
135
+ ap::real_2d_array muc;
136
+ ap::integer_1d_array nc;
137
+ ap::real_2d_array sw;
138
+ ap::real_2d_array st;
139
+ ap::real_2d_array z;
140
+ ap::real_2d_array z2;
141
+ ap::real_2d_array tm;
142
+ ap::real_2d_array sbroot;
143
+ ap::real_2d_array a;
144
+ ap::real_2d_array xyproj;
145
+ ap::real_2d_array wproj;
146
+ ap::real_1d_array tf;
147
+ ap::real_1d_array d;
148
+ ap::real_1d_array d2;
149
+ ap::real_1d_array work;
150
+
151
+
152
+ //
153
+ // Test data
154
+ //
155
+ if( npoints<0||nvars<1||nclasses<2 )
156
+ {
157
+ info = -1;
158
+ return;
159
+ }
160
+ for(i = 0; i <= npoints-1; i++)
161
+ {
162
+ if( ap::round(xy(i,nvars))<0||ap::round(xy(i,nvars))>=nclasses )
163
+ {
164
+ info = -2;
165
+ return;
166
+ }
167
+ }
168
+ info = 1;
169
+
170
+ //
171
+ // Special case: NPoints<=1
172
+ // Degenerate task.
173
+ //
174
+ if( npoints<=1 )
175
+ {
176
+ info = 2;
177
+ w.setbounds(0, nvars-1, 0, nvars-1);
178
+ for(i = 0; i <= nvars-1; i++)
179
+ {
180
+ for(j = 0; j <= nvars-1; j++)
181
+ {
182
+ if( i==j )
183
+ {
184
+ w(i,j) = 1;
185
+ }
186
+ else
187
+ {
188
+ w(i,j) = 0;
189
+ }
190
+ }
191
+ }
192
+ return;
193
+ }
194
+
195
+ //
196
+ // Prepare temporaries
197
+ //
198
+ tf.setbounds(0, nvars-1);
199
+ work.setbounds(1, ap::maxint(nvars, npoints));
200
+
201
+ //
202
+ // Convert class labels from reals to integers (just for convenience)
203
+ //
204
+ c.setbounds(0, npoints-1);
205
+ for(i = 0; i <= npoints-1; i++)
206
+ {
207
+ c(i) = ap::round(xy(i,nvars));
208
+ }
209
+
210
+ //
211
+ // Calculate class sizes and means
212
+ //
213
+ mu.setbounds(0, nvars-1);
214
+ muc.setbounds(0, nclasses-1, 0, nvars-1);
215
+ nc.setbounds(0, nclasses-1);
216
+ for(j = 0; j <= nvars-1; j++)
217
+ {
218
+ mu(j) = 0;
219
+ }
220
+ for(i = 0; i <= nclasses-1; i++)
221
+ {
222
+ nc(i) = 0;
223
+ for(j = 0; j <= nvars-1; j++)
224
+ {
225
+ muc(i,j) = 0;
226
+ }
227
+ }
228
+ for(i = 0; i <= npoints-1; i++)
229
+ {
230
+ ap::vadd(&mu(0), &xy(i, 0), ap::vlen(0,nvars-1));
231
+ ap::vadd(&muc(c(i), 0), &xy(i, 0), ap::vlen(0,nvars-1));
232
+ nc(c(i)) = nc(c(i))+1;
233
+ }
234
+ for(i = 0; i <= nclasses-1; i++)
235
+ {
236
+ v = double(1)/double(nc(i));
237
+ ap::vmul(&muc(i, 0), ap::vlen(0,nvars-1), v);
238
+ }
239
+ v = double(1)/double(npoints);
240
+ ap::vmul(&mu(0), ap::vlen(0,nvars-1), v);
241
+
242
+ //
243
+ // Create ST matrix
244
+ //
245
+ st.setbounds(0, nvars-1, 0, nvars-1);
246
+ for(i = 0; i <= nvars-1; i++)
247
+ {
248
+ for(j = 0; j <= nvars-1; j++)
249
+ {
250
+ st(i,j) = 0;
251
+ }
252
+ }
253
+ for(k = 0; k <= npoints-1; k++)
254
+ {
255
+ ap::vmove(&tf(0), &xy(k, 0), ap::vlen(0,nvars-1));
256
+ ap::vsub(&tf(0), &mu(0), ap::vlen(0,nvars-1));
257
+ for(i = 0; i <= nvars-1; i++)
258
+ {
259
+ v = tf(i);
260
+ ap::vadd(&st(i, 0), &tf(0), ap::vlen(0,nvars-1), v);
261
+ }
262
+ }
263
+
264
+ //
265
+ // Create SW matrix
266
+ //
267
+ sw.setbounds(0, nvars-1, 0, nvars-1);
268
+ for(i = 0; i <= nvars-1; i++)
269
+ {
270
+ for(j = 0; j <= nvars-1; j++)
271
+ {
272
+ sw(i,j) = 0;
273
+ }
274
+ }
275
+ for(k = 0; k <= npoints-1; k++)
276
+ {
277
+ ap::vmove(&tf(0), &xy(k, 0), ap::vlen(0,nvars-1));
278
+ ap::vsub(&tf(0), &muc(c(k), 0), ap::vlen(0,nvars-1));
279
+ for(i = 0; i <= nvars-1; i++)
280
+ {
281
+ v = tf(i);
282
+ ap::vadd(&sw(i, 0), &tf(0), ap::vlen(0,nvars-1), v);
283
+ }
284
+ }
285
+
286
+ //
287
+ // Maximize ratio J=(w'*ST*w)/(w'*SW*w).
288
+ //
289
+ // First, make transition from w to v such that w'*ST*w becomes v'*v:
290
+ // v = root(ST)*w = R*w
291
+ // R = root(D)*Z'
292
+ // w = (root(ST)^-1)*v = RI*v
293
+ // RI = Z*inv(root(D))
294
+ // J = (v'*v)/(v'*(RI'*SW*RI)*v)
295
+ // ST = Z*D*Z'
296
+ //
297
+ // so we have
298
+ //
299
+ // J = (v'*v) / (v'*(inv(root(D))*Z'*SW*Z*inv(root(D)))*v) =
300
+ // = (v'*v) / (v'*A*v)
301
+ //
302
+ if( !smatrixevd(st, nvars, 1, true, d, z) )
303
+ {
304
+ info = -4;
305
+ return;
306
+ }
307
+ w.setbounds(0, nvars-1, 0, nvars-1);
308
+ if( d(nvars-1)<=0||d(0)<=1000*ap::machineepsilon*d(nvars-1) )
309
+ {
310
+
311
+ //
312
+ // Special case: D[NVars-1]<=0
313
+ // Degenerate task (all variables takes the same value).
314
+ //
315
+ if( d(nvars-1)<=0 )
316
+ {
317
+ info = 2;
318
+ for(i = 0; i <= nvars-1; i++)
319
+ {
320
+ for(j = 0; j <= nvars-1; j++)
321
+ {
322
+ if( i==j )
323
+ {
324
+ w(i,j) = 1;
325
+ }
326
+ else
327
+ {
328
+ w(i,j) = 0;
329
+ }
330
+ }
331
+ }
332
+ return;
333
+ }
334
+
335
+ //
336
+ // Special case: degenerate ST matrix, multicollinearity found.
337
+ // Since we know ST eigenvalues/vectors we can translate task to
338
+ // non-degenerate form.
339
+ //
340
+ // Let WG is orthogonal basis of the non zero variance subspace
341
+ // of the ST and let WZ is orthogonal basis of the zero variance
342
+ // subspace.
343
+ //
344
+ // Projection on WG allows us to use LDA on reduced M-dimensional
345
+ // subspace, N-M vectors of WZ allows us to update reduced LDA
346
+ // factors to full N-dimensional subspace.
347
+ //
348
+ m = 0;
349
+ for(k = 0; k <= nvars-1; k++)
350
+ {
351
+ if( d(k)<=1000*ap::machineepsilon*d(nvars-1) )
352
+ {
353
+ m = k+1;
354
+ }
355
+ }
356
+ ap::ap_error::make_assertion(m!=0, "FisherLDAN: internal error #1");
357
+ xyproj.setbounds(0, npoints-1, 0, nvars-m);
358
+ matrixmatrixmultiply(xy, 0, npoints-1, 0, nvars-1, false, z, 0, nvars-1, m, nvars-1, false, 1.0, xyproj, 0, npoints-1, 0, nvars-m-1, 0.0, work);
359
+ for(i = 0; i <= npoints-1; i++)
360
+ {
361
+ xyproj(i,nvars-m) = xy(i,nvars);
362
+ }
363
+ fisherldan(xyproj, npoints, nvars-m, nclasses, info, wproj);
364
+ if( info<0 )
365
+ {
366
+ return;
367
+ }
368
+ matrixmatrixmultiply(z, 0, nvars-1, m, nvars-1, false, wproj, 0, nvars-m-1, 0, nvars-m-1, false, 1.0, w, 0, nvars-1, 0, nvars-m-1, 0.0, work);
369
+ for(k = nvars-m; k <= nvars-1; k++)
370
+ {
371
+ ap::vmove(w.getcolumn(k, 0, nvars-1), z.getcolumn(k-(nvars-m), 0, nvars-1));
372
+ }
373
+ info = 2;
374
+ }
375
+ else
376
+ {
377
+
378
+ //
379
+ // General case: no multicollinearity
380
+ //
381
+ tm.setbounds(0, nvars-1, 0, nvars-1);
382
+ a.setbounds(0, nvars-1, 0, nvars-1);
383
+ matrixmatrixmultiply(sw, 0, nvars-1, 0, nvars-1, false, z, 0, nvars-1, 0, nvars-1, false, 1.0, tm, 0, nvars-1, 0, nvars-1, 0.0, work);
384
+ matrixmatrixmultiply(z, 0, nvars-1, 0, nvars-1, true, tm, 0, nvars-1, 0, nvars-1, false, 1.0, a, 0, nvars-1, 0, nvars-1, 0.0, work);
385
+ for(i = 0; i <= nvars-1; i++)
386
+ {
387
+ for(j = 0; j <= nvars-1; j++)
388
+ {
389
+ a(i,j) = a(i,j)/sqrt(d(i)*d(j));
390
+ }
391
+ }
392
+ if( !smatrixevd(a, nvars, 1, true, d2, z2) )
393
+ {
394
+ info = -4;
395
+ return;
396
+ }
397
+ for(k = 0; k <= nvars-1; k++)
398
+ {
399
+ for(i = 0; i <= nvars-1; i++)
400
+ {
401
+ tf(i) = z2(i,k)/sqrt(d(i));
402
+ }
403
+ for(i = 0; i <= nvars-1; i++)
404
+ {
405
+ v = ap::vdotproduct(&z(i, 0), &tf(0), ap::vlen(0,nvars-1));
406
+ w(i,k) = v;
407
+ }
408
+ }
409
+ }
410
+
411
+ //
412
+ // Post-processing:
413
+ // * normalization
414
+ // * converting to non-negative form, if possible
415
+ //
416
+ for(k = 0; k <= nvars-1; k++)
417
+ {
418
+ v = ap::vdotproduct(w.getcolumn(k, 0, nvars-1), w.getcolumn(k, 0, nvars-1));
419
+ v = 1/sqrt(v);
420
+ ap::vmul(w.getcolumn(k, 0, nvars-1), v);
421
+ v = 0;
422
+ for(i = 0; i <= nvars-1; i++)
423
+ {
424
+ v = v+w(i,k);
425
+ }
426
+ if( v<0 )
427
+ {
428
+ ap::vmul(w.getcolumn(k, 0, nvars-1), -1);
429
+ }
430
+ }
431
+ }
432
+
433
+
434
+