/* Copyright (C) 2003 Niels Elken Sønderby 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 ferdinando@ametrano.net The license is also available online at http://quantlib.org/html/license.html 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. */ /* NesQuant is an extension to QuantLib http://www.nielses.dk/quantlib/nesquant */ #ifndef nesquant_polynomialfit_h #define nesquant_polynomialfit_h #include #include using namespace QuantLib; using namespace QuantLib::Math; namespace NesQuant { class PolynomialFit { public: PolynomialFit::PolynomialFit(Matrix data, Size degree = 2) : data_(data), degree_(degree), fitFunctions_(degree_ * (data.columns() - 1) + 1), parameters_(Array(fitFunctions_)), value_(Array(data_.rows())), isPerformed_(false) { QL_REQUIRE(data.rows() > 0, "no data"); QL_REQUIRE(degree >= 1, "degree of polynomium must be >= 1"); }; PolynomialFit::PolynomialFit(std::vector x, std::vector y, Size degree = 2) : degree_(degree), fitFunctions_(degree_ + 1), parameters_(Array(fitFunctions_)), value_(Array(x.size())), isPerformed_(false) { QL_REQUIRE(x.size() == y.size(), "vectors must have same size"); QL_REQUIRE(x.size() > 0, "empty vectors not allowed"); QL_REQUIRE(degree >= 1, "degree of polynomium must be >= 1"); data_ = Matrix(x.size(), 2); for (Size i = 0; i < x.size(); ++i) { data_[i][0] = x[i]; data_[i][1] = y[i]; } }; PolynomialFit::PolynomialFit(std::vector x, std::vector y, std::vector z, Size degree = 2) : degree_(degree), fitFunctions_(2 * degree_ + 1), parameters_(Array(fitFunctions_)), value_(Array(x.size())), isPerformed_(false) { QL_REQUIRE(x.size() == y.size() && y.size() == z.size(), "vectors must have same size"); QL_REQUIRE(x.size() > 0, "empty vectors not allowed"); QL_REQUIRE(degree >= 1, "degree of polynomium must be >= 1"); data_ = Matrix(x.size(), 3); for (Size i = 0; i < x.size(); ++i) { data_[i][0] = x[i]; data_[i][1] = y[i]; data_[i][2] = z[i]; } }; const Array value(); const Array parameters(); private: void perform(); Matrix data_; Size degree_; Size fitFunctions_; mutable bool isPerformed_; mutable Array value_; mutable Array parameters_; }; } #endif