summaryrefslogtreecommitdiff
path: root/fits.cpp
blob: 9696e167ebb848b66e2e9c56a208b51d0b6c9486 (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
#include <getopt.h>
#include <iomanip>

#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 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 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;
}

int main(int argc, char* argv[]) {
  unsigned M = 10;
  unsigned nBatches = 5;
  Real σ = 0.2;
  Real iniVar = 0.0;
  long unsigned maxSteps = 1e12;

  int opt;

  while ((opt = getopt(argc, argv, "M:s:S:B:i:")) != -1) {
    switch (opt) {
    case 'M':
      M = (unsigned)atof(optarg);
      break;
    case 's':
      σ = atof(optarg);
      break;
    case 'S':
      maxSteps = (long unsigned)atof(optarg);
      break;
    case 'B':
      nBatches = (unsigned)atof(optarg);
      break;
    case 'i':
      iniVar = atof(optarg);
      break;
    default:
      exit(1);
    }
  }

  Rng r;

  Data data = generateData([](Real x) {return std::sin(2 * M_PI * x);}, M, σ, r);

  std::cout << std::setprecision(15);

  for (std::tuple<Real, Real> datum : data) {
    std::cout << std::get<0>(datum) << " " << std::get<1>(datum) << " ";
  }
  std::cout << std::endl;

  for (unsigned N = 1; N <= 2 * M; N++) {
    Vector a₀ = Vector::Zero(N);
    for (Real& aa : a₀) {
      aa = r.variate<Real, std::normal_distribution>(0, iniVar);
    }

    Vector a = stochasticGradientDescent(data, a₀, nBatches, maxSteps);

    for (Real ai : a) {
      std::cout << ai << " ";
    }
    std::cout << std::endl;
  }

  return 0;
}