|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Swift Navigation Inc. |
| 3 | + * Contact: Swift Navigation <dev@swiftnav.com> |
| 4 | + * |
| 5 | + * This source is subject to the license found in the file 'LICENSE' which must |
| 6 | + * be distributed together with this source. All other rights reserved. |
| 7 | + * |
| 8 | + * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
| 9 | + * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED |
| 10 | + * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + */ |
| 12 | + |
| 13 | +#ifndef ALBATROSS_COVARIANCE_FUNCTIONS_SCALING_FUNCTION_H |
| 14 | +#define ALBATROSS_COVARIANCE_FUNCTIONS_SCALING_FUNCTION_H |
| 15 | + |
| 16 | +#include "covariance_term.h" |
| 17 | +#include <sstream> |
| 18 | +#include <utility> |
| 19 | + |
| 20 | +namespace albatross { |
| 21 | + |
| 22 | +class ScalingFunction : public ParameterHandlingMixin { |
| 23 | +public: |
| 24 | + virtual std::string get_name() const = 0; |
| 25 | + |
| 26 | + // A scaling function should also implement operators |
| 27 | + // for whichever types it is intended to scale using |
| 28 | + // the signature: |
| 29 | + // double operator(const X &x) const; |
| 30 | +}; |
| 31 | + |
| 32 | +/* |
| 33 | + * A scaling term is not actually a covariance function |
| 34 | + * in the rigorous sense. It doesn't describe the uncertainty |
| 35 | + * between variables, but instead operates deterministically |
| 36 | + * on other uncertain variables. For instance, you may have |
| 37 | + * some random variable, |
| 38 | + * y ~ N(0, S) with S_ij = cov(y_i, y_j) |
| 39 | + * And you may then make observations of that random variable |
| 40 | + * but through a known transformation, |
| 41 | + * z = f(y) y |
| 42 | + * where f is a determinstic function of y that returns a scalar. |
| 43 | + * You might then ask what the covariance between two elements in |
| 44 | + * z is which is woudl be given by, |
| 45 | + * cov(z_i, z_j) = f(y_i) * cov(y_i, y_j) * f(y_j) |
| 46 | + * but you might also be interested in the covariance between |
| 47 | + * some y_i and an observation z_j, |
| 48 | + * cov(y_i, z_j) = cov(y_i, y_j) * f(y_j) |
| 49 | + * Here we see that for a typical covariance term, the covariance |
| 50 | + * is only defined for two pairs of the same type, in this case |
| 51 | + * operator()(Y &y, Y &y) |
| 52 | + * but by multiplying by a ScalingTerm we end up with definitions |
| 53 | + * for, |
| 54 | + * operator()(Y &y, Z &z) |
| 55 | + * which provides us with a way of computing the covariance between |
| 56 | + * some hidden representation of a variable (y) and the actual |
| 57 | + * observations (z) using a single determinstic mapping (f). |
| 58 | + * |
| 59 | + * This might be better explained by example which can be found |
| 60 | + * in the tests (test_scaling_function). |
| 61 | + */ |
| 62 | +template <typename ScalingFunction> class ScalingTerm : public CovarianceTerm { |
| 63 | +public: |
| 64 | + ScalingTerm() : CovarianceTerm(){}; |
| 65 | + virtual ~ScalingTerm(){}; |
| 66 | + |
| 67 | + /* |
| 68 | + * The following methods forward any requests dealing with |
| 69 | + * the ParameterHandlingMixin to the ScalingFunction. |
| 70 | + */ |
| 71 | + std::string get_name() const override { return scaling_function_.get_name(); } |
| 72 | + |
| 73 | + std::string pretty_string() const { |
| 74 | + return scaling_function_.pretty_string(); |
| 75 | + } |
| 76 | + |
| 77 | + void set_params(const ParameterStore ¶ms) { |
| 78 | + scaling_function_.set_params(params); |
| 79 | + } |
| 80 | + |
| 81 | + virtual ParameterStore get_params() const { |
| 82 | + return scaling_function_.get_params(); |
| 83 | + } |
| 84 | + |
| 85 | + template <class Archive> void save(Archive &archive) const { |
| 86 | + archive(cereal::make_nvp("base_class", |
| 87 | + cereal::base_class<CovarianceTerm>(this))); |
| 88 | + archive(cereal::make_nvp("scaling_function", scaling_function_)); |
| 89 | + } |
| 90 | + |
| 91 | + template <class Archive> void load(Archive &archive) { |
| 92 | + archive(cereal::make_nvp("base_class", |
| 93 | + cereal::base_class<CovarianceTerm>(this))); |
| 94 | + archive(cereal::make_nvp("scaling_function", scaling_function_)); |
| 95 | + } |
| 96 | + |
| 97 | + void unchecked_set_param(const std::string &name, |
| 98 | + const double value) override { |
| 99 | + scaling_function_.set_param(name, value); |
| 100 | + } |
| 101 | + |
| 102 | + /* |
| 103 | + * If both Scaling and Covariance have a valid call method for the types X |
| 104 | + * and Y this will return the product of the two. |
| 105 | + */ |
| 106 | + template < |
| 107 | + typename X, typename Y, |
| 108 | + typename std::enable_if<(has_call_operator<ScalingFunction, X &>::value && |
| 109 | + has_call_operator<ScalingFunction, Y &>::value), |
| 110 | + int>::type = 0> |
| 111 | + double operator()(X &x, Y &y) const { |
| 112 | + return this->scaling_function_(x) * this->scaling_function_(y); |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + * If only one of the types has a scaling function we ignore the other. |
| 117 | + */ |
| 118 | + template <typename X, typename Y, |
| 119 | + typename std::enable_if< |
| 120 | + (!has_call_operator<ScalingFunction, X &>::value && |
| 121 | + has_call_operator<ScalingFunction, Y &>::value), |
| 122 | + int>::type = 0> |
| 123 | + double operator()(X &x, Y &y) const { |
| 124 | + return this->scaling_function_(y); |
| 125 | + } |
| 126 | + |
| 127 | + template < |
| 128 | + typename X, typename Y, |
| 129 | + typename std::enable_if<(has_call_operator<ScalingFunction, X &>::value && |
| 130 | + !has_call_operator<ScalingFunction, Y &>::value), |
| 131 | + int>::type = 0> |
| 132 | + double operator()(X &x, Y &y) const { |
| 133 | + return this->scaling_function_(x); |
| 134 | + } |
| 135 | + |
| 136 | +private: |
| 137 | + ScalingFunction scaling_function_; |
| 138 | +}; |
| 139 | +} |
| 140 | +#endif |
0 commit comments