Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation functions #1814

Closed
wants to merge 79 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
f340d6f
added interpolation functions
pgree Mar 31, 2020
63172f1
moved prim tests to new files
pgree Mar 31, 2020
389f42b
Merge branch 'develop' into feature/58-interp
pgree Apr 1, 2020
98fdf8b
[Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/…
stan-buildbot Apr 1, 2020
efde025
fixing messed up branch
pgree Apr 1, 2020
2169b76
fixing messed up branch again
pgree Apr 1, 2020
b36451b
trying merge
pgree Apr 1, 2020
6c8726d
[Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (ta…
stan-buildbot Apr 1, 2020
ee28a65
changes to reflect design doc discussion
pgree Aug 4, 2020
7582c50
fixing tests
pgree Aug 4, 2020
e91a3fc
reflects steve's changes
pgree Aug 4, 2020
eb0a0d9
adding docs
pgree Aug 5, 2020
fc4abb5
Merge branch 'feature/58-interp' of https://github.com/pgree/math int…
pgree Aug 5, 2020
774dccb
Merge commit '5b30e403bb1b2276dee2dd3d6736e52e67960651' into HEAD
yashikno Aug 5, 2020
c4582be
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 5, 2020
74b8419
fixing formatting
pgree Aug 5, 2020
0ce6663
doc typo fix
pgree Aug 5, 2020
c4dd75d
another doc typo fix
pgree Aug 5, 2020
be423da
another doc typo fix
pgree Aug 5, 2020
05737f4
another doc typo fix
pgree Aug 5, 2020
149f0d7
adding checks and tests
pgree Aug 5, 2020
8594a47
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 6, 2020
ee5b696
fix namespace
pgree Aug 6, 2020
adbfc60
fix namespace
pgree Aug 6, 2020
e4589c0
Merge branch 'feature/58-interp' of https://github.com/pgree/math int…
pgree Aug 6, 2020
3fde18a
added test for lin_interp
pgree Aug 6, 2020
c624bd0
added test for lin_interp
pgree Aug 6, 2020
77c0d25
adding includes
pgree Aug 6, 2020
f4dde7b
adding includes
pgree Aug 6, 2020
d37c844
adding inline statements
pgree Aug 7, 2020
d052649
Merge commit '988a44ac0960f73b05c1b91baa847c29e3822f43' into HEAD
yashikno Aug 8, 2020
dea4306
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 8, 2020
2a3020b
adding inline statements
pgree Aug 8, 2020
cdf5bbb
Merge branch 'feature/58-interp' of https://github.com/pgree/math int…
pgree Aug 8, 2020
cce952b
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 8, 2020
e9e9786
redo order of initializations
pgree Aug 12, 2020
2055a03
Merge branch 'feature/58-interp' of https://github.com/pgree/math int…
pgree Aug 12, 2020
9ee99cc
Merge commit 'f94d6f1397612bf7c84d0aaef1875d062a2398e2' into HEAD
yashikno Aug 12, 2020
4544f5e
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 12, 2020
5b9d65e
update docs
pgree Aug 12, 2020
06d55bd
Merge branch 'feature/58-interp' of https://github.com/pgree/math int…
pgree Aug 12, 2020
a5d00b6
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 12, 2020
b401faf
adding include
pgree Aug 12, 2020
15e820f
Merge branch 'feature/58-interp' of https://github.com/pgree/math int…
pgree Aug 12, 2020
2401a63
rand_r fix
pgree Aug 12, 2020
26b4529
rand fix
pgree Aug 12, 2020
561c909
rand fix
pgree Aug 13, 2020
2b98654
change rand
pgree Aug 14, 2020
0cc8ae9
modified mix test
pgree Aug 18, 2020
a3f988b
remove extra initializations
pgree Aug 20, 2020
85da4f6
remove using std
pgree Aug 21, 2020
e1e423e
Merge commit '7ba6890d5e64859c21fd09d69a465bfb06dab9d3' into HEAD
yashikno Aug 21, 2020
490cc03
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 21, 2020
22280c9
namespace issues
pgree Aug 21, 2020
71d97f3
Merge branch 'feature/58-interp' of https://github.com/pgree/math int…
pgree Aug 21, 2020
f5bbc96
namespace issues
pgree Aug 22, 2020
0e32b0c
params changed to vector
pgree Aug 31, 2020
dc49bc1
added new conv_gaus_line for sum of convolutions
pgree Sep 1, 2020
c1cc956
various fixes
pgree Sep 2, 2020
09829be
change distance to pointer arithmetic
pgree Sep 2, 2020
c25fc04
Merge commit '4144cdc2446da9029da5c3595d86a73f3dac0092' into HEAD
yashikno Sep 2, 2020
91199f8
[Jenkins] auto-formatting by clang-format version 6.0.1-14 (tags/RELE…
stan-buildbot Sep 2, 2020
07060e7
revert conv_gaus_line
pgree Sep 3, 2020
b65bb3a
fix merge conflicts
pgree Sep 3, 2020
443d6ff
Merge commit '0f3ab571e56c9210d31bbb9e9f62126a3b3fb257' into HEAD
yashikno Sep 3, 2020
57c7b0d
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Sep 3, 2020
fa5067f
fix issues
pgree Oct 27, 2020
68654cd
fix merge conflicts
pgree Oct 27, 2020
64853bc
Merge commit '43295fbda6d6ab5b8cb2323ba2a001faedaa37a5' into HEAD
yashikno Oct 27, 2020
62ea083
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Oct 27, 2020
b5f69eb
merge conflicts
pgree Oct 28, 2020
d0d75a2
merge conf
pgree Oct 28, 2020
a992cad
rerun tests
pgree Nov 11, 2020
226ee16
Merge commit '5291fc8cf03c901201243014957843ffb61c4705' into HEAD
yashikno Nov 11, 2020
57c2f8b
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Nov 11, 2020
2cbbad1
renaming
pgree Sep 21, 2021
a9f71a2
Merge branch 'feature/58-interp' of https://github.com/pgree/math int…
pgree Sep 28, 2021
7c8ab8b
Merge commit '57f05b7354af652031e10f7e278f82bdbedb1a40' into HEAD
yashikno Sep 28, 2021
c19300c
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Sep 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions stan/math/prim/fun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <stan/math/prim/fun/columns_dot_self.hpp>
#include <stan/math/prim/fun/conj.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/conv_gaus_line.hpp>
#include <stan/math/prim/fun/copysign.hpp>
#include <stan/math/prim/fun/corr_constrain.hpp>
#include <stan/math/prim/fun/corr_free.hpp>
Expand Down Expand Up @@ -97,6 +98,7 @@
#include <stan/math/prim/fun/fmin.hpp>
#include <stan/math/prim/fun/gamma_p.hpp>
#include <stan/math/prim/fun/gamma_q.hpp>
#include <stan/math/prim/fun/gaus_interp.hpp>
#include <stan/math/prim/fun/get.hpp>
#include <stan/math/prim/fun/get_base1.hpp>
#include <stan/math/prim/fun/get_base1_lhs.hpp>
Expand Down
67 changes: 67 additions & 0 deletions stan/math/prim/fun/conv_gaus_line.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef STAN_MATH_PRIM_FUN_CONV_GAUS_LINE
#define STAN_MATH_PRIM_FUN_CONV_GAUS_LINE

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/prob/normal_cdf.hpp>
#include <cmath>
#include <vector>

namespace stan {
namespace math {

/*
evaluate the derivative of conv_gaus_line with respect to x
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need full docs for external facing functions

double der_conv_gaus_line(double t0, double t1, double a, double b, double x0,
double sig2) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we want prim functions to be templated generically to work with any type, then the specialization in rev will be for analytic calculations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this function to the rev version of this file

using stan::math::normal_cdf;
double pi = stan::math::pi();
using std::exp;
using std::pow;
using std::sqrt;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put using aliases together at the top of the function

double sig = sqrt(sig2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
double sig = sqrt(sig2);
const double sig = sqrt(sig2);

double y;

double alpha = sqrt(2 * pi * sig2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
double alpha = sqrt(2 * pi * sig2);
const double alpha = sqrt(2 * pi * sig2);

y = (a * x0 + b) / alpha
* (-exp(-pow(t1 - x0, 2) / (2 * sig2))
+ exp(-pow(t0 - x0, 2) / (2 * sig2)));
y += a * (normal_cdf(t1, x0, sig) - normal_cdf(t0, x0, sig));
y -= a / alpha
* ((t1 - x0) * exp(-pow(t1 - x0, 2) / (2 * sig2))
- (t0 - x0) * exp(-pow(t0 - x0, 2) / (2 * sig2)));
return y;
}

/*
evaluate the integral

| t1
(2*pi*sig2)**-(1/2) | (a*t + b) * exp(-(t - x0)^2 / (2*sig2)) dt
| t0

*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

full docs

template <typename Tx>
double conv_gaus_line(double t0, double t1, double a, double b, Tx const& x,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an fyi we use west const

Suggested change
double conv_gaus_line(double t0, double t1, double a, double b, Tx const& x,
double conv_gaus_line(double t0, double t1, double a, double b, const Tx& x,

double sig2) {
using stan::math::normal_cdf;
double pi = stan::math::pi();
using std::exp;
using std::pow;
using std::sqrt;
double sig = sqrt(sig2);

double y
= (a * value_of(x) + b)
* (normal_cdf(t1, value_of(x), sig) - normal_cdf(t0, value_of(x), sig));
y += -a * sig2 / sqrt(2 * pi * sig2)
* (exp(-pow(t1 - value_of(x), 2) / (2 * sig2))
- exp(-pow(t0 - value_of(x), 2) / (2 * sig2)));

return y;
}

} // namespace math
} // namespace stan

#endif
167 changes: 167 additions & 0 deletions stan/math/prim/fun/gaus_interp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#ifndef STAN_MATH_PRIM_FUN_GAUS_INTERP
#define STAN_MATH_PRIM_FUN_GAUS_INTERP

#include <stan/math/prim/fun/conv_gaus_line.hpp>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

namespace stan {
namespace math {

// set tolerance for how close interpolated values need to be to
// the specified function values
const double INTERP_TOL = 1e-8;
const double SIG2_SCALE = 0.1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is specific to a function they should be in the function instead of floating in the stan math namespace


/*
given a set of points (x_i, y_i) with x_i's in increasing order, find
a_i, b_i such that the line between (x_i, y_i) and (x_{i+1}, y_{i+1}) is
f(t) = a_i*t + b_i, store the a_i's and b_i's in as and bs.
*/
void lin_interp_coefs(int n, std::vector<double> xs, std::vector<double> ys,
std::vector<double>& as, std::vector<double>& bs) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm generally against in/out parameters and I think this function's definition could just be inlined where it's used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been inlined

// initialize size of vectors of linear coefficients
as.resize(n - 1);
bs.resize(n - 1);

// find slope and intercept between each point
for (int i = 0; i < n - 1; i++) {
as[i] = (ys[i + 1] - ys[i]) / (xs[i + 1] - xs[i]);
bs[i] = -xs[i] * as[i] + ys[i];
}
}

/*
linear interpolation at one point, x, given the coefficients
*/
double lin_interp_pt(int n, vector<double> xs, vector<double> ys,
vector<double> as, vector<double> bs, double x) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should all be const& parameters so copies are not made

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true for everywhere you are using std::vector, arithmetic types can be passed by value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made these changes generally and also I created a new file for lin_interp. It seemed to make sense to separate the linear interpolation from this smooth interpolation.

// find interval where x lives
if (x <= xs[0])
return ys[0];
if (x >= xs[n - 1])
return ys[n - 1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (x <= xs[0])
return ys[0];
if (x >= xs[n - 1])
return ys[n - 1];
if (x <= xs[0]) {
return ys[0];
}
if (x >= xs[n - 1]) {
return ys[n - 1];
}


auto lb = upper_bound(xs.begin(), xs.end(), x);
int ind = distance(xs.begin(), lb) - 1;
ind = std::max(0, ind);

return as[ind] * x + bs[ind];
}

/*
linear interpolation at a vector of points
*/
vector<double> lin_interp(int n, std::vector<double> xs, std::vector<double> ys,
int n_new, std::vector<double> xs_new) {
// compute coefficients of linear interpolation
vector<double> as, bs;
lin_interp_coefs(n, xs, ys, as, bs);

// evaluate at new points
vector<double> ys_new;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reserve the vector size ahead of the loop to avoid reallocation

Suggested change
vector<double> ys_new;
vector<double> ys_new;
ys_new.reserve(n_new);

for (int i = 0; i < n_new; i++) {
ys_new.push_back(lin_interp_pt(n, xs, ys, as, bs, xs_new[i]));
}
return ys_new;
}

/*
given a set of points and a width for the gaussian kernel, do a convolution
and evaluate at one point, x
*/
template <typename Tx>
inline return_type_t<Tx> interp_gaus_pt(int n, std::vector<double> xs,
std::vector<double> ys,
std::vector<double> as,
std::vector<double> bs, Tx const& x,
double sig2) {
// extend out first and last lines for convolution
double sig = std::sqrt(sig2);
xs[0] += -10 * sig;
xs[n - 1] += 10 * sig;

// no need to convolve far from center of gaussian, so
// get lower and upper indexes for integration bounds
auto lb = upper_bound(xs.begin(), xs.end(), x - 10 * sig);
int ind_start = distance(xs.begin(), lb) - 1;
ind_start = std::max(0, ind_start);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is wrong if you are getting a negative number here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is std::lower_bound finds the first element in a vector that is larger than an inputted value, x. But what we really want is the previous element, the last element that's less than x. So we need to subtract 1. However when the first element in the list is greater than x, that would give us -1, so we need to compensate for that. I couldn't figure out a cleaner way to do it, but let me know if you have a thought on how to do it.


auto ub = upper_bound(xs.begin(), xs.end(), x + 10 * sig);
int ind_end = distance(xs.begin(), ub);
ind_end = std::min(n - 1, ind_end);

// sum convolutions over intervals
using return_t = return_type_t<Tx>;
return_t y = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use something once no need to make an alias

Suggested change
using return_t = return_type_t<Tx>;
return_t y = 0;
return_type_t<Tx> y = 0;

for (int i = ind_start; i < ind_end; i++) {
y += conv_gaus_line(xs[i], xs[i + 1], as[i], bs[i], x, sig2);
}

return y;
}

/*
find the smallest difference between successive elements in a sorted vector
*/
template <typename Tx>
double min_diff(int n, std::vector<Tx> xs) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only used in this file as an internal function then it should be in the internal namespace

double dmin = value_of(xs[1]) - value_of(xs[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idt this will work for fvar<var>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is no longer relevant, because now the xs (and ys) will only be inputted as data

for (int i = 1; i < n - 1; i++) {
if (value_of(xs[i + 1]) - value_of(xs[i]) < dmin) {
dmin = value_of(xs[i + 1]) - value_of(xs[i]);
}
}
return dmin;
}

/*
given a set of pairs (x_i, y_i), do a gaussian interpolation through those
points and evaluate the interpolation at the points xs_new
*/
template <typename Tx>
inline vector<Tx> gaus_interp(int n, std::vector<double> xs,
std::vector<double> ys, int n_new,
std::vector<Tx> xs_new) {
// find minimum distance between points for std of gaussian kernel
double sig2 = square(min_diff(n, xs) * SIG2_SCALE);

// copy ys into a new vector
std::vector<double> y2s;
y2s.resize(n);
for (int i = 0; i < n; i++) {
y2s[i] = ys[i];
}

// interatively find interpolation that coincides with ys at xs
int max_iters = 50;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be user defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so

double dmax, dd;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normally seperate out declerations

Suggested change
double dmax, dd;
double dmax
double dd;

std::vector<double> as, bs;
for (int j = 0; j < max_iters; j++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] both could be just size_t

// linear interpolation for new ys
lin_interp_coefs(n, xs, y2s, as, bs);
dmax = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can define dmax here

for (int i = 0; i < n; i++) {
dd = ys[i] - interp_gaus_pt(n, xs, y2s, as, bs, xs[i], sig2);
y2s[i] += dd;
dmax = std::max(std::abs(dd), dmax);
}
if (dmax < INTERP_TOL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just call INTERP_TOL max_tolerance or something

break;
}

// fill ys_new with interpolated values at new_xs
std::vector<Tx> ys_new;
for (int i = 0; i < n_new; i++) {
ys_new.push_back(interp_gaus_pt(n, xs, y2s, as, bs, xs_new[i], sig2));
}
return ys_new;
}

} // namespace math
} // namespace stan

#endif
1 change: 1 addition & 0 deletions stan/math/rev/fun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stan/math/rev/fun/cholesky_decompose.hpp>
#include <stan/math/rev/fun/columns_dot_product.hpp>
#include <stan/math/rev/fun/columns_dot_self.hpp>
#include <stan/math/rev/fun/conv_gaus_line.hpp>
#include <stan/math/rev/fun/conj.hpp>
#include <stan/math/rev/fun/cos.hpp>
#include <stan/math/rev/fun/cosh.hpp>
Expand Down
45 changes: 45 additions & 0 deletions stan/math/rev/fun/conv_gaus_line.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef STAN_MATH_REV_FUN_CONV_GAUS_LINE
#define STAN_MATH_REV_FUN_CONV_GAUS_LINE

#include <stan/math/rev/meta.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/prim/fun/conv_gaus_line.hpp>

namespace stan {
namespace math {

namespace internal {
class conv_gaus_line_vari : public op_v_vari {
double t0_;
double t1_;
double a_;
double b_;
double sig2_;

public:
explicit conv_gaus_line_vari(double t0, double t1, double a, double b,
vari* avi, double sig2)
: t0_(t0),
t1_(t1),
a_(a),
b_(b),
sig2_(sig2),
op_v_vari(conv_gaus_line(t0, t1, a, b, avi->val_, sig2), avi) {}

void chain() {
avi_->adj_
+= adj_ * der_conv_gaus_line(t0_, t1_, a_, b_, avi_->val_, sig2_);
}
};

} // namespace internal

inline var conv_gaus_line(double t0, double t1, double a, double b,
const var& x, double sig2) {
return var(new internal::conv_gaus_line_vari(t0, t1, a, b, x.vi_, sig2));
}

} // namespace math
} // namespace stan

#endif
35 changes: 35 additions & 0 deletions test/unit/math/mix/fun/conv_gaus_line_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <test/unit/math/test_ad.hpp>
#include <limits>
#include <vector>

/*
check derivative using finite differences and autodiffing
*/
TEST(mathMixGausInterp, gaus_conv_der) {
double t0, t1, a, b, x0, sig2, y, h, yn, yp, x0p, x0n, dder, dder2;
double alpha, sig;
using stan::math::conv_gaus_line;

t0 = 0;
t1 = 5;
a = -1;
b = 2;
x0 = 0.99;
sig2 = 2;

// derivative with finite difference
h = 1e-4;
x0n = x0 + h;
x0p = x0 - h;
yn = conv_gaus_line(t0, t1, a, b, x0n, sig2);
yp = conv_gaus_line(t0, t1, a, b, x0p, sig2);
dder = (yn - yp) / (2 * h);

// derivative with autodiff
using stan::math::var;
var v = x0;
var vy = conv_gaus_line(t0, t1, a, b, v, sig2);
vy.grad();

ASSERT_NEAR(dder, v.adj(), 1e-5);
}
Loading