-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest_nlreg.cpp
92 lines (70 loc) · 2.81 KB
/
test_nlreg.cpp
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
// Copyright (C) 2021-2022 Petter Nilsson. MIT License.
#include <gtest/gtest.h>
#include "nlreg_data.hpp"
#include "smooth/optim.hpp"
TEST(NlReg, Misra1aStatic)
{
static constexpr int np = 2;
static constexpr int nobs = 14;
auto [f, data, start1, start2, optim] = Misra1a();
auto f_vec = [m_f = std::move(f),
m_data = std::move(data)](const Eigen::Matrix<double, np, 1> & p) -> Eigen::Matrix<double, nobs, 1> {
return m_data.col(0).binaryExpr(m_data.col(1), [&](double y, double x) { return m_f(y, x, p); });
};
Eigen::Matrix<double, np, 1> p1 = start1;
smooth::minimize(f_vec, smooth::wrt(p1));
ASSERT_TRUE(p1.isApprox(optim, 1e-7));
Eigen::Matrix<double, np, 1> p2 = start2;
smooth::minimize(f_vec, smooth::wrt(p2));
ASSERT_TRUE(p2.isApprox(optim, 1e-7));
}
TEST(NlReg, Misra1aDynamic)
{
auto [f, data, start1, start2, optim] = Misra1a();
auto f_vec = [m_f = std::move(f),
m_data = std::move(data)](const Eigen::Matrix<double, -1, 1> & p) -> Eigen::Matrix<double, -1, 1> {
return m_data.col(0).binaryExpr(m_data.col(1), [&](double y, double x) { return m_f(y, x, p); });
};
Eigen::Matrix<double, -1, 1> p1 = start1;
smooth::minimize(f_vec, smooth::wrt(p1));
ASSERT_TRUE(p1.isApprox(optim, 1e-7));
Eigen::Matrix<double, -1, 1> p2 = start2;
smooth::minimize(f_vec, smooth::wrt(p2));
ASSERT_TRUE(p2.isApprox(optim, 1e-7));
}
TEST(NlReg, Kirby2Static)
{
static constexpr int np = 5;
static constexpr int nobs = 151;
auto [f, data, start1, start2, optim] = Kirby2();
auto f_vec = [m_f = std::move(f),
m_data = std::move(data)](const Eigen::Matrix<double, np, 1> & p) -> Eigen::Matrix<double, nobs, 1> {
return m_data.col(0).binaryExpr(m_data.col(1), [&](double y, double x) { return m_f(y, x, p); });
};
smooth::MinimizeOptions opts;
opts.ftol = 1e-12;
opts.ptol = 1e-12;
Eigen::Matrix<double, np, 1> p1 = start1;
smooth::minimize(f_vec, smooth::wrt(p1), opts);
ASSERT_TRUE(p1.isApprox(optim, 1e-7));
Eigen::Matrix<double, np, 1> p2 = start2;
smooth::minimize(f_vec, smooth::wrt(p2), opts);
ASSERT_TRUE(p2.isApprox(optim, 1e-7));
}
TEST(NlReg, Kirby2Dynamic)
{
auto [f, data, start1, start2, optim] = Kirby2();
auto f_vec = [m_f = std::move(f),
m_data = std::move(data)](const Eigen::Matrix<double, -1, 1> & p) -> Eigen::Matrix<double, -1, 1> {
return m_data.col(0).binaryExpr(m_data.col(1), [&](double y, double x) { return m_f(y, x, p); });
};
smooth::MinimizeOptions opts;
opts.ftol = 1e-12;
opts.ptol = 1e-12;
Eigen::Matrix<double, -1, 1> p1 = start1;
smooth::minimize(f_vec, smooth::wrt(p1), opts);
ASSERT_TRUE(p1.isApprox(optim, 1e-7));
Eigen::Matrix<double, -1, 1> p2 = start2;
smooth::minimize(f_vec, smooth::wrt(p2), opts);
ASSERT_TRUE(p2.isApprox(optim, 1e-7));
}