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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Ferdinando Ametrano
Copyright (C) 2015 Paolo Mazzocchi
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/math/polynomialmathfunction.hpp>
#include <ql/math/pascaltriangle.hpp>
namespace QuantLib {
PolynomialFunction::PolynomialFunction(const std::vector<Real>& coeff) {
QL_REQUIRE(!coeff.empty(), "empty coefficient vector");
order_ = coeff.size();
c_ = coeff;
derC_ = std::vector<Real>(order_-1);
prC_ = std::vector<Real>(order_);
K_ = 0.0;
eqs_ = Matrix(order_, order_, 0.0);
Size i;
for (i=0; i<order_-1; ++i) {
prC_[i] = c_[i]/(i+1);
derC_[i] = c_[i+1]*(i+1);
}
prC_[i] = c_[i]/(i + 1);
}
Real PolynomialFunction::operator()(Time t) const {
Real result=0.0, tPower=1.0;
for (Size i=0; i<order_; ++i) {
result += c_[i] * tPower;
tPower *= t;
}
return result;
}
Real PolynomialFunction::derivative(Time t) const {
Real result=0.0, tPower=1.0;
for (Size i=0; i<order_-1; ++i) {
result += derC_[i] * tPower;
tPower *= t;
}
return result;
}
Real PolynomialFunction::primitive(Time t) const {
Real result=K_, tPower=t;
for (Size i=0; i<order_; ++i) {
result += prC_[i] * tPower;
tPower *= t;
}
return result;
}
Real PolynomialFunction::definiteIntegral(Time t1,
Time t2) const {
return primitive(t2)-primitive(t1);
}
void PolynomialFunction::initializeEqs_(Time t,
Time t2) const {
Time dt = t2 - t;
Real tau;
for (Size i=0; i<order_; ++i) {
tau = 1.0;
for (Size j=i; j<order_; ++j) {
tau *= dt;
eqs_[i][j] = (tau * PascalTriangle::get(j + 1)[i]) / (j + 1);
}
}
}
std::vector<Real>
PolynomialFunction::definiteIntegralCoefficients(Time t,
Time t2) const {
Array k(c_.begin(), c_.end());
initializeEqs_(t, t2);
Array coeff = eqs_ * k;
std::vector<Real> result(coeff.begin(), coeff.end());
return result;
}
std::vector<Real>
PolynomialFunction::definiteDerivativeCoefficients(Time t,
Time t2) const {
Array k(c_.begin(), c_.end());
initializeEqs_(t, t2);
Array coeff = inverse(eqs_) * k;
std::vector<Real> result(coeff.begin(), coeff.end());
return result;
}
}
|