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

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

/*
inline Real basisFunctions(unsigned N, unsigned i, Real x) {
  if (i == 0) {
    return 1;
  } else {
    return pow(x, i);
  }
}
*/

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

/*
inline Real basisFunctions(unsigned N, unsigned i, Real x) {
  return abs(x - i / (N - 1.0));
}
*/

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(const std::vector<std::tuple<Real, Real>>& data, const Vector& coeffs) {
  Real c = 0;

#pragma omp parallel for reduction(+:c)
  for (const std::tuple<Real, Real>& xy : data) {
    Real x = std::get<0>(xy);
    Real y = std::get<1>(xy);
    c += 0.5 * pow(value(coeffs, x) - y, 2);
  }

  return c;
}

Vector dCost(const std::vector<std::tuple<Real, Real>>& data, const Vector& coeffs) {
  Vector dC = Vector::Zero(coeffs.size());

  for (const std::tuple<Real, Real>& xy : data) {
    Real x = std::get<0>(xy);
    Real y = std::get<1>(xy);
    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 gradientDescent(const std::vector<std::tuple<Real, Real>>& data, const Vector& a₀, unsigned maxSteps, Real ε = 1e-12) {
  Vector xₜ = a₀;
  Real Hₜ = cost(data, a₀);
  Real α = 1.0;
  Real m;
  Vector ∇H;
  unsigned steps = 0;

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

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

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

  return xₜ;
}

Vector dCostRand(const std::vector<std::tuple<Real, Real>>& data, const Vector& coeffs, Real batchP, Rng& r) {
  Vector dC = Vector::Zero(coeffs.size());

  for (unsigned j = 0; j < coeffs.size(); j++) {
    for (const std::tuple<Real, Real>& xy : data) {
      if (batchP > r.uniform(0.0, 1.0)) {
        Real x = std::get<0>(xy);
        Real y = std::get<1>(xy);
        dC[j] += (value(coeffs, x) - y) * basisFunctions(coeffs.size(), j, x);
      }
    }
  }

  return dC;
}

Vector stochasticGradientDescent(std::vector<std::tuple<Real, Real>>& data, const Vector& a₀, Rng& r, unsigned maxSteps, Real ε = 1e-12) {
  Vector xₜ = a₀;
  Real α = 1e-3;
  Real m;
  Vector ∇H;
  unsigned steps = 0;

  while (
    ∇H = dCostRand(data, xₜ, 0.1, r), m = ∇H.squaredNorm(),
    m > ε && steps < maxSteps
  ) {
    xₜ = xₜ - α * ∇H;
    steps++;
  }

  return xₜ;
}

std::vector<std::tuple<Real, Real>> generateData(Real(*f)(Real), unsigned M, Real σ, Rng& r) {
  std::vector<std::tuple<Real, Real>> 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 N = 10;
  unsigned M = 10;
  Real σ = 0.2;
  long unsigned maxSteps = 1e12;

  int opt;

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

  Rng r;

  std::vector<std::tuple<Real, Real>> data = generateData([](Real x) {return std::cos(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;

  Vector a₀ = Vector::Zero(N);
  for (Real& aa : a₀) {
    aa = r.variate<Real, std::normal_distribution>(0, 0);
  }
  Vector a = gradientDescent(data, a₀, maxSteps);

  for (unsigned i = 0; i < N; i++) {
    std::cout << a[i] << " ";
  }
  std::cout << std::endl;

  return 0;
}