hops
Rosenbrock.hpp
Go to the documentation of this file.
1 #ifndef HOPS_ROSENBROCK_HPP
2 #define HOPS_ROSENBROCK_HPP
3 
4 #include <cmath>
5 #include <stdexcept>
6 #include <unsupported/Eigen/MatrixFunctions>
7 #include <utility>
8 
9 #include <hops/Model/Model.hpp>
12 
13 namespace hops {
14 
22  class Rosenbrock : public Model {
23  public:
29  Rosenbrock(double scaleParameter, VectorType shiftParameter);
30 
31  [[nodiscard]] typename MatrixType::Scalar computeNegativeLogLikelihood(const VectorType &x) const override;
32 
33  [[nodiscard]] MatrixType computeHessian(const VectorType &x) const;
34 
35  [[nodiscard]] std::optional<VectorType> computeLogLikelihoodGradient(const VectorType &x) const override;
36 
43  [[nodiscard]] std::optional<MatrixType> computeExpectedFisherInformation(const VectorType &x) const override;
44 
45  private:
46  typename MatrixType::Scalar scaleParameter;
47  VectorType shiftParameter;
48  long numberOfDimensions;
49  };
50 
51  Rosenbrock::Rosenbrock(double scaleParameter,
52  VectorType shiftParameter) :
53  scaleParameter(scaleParameter),
54  shiftParameter(std::move(shiftParameter)) {
55  numberOfDimensions = this->shiftParameter.rows() * 2;
56  }
57 
58  typename MatrixType::Scalar
60  if (x.rows() != numberOfDimensions) {
61  throw std::runtime_error("Input x has wrong number of rows.");
62  }
63  typename MatrixType::Scalar result = 0;
64  for (long i = 0; i < shiftParameter.rows(); ++i) {
65  result += scaleParameter *
66  (100 * std::pow(std::pow(x(2 * i), 2) - x(2 * i + 1), 2) +
67  std::pow(x(2 * i) - shiftParameter(i), 2));
68  }
69  return result;
70  }
71 
74  MatrixType observedFisherInformation(x.rows(), x.rows());
75 
76  for (long i = 0; i < shiftParameter.rows(); ++i) {
77  observedFisherInformation(2 * i, 2 * i) =
78  scaleParameter * (1200 * std::pow(x(2 * i), 2) - 400 * x(2 * i + 1) + 2);
79  observedFisherInformation(2 * i + 1, 2 * i) = scaleParameter * -400 * x(2 * i);
80  observedFisherInformation(2 * i, 2 * i + 1) = scaleParameter * -400 * x(2 * i);
81  observedFisherInformation(2 * i + 1, 2 * i + 1) = scaleParameter * 200;
82  }
83 
84  return observedFisherInformation;
85  }
86 
87  std::optional<VectorType>
89  VectorType gradient(x.rows());
90  for (long i = 0; i < shiftParameter.rows(); ++i) {
91  gradient(2 * i) = scaleParameter * (4 * 100 * (x(2 * i + 1) - std::pow(x(2 * i), 2)) * (-2 * x(2 * i)) +
92  2 * (x(2 * i) - shiftParameter(i)));
93 
94  gradient(2 * i + 1) = scaleParameter * 2 * 100 * (x(2 * i + 1) - std::pow(x(2 * i), 2));
95  }
96  return gradient;
97  }
98 
99  std::optional<MatrixType>
101  MatrixType hessian = computeHessian(x);
102  // regularization should be between 0 and infinity. This value is guessed for now.
103  double regularization = 1. / hessian.maxCoeff();
104  MatrixType expPositive = (regularization * hessian).exp();
105  MatrixType expNegative = (-regularization * hessian).exp();
106  hessian = (expPositive + expNegative) * hessian * (expPositive - expNegative).inverse();
107  return
108  hessian;
109  }
110 }
111 
112 #endif //HOPS_ROSENBROCK_HPP
hops::MatrixType
Eigen::MatrixXd MatrixType
Definition: MatrixType.hpp:7
hops::Rosenbrock::computeNegativeLogLikelihood
MatrixType::Scalar computeNegativeLogLikelihood(const VectorType &x) const override
Evaluates the negative log likelihood for input x.
Definition: Rosenbrock.hpp:59
hops::Rosenbrock::Rosenbrock
Rosenbrock(double scaleParameter, VectorType shiftParameter)
Definition: Rosenbrock.hpp:51
hops::Rosenbrock
multi-dimensional extension of rosenbrock function to N dimensions. Only defined on spaces of even N!
Definition: Rosenbrock.hpp:22
hops::Rosenbrock::computeLogLikelihoodGradient
std::optional< VectorType > computeLogLikelihoodGradient(const VectorType &x) const override
Definition: Rosenbrock.hpp:88
Model.hpp
hops
Definition: CsvReader.hpp:8
VectorType.hpp
MatrixType.hpp
hops::Rosenbrock::computeExpectedFisherInformation
std::optional< MatrixType > computeExpectedFisherInformation(const VectorType &x) const override
Actually this computes the softmax of the hessian instead of the the expected fisher information is i...
Definition: Rosenbrock.hpp:100
hops::Model
Definition: Model.hpp:12
hops::VectorType
Eigen::VectorXd VectorType
Definition: VectorType.hpp:7
hops::Rosenbrock::computeHessian
MatrixType computeHessian(const VectorType &x) const
Definition: Rosenbrock.hpp:73