-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an interpolation model which provides special methods for
computing first and second derivatives of the interpolated mean
Showing
9 changed files
with
274 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright (C) 2020 Swift Navigation Inc. | ||
* Contact: Swift Navigation <dev@swiftnav.com> | ||
* | ||
* This source is subject to the license found in the file 'LICENSE' which must | ||
* be distributed together with this source. All other rights reserved. | ||
* | ||
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
#ifndef ALBATROSS_INTERPOLATE_H | ||
#define ALBATROSS_INTERPOLATE_H | ||
|
||
#include "GP" | ||
#include <albatross/src/models/interpolate.hpp> | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2020 Swift Navigation Inc. | ||
* Contact: Swift Navigation <dev@swiftnav.com> | ||
* | ||
* This source is subject to the license found in the file 'LICENSE' which must | ||
* be distributed together with this source. All other rights reserved. | ||
* | ||
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
#ifndef INCLUDE_ALBATROSS_SRC_COVARIANCE_FUNCTIONS_DERIVATIVE_HPP_ | ||
#define INCLUDE_ALBATROSS_SRC_COVARIANCE_FUNCTIONS_DERIVATIVE_HPP_ | ||
|
||
namespace albatross { | ||
|
||
template <typename T> struct Derivative { | ||
|
||
Derivative() : value(){}; | ||
|
||
Derivative(const T &t) : value(t){}; | ||
|
||
T value; | ||
}; | ||
|
||
template <typename T> struct SecondDerivative { | ||
|
||
SecondDerivative() : value(){}; | ||
|
||
SecondDerivative(const T &t) : value(t){}; | ||
|
||
T value; | ||
}; | ||
|
||
} // namespace albatross | ||
|
||
#endif /* INCLUDE_ALBATROSS_SRC_COVARIANCE_FUNCTIONS_DERIVATIVE_HPP_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (C) 2020 Swift Navigation Inc. | ||
* Contact: Swift Navigation <dev@swiftnav.com> | ||
* | ||
* This source is subject to the license found in the file 'LICENSE' which must | ||
* be distributed together with this source. All other rights reserved. | ||
* | ||
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
#ifndef INCLUDE_ALBATROSS_SRC_MODELS_INTERPOLATE_HPP_ | ||
#define INCLUDE_ALBATROSS_SRC_MODELS_INTERPOLATE_HPP_ | ||
|
||
namespace albatross { | ||
|
||
auto interpolation_cov_func() { | ||
SquaredExponential<EuclideanDistance> sqr_exp; | ||
IndependentNoise<double> noise; | ||
return sqr_exp + measurement_only(noise); | ||
} | ||
|
||
using InterpolationFunction = decltype(interpolation_cov_func()); | ||
|
||
/* | ||
* Generic Gaussian Process Implementation. | ||
*/ | ||
class GaussianProcessInterpolator | ||
: public GaussianProcessBase<InterpolationFunction, ZeroMean, | ||
GaussianProcessInterpolator> { | ||
public: | ||
using Base = GaussianProcessBase<InterpolationFunction, ZeroMean, | ||
GaussianProcessInterpolator>; | ||
}; | ||
|
||
using GPInterpolatorFitType = | ||
typename fit_type<GaussianProcessInterpolator, double>::type; | ||
|
||
template <> | ||
class Prediction<GaussianProcessInterpolator, double, GPInterpolatorFitType> { | ||
|
||
public: | ||
Prediction(const GaussianProcessInterpolator &model, | ||
const GPInterpolatorFitType &fit, | ||
const std::vector<double> &features) | ||
: model_(model), fit_(fit), features_(features) {} | ||
|
||
Prediction(GaussianProcessInterpolator &&model, GPInterpolatorFitType &&fit, | ||
const std::vector<double> &features) | ||
: model_(std::move(model)), fit_(std::move(fit)), features_(features) {} | ||
|
||
// Mean | ||
Eigen::VectorXd mean() const { | ||
return MeanPredictor()._mean(model_, fit_, features_); | ||
} | ||
|
||
Eigen::VectorXd derivative() const { | ||
|
||
std::vector<Derivative<double>> derivative_features; | ||
for (const auto &f : features_) { | ||
derivative_features.emplace_back(f); | ||
} | ||
|
||
return MeanPredictor()._mean(model_, fit_, derivative_features); | ||
} | ||
|
||
Eigen::VectorXd second_derivative() const { | ||
|
||
std::vector<SecondDerivative<double>> derivative_features; | ||
for (const auto &f : features_) { | ||
derivative_features.emplace_back(f); | ||
} | ||
return MeanPredictor()._mean(model_, fit_, derivative_features); | ||
} | ||
|
||
const GaussianProcessInterpolator model_; | ||
const GPInterpolatorFitType fit_; | ||
const std::vector<double> features_; | ||
}; | ||
|
||
} // namespace albatross | ||
#endif /* INCLUDE_ALBATROSS_SRC_MODELS_INTERPOLATE_HPP_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2020 Swift Navigation Inc. | ||
* Contact: Swift Navigation <dev@swiftnav.com> | ||
* | ||
* This source is subject to the license found in the file 'LICENSE' which must | ||
* be distributed together with this source. All other rights reserved. | ||
* | ||
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
#include <albatross/Interpolate> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace albatross { | ||
|
||
std::vector<double> uniform_points_on_line(const std::size_t n, | ||
const double low, | ||
const double high) { | ||
std::vector<double> xs; | ||
for (std::size_t i = 0; i < n; i++) { | ||
double ratio = (double)i / (double)(n - 1); | ||
xs.push_back(low + ratio * (high - low)); | ||
} | ||
return xs; | ||
}; | ||
|
||
TEST(test_interpolator, test_interpolate) { | ||
|
||
const auto xs = uniform_points_on_line(21, 0., 2 * 3.14159); | ||
|
||
Eigen::VectorXd targets(xs.size()); | ||
for (std::size_t i = 0; i < xs.size(); ++i) { | ||
targets[i] = std::sin(xs[i]); | ||
} | ||
|
||
const auto interp_xs = uniform_points_on_line(101, 0., 2 * 3.14159); | ||
|
||
Eigen::VectorXd mean_truth(interp_xs.size()); | ||
Eigen::VectorXd derivative_truth(interp_xs.size()); | ||
Eigen::VectorXd second_derivative_truth(interp_xs.size()); | ||
|
||
for (std::size_t i = 0; i < interp_xs.size(); ++i) { | ||
mean_truth[i] = std::sin(interp_xs[i]); | ||
derivative_truth[i] = std::cos(interp_xs[i]); | ||
second_derivative_truth[i] = -std::sin(interp_xs[i]); | ||
} | ||
|
||
GaussianProcessInterpolator interpolator; | ||
interpolator.set_param("squared_exponential_length_scale", 10.); | ||
interpolator.set_param("sigma_squared_exponential", 10.); | ||
interpolator.set_param("sigma_independent_noise", 1e-6); | ||
|
||
const auto predictor = interpolator.fit(xs, targets).predict(interp_xs); | ||
|
||
EXPECT_LT((predictor.mean() - mean_truth).norm(), 1e-3); | ||
EXPECT_LT((predictor.derivative() - derivative_truth).norm(), 1e-2); | ||
EXPECT_LT((predictor.second_derivative() - second_derivative_truth).norm(), | ||
1e-1); | ||
} | ||
|
||
} // namespace albatross |