lsq_solver.dart 5.4 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:math' as math;
import 'dart:typed_data';
7

8
// TODO(abarth): Consider using vector_math.
9
class _Vector {
10
  _Vector(int size) : _offset = 0, _length = size, _elements = new Float64List(size);
11

12
  _Vector.fromVOL(List<double> values, int offset, int length)
13
    : _offset = offset, _length = length, _elements = values;
14

Ian Hickson's avatar
Ian Hickson committed
15 16 17 18 19
  final int _offset;

  final int _length;

  final List<double> _elements;
20

Ian Hickson's avatar
Ian Hickson committed
21 22 23 24
  double operator [](int i) => _elements[i + _offset];
  void operator []=(int i, double value) {
    _elements[i + _offset] = value;
  }
25

Ian Hickson's avatar
Ian Hickson committed
26
  double operator *(_Vector a) {
27
    double result = 0.0;
Ian Hickson's avatar
Ian Hickson committed
28
    for (int i = 0; i < _length; i += 1)
29 30 31 32 33 34 35
      result += this[i] * a[i];
    return result;
  }

  double norm() => math.sqrt(this * this);
}

36
// TODO(abarth): Consider using vector_math.
37 38
class _Matrix {
  _Matrix(int rows, int cols)
39
  : _columns = cols,
40
    _elements = new Float64List(rows * cols);
41

Ian Hickson's avatar
Ian Hickson committed
42 43 44
  final int _columns;
  final List<double> _elements;

45
  double get(int row, int col) => _elements[row * _columns + col];
46
  void set(int row, int col, double value) {
47
    _elements[row * _columns + col] = value;
48 49
  }

50 51 52 53 54
  _Vector getRow(int row) => new _Vector.fromVOL(
    _elements,
    row * _columns,
    _columns
  );
55 56
}

57
/// An nth degree polynomial fit to a dataset.
58
class PolynomialFit {
59 60 61
  /// Creates a polynomial fit of the given degree.
  ///
  /// There are n + 1 coefficients in a fit of degree n.
62 63
  PolynomialFit(int degree) : coefficients = new Float64List(degree + 1);

64
  /// The polynomial coefficients of the fit.
65
  final List<double> coefficients;
66 67 68 69

  /// An indicator of the quality of the fit.
  ///
  /// Larger values indicate greater quality.
70 71 72
  double confidence;
}

73
/// Uses the least-squares algorithm to fit a polynomial to a set of data.
74
class LeastSquaresSolver {
75 76
  /// Creates a least-squares solver.
  ///
77
  /// The [x], [y], and [w] arguments must not be null.
78 79 80
  LeastSquaresSolver(this.x, this.y, this.w)
    : assert(x.length == y.length),
      assert(y.length == w.length);
81

82
  /// The x-coordinates of each data point.
83
  final List<double> x;
84 85

  /// The y-coordinates of each data point.
86
  final List<double> y;
87 88

  /// The weight to use for each data point.
89 90
  final List<double> w;

91
  /// Fits a polynomial of the given degree to the data points.
92
  PolynomialFit solve(int degree) {
Florian Loitsch's avatar
Florian Loitsch committed
93
    if (degree > x.length) // Not enough data to fit a curve.
94 95
      return null;

96
    final PolynomialFit result = new PolynomialFit(degree);
97

Florian Loitsch's avatar
Florian Loitsch committed
98
    // Shorthands for the purpose of notation equivalence to original C++ code.
99 100 101 102
    final int m = x.length;
    final int n = degree + 1;

    // Expand the X vector to a matrix A, pre-multiplied by the weights.
103
    final _Matrix a = new _Matrix(n, m);
Ian Hickson's avatar
Ian Hickson committed
104
    for (int h = 0; h < m; h += 1) {
105
      a.set(0, h, w[h]);
Ian Hickson's avatar
Ian Hickson committed
106
      for (int i = 1; i < n; i += 1)
107 108 109 110 111 112
        a.set(i, h, a.get(i - 1, h) * x[h]);
    }

    // Apply the Gram-Schmidt process to A to obtain its QR decomposition.

    // Orthonormal basis, column-major ordVectorer.
113
    final _Matrix q = new _Matrix(n, m);
114
    // Upper triangular matrix, row-major order.
115
    final _Matrix r = new _Matrix(n, n);
Ian Hickson's avatar
Ian Hickson committed
116 117
    for (int j = 0; j < n; j += 1) {
      for (int h = 0; h < m; h += 1)
118
        q.set(j, h, a.get(j, h));
Ian Hickson's avatar
Ian Hickson committed
119
      for (int i = 0; i < j; i += 1) {
120
        final double dot = q.getRow(j) * q.getRow(i);
Ian Hickson's avatar
Ian Hickson committed
121
        for (int h = 0; h < m; h += 1)
122 123 124
          q.set(j, h, q.get(j, h) - dot * q.get(i, h));
      }

125
      final double norm = q.getRow(j).norm();
126
      if (norm < 0.000001) {
Florian Loitsch's avatar
Florian Loitsch committed
127
        // Vectors are linearly dependent or zero so no solution.
128 129 130
        return null;
      }

131
      final double inverseNorm = 1.0 / norm;
Ian Hickson's avatar
Ian Hickson committed
132
      for (int h = 0; h < m; h += 1)
133
        q.set(j, h, q.get(j, h) * inverseNorm);
Ian Hickson's avatar
Ian Hickson committed
134
      for (int i = 0; i < n; i += 1)
135
        r.set(j, i, i < j ? 0.0 : q.getRow(j) * a.getRow(i));
136 137
    }

138
    // Solve R B = Qt W Y to find B. This is easy because R is upper triangular.
139
    // We just work from bottom-right to top-left calculating B's coefficients.
140
    final _Vector wy = new _Vector(m);
Ian Hickson's avatar
Ian Hickson committed
141
    for (int h = 0; h < m; h += 1)
142
      wy[h] = y[h] * w[h];
Ian Hickson's avatar
Ian Hickson committed
143
    for (int i = n - 1; i >= 0; i -= 1) {
144
      result.coefficients[i] = q.getRow(i) * wy;
Ian Hickson's avatar
Ian Hickson committed
145
      for (int j = n - 1; j > i; j -= 1)
146 147
        result.coefficients[i] -= r.get(i, j) * result.coefficients[j];
      result.coefficients[i] /= r.get(i, i);
148 149
    }

150
    // Calculate the coefficient of determination (confidence) as:
Ian Hickson's avatar
Ian Hickson committed
151 152
    //   1 - (sumSquaredError / sumSquaredTotal)
    // ...where sumSquaredError is the residual sum of squares (variance of the
153 154 155
    // error), and sumSquaredTotal is the total sum of squares (variance of the
    // data) where each has been weighted.
    double yMean = 0.0;
Ian Hickson's avatar
Ian Hickson committed
156
    for (int h = 0; h < m; h += 1)
157 158
      yMean += y[h];
    yMean /= m;
159

160 161
    double sumSquaredError = 0.0;
    double sumSquaredTotal = 0.0;
Ian Hickson's avatar
Ian Hickson committed
162
    for (int h = 0; h < m; h += 1) {
163
      double term = 1.0;
Ian Hickson's avatar
Ian Hickson committed
164 165
      double err = y[h] - result.coefficients[0];
      for (int i = 1; i < n; i += 1) {
166
        term *= x[h];
167
        err -= term * result.coefficients[i];
168
      }
169
      sumSquaredError += w[h] * w[h] * err * err;
Ian Hickson's avatar
Ian Hickson committed
170
      final double v = y[h] - yMean;
171
      sumSquaredTotal += w[h] * w[h] * v * v;
172 173
    }

Ian Hickson's avatar
Ian Hickson committed
174 175
    result.confidence = sumSquaredTotal <= 0.000001 ? 1.0 :
                          1.0 - (sumSquaredError / sumSquaredTotal);
176 177 178 179 180

    return result;
  }

}