quant-pricer-cpp
Loading...
Searching...
No Matches
term_structures.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <algorithm>
5#include <stdexcept>
6#include <vector>
7
8namespace quant {
9
11 std::vector<double> times; // strictly increasing, terminal time inclusive
12 std::vector<double> values; // same size as times (value on (t_{i-1}, t_i])
13
14 double value(double t) const {
15 if (times.empty() || values.empty() || times.size() != values.size()) {
16 throw std::runtime_error("PiecewiseConstant: invalid schedule");
17 }
18 // Right-closed intervals: value is constant on (t_{i-1}, t_i],
19 // and for t <= times.front() we return the first value.
20 if (t <= times.front())
21 return values.front();
22 auto it = std::lower_bound(times.begin(), times.end(), t); // first time >= t
23 std::size_t idx = static_cast<std::size_t>(std::distance(times.begin(), it));
24 if (idx >= values.size())
25 return values.back();
26 return values[idx];
27 }
28};
29
30} // namespace quant
Barrier option primitives and configuration.
Definition american.hpp:11
Definition term_structures.hpp:10
std::vector< double > values
Definition term_structures.hpp:12
std::vector< double > times
Definition term_structures.hpp:11
double value(double t) const
Definition term_structures.hpp:14