• 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

97.92
/tests/NuggetKrigingUpdateTest.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/NuggetKriging.hpp"
8
// clang-format on
9

10
TEST_CASE("NuggetKrigingUpdateTest - Updated model equals combined fit", "[update][nuggetkriging]") {
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 with noise
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)) + 0.01 * arma::randn();
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)) + 0.01 * arma::randn();
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
    NuggetKriging nk_updated("gauss");
2✔
45
    NuggetKriging::Parameters params{std::nullopt, true, std::nullopt, true, std::nullopt, true, std::nullopt, true};
1✔
46
    nk_updated.fit(y_old, X_old, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
1✔
47
    
48
    // Update with new data
49
    nk_updated.update(y_new, X_new, true);
1✔
50
    
51
    // Fit model on combined data
52
    NuggetKriging nk_combined("gauss");
2✔
53
    nk_combined.fit(y_combined, X_combined, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
1✔
54
    
55
    // Compare parameters
56
    INFO("Updated theta: " << nk_updated.theta().t());
3✔
57
    INFO("Combined theta: " << nk_combined.theta().t());
3✔
58
    INFO("Updated sigma2: " << nk_updated.sigma2());
1✔
59
    INFO("Combined sigma2: " << nk_combined.sigma2());
1✔
60
    INFO("Updated nugget: " << nk_updated.nugget());
1✔
61
    INFO("Combined nugget: " << nk_combined.nugget());
1✔
62
    INFO("Updated beta: " << nk_updated.beta().t());
3✔
63
    INFO("Combined beta: " << nk_combined.beta().t());
3✔
64
    
65
    // Check theta (should be very close) - using relative precision 0.05 (5%)
66
    // Use relative comparison, which handles zero values by falling back to absolute comparison
67
    CHECK(arma::approx_equal(nk_updated.theta(), nk_combined.theta(), "reldiff", 0.05));
1✔
68
    
69
    // Check sigma2 - using relative precision 0.05 (5%)
70
    if (nk_combined.sigma2() == 0.0) {
1✔
NEW
71
      CHECK(nk_updated.sigma2() == 0.0);  // If combined is zero, updated should also be zero
×
72
    } else {
73
      CHECK(std::abs(nk_updated.sigma2() - nk_combined.sigma2()) / std::abs(nk_combined.sigma2()) < 0.05);
1✔
74
    }
75
    
76
    // Check nugget - using relative precision 0.05 (5%)
77
    if (nk_combined.nugget() == 0.0) {
1✔
78
      CHECK(nk_updated.nugget() == 0.0);  // If combined is zero, updated should also be zero
1✔
79
    } else {
NEW
80
      CHECK(std::abs(nk_updated.nugget() - nk_combined.nugget()) / std::abs(nk_combined.nugget()) < 0.05);
×
81
    }
82
    
83
    // Check beta - using relative precision 0.05 (5%)
84
    // Use relative comparison, which handles zero values by falling back to absolute comparison
85
    CHECK(arma::approx_equal(nk_updated.beta(), nk_combined.beta(), "reldiff", 0.05));
1✔
86
    
87
    // Test predictions on new points (main validation)
88
    arma::mat X_test(5, d, arma::fill::randu);
1✔
89
    
90
    auto pred_updated = nk_updated.predict(X_test, true, false, false);
1✔
91
    auto pred_combined = nk_combined.predict(X_test, true, false, false);
1✔
92
    
93
    INFO("Max prediction difference: " << arma::max(arma::abs(std::get<0>(pred_updated) - std::get<0>(pred_combined))));
2✔
94
    
95
    CHECK(arma::approx_equal(std::get<0>(pred_updated), std::get<0>(pred_combined), "reldiff", 0.05));
1✔
96
  }
5✔
97

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

126
  SECTION("Different kernels") {
4✔
127
    std::vector<std::string> kernels = {"gauss", "exp", "matern3_2", "matern5_2"};
7✔
128
    
129
    for (const auto& kernel : kernels) {
5✔
130
      INFO("Testing kernel: " << kernel);
4✔
131
      
132
      // Fit and update
133
      NuggetKriging nk_updated(kernel);
4✔
134
      NuggetKriging::Parameters params{std::nullopt, true, std::nullopt, true, std::nullopt, true, std::nullopt, true};
4✔
135
      nk_updated.fit(y_old, X_old, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
4✔
136
      nk_updated.update(y_new, X_new, true);
4✔
137
      
138
      // Combined fit
139
      NuggetKriging nk_combined(kernel);
4✔
140
      nk_combined.fit(y_combined, X_combined, Trend::RegressionModel::Constant, false, "BFGS", "LL", params);
4✔
141
      
142
      // Test predictions
143
      arma::mat X_test(3, d, arma::fill::randu);
4✔
144
      auto pred_updated = nk_updated.predict(X_test, true, false, false);
4✔
145
      auto pred_combined = nk_combined.predict(X_test, true, false, false);
4✔
146
      
147
      CHECK(arma::approx_equal(std::get<0>(pred_updated), std::get<0>(pred_combined), "reldiff", 0.05));
4✔
148
    }
4✔
149
  }
5✔
150

151
  SECTION("Different trend models") {
4✔
152
    std::vector<Trend::RegressionModel> trends = {
153
      Trend::RegressionModel::Constant,
154
      Trend::RegressionModel::Linear,
155
      Trend::RegressionModel::Quadratic
156
    };
1✔
157
    
158
    for (const auto& trend : trends) {
4✔
159
      INFO("Testing trend model");
3✔
160
      
161
      // Fit and update
162
      NuggetKriging nk_updated("gauss");
6✔
163
      NuggetKriging::Parameters params{std::nullopt, true, std::nullopt, true, std::nullopt, true, std::nullopt, true};
3✔
164
      nk_updated.fit(y_old, X_old, trend, false, "BFGS", "LL", params);
3✔
165
      nk_updated.update(y_new, X_new, true);
3✔
166
      
167
      // Combined fit
168
      NuggetKriging nk_combined("gauss");
6✔
169
      nk_combined.fit(y_combined, X_combined, trend, false, "BFGS", "LL", params);
3✔
170
      
171
      // Test predictions
172
      arma::mat X_test(3, d, arma::fill::randu);
3✔
173
      auto pred_updated = nk_updated.predict(X_test, true, false, false);
3✔
174
      auto pred_combined = nk_combined.predict(X_test, true, false, false);
3✔
175
      
176
      CHECK(arma::approx_equal(std::get<0>(pred_updated), std::get<0>(pred_combined), "reldiff", 0.05));
3✔
177
    }
3✔
178
  }
5✔
179
}
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