fastlowess-cpp
High-performance LOWESS smoothing
Loading...
Searching...
No Matches
fastlowess.hpp
Go to the documentation of this file.
1
9#ifndef FASTLOWESS_HPP
10#define FASTLOWESS_HPP
11
12#ifdef _WIN32
13#ifndef NOMINMAX
14#define NOMINMAX
15#endif
16#include <windows.h> // NOLINT(misc-include-cleaner)
17
18#include <errhandlingapi.h>
19#include <libloaderapi.h>
20#include <minwindef.h>
21#include <processthreadsapi.h>
22#include <winbase.h>
23#endif
24
25#include <algorithm>
26#include <atomic>
27#include <cmath>
28#include <cstdint>
29#include <cstdio> // stdin, fileno / _fileno
30#include <cstdlib>
31#include <cstring>
32#include <filesystem>
33#include <fstream>
34#include <initializer_list>
35#include <iostream>
36#include <limits>
37#include <optional>
38#include <stdexcept>
39#include <string>
40#include <system_error>
41#include <utility>
42#include <vector>
43
44#if defined(__linux__) && !defined(__ANDROID__)
45#include <features.h>
46#endif
47
48#ifdef __APPLE__
49#include <TargetConditionals.h> // IWYU pragma: keep
50#endif
51
52#ifdef _WIN32
53#include <io.h> // _isatty // NOLINT(misc-include-cleaner)
54#include <process.h>
55#else
56#include <cerrno>
57#include <dlfcn.h>
58#include <spawn.h>
59#include <sys/wait.h>
60#include <unistd.h> // isatty
61#endif
62
63#ifdef __APPLE__
64extern "C" char **environ;
65#endif
66
67// Include the C header
68#include "fastlowess.h"
69#include "fastlowess_version.h" // IWYU pragma: export
70
71namespace fastlowess {
72
73namespace detail {
74constexpr double k_default_fraction = 0.67;
75constexpr int k_default_cv_k = 5;
76constexpr int k_default_chunk_size = 5000;
79constexpr int k_default_overlap = -1;
80constexpr int k_default_window_capacity = 1000;
81constexpr int k_default_min_points = 2;
82} // namespace detail
83
84struct CVOptions {
85 std::vector<double> fractions;
86 std::string method = "kfold";
88};
89
91 double confidence = NAN;
92 double prediction = NAN;
94 size_t bootstrap = 0;
95};
96
97inline bool hasOutput(const std::vector<std::string> &outputs,
98 const char *name) {
99 return std::find(outputs.begin(), outputs.end(), name) != outputs.end();
100}
101
105class LowessError : public std::runtime_error {
106public:
107 explicit LowessError(const std::string &message)
108 : std::runtime_error(message) {}
109};
110
111inline void validateOutputs(const std::vector<std::string> &outputs,
112 std::initializer_list<const char *> supported,
113 const char *adapter) {
114 for (const auto &output : outputs) {
115 const auto *const found =
116 std::find_if(supported.begin(), supported.end(),
117 [&output](const char *name) { return output == name; });
118 if (found == supported.end()) {
119 throw LowessError("Unknown output '" + output + "' for " + adapter);
120 }
121 }
122}
123
124[[noreturn]] inline void throwInitializationError(const char *fallback) {
125 if (const char *message = fastlowess_last_error_message()) {
126 throw LowessError(message);
127 }
128 throw LowessError(fallback);
129}
130
135template <typename T> class Expected {
136public:
137 // Success constructor
138 explicit Expected(T val) : val_(std::move(val)), has_val_(true) {}
139
140 // Error constructor
141 struct ErrorTag {};
142 static Expected make_error(std::string msg) {
143 return Expected(std::move(msg), ErrorTag{});
144 }
145
146 bool has_value() const { return has_val_; }
147
148 explicit operator bool() const { return has_val_; }
149
150 T &value() & {
151 if (!has_val_) {
152 throw LowessError(err_);
153 }
154 return val_;
155 }
156
157 const T &value() const & {
158 if (!has_val_) {
159 throw LowessError(err_);
160 }
161 return val_;
162 }
163
164 T &&value() && {
165 if (!has_val_) {
166 throw LowessError(err_);
167 }
168 return std::move(val_);
169 }
170
171 const std::string &error() const {
172 if (has_val_) {
173 throw LowessError("Bad expected access: has value");
174 }
175 return err_;
176 }
177
178private:
179 Expected(std::string err, ErrorTag error_tag)
180 : err_(std::move(err)), has_val_(false) {
181 static_cast<void>(error_tag);
182 }
183
184 // We store both to avoid manual union management, relying on T's cheap
185 // default ctor. LowessResult's default ctor is cheap (zero-init).
186 T val_;
187 std::string err_;
188 bool has_val_;
189};
190
196 int iterations = 3;
197 double delta = NAN;
198
199 std::string weight_function = "tricube";
200 std::string robustness_method = "bisquare";
201 std::string scaling_method = "mad";
202 std::string boundary_policy = "extend";
203 std::string zero_weight_fallback = "use_local_mean";
204
206 std::optional<uint64_t> seed;
207 double auto_converge = NAN;
208
211 std::vector<std::string> outputs;
212 bool parallel = true;
215 std::string backend = "cpu";
218 std::string missing = "error";
219
222
225 std::vector<double> custom_weights;
226
229 bool retain_model = false;
230};
231
243
254 int iterations = 0;
255 double delta = NAN;
256
257 std::string weight_function = "tricube";
258 std::string robustness_method = "bisquare";
259 std::string scaling_method = "mad";
260 std::string boundary_policy = "extend";
261 std::string zero_weight_fallback = "use_local_mean";
262
263 double auto_converge = NAN;
264
267 std::vector<std::string> outputs;
269 std::optional<uint64_t> seed;
270
273 std::string missing = "error";
274
277 std::string update_mode = "incremental";
278};
279
281enum class OnlinePointStatus : std::int8_t {
282 Filling = 0,
283 Ready = 1,
284 Dropped = -1,
285};
286
288 if (has_value == 1) {
290 }
291 if (has_value < 0) {
293 }
295}
296
305public:
307 bool has_value() const { return status_ == OnlinePointStatus::Ready; }
308
310 bool dropped() const { return status_ == OnlinePointStatus::Dropped; }
311
313 OnlinePointStatus status() const { return status_; }
314
316 double y() const { return y_; }
317
319 double standard_error() const { return standard_error_; }
320
322 double residual() const { return residual_; }
323
325 double robustness_weight() const { return robustness_weight_; }
326
328 int iterations_used() const { return iterations_used_; }
329
331 double derivative() const { return derivative_; }
332
335 double confidence_lower() const { return confidence_lower_; }
336
339 double confidence_upper() const { return confidence_upper_; }
340
343 double prediction_lower() const { return prediction_lower_; }
344
347 double prediction_upper() const { return prediction_upper_; }
348
349private:
350 friend class OnlineLowess;
351 template <typename U> friend class Expected;
352 OnlineOutput() = default;
353 explicit OnlineOutput(const fastlowess_CppOnlineOutput &raw)
354 : status_(onlinePointStatusFromRaw(raw.has_value)), y_(raw.y),
355 standard_error_(raw.standard_error), residual_(raw.residual),
356 robustness_weight_(raw.robustness_weight),
357 iterations_used_(raw.iterations_used), derivative_(raw.derivative),
358 confidence_lower_(raw.confidence_lower),
359 confidence_upper_(raw.confidence_upper),
360 prediction_lower_(raw.prediction_lower),
361 prediction_upper_(raw.prediction_upper) {}
362
364 double y_ = std::numeric_limits<double>::quiet_NaN();
365 double standard_error_ = std::numeric_limits<double>::quiet_NaN();
366 double residual_ = std::numeric_limits<double>::quiet_NaN();
367 double robustness_weight_ = std::numeric_limits<double>::quiet_NaN();
368 int iterations_used_ = -1;
369 double derivative_ = std::numeric_limits<double>::quiet_NaN();
370 double confidence_lower_ = std::numeric_limits<double>::quiet_NaN();
371 double confidence_upper_ = std::numeric_limits<double>::quiet_NaN();
372 double prediction_lower_ = std::numeric_limits<double>::quiet_NaN();
373 double prediction_upper_ = std::numeric_limits<double>::quiet_NaN();
374};
375
380public:
381 Diagnostics() = default;
382
383 explicit Diagnostics(const fastlowess_CppLowessResult &result)
384 : rmse_(result.rmse), mae_(result.mae), r_squared_(result.r_squared),
385 aic_(std::isnan(result.aic) ? std::nullopt
386 : std::optional<double>(result.aic)),
387 aicc_(std::isnan(result.aicc) ? std::nullopt
388 : std::optional<double>(result.aicc)),
389 effective_df_(std::isnan(result.effective_df)
390 ? std::nullopt
391 : std::optional<double>(result.effective_df)),
392 residual_sd_(result.residual_sd) {}
393
394 bool has_value() const { return !std::isnan(rmse_); }
395
396 double rmse() const { return rmse_; }
397 double mae() const { return mae_; }
398 double r_squared() const { return r_squared_; }
401 double residual_sd() const { return residual_sd_; }
403 std::optional<double> aic() const { return aic_; }
405 std::optional<double> aicc() const { return aicc_; }
407 std::optional<double> effective_df() const { return effective_df_; }
408
409private:
410 double rmse_ = NAN;
411 double mae_ = NAN;
412 double r_squared_ = NAN;
413 std::optional<double> aic_;
414 std::optional<double> aicc_;
415 std::optional<double> effective_df_;
416 double residual_sd_ = NAN;
417};
418
424 std::vector<std::string> outputs;
426 std::optional<uint64_t> seed;
429 std::string extrapolation = "clamp";
438};
439
446public:
447 PredictResult() = default;
448
449 explicit PredictResult(const fastlowess_CppPredictResult &c_result)
450 : result_(c_result) {}
451
452 ~PredictResult() { fastlowess_predict_free_result(&result_); }
453
454 // Move-only
455 PredictResult(const PredictResult &) = delete;
457
458 PredictResult(PredictResult &&other) noexcept : result_(other.result_) {
459 other.result_ = fastlowess_CppPredictResult{};
460 }
461
463 if (this != &other) {
464 fastlowess_predict_free_result(&result_);
465 result_ = other.result_;
466 other.result_ = fastlowess_CppPredictResult{};
467 }
468 return *this;
469 }
470
472 size_t size() const { return static_cast<size_t>(result_.n); }
473
475 bool valid() const { return result_.n > 0 && result_.error == nullptr; }
476
478 std::string error() const {
479 return result_.error != nullptr ? std::string(result_.error) : "";
480 }
481
483 std::vector<double> y() const {
484 if (result_.n == 0 || result_.y == nullptr) {
485 return {};
486 }
487 return std::vector<double>(result_.y, result_.y + result_.n);
488 }
489
491 std::vector<double> standard_errors() const {
492 if (result_.standard_errors != nullptr) {
493 return std::vector<double>(result_.standard_errors,
494 result_.standard_errors + result_.n);
495 }
496 return {};
497 }
498
500 std::vector<double> confidence_lower() const {
501 if (result_.confidence_lower != nullptr) {
502 return std::vector<double>(result_.confidence_lower,
503 result_.confidence_lower + result_.n);
504 }
505 return {};
506 }
507
509 std::vector<double> confidence_upper() const {
510 if (result_.confidence_upper != nullptr) {
511 return std::vector<double>(result_.confidence_upper,
512 result_.confidence_upper + result_.n);
513 }
514 return {};
515 }
516
518 std::vector<double> prediction_lower() const {
519 if (result_.prediction_lower != nullptr) {
520 return std::vector<double>(result_.prediction_lower,
521 result_.prediction_lower + result_.n);
522 }
523 return {};
524 }
525
527 std::vector<double> prediction_upper() const {
528 if (result_.prediction_upper != nullptr) {
529 return std::vector<double>(result_.prediction_upper,
530 result_.prediction_upper + result_.n);
531 }
532 return {};
533 }
534
537 std::vector<double> derivative() const {
538 if (result_.derivative != nullptr) {
539 return std::vector<double>(result_.derivative,
540 result_.derivative + result_.n);
541 }
542 return {};
543 }
544
545private:
546 fastlowess_CppPredictResult result_ = {};
547};
548
556public:
557 PredictModel() = default;
558
559 explicit PredictModel(fastlowess_CppPredictHandle *handle) : ptr_(handle) {}
560
562 if (ptr_ != nullptr) {
563 fastlowess_predict_handle_free(ptr_);
564 }
565 }
566
567 // Non-copyable
568 PredictModel(const PredictModel &) = delete;
570
571 // Move-able
572 PredictModel(PredictModel &&other) noexcept : ptr_(other.ptr_) {
573 other.ptr_ = nullptr;
574 }
575
577 if (this != &other) {
578 if (ptr_ != nullptr) {
579 fastlowess_predict_handle_free(ptr_);
580 }
581 ptr_ = other.ptr_;
582 other.ptr_ = nullptr;
583 }
584 return *this;
585 }
586
588 bool valid() const { return ptr_ != nullptr; }
589
592 PredictResult predict(const std::vector<double> &new_x,
593 const PredictOptions &options = {}) const {
594 validateOutputs(options.outputs, {"se", "derivative"}, "Predict");
595 const auto result = fastlowess_predict(
596 ptr_, new_x.data(), static_cast<size_t>(new_x.size()),
597 hasOutput(options.outputs, "se") ? 1 : 0, options.intervals.confidence,
598 options.intervals.prediction,
599 hasOutput(options.outputs, "derivative") ? 1 : 0,
600 options.extrapolation.c_str(), options.max_extrapolation_distance,
601 options.max_neighbor_distance, options.intervals.bootstrap,
602 options.seed.value_or(0), options.seed.has_value() ? 1 : 0);
603 return PredictResult(result);
604 }
605
606private:
607 fastlowess_CppPredictHandle *ptr_ = nullptr;
608};
609
616public:
617 LowessResult() : result_(empty_result()) {}
618
619 explicit LowessResult(const fastlowess_CppLowessResult &c_result)
620 : result_(c_result) {}
621
622 ~LowessResult() { release(); }
623
624 // Move-only
625 LowessResult(const LowessResult &) = delete;
627
628 LowessResult(LowessResult &&other) noexcept : result_(other.result_) {
629 other.result_ = empty_result();
630 }
631
633 if (this != &other) {
634 release();
635 result_ = other.result_;
636 other.result_ = empty_result();
637 }
638 return *this;
639 }
640
642 size_t size() const { return static_cast<size_t>(result_.n); }
643
645 bool valid() const { return result_.n > 0 && result_.error == nullptr; }
646
648 std::string error() const {
649 return result_.error != nullptr ? std::string(result_.error) : "";
650 }
651
653 double x_value(size_t index) const {
654 if (result_.x == nullptr || index >= size()) {
655 throw std::out_of_range("LOWESS x index out of range");
656 }
657 return result_.x[index];
658 }
659
661 double y_value(size_t index) const {
662 if (result_.y == nullptr || index >= size()) {
663 throw std::out_of_range("LOWESS y index out of range");
664 }
665 return result_.y[index];
666 }
667
669 std::vector<double> x_vector() const {
670 if (result_.n == 0 || result_.x == nullptr) {
671 return {};
672 }
673 return std::vector<double>(result_.x, result_.x + result_.n);
674 }
675
677 std::vector<double> y_vector() const {
678 if (result_.n == 0 || result_.y == nullptr) {
679 return {};
680 }
681 return std::vector<double>(result_.y, result_.y + result_.n);
682 }
683
685 std::vector<double> residuals() const {
686 if (result_.residuals != nullptr) {
687 return std::vector<double>(result_.residuals,
688 result_.residuals + result_.n);
689 }
690 return {};
691 }
692
694 std::vector<double> standard_errors() const {
695 if (result_.standard_errors != nullptr) {
696 return std::vector<double>(result_.standard_errors,
697 result_.standard_errors + result_.n);
698 }
699 return {};
700 }
701
703 std::vector<double> confidence_lower() const {
704 if (result_.confidence_lower != nullptr) {
705 return std::vector<double>(result_.confidence_lower,
706 result_.confidence_lower + result_.n);
707 }
708 return {};
709 }
710
712 std::vector<double> confidence_upper() const {
713 if (result_.confidence_upper != nullptr) {
714 return std::vector<double>(result_.confidence_upper,
715 result_.confidence_upper + result_.n);
716 }
717 return {};
718 }
719
721 std::vector<double> prediction_lower() const {
722 if (result_.prediction_lower != nullptr) {
723 return std::vector<double>(result_.prediction_lower,
724 result_.prediction_lower + result_.n);
725 }
726 return {};
727 }
728
730 std::vector<double> prediction_upper() const {
731 if (result_.prediction_upper != nullptr) {
732 return std::vector<double>(result_.prediction_upper,
733 result_.prediction_upper + result_.n);
734 }
735 return {};
736 }
737
739 std::vector<double> robustness_weights() const {
740 if (result_.robustness_weights != nullptr) {
741 return std::vector<double>(result_.robustness_weights,
742 result_.robustness_weights + result_.n);
743 }
744 return {};
745 }
746
748 std::vector<double> derivative() const {
749 if (result_.derivative != nullptr) {
750 return std::vector<double>(result_.derivative,
751 result_.derivative + result_.n);
752 }
753 return {};
754 }
755
757 std::vector<double> cv_scores() const {
758 if (result_.cv_scores != nullptr) {
759 return std::vector<double>(result_.cv_scores,
760 result_.cv_scores + result_.cv_scores_len);
761 }
762 return {};
763 }
764
766 double fraction_used() const { return result_.fraction_used; }
767
769 int iterations_used() const { return result_.iterations_used; }
770
772 Diagnostics diagnostics() const { return Diagnostics(result_); }
773
779 return PredictModel(fastlowess_lowess_take_predict_handle(&result_));
780 }
781
782private:
783 static fastlowess_CppLowessResult empty_result() {
784 fastlowess_CppLowessResult result{};
785 result.iterations_used = -1;
786 result.rmse = NAN;
787 result.mae = NAN;
788 result.r_squared = NAN;
789 result.aic = NAN;
790 result.aicc = NAN;
791 result.effective_df = NAN;
792 result.residual_sd = NAN;
793 return result;
794 }
795
796 void release() noexcept { fastlowess_lowess_free_result(&result_); }
797
798 fastlowess_CppLowessResult result_ = {};
799};
800
804class Lowess {
805public:
806 explicit Lowess(const LowessOptions &options = {}) {
808 options.outputs,
809 {"diagnostics", "residuals", "weights", "derivative", "se", "sorted"},
810 "Lowess");
811 custom_weights_ = options.custom_weights;
812 ptr_ = fastlowess_lowess_new(
813 options.fraction, options.iterations, options.delta,
814 options.weight_function.c_str(), options.robustness_method.c_str(),
815 options.scaling_method.c_str(), options.boundary_policy.c_str(),
816 options.intervals.confidence, options.intervals.prediction,
817 hasOutput(options.outputs, "diagnostics") ? 1 : 0,
818 hasOutput(options.outputs, "residuals") ? 1 : 0,
819 hasOutput(options.outputs, "weights") ? 1 : 0,
820 hasOutput(options.outputs, "derivative") ? 1 : 0,
821 options.zero_weight_fallback.c_str(), options.auto_converge,
822 options.cv.fractions.empty() ? nullptr : options.cv.fractions.data(),
823 static_cast<size_t>(options.cv.fractions.size()),
824 options.cv.method.c_str(), options.cv.k, options.parallel ? 1 : 0,
825 hasOutput(options.outputs, "se") ? 1 : 0,
826 hasOutput(options.outputs, "sorted") ? 1 : 0, options.backend.c_str(),
827 options.missing.c_str(), options.retain_model ? 1 : 0);
828 if (ptr_ == nullptr) {
829 throwInitializationError("Failed to initialize Lowess");
830 }
831 if (options.seed.has_value()) {
832 fastlowess_lowess_set_seed(ptr_, *options.seed);
833 }
834 if (options.intervals.bootstrap > 0) {
835 fastlowess_lowess_set_bootstrap(ptr_, options.intervals.bootstrap);
836 }
837 }
838
840 if (ptr_ != nullptr) {
841 fastlowess_lowess_free(ptr_);
842 }
843 }
844
845 // Non-copyable
846 Lowess(const Lowess &) = delete;
847 Lowess &operator=(const Lowess &) = delete;
848
849 // Move-able
850 Lowess(Lowess &&other) noexcept : ptr_(other.ptr_) { other.ptr_ = nullptr; }
851
852 Lowess &operator=(Lowess &&other) noexcept {
853 if (this != &other) {
854 if (ptr_ != nullptr) {
855 fastlowess_lowess_free(ptr_);
856 }
857 ptr_ = other.ptr_;
858 other.ptr_ = nullptr;
859 }
860 return *this;
861 }
862
863 // NOLINTBEGIN(bugprone-easily-swappable-parameters): intentional (x, y,
864 // weights) API
865 Expected<LowessResult> fit(const std::vector<double> &x_values,
866 const std::vector<double> &y_values,
867 const std::vector<double> &custom_weights = {}) {
868 // NOLINTEND(bugprone-easily-swappable-parameters)
869 if (x_values.size() != y_values.size()) {
871 "x and y must have the same length");
872 }
873 if (x_values.empty()) {
875 "Input arrays must not be empty");
876 }
877 const std::vector<double> &weights =
878 custom_weights.empty() ? custom_weights_ : custom_weights;
879 if (!weights.empty() && weights.size() != y_values.size()) {
881 "custom_weights length must match y length");
882 }
883
884 auto result =
885 fastlowess_lowess_fit(ptr_, x_values.data(), y_values.data(),
886 static_cast<size_t>(x_values.size()),
887 weights.empty() ? nullptr : weights.data(),
888 static_cast<size_t>(weights.size()));
889
890 if (result.error != nullptr) {
891 const std::string error_msg(result.error);
892 fastlowess_lowess_free_result(&result);
893 return Expected<LowessResult>::make_error(error_msg);
894 }
895
896 return Expected<LowessResult>(LowessResult(result));
897 }
898
899private:
900 fastlowess_CppLowess *ptr_ = nullptr;
901 std::vector<double> custom_weights_;
902};
903
908public:
910 validate_options(options);
912 options.fraction, options.iterations, options.delta,
913 options.weight_function.c_str(), options.robustness_method.c_str(),
914 options.scaling_method.c_str(), options.boundary_policy.c_str(),
915 hasOutput(options.outputs, "diagnostics") ? 1 : 0,
916 hasOutput(options.outputs, "residuals") ? 1 : 0,
917 hasOutput(options.outputs, "weights") ? 1 : 0,
918 hasOutput(options.outputs, "derivative") ? 1 : 0,
919 options.zero_weight_fallback.c_str(), options.auto_converge,
920 options.parallel ? 1 : 0, options.chunk_size, options.overlap,
921 options.merge_strategy.c_str(), options.missing.c_str(),
922 hasOutput(options.outputs, "se") ? 1 : 0, options.intervals.confidence,
923 options.intervals.prediction, options.intervals.bootstrap,
924 options.seed.value_or(0), options.seed.has_value() ? 1 : 0);
925 if (ptr_ == nullptr) {
926 throwInitializationError("Failed to initialize StreamingLowess");
927 }
928 }
929
931 if (ptr_ != nullptr) {
933 }
934 }
935
939 other.ptr_ = nullptr;
940 }
942 if (this != &other) {
943 if (ptr_ != nullptr) {
945 }
946 ptr_ = other.ptr_;
947 other.ptr_ = nullptr;
948 }
949 return *this;
950 }
951
953 const std::vector<double> &y_values) {
954 if (expect_finalized_) {
955 return Expected<LowessResult>::make_error("Model already finalized");
956 }
957 if (x_values.size() != y_values.size()) {
958 return Expected<LowessResult>::make_error("x and y length mismatch");
959 }
960
961 auto result =
963 static_cast<size_t>(x_values.size()));
964
965 if (result.error != nullptr) {
966 const std::string error_msg(result.error);
969 }
971 }
972
974 if (expect_finalized_) {
975 return Expected<LowessResult>::make_error("Model already finalized");
976 }
977 expect_finalized_ = true;
978
980 if (result.error != nullptr) {
981 const std::string error_msg(result.error);
984 }
986 }
987
988private:
989 static void validate_options(const StreamingOptions &options) {
990 validateOutputs(options.outputs,
991 {"diagnostics", "residuals", "weights", "derivative", "se"},
992 "StreamingLowess");
993 if (!options.cv.fractions.empty() || options.backend != "cpu" ||
994 !options.custom_weights.empty() ||
995 hasOutput(options.outputs, "sorted") || options.retain_model) {
996 throw LowessError(
997 "StreamingLowess does not support Batch-only options: cv, backend, "
998 "custom_weights, outputs containing 'sorted', or retain_model");
999 }
1000 }
1001
1002 fastlowess_CppStreamingLowess *ptr_ = nullptr;
1003 bool expect_finalized_ = false;
1004};
1005
1010public:
1011 explicit OnlineLowess(const OnlineOptions &options = {}) {
1012 validateOutputs(options.outputs, {"weights", "derivative", "se"},
1013 "OnlineLowess");
1014 ptr_ = fastlowess_online_new(
1015 options.fraction, options.iterations, options.delta,
1016 options.weight_function.c_str(), options.robustness_method.c_str(),
1017 options.scaling_method.c_str(), options.boundary_policy.c_str(),
1018 hasOutput(options.outputs, "weights") ? 1 : 0,
1019 hasOutput(options.outputs, "derivative") ? 1 : 0,
1020 options.zero_weight_fallback.c_str(), options.auto_converge,
1021 options.window_capacity, options.min_points,
1022 options.update_mode.c_str(), options.missing.c_str(),
1023 hasOutput(options.outputs, "se") ? 1 : 0, options.intervals.confidence,
1024 options.intervals.prediction, options.intervals.bootstrap,
1025 options.seed.value_or(0), options.seed.has_value() ? 1 : 0);
1026 if (ptr_ == nullptr) {
1027 throwInitializationError("Failed to initialize OnlineLowess");
1028 }
1029 }
1030
1032 if (ptr_ != nullptr) {
1034 }
1035 }
1036
1037 OnlineLowess(const OnlineLowess &) = delete;
1040 other.ptr_ = nullptr;
1041 }
1043 if (this != &other) {
1044 if (ptr_ != nullptr) {
1046 }
1047 ptr_ = other.ptr_;
1048 other.ptr_ = nullptr;
1049 }
1050 return *this;
1051 }
1052
1061 auto raw = fastlowess_online_add_point(ptr_, x, y);
1062
1063 if (raw.error != nullptr) {
1064 const std::string error_msg(raw.error);
1067 }
1069 }
1070
1071private:
1072 fastlowess_CppOnlineLowess *ptr_ = nullptr;
1073};
1074
1075namespace gpu {
1076
1077namespace detail {
1078inline std::optional<std::string> platformTag() {
1079#ifdef _WIN32
1080 return "windows";
1081#elif defined(__APPLE__) && TARGET_OS_OSX
1082 return "macos";
1083#elif defined(__ANDROID__)
1084 return std::nullopt;
1085#elif defined(__linux__) && defined(__GLIBC__)
1086 return "linux";
1087#else
1088 return std::nullopt;
1089#endif
1090}
1091
1092inline std::optional<std::string> archTag() {
1093#if defined(__aarch64__) || defined(_M_ARM64)
1094 return "aarch64";
1095#elif defined(__x86_64__) || defined(_M_X64)
1096 return "x86_64";
1097#else
1098 return std::nullopt;
1099#endif
1100}
1101
1102inline std::string libraryExt() {
1103#ifdef _WIN32
1104 return "dll";
1105#elif defined(__APPLE__)
1106 return "dylib";
1107#else
1108 return "so";
1109#endif
1110}
1111
1112inline bool stdinIsInteractive() {
1113#ifdef _WIN32
1114 return _isatty(_fileno(stdin)) != 0; // NOLINT(misc-include-cleaner)
1115#else
1116 return isatty(fileno(stdin)) != 0; // NOLINT(misc-include-cleaner)
1117#endif
1118}
1119
1120inline std::string cacheDir() {
1121#ifdef _WIN32
1122 char *home_buf = nullptr;
1123 size_t home_sz = 0;
1124 _dupenv_s(&home_buf, &home_sz, "USERPROFILE"); // NOLINT(misc-include-cleaner)
1125 const std::string base = (home_buf != nullptr) ? home_buf : ".";
1126 free(home_buf);
1127#else
1128 const char *home = std::getenv("HOME");
1129 const std::string base = (home != nullptr) ? home : ".";
1130#endif
1131 return base + "/.fastlowess/gpu";
1132}
1133
1134inline bool makeDir(const std::string &dir) {
1135 std::error_code error;
1136 std::filesystem::create_directories(dir, error);
1137 return !error && std::filesystem::is_directory(dir, error) && !error;
1138}
1139
1140inline bool fileExists(const std::string &path) {
1141 const std::ifstream file(path);
1142 return file.good();
1143}
1144
1145inline bool copyFile(const std::string &src, const std::string &dst) {
1146 std::error_code error;
1147 return std::filesystem::copy_file(
1148 src, dst, std::filesystem::copy_options::overwrite_existing, error);
1149}
1150
1151inline bool gpuLibraryEnabled(const std::string &path) {
1152 using GpuEnabledFunction = int (*)();
1153#ifdef _WIN32
1155 const bool restore_error_mode =
1157 &previous_error_mode) != 0;
1158 const HMODULE library = LoadLibraryA(path.c_str());
1159 if (restore_error_mode) {
1161 }
1162 if (library == nullptr) {
1163 return false;
1164 }
1165 const auto gpu_enabled = reinterpret_cast<GpuEnabledFunction>(
1166 GetProcAddress(library, "fastlowess_gpu_enabled"));
1167 const bool enabled = gpu_enabled != nullptr && gpu_enabled() != 0;
1169#else
1170 void *library = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
1171 if (library == nullptr) {
1172 return false;
1173 }
1174 const auto gpu_enabled = reinterpret_cast<GpuEnabledFunction>(
1175 dlsym(library, "fastlowess_gpu_enabled"));
1176 const bool enabled = gpu_enabled != nullptr && gpu_enabled() != 0;
1178#endif
1179 return enabled;
1180}
1181
1182inline std::string stagingPath(const std::string &destination) {
1183 static std::atomic<unsigned long long> sequence{0};
1184#ifdef _WIN32
1185 const auto process_id = GetCurrentProcessId();
1186#else
1187 const auto process_id = getpid();
1188#endif
1189 return destination + ".tmp-" + std::to_string(process_id) + "-" +
1190 std::to_string(sequence.fetch_add(1, std::memory_order_relaxed));
1191}
1192
1193inline void removeFile(const std::string &path) {
1194 std::error_code error;
1195 std::filesystem::remove(path, error);
1196}
1197
1198inline bool promoteFile(const std::string &staging,
1199 const std::string &destination) {
1200#ifdef _WIN32
1201 return MoveFileExA(staging.c_str(), destination.c_str(),
1203#else
1204 std::error_code error;
1205 std::filesystem::rename(staging, destination, error);
1206 return !error;
1207#endif
1208}
1209
1210inline bool installLocalGpuFile(const std::filesystem::path &source,
1211 const std::string &destination) {
1212 const std::string source_path = source.string();
1213 if (!gpuLibraryEnabled(source_path)) {
1214 return false;
1215 }
1216 const std::string staging = stagingPath(destination);
1217 if (!copyFile(source_path, staging)) {
1218 removeFile(staging);
1219 return false;
1220 }
1221 if (!gpuLibraryEnabled(staging) || !promoteFile(staging, destination)) {
1222 removeFile(staging);
1223 return false;
1224 }
1225 return true;
1226}
1227
1228inline bool runProcess(const std::vector<std::string> &arguments) {
1229 if (arguments.empty()) {
1230 return false;
1231 }
1232
1233#ifdef _WIN32
1234 std::vector<const char *> argv;
1235 argv.reserve(arguments.size() + 1);
1236 for (const auto &argument : arguments) {
1237 argv.push_back(argument.c_str());
1238 }
1239 argv.push_back(nullptr);
1240 return _spawnvp(_P_WAIT, argv.front(), argv.data()) == 0;
1241#else
1242 std::vector<char *> argv;
1243 argv.reserve(arguments.size() + 1);
1244 for (const auto &argument : arguments) {
1245 argv.push_back(const_cast<char *>(argument.c_str()));
1246 }
1247 argv.push_back(nullptr);
1248
1249 auto child = decltype(waitpid(0, nullptr, 0)){};
1250 if (posix_spawnp(&child, argv.front(), nullptr, nullptr, argv.data(),
1251 environ) != 0) {
1252 return false;
1253 }
1254 int status = 0;
1255 decltype(child) waited;
1256 do {
1257 waited = waitpid(child, &status, 0);
1258 } while (waited < 0 && errno == EINTR);
1259 // NOLINTNEXTLINE(misc-include-cleaner): wait macros come from sys/wait.h.
1260 return waited == child && WIFEXITED(status) && WEXITSTATUS(status) == 0;
1261#endif
1262}
1263
1264} // namespace detail
1265
1268inline bool available() { return fastlowess_gpu_enabled() != 0; }
1269
1292inline bool install(bool yes = false, const std::string &local_path = "") {
1293 if (available()) {
1294 std::cout << "GPU backend is already active.\n";
1295 return true;
1296 }
1297
1298 const std::string dir = detail::cacheDir();
1299 if (!detail::makeDir(dir)) {
1300 std::cerr << "Failed to create GPU cache directory: " << dir << "\n";
1301 return false;
1302 }
1303
1304 if (!local_path.empty()) {
1305 if (!detail::fileExists(local_path)) {
1306 std::cerr << "No such file: " << local_path << "\n";
1307 return false;
1308 }
1309 if (!detail::gpuLibraryEnabled(local_path)) {
1310 std::cerr << "The library at " << local_path
1311 << " does not report GPU support.\n";
1312 return false;
1313 }
1314 if (!yes) {
1315 if (!detail::stdinIsInteractive()) {
1316 std::cerr << "install_gpu() requires confirmation; pass yes=true to "
1317 "proceed non-interactively."
1318 << "\n";
1319 return false;
1320 }
1321 std::cout << "Install " << local_path
1322 << " in place of the current build? [y/N] ";
1323 std::string answer;
1324 std::getline(std::cin, answer);
1325 if (answer != "y" && answer != "Y" && answer != "yes") {
1326 std::cout << "Aborted.\n";
1327 return false;
1328 }
1329 }
1330
1331 const std::string dest = (std::filesystem::path(dir) /
1332 std::filesystem::path(local_path).filename())
1333 .string();
1334 std::cout << "Installing " << local_path << " ...\n";
1335 if (!detail::installLocalGpuFile(local_path, dest)) {
1336 std::cerr << "Failed to validate/install " << local_path << " to " << dest
1337 << "\n";
1338 return false;
1339 }
1340 std::cout << "Installed to " << dest << "\n";
1341 std::cout << "Relink your application against this library (or "
1342 "dlopen/LoadLibrary it) and restart to use the GPU backend."
1343 << "\n";
1344 return true;
1345 }
1346
1347 const auto platform = detail::platformTag();
1348 const auto architecture = detail::archTag();
1349 if (!platform || !architecture) {
1350 std::cerr << "No prebuilt GPU library is available for this platform and "
1351 "architecture."
1352 << "\n";
1353 return false;
1354 }
1355
1356 const std::string version = fastlowess_version();
1357 const std::string asset = "libfastlowess_cpp-gpu-v" + version + "-" +
1358 *platform + "-" + *architecture + "." +
1359 detail::libraryExt();
1360 // GPU artifacts across all versions live in this one perpetual release
1361 // instead of cluttering each version's own release page; the source
1362 // version is embedded in the asset filename above instead.
1363 const std::string url =
1364 "https://github.com/thisisamirv/lowess-project/releases/download/"
1365 "gpu-builds/" +
1366 asset;
1367
1368 if (!yes) {
1369 if (!detail::stdinIsInteractive()) {
1370 std::cerr << "install_gpu() requires confirmation; pass yes=true to "
1371 "proceed non-interactively."
1372 << "\n";
1373 return false;
1374 }
1375 std::cout << "Download and install " << asset
1376 << " from github.com/thisisamirv/lowess-project? [y/N] ";
1377 std::string answer;
1378 std::getline(std::cin, answer);
1379 if (answer != "y" && answer != "Y" && answer != "yes") {
1380 std::cout << "Aborted.\n";
1381 return false;
1382 }
1383 }
1384
1385 const std::string dest = (std::filesystem::path(dir) / asset).string();
1386 const std::string staging = detail::stagingPath(dest);
1387
1388 std::cout << "Downloading " << url << " ...\n";
1389 if (!detail::runProcess(
1390 {"curl", "-fL", "--progress-bar", "-o", staging, url})) {
1391 detail::removeFile(staging);
1392 std::cerr << "Download failed. A matching GPU build may not exist for "
1393 "this platform/version yet, or `curl` is not installed."
1394 << "\n";
1395 return false;
1396 }
1397 if (!detail::gpuLibraryEnabled(staging)) {
1398 detail::removeFile(staging);
1399 std::cerr << "Downloaded library does not report GPU support.\n";
1400 return false;
1401 }
1402 if (!detail::promoteFile(staging, dest)) {
1403 detail::removeFile(staging);
1404 std::cerr << "Failed to install downloaded library to " << dest << "\n";
1405 return false;
1406 }
1407
1408 std::cout << "Downloaded to " << dest << "\n";
1409 std::cout << "Relink your application against this library (or "
1410 "dlopen/LoadLibrary it) and restart to use the GPU backend."
1411 << "\n";
1412 return true;
1413}
1414
1415} // namespace gpu
1416
1417} // namespace fastlowess
1418
1419#endif // FASTLOWESS_HPP
Diagnostics from LOWESS fitting.
Definition fastlowess.hpp:379
std::optional< double > aic() const
Akaike Information Criterion, if computed.
Definition fastlowess.hpp:403
std::optional< double > aicc() const
Corrected AIC, if computed.
Definition fastlowess.hpp:405
bool has_value() const
Definition fastlowess.hpp:394
Diagnostics(const fastlowess_CppLowessResult &result)
Definition fastlowess.hpp:383
double rmse() const
Definition fastlowess.hpp:396
double mae() const
Definition fastlowess.hpp:397
double r_squared() const
Definition fastlowess.hpp:398
double residual_sd() const
Batch: robust residual scale estimate (1.4826 * MAD); Streaming: cumulative sample SD of emitted resi...
Definition fastlowess.hpp:401
std::optional< double > effective_df() const
Effective degrees of freedom, if computed.
Definition fastlowess.hpp:407
A result type that holds either a value or an error.
Definition fastlowess.hpp:135
static Expected make_error(std::string msg)
Definition fastlowess.hpp:142
const std::string & error() const
Definition fastlowess.hpp:171
bool has_value() const
Definition fastlowess.hpp:146
T && value() &&
Definition fastlowess.hpp:164
Expected(T val)
Definition fastlowess.hpp:138
const T & value() const &
Definition fastlowess.hpp:157
T & value() &
Definition fastlowess.hpp:150
Exception thrown when LOWESS operation fails.
Definition fastlowess.hpp:105
LowessError(const std::string &message)
Definition fastlowess.hpp:107
Result of LOWESS smoothing operation.
Definition fastlowess.hpp:615
LowessResult & operator=(const LowessResult &)=delete
LowessResult(LowessResult &&other) noexcept
Definition fastlowess.hpp:628
std::vector< double > confidence_lower() const
Get confidence interval lower bounds.
Definition fastlowess.hpp:703
bool valid() const
Check if result is valid.
Definition fastlowess.hpp:645
std::vector< double > residuals() const
Get residuals (empty if not computed)
Definition fastlowess.hpp:685
double y_value(size_t index) const
Access smoothed y value at index.
Definition fastlowess.hpp:661
double x_value(size_t index) const
Access x value at index.
Definition fastlowess.hpp:653
std::vector< double > cv_scores() const
Get cross-validation scores (empty if not computed)
Definition fastlowess.hpp:757
std::vector< double > confidence_upper() const
Get confidence interval upper bounds.
Definition fastlowess.hpp:712
std::vector< double > prediction_lower() const
Get prediction interval lower bounds.
Definition fastlowess.hpp:721
std::vector< double > standard_errors() const
Get standard errors (empty if not computed)
Definition fastlowess.hpp:694
std::vector< double > y_vector() const
Get smoothed y values as vector.
Definition fastlowess.hpp:677
double fraction_used() const
Fraction used for smoothing.
Definition fastlowess.hpp:766
int iterations_used() const
Number of iterations performed (-1 if not available)
Definition fastlowess.hpp:769
Diagnostics diagnostics() const
Get diagnostics.
Definition fastlowess.hpp:772
LowessResult(const LowessResult &)=delete
std::string error() const
Get error message (empty if no error)
Definition fastlowess.hpp:648
size_t size() const
Number of data points.
Definition fastlowess.hpp:642
std::vector< double > derivative() const
Get the per-point local fit derivative (slope) (empty if not computed)
Definition fastlowess.hpp:748
std::vector< double > prediction_upper() const
Get prediction interval upper bounds.
Definition fastlowess.hpp:730
std::vector< double > robustness_weights() const
Get robustness weights (empty if not computed)
Definition fastlowess.hpp:739
LowessResult(const fastlowess_CppLowessResult &c_result)
Definition fastlowess.hpp:619
std::vector< double > x_vector() const
Get x values as vector.
Definition fastlowess.hpp:669
LowessResult()
Definition fastlowess.hpp:617
LowessResult & operator=(LowessResult &&other) noexcept
Definition fastlowess.hpp:632
~LowessResult()
Definition fastlowess.hpp:622
PredictModel predict_model()
Extract the retained predict model (only available when LowessOptions::retain_model was set to true b...
Definition fastlowess.hpp:778
Batch LOWESS model.
Definition fastlowess.hpp:804
Lowess & operator=(Lowess &&other) noexcept
Definition fastlowess.hpp:852
Expected< LowessResult > fit(const std::vector< double > &x_values, const std::vector< double > &y_values, const std::vector< double > &custom_weights={})
Definition fastlowess.hpp:865
Lowess(const Lowess &)=delete
~Lowess()
Definition fastlowess.hpp:839
Lowess & operator=(const Lowess &)=delete
Lowess(const LowessOptions &options={})
Definition fastlowess.hpp:806
Lowess(Lowess &&other) noexcept
Definition fastlowess.hpp:850
Online LOWESS model.
Definition fastlowess.hpp:1009
OnlineLowess(const OnlineOptions &options={})
Definition fastlowess.hpp:1011
OnlineLowess(const OnlineLowess &)=delete
Expected< OnlineOutput > add_point(double x, double y)
Add a single point and return the smoothed value.
Definition fastlowess.hpp:1060
OnlineLowess(OnlineLowess &&other) noexcept
Definition fastlowess.hpp:1039
~OnlineLowess()
Definition fastlowess.hpp:1031
OnlineLowess & operator=(const OnlineLowess &)=delete
OnlineLowess & operator=(OnlineLowess &&other) noexcept
Definition fastlowess.hpp:1042
Result of a single online update step.
Definition fastlowess.hpp:304
OnlinePointStatus status() const
Detailed outcome of the latest add_point call.
Definition fastlowess.hpp:313
double confidence_upper() const
Upper confidence interval bound for the latest point (NaN if not computed).
Definition fastlowess.hpp:339
bool dropped() const
True when missing = "drop" ignored a non-finite point.
Definition fastlowess.hpp:310
double derivative() const
Local fit derivative (slope) for the latest point (NaN if not computed).
Definition fastlowess.hpp:331
double standard_error() const
Standard error (NaN if not computed).
Definition fastlowess.hpp:319
double prediction_upper() const
Upper prediction interval bound for the latest point (NaN if not computed).
Definition fastlowess.hpp:347
double confidence_lower() const
Lower confidence interval bound for the latest point (NaN if not computed).
Definition fastlowess.hpp:335
double prediction_lower() const
Lower prediction interval bound for the latest point (NaN if not computed).
Definition fastlowess.hpp:343
double robustness_weight() const
Robustness weight for the latest point (NaN if not computed).
Definition fastlowess.hpp:325
double y() const
Smoothed value for the latest point (valid only when has_value() == true).
Definition fastlowess.hpp:316
int iterations_used() const
Number of robustness iterations performed (−1 if not applicable).
Definition fastlowess.hpp:328
double residual() const
Residual y − smoothed (NaN if not computed).
Definition fastlowess.hpp:322
bool has_value() const
True when the window has enough points to produce a smoothed estimate.
Definition fastlowess.hpp:307
Retained fitted-model state enabling out-of-sample predict().
Definition fastlowess.hpp:555
PredictModel(const PredictModel &)=delete
PredictModel & operator=(const PredictModel &)=delete
PredictModel(fastlowess_CppPredictHandle *handle)
Definition fastlowess.hpp:559
PredictResult predict(const std::vector< double > &new_x, const PredictOptions &options={}) const
Evaluate the fitted model at out-of-sample query points not in the training set.
Definition fastlowess.hpp:592
PredictModel & operator=(PredictModel &&other) noexcept
Definition fastlowess.hpp:576
bool valid() const
True if this model was actually retained (retain_model was set).
Definition fastlowess.hpp:588
PredictModel(PredictModel &&other) noexcept
Definition fastlowess.hpp:572
~PredictModel()
Definition fastlowess.hpp:561
Result of PredictModel::predict().
Definition fastlowess.hpp:445
std::vector< double > y() const
Predicted y values, one per query point.
Definition fastlowess.hpp:483
std::vector< double > prediction_lower() const
Lower prediction interval bounds (empty if not requested)
Definition fastlowess.hpp:518
std::vector< double > standard_errors() const
Standard errors (empty if not requested)
Definition fastlowess.hpp:491
std::vector< double > confidence_lower() const
Lower confidence interval bounds (empty if not requested)
Definition fastlowess.hpp:500
PredictResult(const fastlowess_CppPredictResult &c_result)
Definition fastlowess.hpp:449
PredictResult(const PredictResult &)=delete
PredictResult & operator=(PredictResult &&other) noexcept
Definition fastlowess.hpp:462
bool valid() const
Check if result is valid.
Definition fastlowess.hpp:475
std::string error() const
Get error message (empty if no error)
Definition fastlowess.hpp:478
~PredictResult()
Definition fastlowess.hpp:452
std::vector< double > confidence_upper() const
Upper confidence interval bounds (empty if not requested)
Definition fastlowess.hpp:509
std::vector< double > derivative() const
Local fit's derivative (slope) at each query point (empty if not requested)
Definition fastlowess.hpp:537
PredictResult(PredictResult &&other) noexcept
Definition fastlowess.hpp:458
std::vector< double > prediction_upper() const
Upper prediction interval bounds (empty if not requested)
Definition fastlowess.hpp:527
PredictResult & operator=(const PredictResult &)=delete
size_t size() const
Number of query points.
Definition fastlowess.hpp:472
Streaming LOWESS model.
Definition fastlowess.hpp:907
StreamingLowess & operator=(StreamingLowess &&other) noexcept
Definition fastlowess.hpp:941
StreamingLowess & operator=(const StreamingLowess &)=delete
Expected< LowessResult > finalize()
Definition fastlowess.hpp:973
StreamingLowess(StreamingLowess &&other) noexcept
Definition fastlowess.hpp:938
StreamingLowess(const StreamingLowess &)=delete
Expected< LowessResult > process_chunk(const std::vector< double > &x_values, const std::vector< double > &y_values)
Definition fastlowess.hpp:952
StreamingLowess(const StreamingOptions &options={})
Definition fastlowess.hpp:909
~StreamingLowess()
Definition fastlowess.hpp:930
constexpr int k_default_min_points
Definition fastlowess.hpp:81
constexpr int k_default_overlap
Sentinel meaning "use the library default" (chunk_size / 10, clamped to [1, chunk_size - 10]); negati...
Definition fastlowess.hpp:79
constexpr int k_default_chunk_size
Definition fastlowess.hpp:76
constexpr int k_default_cv_k
Definition fastlowess.hpp:75
constexpr double k_default_fraction
Definition fastlowess.hpp:74
constexpr int k_default_window_capacity
Definition fastlowess.hpp:80
Definition fastlowess.hpp:71
void throwInitializationError(const char *fallback)
Definition fastlowess.hpp:124
OnlinePointStatus
Outcome of OnlineLowess::add_point.
Definition fastlowess.hpp:281
@ Dropped
Point ignored under missing = "drop".
@ Filling
Window still has fewer than min_points finite observations.
@ Ready
Smoothed fields are valid for the latest point.
OnlinePointStatus onlinePointStatusFromRaw(int has_value)
Definition fastlowess.hpp:287
bool hasOutput(const std::vector< std::string > &outputs, const char *name)
Definition fastlowess.hpp:97
void validateOutputs(const std::vector< std::string > &outputs, std::initializer_list< const char * > supported, const char *adapter)
Definition fastlowess.hpp:111
Definition fastlowess.hpp:84
std::string method
Definition fastlowess.hpp:86
int k
Definition fastlowess.hpp:87
std::vector< double > fractions
Definition fastlowess.hpp:85
Definition fastlowess.hpp:141
Definition fastlowess.hpp:90
size_t bootstrap
Zero disables bootstrap refits.
Definition fastlowess.hpp:94
double confidence
Definition fastlowess.hpp:91
double prediction
Definition fastlowess.hpp:92
Options for configuring LOWESS smoothing.
Definition fastlowess.hpp:194
double delta
Interpolation threshold (NaN = auto)
Definition fastlowess.hpp:197
std::string scaling_method
Definition fastlowess.hpp:201
IntervalsOptions intervals
Definition fastlowess.hpp:205
std::vector< double > custom_weights
Per-observation case weights.
Definition fastlowess.hpp:225
std::string missing
Policy for non-finite (NaN/Inf) values in input data: "error" (default) or "drop".
Definition fastlowess.hpp:218
int iterations
Robustness iterations.
Definition fastlowess.hpp:196
std::string robustness_method
Definition fastlowess.hpp:200
bool parallel
Definition fastlowess.hpp:212
std::string boundary_policy
Definition fastlowess.hpp:202
std::string weight_function
Definition fastlowess.hpp:199
std::string backend
Execution backend: "cpu" (default) or "gpu" (requires the library to be built with the gpu Cargo feat...
Definition fastlowess.hpp:215
std::string zero_weight_fallback
Definition fastlowess.hpp:203
bool retain_model
Retain the fitted model's training data, enabling LowessResult::predict_model().
Definition fastlowess.hpp:229
std::optional< uint64_t > seed
Definition fastlowess.hpp:206
CVOptions cv
Cross-validation configuration. Empty fractions disable CV.
Definition fastlowess.hpp:221
double auto_converge
Auto-convergence threshold.
Definition fastlowess.hpp:207
double fraction
Smoothing fraction (0, 1].
Definition fastlowess.hpp:195
std::vector< std::string > outputs
Optional result components: diagnostics, residuals, weights, derivative, se, and sorted.
Definition fastlowess.hpp:211
Options for online LOWESS.
Definition fastlowess.hpp:252
int iterations
Robustness iterations (requires update_mode = "full")
Definition fastlowess.hpp:254
std::vector< std::string > outputs
Optional result components: "weights", "derivative", and/or "se".
Definition fastlowess.hpp:267
std::optional< uint64_t > seed
Definition fastlowess.hpp:269
double auto_converge
Auto-convergence threshold.
Definition fastlowess.hpp:263
double fraction
Smoothing fraction (0, 1].
Definition fastlowess.hpp:253
std::string update_mode
Definition fastlowess.hpp:277
int min_points
Definition fastlowess.hpp:276
std::string weight_function
Definition fastlowess.hpp:257
double delta
Interpolation threshold (NaN = auto)
Definition fastlowess.hpp:255
std::string boundary_policy
Definition fastlowess.hpp:260
std::string robustness_method
Definition fastlowess.hpp:258
std::string missing
Policy for non-finite (NaN/Inf) x/y values passed to add_point(): "error" (default) or "drop".
Definition fastlowess.hpp:273
int window_capacity
Definition fastlowess.hpp:275
std::string scaling_method
Definition fastlowess.hpp:259
IntervalsOptions intervals
Definition fastlowess.hpp:268
std::string zero_weight_fallback
Definition fastlowess.hpp:261
Options for PredictModel::predict().
Definition fastlowess.hpp:422
IntervalsOptions intervals
Definition fastlowess.hpp:425
std::vector< std::string > outputs
Optional prediction components: "se" and/or "derivative".
Definition fastlowess.hpp:424
std::optional< uint64_t > seed
Definition fastlowess.hpp:426
double max_extrapolation_distance
Under "linear" extrapolation, the maximum allowed distance beyond the training boundary before predic...
Definition fastlowess.hpp:433
std::string extrapolation
Behavior for query points outside the training range ("clamp", "linear", "error").
Definition fastlowess.hpp:429
double max_neighbor_distance
Maximum allowed distance to the farthest point in a query's local window before predict() errors,...
Definition fastlowess.hpp:437
Options for streaming LOWESS.
Definition fastlowess.hpp:235
int overlap
Negative (the default) means "use the library default" (chunk_size / 10, clamped to [1,...
Definition fastlowess.hpp:239
std::string merge_strategy
weighted_average, average, take_first, take_last
Definition fastlowess.hpp:240
int chunk_size
Definition fastlowess.hpp:236