QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
polynomialmathfunction.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2015 Ferdinando Ametrano
5 Copyright (C) 2015 Paolo Mazzocchi
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
23
24namespace QuantLib {
25
26 PolynomialFunction::PolynomialFunction(const std::vector<Real>& coeff) {
27
28 QL_REQUIRE(!coeff.empty(), "empty coefficient vector");
29 order_ = coeff.size();
30 c_ = coeff;
31 derC_ = std::vector<Real>(order_-1);
32 prC_ = std::vector<Real>(order_);
33 K_ = 0.0;
34 eqs_ = Matrix(order_, order_, 0.0);
35
36 Size i;
37 for (i=0; i<order_-1; ++i) {
38 prC_[i] = c_[i]/(i+1);
39 derC_[i] = c_[i+1]*(i+1);
40 }
41 prC_[i] = c_[i]/(i + 1);
42 }
43
45 Real result=0.0, tPower=1.0;
46 for (Size i=0; i<order_; ++i) {
47 result += c_[i] * tPower;
48 tPower *= t;
49 }
50 return result;
51 }
52
54 Real result=0.0, tPower=1.0;
55 for (Size i=0; i<order_-1; ++i) {
56 result += derC_[i] * tPower;
57 tPower *= t;
58 }
59 return result;
60 }
61
63 Real result=K_, tPower=t;
64 for (Size i=0; i<order_; ++i) {
65 result += prC_[i] * tPower;
66 tPower *= t;
67 }
68 return result;
69 }
70
72 Time t2) const {
73 return primitive(t2)-primitive(t1);
74 }
75
77 Time t2) const {
78 Time dt = t2 - t;
79 Real tau;
80 for (Size i=0; i<order_; ++i) {
81 tau = 1.0;
82 for (Size j=i; j<order_; ++j) {
83 tau *= dt;
84 eqs_[i][j] = (tau * PascalTriangle::get(j + 1)[i]) / (j + 1);
85 }
86 }
87 }
88
89 std::vector<Real>
91 Time t2) const {
92 Array k(c_.begin(), c_.end());
93 initializeEqs_(t, t2);
94 Array coeff = eqs_ * k;
95 std::vector<Real> result(coeff.begin(), coeff.end());
96 return result;
97 }
98
99 std::vector<Real>
101 Time t2) const {
102 Array k(c_.begin(), c_.end());
103 initializeEqs_(t, t2);
104 Array coeff = inverse(eqs_) * k;
105 std::vector<Real> result(coeff.begin(), coeff.end());
106 return result;
107 }
108
109}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
const_iterator begin() const
Definition: array.hpp:503
Matrix used in linear algebra.
Definition: matrix.hpp:41
static const std::vector< BigNatural > & get(Size order)
Get and store one vector of coefficients after another.
Real definiteIntegral(Time t1, Time t2) const
void initializeEqs_(Time t, Time t2) const
std::vector< Real > definiteIntegralCoefficients(Time t, Time t2) const
Real operator()(Time t) const
function value at time t:
PolynomialFunction(const std::vector< Real > &coeff)
std::vector< Real > definiteDerivativeCoefficients(Time t, Time t2) const
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Matrix inverse(const Matrix &m)
Definition: matrix.cpp:44
Pascal triangle coefficients calculator.