File: Nonlinear-Least_002dSquares-Large-Example.html

package info (click to toggle)
gsl-ref-html 2.3-1
  • links: PTS
  • area: non-free
  • in suites: bookworm, buster, sid, trixie
  • size: 6,876 kB
  • ctags: 4,574
  • sloc: makefile: 35
file content (361 lines) | stat: -rw-r--r-- 13,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 The GSL Team.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Free Software
Needs Free Documentation", the Front-Cover text being "A GNU Manual",
and with the Back-Cover Text being (a) (see below). A copy of the
license is included in the section entitled "GNU Free Documentation
License".

(a) The Back-Cover Text is: "You have the freedom to copy and modify this
GNU Manual." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Scientific Library &ndash; Reference Manual: Nonlinear Least-Squares Large Example</title>

<meta name="description" content="GNU Scientific Library &ndash; Reference Manual: Nonlinear Least-Squares Large Example">
<meta name="keywords" content="GNU Scientific Library &ndash; Reference Manual: Nonlinear Least-Squares Large Example">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Function-Index.html#Function-Index" rel="index" title="Function Index">
<link href="Nonlinear-Least_002dSquares-Examples.html#Nonlinear-Least_002dSquares-Examples" rel="up" title="Nonlinear Least-Squares Examples">
<link href="Nonlinear-Least_002dSquares-References-and-Further-Reading.html#Nonlinear-Least_002dSquares-References-and-Further-Reading" rel="next" title="Nonlinear Least-Squares References and Further Reading">
<link href="Nonlinear-Least_002dSquares-Comparison-Example.html#Nonlinear-Least_002dSquares-Comparison-Example" rel="previous" title="Nonlinear Least-Squares Comparison Example">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Nonlinear-Least_002dSquares-Large-Example"></a>
<div class="header">
<p>
Previous: <a href="Nonlinear-Least_002dSquares-Comparison-Example.html#Nonlinear-Least_002dSquares-Comparison-Example" accesskey="p" rel="previous">Nonlinear Least-Squares Comparison Example</a>, Up: <a href="Nonlinear-Least_002dSquares-Examples.html#Nonlinear-Least_002dSquares-Examples" accesskey="u" rel="up">Nonlinear Least-Squares Examples</a> &nbsp; [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Large-Nonlinear-Least-Squares-Example"></a>
<h4 class="subsection">39.12.4 Large Nonlinear Least Squares Example</h4>

<p>The following program illustrates the large nonlinear least
squares solvers on a system with significant sparse structure
in the Jacobian. The cost function is given by
</p>
<div class="example">
<pre class="example">\Phi(x) &amp;= 1/2 \sum_{i=1}^{p+1} f_i^2
f_i &amp;= \sqrt{\alpha} (x_i - 1), 1 \le i \le p
f_{p+1} &amp;= ||x||^2 - 1/4
</pre></div>

<p>with <em>\alpha = 10^{-5}</em>. The residual <em>f_{p+1}</em> imposes a constraint on the <em>p</em>
parameters <em>x</em>, to ensure that <em>||x||^2 \approx {1 \over 4}</em>.
The <em>(p+1)</em>-by-<em>p</em> Jacobian for this system is given by
</p>
<div class="example">
<pre class="example">J(x) = [ \sqrt{alpha} I_p; 2 x^T ]
</pre></div>

<p>and the normal equations matrix is given by
</p>
<div class="example">
<pre class="example">J^T J = [ \alpha I_p + 4 x x^T ]
</pre></div>

<p>Finally, the second directional derivative of <em>f</em> for the
geodesic acceleration method is given by
</p>
<div class="example">
<pre class="example">fvv = [ 0; 2 ||v||^2 ]
</pre></div>

<p>Since the upper <em>p</em>-by-<em>p</em> block of <em>J</em> is diagonal,
this sparse structure should be exploited in the nonlinear solver.
For comparison, the following program solves the system for <em>p = 2000</em>
using the dense direct Cholesky solver based on the normal equations matrix
<em>J^T J</em>, as well as the iterative Steihaug-Toint solver, based on
sparse matrix-vector products <em>J u</em> and <em>J^T u</em>. The
program output is shown below.
</p>
<div class="smallformat">
<pre class="verbatim">Method                    NITER NFEV NJUEV NJTJEV NAEV Init Cost  Final cost cond(J) Final |x|^2 Time (s)  
levenberg-marquardt       25    31   26    26     0    7.1218e+18 1.9555e-02 447.50  2.5044e-01  46.28
levenberg-marquardt+accel 22    23   45    23     22   7.1218e+18 1.9555e-02 447.64  2.5044e-01  33.92
dogleg                    37    87   36    36     0    7.1218e+18 1.9555e-02 447.59  2.5044e-01  56.05
double-dogleg             35    88   34    34     0    7.1218e+18 1.9555e-02 447.62  2.5044e-01  52.65
2D-subspace               37    88   36    36     0    7.1218e+18 1.9555e-02 447.71  2.5044e-01  59.75
steihaug-toint            35    88   345   0      0    7.1218e+18 1.9555e-02 inf     2.5044e-01  0.09
</pre></div>

<p>The first five rows use methods based on factoring the dense <em>J^T J</em> matrix
while the last row uses the iterative Steihaug-Toint method. While the number
of Jacobian matrix-vector products (NJUEV) is less for the dense methods, the added time
to construct and factor the <em>J^T J</em> matrix (NJTJEV) results in a much larger runtime than the
iterative method (see last column).
</p>
<p>The program is given below.
</p>
<div class="example">
<pre class="verbatim">#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;sys/time.h&gt;
#include &lt;gsl/gsl_vector.h&gt;
#include &lt;gsl/gsl_matrix.h&gt;
#include &lt;gsl/gsl_blas.h&gt;
#include &lt;gsl/gsl_multilarge_nlinear.h&gt;
#include &lt;gsl/gsl_spblas.h&gt;
#include &lt;gsl/gsl_spmatrix.h&gt;

/* parameters for functions */
struct model_params
{
  double alpha;
  gsl_spmatrix *J;
};

/* penalty function */
int
penalty_f (const gsl_vector * x, void *params, gsl_vector * f)
{
  struct model_params *par = (struct model_params *) params;
  const double sqrt_alpha = sqrt(par-&gt;alpha);
  const size_t p = x-&gt;size;
  size_t i;
  double sum = 0.0;

  for (i = 0; i &lt; p; ++i)
    {
      double xi = gsl_vector_get(x, i);

      gsl_vector_set(f, i, sqrt_alpha*(xi - 1.0));

      sum += xi * xi;
    }

  gsl_vector_set(f, p, sum - 0.25);

  return GSL_SUCCESS;
}

int
penalty_df (CBLAS_TRANSPOSE_t TransJ, const gsl_vector * x,
            const gsl_vector * u, void * params, gsl_vector * v,
            gsl_matrix * JTJ)
{
  struct model_params *par = (struct model_params *) params;
  const size_t p = x-&gt;size;
  size_t j;

  /* store 2*x in last row of J */
  for (j = 0; j &lt; p; ++j)
    {
      double xj = gsl_vector_get(x, j);
      gsl_spmatrix_set(par-&gt;J, p, j, 2.0 * xj);
    }

  /* compute v = op(J) u */
  if (v)
    gsl_spblas_dgemv(TransJ, 1.0, par-&gt;J, u, 0.0, v);

  if (JTJ)
    {
      gsl_vector_view diag = gsl_matrix_diagonal(JTJ);

      /* compute J^T J = [ alpha*I_p + 4 x x^T ] */
      gsl_matrix_set_zero(JTJ);

      /* store 4 x x^T in lower half of JTJ */
      gsl_blas_dsyr(CblasLower, 4.0, x, JTJ);

      /* add alpha to diag(JTJ) */
      gsl_vector_add_constant(&amp;diag.vector, par-&gt;alpha);
    }

  return GSL_SUCCESS;
}

int
penalty_fvv (const gsl_vector * x, const gsl_vector * v,
             void *params, gsl_vector * fvv)
{
  const size_t p = x-&gt;size;
  double normv = gsl_blas_dnrm2(v);

  gsl_vector_set_zero(fvv);
  gsl_vector_set(fvv, p, 2.0 * normv * normv);

  (void)params; /* avoid unused parameter warning */

  return GSL_SUCCESS;
}

void
solve_system(const gsl_vector *x0, gsl_multilarge_nlinear_fdf *fdf,
             gsl_multilarge_nlinear_parameters *params)
{
  const gsl_multilarge_nlinear_type *T = gsl_multilarge_nlinear_trust;
  const size_t max_iter = 200;
  const double xtol = 1.0e-8;
  const double gtol = 1.0e-8;
  const double ftol = 1.0e-8;
  const size_t n = fdf-&gt;n;
  const size_t p = fdf-&gt;p;
  gsl_multilarge_nlinear_workspace *work =
    gsl_multilarge_nlinear_alloc(T, params, n, p);
  gsl_vector * f = gsl_multilarge_nlinear_residual(work);
  gsl_vector * x = gsl_multilarge_nlinear_position(work);
  int info;
  double chisq0, chisq, rcond, xsq;
  struct timeval tv0, tv1;

  gettimeofday(&amp;tv0, NULL);

  /* initialize solver */
  gsl_multilarge_nlinear_init(x0, fdf, work);

  /* store initial cost */
  gsl_blas_ddot(f, f, &amp;chisq0);

  /* iterate until convergence */
  gsl_multilarge_nlinear_driver(max_iter, xtol, gtol, ftol,
                                NULL, NULL, &amp;info, work);

  gettimeofday(&amp;tv1, NULL);

  /* store final cost */
  gsl_blas_ddot(f, f, &amp;chisq);

  /* compute final ||x||^2 */
  gsl_blas_ddot(x, x, &amp;xsq);

  /* store cond(J(x)) */
  gsl_multilarge_nlinear_rcond(&amp;rcond, work);

  /* print summary */
  fprintf(stderr, &quot;%-25s %-5zu %-4zu %-5zu %-6zu %-4zu %-10.4e %-10.4e %-7.2f %-11.4e %.2f\n&quot;,
          gsl_multilarge_nlinear_trs_name(work),
          gsl_multilarge_nlinear_niter(work),
          fdf-&gt;nevalf,
          fdf-&gt;nevaldfu,
          fdf-&gt;nevaldf2,
          fdf-&gt;nevalfvv,
          chisq0,
          chisq,
          1.0 / rcond,
          xsq,
          (tv1.tv_sec - tv0.tv_sec) + 1.0e-6 * (tv1.tv_usec - tv0.tv_usec));

  gsl_multilarge_nlinear_free(work);
}

int
main (void)
{
  const size_t p = 2000;
  const size_t n = p + 1;
  gsl_vector *f = gsl_vector_alloc(n);
  gsl_vector *x = gsl_vector_alloc(p);

  /* allocate sparse Jacobian matrix with 2*p non-zero elements in triplet format */
  gsl_spmatrix *J = gsl_spmatrix_alloc_nzmax(n, p, 2 * p, GSL_SPMATRIX_TRIPLET);

  gsl_multilarge_nlinear_fdf fdf;
  gsl_multilarge_nlinear_parameters fdf_params =
    gsl_multilarge_nlinear_default_parameters();
  struct model_params params;
  size_t i;

  params.alpha = 1.0e-5;
  params.J = J;

  /* define function to be minimized */
  fdf.f = penalty_f;
  fdf.df = penalty_df;
  fdf.fvv = penalty_fvv;
  fdf.n = n;
  fdf.p = p;
  fdf.params = &amp;params;

  for (i = 0; i &lt; p; ++i)
    {
      /* starting point */
      gsl_vector_set(x, i, i + 1.0);

      /* store sqrt(alpha)*I_p in upper p-by-p block of J */
      gsl_spmatrix_set(J, i, i, sqrt(params.alpha));
    }

  fprintf(stderr, &quot;%-25s %-4s %-4s %-5s %-6s %-4s %-10s %-10s %-7s %-11s %-10s\n&quot;,
          &quot;Method&quot;, &quot;NITER&quot;, &quot;NFEV&quot;, &quot;NJUEV&quot;, &quot;NJTJEV&quot;, &quot;NAEV&quot;, &quot;Init Cost&quot;,
          &quot;Final cost&quot;, &quot;cond(J)&quot;, &quot;Final |x|^2&quot;, &quot;Time (s)&quot;);
  
  fdf_params.scale = gsl_multilarge_nlinear_scale_levenberg;

  fdf_params.trs = gsl_multilarge_nlinear_trs_lm;
  solve_system(x, &amp;fdf, &amp;fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_lmaccel;
  solve_system(x, &amp;fdf, &amp;fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_dogleg;
  solve_system(x, &amp;fdf, &amp;fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_ddogleg;
  solve_system(x, &amp;fdf, &amp;fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_subspace2D;
  solve_system(x, &amp;fdf, &amp;fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_cgst;
  solve_system(x, &amp;fdf, &amp;fdf_params);

  gsl_vector_free(f);
  gsl_vector_free(x);
  gsl_spmatrix_free(J);

  return 0;
}
</pre></div>

<hr>
<div class="header">
<p>
Previous: <a href="Nonlinear-Least_002dSquares-Comparison-Example.html#Nonlinear-Least_002dSquares-Comparison-Example" accesskey="p" rel="previous">Nonlinear Least-Squares Comparison Example</a>, Up: <a href="Nonlinear-Least_002dSquares-Examples.html#Nonlinear-Least_002dSquares-Examples" accesskey="u" rel="up">Nonlinear Least-Squares Examples</a> &nbsp; [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>