fastlowess-cpp
High-performance LOWESS smoothing
Loading...
Searching...
No Matches
Quick Start

Quick Start

Get up and running with LOWESS in minutes.

Basic Smoothing

Smooth a noisy sine wave — the kind of signal where LOWESS shines. Each example recovers the underlying trend from 100 points of Gaussian noise.

#include <fastlowess.hpp>
#include <cmath>
#include <iostream>
#include <vector>
int main() {
// 100-point noisy sine wave (deterministic)
const int n = 100;
std::vector<double> x(n), y(n);
for (int i = 0; i < n; ++i) {
x[i] = i * 2 * M_PI / (n - 1);
y[i] = std::sin(x[i]) + ((i * 7 + 3) % 17 / 17.0 - 0.5) * 0.6;
}
fastlowess::Lowess model({ .fraction = 0.3, .iterations = 3 });
auto result = model.fit(x, y).value();
std::cout << "First smoothed value: " << result.y_vector()[0]
<< " (true: " << std::sin(x[0]) << ")\n";
return 0;
}
T & value() &
Definition fastlowess.hpp:150
Batch LOWESS model.
Definition fastlowess.hpp:804
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
C++ wrapper for fastLowess library.
First smoothed value: 0.0272725 (true: 0)

With Confidence Intervals

#include <fastlowess.hpp>
#include <cmath>
#include <iostream>
#include <vector>
int main() {
const int n = 100;
std::vector<double> x(n), y(n);
for (int i = 0; i < n; ++i) {
x[i] = i * 2 * M_PI / (n - 1);
y[i] = std::sin(x[i]) + 0.1;
}
options.fraction = 0.5;
options.iterations = 3;
options.outputs = {"diagnostics"};
options.intervals.confidence = 0.95;
options.intervals.prediction = 0.95;
fastlowess::Lowess model(options);
auto result = model.fit(x, y).value();
// Access standard C++ vectors
auto lower = result.confidence_lower();
auto upper = result.confidence_upper();
double r2 = result.diagnostics().r_squared();
std::cout << "95% CI: [" << result.confidence_lower()[0] << ", " << result.confidence_upper()[0] << "]\n";
return 0;
}
double confidence
Definition fastlowess.hpp:91
double prediction
Definition fastlowess.hpp:92
Options for configuring LOWESS smoothing.
Definition fastlowess.hpp:194
IntervalsOptions intervals
Definition fastlowess.hpp:205
int iterations
Robustness iterations.
Definition fastlowess.hpp:196
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
95% CI: [0.292929, 0.372154]

Handling Outliers

LOWESS can robustly handle outliers through iterative reweighting:

#include <fastlowess.hpp>
#include <cmath>
#include <iostream>
#include <vector>
int main() {
// Data with an outlier at index 3
std::vector<double> x_out = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
std::vector<double> y_outlier = {2.0, 4.0, 6.0, 50.0, 10.0, 12.0};
options.fraction = 0.7;
options.iterations = 5;
options.robustness_method = "bisquare";
options.outputs = {"weights"};
fastlowess::Lowess model(options);
auto result = model.fit(x_out, y_outlier).value();
// Check weights
auto weights = result.robustness_weights();
for (size_t i = 0; i < weights.size(); ++i) {
if (weights[i] < 0.5) {
std::cout << "Point " << i << " is outlier (weight: " << weights[i] << ")\n";
}
}
return 0;
}
std::string robustness_method
Definition fastlowess.hpp:200
Point 3 is outlier (weight: 0)

Streaming Mode

For datasets too large to fit in memory, stream them in fixed-size chunks with overlap.

#include <fastlowess.hpp>
#include <cmath>
#include <iostream>
#include <vector>
int main() {
const int n = 5000;
std::vector<double> x(n), y(n);
for (int i = 0; i < n; ++i) {
x[i] = i * 10 * M_PI / (n - 1);
y[i] = std::sin(x[i] / M_PI) * std::exp(-x[i] / 30.0)
+ ((i * 7 + 3) % 17 / 17.0 - 0.5) * 0.3;
}
opts.fraction = 0.2;
opts.chunk_size = 1000;
opts.overlap = 100;
for (int start = 0; start <= 4000; start += 1000) {
int end = std::min(start + 1000, n);
model.process_chunk(
std::vector<double>(x.begin() + start, x.begin() + end),
std::vector<double>(y.begin() + start, y.begin() + end)
);
}
auto result = model.finalize().value();
std::cout << "Smoothed " << result.y_vector().size() << " points in streaming mode\n";
return 0;
}
Streaming LOWESS model.
Definition fastlowess.hpp:907
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
int chunk_size
Definition fastlowess.hpp:236
Smoothed 100 points in streaming mode

Next Steps

Topic Link
How LOWESS works Concepts
All parameters explained API Reference
Batch vs Streaming vs Online Execution Modes
Edge handling Boundary
Outlier handling in depth Robustness
Full API per language API Reference