Skip to content

Commit d4b9d78

Browse files
committed
🚀 Add AR(p) model example
1 parent 337d855 commit d4b9d78

File tree

9 files changed

+80
-2
lines changed

9 files changed

+80
-2
lines changed

crates/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory("kf_linear")
2-
add_subdirectory("simple_optimizers")
2+
add_subdirectory("simple_optimizers")
3+
add_subdirectory("ar_models")

crates/ar_models/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Header-only target
2+
add_library(ar_models INTERFACE)
3+
4+
# Public includes (exports the include/ dir to consumers)
5+
target_include_directories(ar_models INTERFACE
6+
${CMAKE_CURRENT_SOURCE_DIR}/include
7+
)
8+
9+
# Link Eigen and set language level
10+
target_link_libraries(ar_models INTERFACE Eigen3::Eigen)
11+
target_compile_features(ar_models INTERFACE cxx_std_17)
12+
13+
# Demo executable
14+
add_executable(ar_demo src/main.cpp)
15+
target_link_libraries(ar_demo PRIVATE ar_models)
16+
17+
# Tests
18+
enable_testing()
19+
add_subdirectory(tests)

crates/ar_models/include/ar.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <Eigen/Dense>
4+
5+
namespace cppx::ar_models {
6+
7+
template <int order> class ARModel {
8+
public:
9+
using Vector = Eigen::Vector<double, order>;
10+
11+
ARModel() = default;
12+
ARModel(double mean, double noise_variance) : c_(mean), sigma2_(noise_variance){};
13+
14+
[[nodiscard]] double mean() const noexcept { return c_; }
15+
[[nodiscard]] double noise() const noexcept { return sigma2_; }
16+
[[nodiscard]] const Vector &coefficients() const noexcept { return phi_; }
17+
18+
private:
19+
Vector phi_;
20+
double c_ = 0.0;
21+
double sigma2_ = 1.0;
22+
};
23+
24+
} // namespace cppx::ar_models

crates/ar_models/src/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <iostream>
2+
3+
int main() {
4+
std::cout << "Hello, world!" << "\n";
5+
return 0;
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# crates/simple_optimizers/tests/CMakeLists.txt
2+
3+
add_executable(ar_models_test test_ar_models.cpp)
4+
5+
target_link_libraries(ar_models_test PRIVATE ar_models)
6+
7+
add_test(NAME ar_models_test COMMAND ar_models_test)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "ar.hpp"
2+
#include <cassert>
3+
4+
using namespace cppx::ar_models;
5+
6+
int main() {
7+
8+
{
9+
ARModel<1> model;
10+
assert(model.mean() == 0.0);
11+
assert(model.noise() == 1.0);
12+
assert(model.coefficients().size() == 1);
13+
}
14+
15+
return 0;
16+
};

crates/kf_linear/include/kf_linear.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <stdexcept>
77
#include <vector>
88

9+
namespace cppx::kf_linear {
10+
911
/**
1012
* @brief Generic linear Kalman filter (templated, no control term).
1113
*
@@ -121,3 +123,5 @@ template <int Nx, int Ny> class KFLinear {
121123
StateVec x_; ///< State mean
122124
StateMat P_; ///< State covariance
123125
};
126+
127+
} // namespace cppx::kf_linear

crates/kf_linear/tests/test_kf_linear.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "kf_linear.hpp"
99

1010
using Catch::Approx;
11+
using namespace cppx::kf_linear;
1112

1213
TEST_CASE("Predict-only step leaves state at A*x and P -> A P A^T + Q") {
1314
using KF = KFLinear<2, 1>;

crates/simple_optimizers/tests/test_simple_optimizers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ int main() {
5555
}
5656

5757
return 0;
58-
}
58+
};

0 commit comments

Comments
 (0)