summaryrefslogtreecommitdiff
path: root/least_squares.cpp
blob: 093892d12b5605f6b6a67aa02b517a627c60e145 (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
#include <eigen3/Eigen/Dense>
#include <random>
#include <getopt.h>

#include "pcg-cpp/include/pcg_random.hpp"
#include "randutils/randutils.hpp"


template <class Real>
using Vector = Eigen::Matrix<Real, Eigen::Dynamic, 1>;

template <class Real>
using Matrix = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;

template <class Real>
class Model {
private:
  Matrix<Real> A;
  Vector<Real> b;

public:
  template <class Generator>
  Model(Real σ, unsigned N, unsigned M, Generator& r) : A(M, N), b(M) {
    std::normal_distribution<Real> aDistribution(0, 1 / sqrt(N));

    for (unsigned i = 0; i < M; i++) {
      for (unsigned j =0; j < N; j++) {
        A(i, j) = aDistribution(r);
      }
    }

    std::normal_distribution<Real> bDistribution(0, σ);

    for (unsigned i = 0; i < M; i++) {
      b(i) = bDistribution(r);
    }
  }

  unsigned N() const {
    return A.cols();
  }

  unsigned M() const {
    return A.rows();
  }

  Vector<Real> V(const Vector<Real>& x) const {
    return A * x + b;
  }

  Matrix<Real> dV(const Vector<Real>& x) const {
    return A;
  }

//  const Real ddV(const Vector<Real>& x) {
//    return Matrix::Zero(;
//  }

  Real H(const Vector<Real>& x) const {
    return V(x).squaredNorm();
  }

  Vector<Real> dH(const Vector<Real>& x) const {
    return dV(x).transpose() * V(x);
  }

  Matrix<Real> ddH(const Vector<Real>& x) const {
    return dV(x).transpose() * dV(x);
  }

  Vector<Real>H(const Vector<Real>& x) const {
    return dH(x) - dH(x).dot(x) * x / x.squaredNorm();
  }

  Matrix<Real> HessH(const Vector<Real>& x) const {
    Matrix<Real> hess = ddH(x) - x.dot(dH(x)) * Matrix<Real>::Identity(N(), N());
    return hess - (hess * x) * x.transpose() / x.squaredNorm();
  }

  Vector<Real> HessSpectrum(const Vector<Real>& x) const {
    Eigen::EigenSolver<Matrix<Real>> eigenS(HessH(x));
    return eigenS.eigenvalues().real();
  }
};

template <typename Derived>
Vector<typename Derived::Scalar> normalize(const Eigen::MatrixBase<Derived>& z) {
  return z * sqrt((double)z.size() / (typename Derived::Scalar)(z.transpose() * z));
}

template <class Real>
Vector<Real> findMinimum(const Model<Real>& M, const Vector<Real>& x0, Real ε) {
  Vector<Real> x = x0;
  Real λ = 100;

  Real H = M.H(x);
  Vector<Real> dH = M.dH(x);
  Matrix<Real> ddH = M.ddH(x);

  Vector<Real> g = dH - x.dot(dH) * x / x.squaredNorm();
  Matrix<Real> m = ddH - (dH * x.transpose() + x.dot(dH) * Matrix<Real>::Identity(M.N(), M.N()) + (ddH * x) * x.transpose()) / x.squaredNorm() + 2.0 * x * x.transpose();

  while (g.norm() / x.size() > ε && λ < 1e8) {
    Vector<Real> dz = (m + λ * (Matrix<Real>)abs(m.diagonal().array()).matrix().asDiagonal()).partialPivLu().solve(g);
    dz -= x.dot(dz) * x / x.squaredNorm();
    Vector<Real> zNew = normalize(x - dz);

    Real HNew = M.H(zNew);
    Vector<Real> dHNew = M.dH(zNew);
    Matrix<Real> ddHNew = M.ddH(zNew);

    if (HNew * 1.0001 <= H) {
      x = zNew;
      H = HNew;
      dH = dHNew;
      ddH = ddHNew;

      g = dH - x.dot(dH) * x / (Real)x.size();
      m = ddH - (dH * x.transpose() + x.dot(dH) * Matrix<Real>::Identity(x.size(), x.size()) + (ddH * x) * x.transpose()) / (Real)x.size() + 2.0 * x * x.transpose();

      λ /= 2;
    } else {
      λ *= 1.5;
    }
  }

  return x;
}

using Rng = randutils::random_generator<pcg32>;
using Real = double;

int main(int argc, char* argv[]) {
  unsigned N = 10;
  Real α = 1;
  Real σ = 1;

  int opt;

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

  unsigned M = (unsigned)(α * N);

  Rng r;

  Model<Real> leastSquares(σ, N, M, r.engine());

  Vector<Real> x = Vector<Real>::Zero(N);
  x(0) = sqrt(N);

  std::cout << leastSquares.H(x) / N << std::endl;

  Vector<Real> xMin = findMinimum(leastSquares, x, 1e-12);

  std::cout << leastSquares.H(xMin) / N << std::endl;
  std::cout << leastSquares.HessSpectrum(xMin)(1) / N << std::endl;

  return 0;
}