• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libKriging / libKriging / 20211252547

14 Dec 2025 05:03PM UTC coverage: 67.712% (+30.3%) from 37.371%
20211252547

push

github

web-flow
Merge pull request #303 from libKriging/multistart-optim_jemalloc

Multistart optim

3946 of 4387 new or added lines in 30 files covered. (89.95%)

20 existing lines in 5 files now uncovered.

8403 of 12410 relevant lines covered (67.71%)

53457.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.9
/tests/KrigingUpdateTest.cpp
1
// clang-format off
2
// Must be first
3
#define CATCH_CONFIG_MAIN
4
#include "libKriging/utils/lk_armadillo.hpp"
5

6
#include <catch2/catch.hpp>
7
#include "libKriging/Kriging.hpp"
8
// clang-format on
9

10
TEST_CASE("KrigingUpdateTest - Updated model equals combined fit", "[update][kriging]") {
4✔
11
  arma::arma_rng::set_seed(123);
4✔
12

13
  // Generate initial training data
14
  const arma::uword n_old = 10;
4✔
15
  const arma::uword n_new = 1;
4✔
16
  const arma::uword d = 2;
4✔
17
  
18
  arma::mat X_old(n_old, d, arma::fill::randu);
4✔
19
  arma::colvec y_old(n_old);
4✔
20
  
21
  // Use a simple test function
22
  auto test_function = [](const arma::rowvec& x) {
44✔
23
    return x(0) * x(0) + x(1) * x(1);
220✔
24
  };
25
  
26
  for (arma::uword i = 0; i < n_old; ++i) {
44✔
27
    y_old(i) = test_function(X_old.row(i));
80✔
28
  }
29
  
30
  // Generate new data points
31
  arma::mat X_new(n_new, d, arma::fill::randu);
4✔
32
  arma::colvec y_new(n_new);
4✔
33
  
34
  for (arma::uword i = 0; i < n_new; ++i) {
8✔
35
    y_new(i) = test_function(X_new.row(i));
8✔
36
  }
37
  
38
  // Combine old and new data
39
  arma::mat X_combined = arma::join_cols(X_old, X_new);
4✔
40
  arma::colvec y_combined = arma::join_cols(y_old, y_new);
4✔
41

42
  SECTION("Update with refit equals combined fit") {
4✔
43
    // Fit model on old data
44
    Kriging kr_updated("gauss");
2✔
45
    Kriging::Parameters params{std::nullopt, true, std::nullopt, true, std::nullopt, true};
1✔
46
    kr_updated.fit(y_old, X_old, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
1✔
47
    
48
    // Update with new data
49
    kr_updated.update(y_new, X_new, true);
1✔
50
    
51
    // Fit model on combined data
52
    Kriging kr_combined("gauss");
2✔
53
    kr_combined.fit(y_combined, X_combined, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
1✔
54
    
55
    // Compare parameters
56
    INFO("Updated theta: " << kr_updated.theta().t());
3✔
57
    INFO("Combined theta: " << kr_combined.theta().t());
3✔
58
    INFO("Updated sigma2: " << kr_updated.sigma2());
1✔
59
    INFO("Combined sigma2: " << kr_combined.sigma2());
1✔
60
    INFO("Updated beta: " << kr_updated.beta().t());
3✔
61
    INFO("Combined beta: " << kr_combined.beta().t());
3✔
62
    
63
    // Check theta (should be very close) - using relative precision 0.05 (5%)
64
    // Use relative comparison, which handles zero values by falling back to absolute comparison
65
    CHECK(arma::approx_equal(kr_updated.theta(), kr_combined.theta(), "reldiff", 0.05));
1✔
66
    
67
    // Check sigma2 - using relative precision 0.05 (5%)
68
    if (kr_combined.sigma2() == 0.0) {
1✔
NEW
69
      CHECK(kr_updated.sigma2() == 0.0);  // If combined is zero, updated should also be zero
×
70
    } else {
71
      CHECK(std::abs(kr_updated.sigma2() - kr_combined.sigma2()) / std::abs(kr_combined.sigma2()) < 0.05);
1✔
72
    }
73
    
74
    // Check beta - using relative precision 0.05 (5%)
75
    // Use relative comparison, which handles zero values by falling back to absolute comparison
76
    CHECK(arma::approx_equal(kr_updated.beta(), kr_combined.beta(), "reldiff", 0.05));
1✔
77
    
78
    // Test predictions on new points
79
    arma::mat X_test(5, d, arma::fill::randu);
1✔
80
    
81
    auto pred_updated = kr_updated.predict(X_test, true, false, false);
1✔
82
    auto pred_combined = kr_combined.predict(X_test, true, false, false);
1✔
83
    
84
    INFO("Max prediction difference: " << arma::max(arma::abs(std::get<0>(pred_updated) - std::get<0>(pred_combined))));
2✔
85
    
86
    CHECK(arma::approx_equal(std::get<0>(pred_updated), std::get<0>(pred_combined), "reldiff", 0.05));
1✔
87
  }
5✔
88

89
  SECTION("Multiple updates equal combined fit") {
4✔
90
    // Fit model on old data
91
    Kriging kr_updated("gauss");
2✔
92
    Kriging::Parameters params{std::nullopt, true, std::nullopt, true, std::nullopt, true};
1✔
93
    kr_updated.fit(y_old, X_old, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
1✔
94
    
95
    // Update one point at a time
96
    for (arma::uword i = 0; i < n_new; ++i) {
2✔
97
      arma::vec y_single = y_new.row(i);
2✔
98
      arma::mat X_single = X_new.row(i);
1✔
99
      kr_updated.update(y_single, X_single, true);
1✔
100
    }
1✔
101
    
102
    // Fit model on combined data
103
    Kriging kr_combined("gauss");
2✔
104
    kr_combined.fit(y_combined, X_combined, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
1✔
105
    
106
    // Test predictions
107
    arma::mat X_test(5, d, arma::fill::randu);
1✔
108
    
109
    auto pred_updated = kr_updated.predict(X_test, true, false, false);
1✔
110
    auto pred_combined = kr_combined.predict(X_test, true, false, false);
1✔
111
    
112
    INFO("Max prediction difference: " << arma::max(arma::abs(std::get<0>(pred_updated) - std::get<0>(pred_combined))));
2✔
113
    
114
    CHECK(arma::approx_equal(std::get<0>(pred_updated), std::get<0>(pred_combined), "reldiff", 0.05));
1✔
115
  }
5✔
116

117
  SECTION("Different kernels") {
4✔
118
    std::vector<std::string> kernels = {"gauss", "exp", "matern3_2", "matern5_2"};
7✔
119
    
120
    for (const auto& kernel : kernels) {
5✔
121
      INFO("Testing kernel: " << kernel);
4✔
122
      
123
      // Fit and update
124
      Kriging kr_updated(kernel);
4✔
125
      Kriging::Parameters params{std::nullopt, true, std::nullopt, true, std::nullopt, true};
4✔
126
      kr_updated.fit(y_old, X_old, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
4✔
127
      kr_updated.update(y_new, X_new, true);
4✔
128
      
129
      // Combined fit
130
      Kriging kr_combined(kernel);
4✔
131
      kr_combined.fit(y_combined, X_combined, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
4✔
132
      
133
      // Test predictions
134
      arma::mat X_test(3, d, arma::fill::randu);
4✔
135
      auto pred_updated = kr_updated.predict(X_test, true, false, false);
4✔
136
      auto pred_combined = kr_combined.predict(X_test, true, false, false);
4✔
137
      
138
      CHECK(arma::approx_equal(std::get<0>(pred_updated), std::get<0>(pred_combined), "reldiff", 0.05));
4✔
139
    }
4✔
140
  }
5✔
141

142
  SECTION("Different trend models") {
4✔
143
    std::vector<Trend::RegressionModel> trends = {
144
      Trend::RegressionModel::Constant,
145
      Trend::RegressionModel::Linear,
146
      Trend::RegressionModel::Quadratic
147
    };
1✔
148
    
149
    for (const auto& trend : trends) {
4✔
150
      INFO("Testing trend model");
3✔
151
      
152
      // Fit and update
153
      Kriging kr_updated("gauss");
6✔
154
      Kriging::Parameters params{std::nullopt, true, std::nullopt, true, std::nullopt, true};
3✔
155
      kr_updated.fit(y_old, X_old, trend, false, "BFGS", "LL", params);
3✔
156
      kr_updated.update(y_new, X_new, true);
3✔
157
      
158
      // Combined fit
159
      Kriging kr_combined("gauss");
6✔
160
      kr_combined.fit(y_combined, X_combined, trend, false, "BFGS", "LL", params);
3✔
161
      
162
      // Test predictions
163
      arma::mat X_test(3, d, arma::fill::randu);
3✔
164
      auto pred_updated = kr_updated.predict(X_test, true, false, false);
3✔
165
      auto pred_combined = kr_combined.predict(X_test, true, false, false);
3✔
166
      
167
      CHECK(arma::approx_equal(std::get<0>(pred_updated), std::get<0>(pred_combined), "reldiff", 0.05));
3✔
168
    }
3✔
169
  }
5✔
170
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc