-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbf.cpp
53 lines (47 loc) · 1.48 KB
/
rbf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "rbf.h"
#include <Eigen/Dense>
#include <cmath>
/**
* @brief Constructor for RBFInterpolator with multiquadric kernel and hardcoded smoothing.
*
* @param k Vector of input points for interpolation (log-moneyness).
* @param y Corresponding values (implied volatilities).
* @param epsilon Regularization parameter for the RBF kernel.
*/
RBFInterpolator::RBFInterpolator(const Eigen::VectorXd &k, const Eigen::VectorXd &y, double epsilon)
: k_(k), y_(y), epsilon_(epsilon), smoothing_(1e-12)
{
Eigen::Index n = k.size();
A_ = Eigen::MatrixXd(n, n);
for (Eigen::Index i = 0; i < n; ++i)
{
for (Eigen::Index j = 0; j < n; ++j)
{
double r = (k(i) - k(j));
A_(i, j) = std::sqrt(1 + (epsilon_ * epsilon_ * r * r));
}
A_(i, i) += smoothing_;
}
weights_ = A_.ldlt().solve(y);
}
/**
* @brief Function to interpolate the values for all inputs.
*
* @param x Vector of points where interpolation is evaluated.
* @return Eigen::VectorXd Vector of interpolated values.
*/
Eigen::VectorXd RBFInterpolator::interpolate(const Eigen::VectorXd &x)
{
Eigen::Index n = k_.size();
Eigen::Index m = x.size();
Eigen::VectorXd result = Eigen::VectorXd::Zero(m);
for (Eigen::Index i = 0; i < m; ++i)
{
for (Eigen::Index j = 0; j < n; ++j)
{
double r = (x(i) - k_(j));
result(i) += weights_(j) * std::sqrt(1 + (epsilon_ * epsilon_ * r * r));
}
}
return result;
}