| 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 | |
| 21 | #include <ql/math/polynomialmathfunction.hpp> |
| 22 | #include <ql/math/pascaltriangle.hpp> |
| 23 | |
| 24 | namespace 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 | |
| 44 | Real PolynomialFunction::operator()(Time t) const { |
| 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 | |
| 53 | Real PolynomialFunction::derivative(Time t) const { |
| 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 | |
| 62 | Real PolynomialFunction::primitive(Time t) const { |
| 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 | |
| 71 | Real PolynomialFunction::definiteIntegral(Time t1, |
| 72 | Time t2) const { |
| 73 | return primitive(t: t2)-primitive(t: t1); |
| 74 | } |
| 75 | |
| 76 | void PolynomialFunction::initializeEqs_(Time t, |
| 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(order: j + 1)[i]) / (j + 1); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | std::vector<Real> |
| 90 | PolynomialFunction::definiteIntegralCoefficients(Time t, |
| 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> |
| 100 | PolynomialFunction::definiteDerivativeCoefficients(Time t, |
| 101 | Time t2) const { |
| 102 | Array k(c_.begin(), c_.end()); |
| 103 | initializeEqs_(t, t2); |
| 104 | Array coeff = inverse(m: eqs_) * k; |
| 105 | std::vector<Real> result(coeff.begin(), coeff.end()); |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |