summaryrefslogtreecommitdiff
path: root/fits_abs.cpp
blob: 087bdbebe3f11c3eea8226008a4b77e02cd4c042 (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
#include <getopt.h>
#include <iomanip>

#include "fits.hpp"

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

  int opt;

  while ((opt = getopt(argc, argv, "s:S:B:i:N:")) != -1) {
    switch (opt) {
    case 'N':
      Nend = (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);
    }
  }

  std::string firstline;
  std::getline(std::cin, firstline);
  std::stringstream ss;
  ss << firstline;

  Data data;

  while (!ss.eof()) {
    Real x, y;
    ss >> x;
    ss >> y;
    data.push_back({x,y});
  }

  unsigned M = data.size();

  Rng r;

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

  for (unsigned N = 1; N <= M; N++) {
    Vector a = underSolve(data, N);

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

  for (unsigned N = Nend; N > 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;
}