summaryrefslogtreecommitdiffstats
path: root/include/2geom/numeric/fitting-tool.h
blob: 78d66ca0261c12c75c86dd0b67ff2722da7da07e (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Fitting Tools
 *
 * Authors:
 *      Marco Cecchetti <mrcekets at gmail.com>
 *
 * Copyright 2008  authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */


#ifndef _NL_FITTING_TOOL_H_
#define _NL_FITTING_TOOL_H_


#include <2geom/numeric/vector.h>
#include <2geom/numeric/matrix.h>

#include <2geom/point.h>

#include <vector>


/*
 *  The least_square_fitter class represents a tool for solving a fitting
 *  problem with respect to a given model that represents an expression
 *  dependent from a parameter where the coefficients of this expression
 *  are the unknowns of the fitting problem.
 *  The minimizing solution is found by computing the pseudo-inverse
 *  of the problem matrix
 */


namespace Geom { namespace NL {

namespace detail {

template< typename ModelT>
class lsf_base
{
  public:
    typedef ModelT                                   model_type;
    typedef typename model_type::parameter_type      parameter_type;
    typedef typename model_type::value_type          value_type;

    lsf_base( model_type const& _model, size_t forecasted_samples )
        : m_model(_model),
          m_total_samples(0),
          m_matrix(forecasted_samples, m_model.size()),
          m_psdinv_matrix(NULL)
    {
    }

    // compute pseudo inverse
    void update()
    {
        if (total_samples() == 0) return;
        if (m_psdinv_matrix != NULL)
        {
            delete m_psdinv_matrix;
        }
        MatrixView mv(m_matrix, 0, 0, total_samples(), m_matrix.columns());
        m_psdinv_matrix = new Matrix( pseudo_inverse(mv) );
        assert(m_psdinv_matrix != NULL);
    }

    size_t total_samples() const
    {
        return m_total_samples;
    }

    bool is_full() const
    {
        return (total_samples() == m_matrix.rows());
    }

    void clear()
    {
        m_total_samples = 0;
    }

    virtual
    ~lsf_base()
    {
        if (m_psdinv_matrix != NULL)
        {
            delete m_psdinv_matrix;
        }
    }

  protected:
    const model_type &      m_model;
    size_t                  m_total_samples;
    Matrix                  m_matrix;
    Matrix*                 m_psdinv_matrix;

};  // end class lsf_base




template< typename ModelT, typename ValueType = typename ModelT::value_type>
class lsf_solution
{
};

// a fitting process on samples with value of type double
// produces a solution of type Vector
template< typename ModelT>
class lsf_solution<ModelT, double>
    : public lsf_base<ModelT>
{
public:
    typedef ModelT                                  model_type;
    typedef typename model_type::parameter_type     parameter_type;
    typedef typename model_type::value_type         value_type;
    typedef Vector                                  solution_type;
    typedef lsf_base<model_type>                    base_type;

    using base_type::m_model;
    using base_type::m_psdinv_matrix;
    using base_type::total_samples;

public:
    lsf_solution<ModelT, double>( model_type const& _model,
                                  size_t forecasted_samples )
        : base_type(_model, forecasted_samples),
          m_solution(_model.size())
    {
    }

    template< typename VectorT >
    solution_type& result(VectorT const& sample_values)
    {
        assert(sample_values.size() == total_samples());
        ConstVectorView sv(sample_values);
        m_solution = (*m_psdinv_matrix) * sv;
        return m_solution;
    }

    // a comparison between old sample values and the new ones is performed
    // in order to minimize computation
    // prerequisite:
    //     old_sample_values.size() == new_sample_values.size()
    //     no update() call can be performed between two result invocations
    template< typename VectorT >
    solution_type& result( VectorT const& old_sample_values,
                           VectorT const& new_sample_values )
    {
        assert(old_sample_values.size() == total_samples());
        assert(new_sample_values.size() == total_samples());
        Vector diff(total_samples());
        for (size_t i = 0; i < diff.size(); ++i)
        {
            diff[i] = new_sample_values[i] - old_sample_values[i];
        }
        Vector column(m_model.size());
        Vector delta(m_model.size(), 0.0);
        for (size_t i = 0; i < diff.size(); ++i)
        {
            if (diff[i] != 0)
            {
                column = m_psdinv_matrix->column_view(i);
                column.scale(diff[i]);
                delta += column;
            }
        }
        m_solution += delta;
        return m_solution;
    }

    solution_type& result()
    {
        return m_solution;
    }

private:
    solution_type m_solution;

}; // end class lsf_solution<ModelT, double>


// a fitting process on samples with value of type Point
// produces a solution of type Matrix (with 2 columns)
template< typename ModelT>
class lsf_solution<ModelT, Point>
    : public lsf_base<ModelT>
{
public:
    typedef ModelT                                  model_type;
    typedef typename model_type::parameter_type     parameter_type;
    typedef typename model_type::value_type         value_type;
    typedef Matrix                                  solution_type;
    typedef lsf_base<model_type>                    base_type;

    using base_type::m_model;
    using base_type::m_psdinv_matrix;
    using base_type::total_samples;

public:
    lsf_solution<ModelT, Point>( model_type const& _model,
                                 size_t forecasted_samples )
        : base_type(_model, forecasted_samples),
          m_solution(_model.size(), 2)
    {
    }

    solution_type& result(std::vector<Point> const& sample_values)
    {
        assert(sample_values.size() == total_samples());
        Matrix svm(total_samples(), 2);
        for (size_t i = 0; i < total_samples(); ++i)
        {
            svm(i, X) = sample_values[i][X];
            svm(i, Y) = sample_values[i][Y];
        }
        m_solution = (*m_psdinv_matrix) * svm;
        return m_solution;
    }

    // a comparison between old sample values and the new ones is performed
    // in order to minimize computation
    // prerequisite:
    //     old_sample_values.size() == new_sample_values.size()
    //     no update() call can to be performed between two result invocations
    solution_type& result( std::vector<Point> const& old_sample_values,
                           std::vector<Point> const& new_sample_values )
    {
        assert(old_sample_values.size() == total_samples());
        assert(new_sample_values.size() == total_samples());
        Matrix diff(total_samples(), 2);
        for (size_t i = 0; i < total_samples(); ++i)
        {
            diff(i, X) =  new_sample_values[i][X] - old_sample_values[i][X];
            diff(i, Y) =  new_sample_values[i][Y] - old_sample_values[i][Y];
        }
        Vector column(m_model.size());
        Matrix delta(m_model.size(), 2, 0.0);
        VectorView deltax = delta.column_view(X);
        VectorView deltay = delta.column_view(Y);
        for (size_t i = 0; i < total_samples(); ++i)
        {
            if (diff(i, X) != 0)
            {
                column = m_psdinv_matrix->column_view(i);
                column.scale(diff(i, X));
                deltax += column;
            }
            if (diff(i, Y) != 0)
            {
                column = m_psdinv_matrix->column_view(i);
                column.scale(diff(i, Y));
                deltay += column;
            }
        }
        m_solution += delta;
        return m_solution;
    }

    solution_type& result()
    {
        return m_solution;
    }

private:
    solution_type m_solution;

}; // end class lsf_solution<ModelT, Point>




template< typename ModelT,
          bool WITH_FIXED_TERMS = ModelT::WITH_FIXED_TERMS >
class lsf_with_fixed_terms
{
};


// fitting tool for completely unknown models
template< typename ModelT>
class lsf_with_fixed_terms<ModelT, false>
    : public lsf_solution<ModelT>
{
  public:
    typedef ModelT                                      model_type;
    typedef typename model_type::parameter_type         parameter_type;
    typedef typename model_type::value_type             value_type;
    typedef lsf_solution<model_type>                    base_type;
    typedef typename base_type::solution_type           solution_type;

    using base_type::total_samples;
    using base_type::is_full;
    using base_type::m_matrix;
    using base_type::m_total_samples;
    using base_type::m_model;

  public:
      lsf_with_fixed_terms<ModelT, false>( model_type const& _model,
                                           size_t forecasted_samples )
        : base_type(_model, forecasted_samples)
    {
    }

    void append(parameter_type const& sample_parameter)
    {
        assert(!is_full());
        VectorView row = m_matrix.row_view(total_samples());
        m_model.feed(row, sample_parameter);
        ++m_total_samples;
    }

    void append_copy(size_t sample_index)
    {
        assert(!is_full());
        assert(sample_index < total_samples());
        VectorView dest_row = m_matrix.row_view(total_samples());
        VectorView source_row = m_matrix.row_view(sample_index);
        dest_row = source_row;
        ++m_total_samples;
    }

}; // end class lsf_with_fixed_terms<ModelT, false>


// fitting tool for partially known models
template< typename ModelT>
class lsf_with_fixed_terms<ModelT, true>
    : public lsf_solution<ModelT>
{
  public:
    typedef ModelT                                      model_type;
    typedef typename model_type::parameter_type         parameter_type;
    typedef typename model_type::value_type             value_type;
    typedef lsf_solution<model_type>                    base_type;
    typedef typename base_type::solution_type           solution_type;

    using base_type::total_samples;
    using base_type::is_full;
    using base_type::m_matrix;
    using base_type::m_total_samples;
    using base_type::m_model;

  public:
    lsf_with_fixed_terms<ModelT, true>( model_type const& _model,
                                        size_t forecasted_samples )
        : base_type(_model, forecasted_samples),
          m_vector(forecasted_samples),
          m_vector_view(NULL)
    {
    }
    void append(parameter_type const& sample_parameter)
    {
        assert(!is_full());
        VectorView row = m_matrix.row_view(total_samples());
        m_model.feed(row, m_vector[total_samples()], sample_parameter);
        ++m_total_samples;
    }

    void append_copy(size_t sample_index)
    {
        assert(!is_full());
        assert(sample_index < total_samples());
        VectorView dest_row = m_matrix.row_view(total_samples());
        VectorView source_row = m_matrix.row_view(sample_index);
        dest_row = source_row;
        m_vector[total_samples()] = m_vector[sample_index];
        ++m_total_samples;
    }

    void update()
    {
        base_type::update();
        if (total_samples() == 0) return;
        if (m_vector_view != NULL)
        {
            delete m_vector_view;
        }
        m_vector_view = new VectorView(m_vector, base_type::total_samples());
        assert(m_vector_view != NULL);
    }

    
    ~lsf_with_fixed_terms<model_type, true>() override
    {
        if (m_vector_view != NULL)
        {
            delete m_vector_view;
        }
    }

  protected:
    Vector          m_vector;
    VectorView*     m_vector_view;

}; // end class lsf_with_fixed_terms<ModelT, true>


} // end namespace detail




template< typename ModelT,
          typename ValueType = typename ModelT::value_type,
          bool WITH_FIXED_TERMS = ModelT::WITH_FIXED_TERMS >
class least_squeares_fitter
{
};


template< typename ModelT, typename ValueType >
class least_squeares_fitter<ModelT, ValueType, false>
    : public detail::lsf_with_fixed_terms<ModelT>
{
  public:
    typedef ModelT                                      model_type;
    typedef detail::lsf_with_fixed_terms<model_type>    base_type;
    typedef typename base_type::parameter_type          parameter_type;
    typedef typename base_type::value_type              value_type;
    typedef typename base_type::solution_type           solution_type;

  public:
    least_squeares_fitter<ModelT, ValueType, false>( model_type const& _model,
                                                     size_t forecasted_samples )
          : base_type(_model, forecasted_samples)
  {
  }
}; // end class least_squeares_fitter<ModelT, ValueType, true>


template< typename ModelT>
class least_squeares_fitter<ModelT, double, true>
    : public detail::lsf_with_fixed_terms<ModelT>
{
  public:
    typedef ModelT                                      model_type;
    typedef detail::lsf_with_fixed_terms<model_type>    base_type;
    typedef typename base_type::parameter_type          parameter_type;
    typedef typename base_type::value_type              value_type;
    typedef typename base_type::solution_type           solution_type;

    using base_type::m_vector_view;
    //using base_type::result; // VSC legacy support
    solution_type& result( std::vector<Point> const& old_sample_values,
                           std::vector<Point> const& new_sample_values )
    {
        return base_type::result(old_sample_values, new_sample_values);
    }
 
    solution_type& result()
    {
        return base_type::result();
    }

  public:
    least_squeares_fitter<ModelT, double, true>( model_type const& _model,
                                                 size_t forecasted_samples )
        : base_type(_model, forecasted_samples)
    {
    }

    template< typename VectorT >
    solution_type& result(VectorT const& sample_values)
    {
        assert(sample_values.size() == m_vector_view->size());
        Vector sv(sample_values.size());
        for (size_t i = 0; i < sv.size(); ++i)
            sv[i] = sample_values[i] - (*m_vector_view)[i];
        return base_type::result(sv);
    }

}; // end class least_squeares_fitter<ModelT, double, true>


template< typename ModelT>
class least_squeares_fitter<ModelT, Point, true>
    : public detail::lsf_with_fixed_terms<ModelT>
{
  public:
    typedef ModelT                                      model_type;
    typedef detail::lsf_with_fixed_terms<model_type>    base_type;
    typedef typename base_type::parameter_type          parameter_type;
    typedef typename base_type::value_type              value_type;
    typedef typename base_type::solution_type           solution_type;

    using base_type::m_vector_view;
    //using base_type::result; // VCS legacy support
    solution_type& result( std::vector<Point> const& old_sample_values,
                           std::vector<Point> const& new_sample_values )
    {
        return base_type::result(old_sample_values, new_sample_values);
    }
 
    solution_type& result()
    {
        return base_type::result();
    }


  public:
    least_squeares_fitter<ModelT, Point, true>( model_type const& _model,
                                                size_t forecasted_samples )
        : base_type(_model, forecasted_samples)
    {
    }

    solution_type& result(std::vector<Point> const& sample_values)
    {
        assert(sample_values.size() == m_vector_view->size());
        NL::Matrix sv(sample_values.size(), 2);
        for (size_t i = 0; i < sample_values.size(); ++i)
        {
            sv(i, X) = sample_values[i][X] - (*m_vector_view)[i];
            sv(i, Y) = sample_values[i][Y] - (*m_vector_view)[i];
        }
        return base_type::result(sv);
    }

};  // end class least_squeares_fitter<ModelT, Point, true>


}  // end namespace NL
}  // end namespace Geom



#endif  // _NL_FITTING_TOOL_H_


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :