summaryrefslogtreecommitdiff
path: root/fits.hpp
blob: c6e56fa1e24d992a47d9baa68a76f7633a452564 (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
#include "pcg-cpp/include/pcg_random.hpp"
#include "randutils/randutils.hpp"

#include "eigen/Eigen/Dense"

using Rng = randutils::random_generator<pcg32>;

using Real = double;
using Vector = Eigen::Matrix<Real, Eigen::Dynamic, 1>;
using Matrix = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;
using Data = std::vector<std::tuple<Real, Real>>;

#ifndef FITS_ABS_BASIS
inline Real basisFunctions(unsigned N, unsigned i, Real x) {
  return std::legendre(i, 2.0 * (x - 0.5));
}
#endif

#ifdef FITS_ABS_BASIS
inline Real basisFunctions(unsigned N, unsigned i, Real x) {
  return abs(x - i / (N - 1.0));
}
#endif

Real value(const Vector& coeffs, Real x) {
  Real v = 0;

  for (unsigned j = 0; j < coeffs.size(); j++) {
    v += coeffs[j] * basisFunctions(coeffs.size(), j, x);
  }

  return v;
}

Real cost(Data::const_iterator batchBegin, Data::const_iterator batchEnd, const Vector& coeffs) {
  Real c = 0;

#pragma omp parallel for reduction(+:c)
  for (Data::const_iterator it = batchBegin; it != batchEnd; it++) {
    Real x = std::get<0>(*it);
    Real y = std::get<1>(*it);
    c += 0.5 * pow(value(coeffs, x) - y, 2);
  }

  return c;
}

Vector dCost(Data::const_iterator batchBegin, Data::const_iterator batchEnd, const Vector& coeffs) {
  Vector dC = Vector::Zero(coeffs.size());

  for (Data::const_iterator it = batchBegin; it != batchEnd; it++) {
    Real x = std::get<0>(*it);
    Real y = std::get<1>(*it);
    Real Δc = value(coeffs, x) - y;

#pragma omp parallel for
    for (unsigned j = 0; j < coeffs.size(); j++) {
      dC[j] += Δc * basisFunctions(coeffs.size(), j, x);
    }
  }

  return dC;
}

Vector underSolve(const Data& data, unsigned N) {
  unsigned M = data.size();
  Matrix A(M, N);
  Vector b(M);
  for (unsigned i = 0; i < M; i++) {
    Real x = std::get<0>(data[i]);
    Real y = std::get<1>(data[i]);
    b[i] = y;
    for (unsigned j = 0; j < N; j++) {
      A(i, j) = basisFunctions(N, j, x);
    }
  }

  return A.colPivHouseholderQr().solve(b);
}

Vector stochasticGradientDescent(const Data& data, const Vector& a₀, unsigned nBatches, long unsigned maxSteps, Real ε = 1e-12) {
  Vector xₜ = a₀;
  Real Hₜ;
  Real α = 1.0;
  Real m;
  Vector ∇H;
  long unsigned steps = 0;

  unsigned batchSize = data.size() / nBatches;
  Data::const_iterator batchBegin = data.begin();
  Data::const_iterator batchEnd = data.begin() + batchSize;
  Data::const_iterator effectiveEnd = data.begin() + batchSize * nBatches;

  while (
    Hₜ = cost(batchBegin, batchEnd, xₜ),
    ∇H = dCost(batchBegin, batchEnd, xₜ),
    m = ∇H.squaredNorm(),
    m > ε && steps < maxSteps
  ) {
    Vector xₜ₊₁;
    Real Hₜ₊₁;

    while (
      xₜ₊₁ = xₜ - α * ∇H, Hₜ₊₁ = cost(batchBegin, batchEnd, xₜ₊₁),
      Hₜ₊₁ > Hₜ - 0.25 * α * m
    ) {
      α /= 2;
    }

    xₜ = xₜ₊₁;
    α *= 1.25;
    steps++;

    if (batchEnd == data.end()) {
      batchBegin = data.begin();
      batchEnd = data.begin() + batchSize;
    } else if (batchEnd == effectiveEnd) {
      batchBegin = effectiveEnd;
      batchEnd = data.end();
    } else {
      batchBegin += batchSize;
      batchEnd += batchSize;
    }
  }

  return xₜ;
}

Data generateData(Real(*f)(Real), unsigned M, Real σ, Rng& r) {
  Data data;
  data.reserve(M);

  for (unsigned i = 0; i < M; i++) {
    Real x = ((Real)i) / (M - 1.0);
    data.push_back({x, f(x) + r.variate<Real, std::normal_distribution>(0, σ)});
  }

  return data;
}