/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2006 Mark Joshi 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 . The license is also available online at . 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 #include #include namespace QuantLib { Real genericLongstaffSchwartzRegression( std::vector >& simulationData, std::vector >& basisCoefficients) { Size steps = simulationData.size(); basisCoefficients.resize(steps-1); for (Size i=steps-1; i!=0; --i) { std::vector& exerciseData = simulationData[i]; // 1) find the covariance matrix of basis function values and // deflated cash-flows Size N = exerciseData.front().values.size(); std::vector temp(N+1); SequenceStatistics stats(N+1); Size j; for (j=0; j means = stats.mean(); Matrix covariance = stats.covariance(); Matrix C(N,N); Array target(N); for (Size k=0; k& estimatedData = simulationData[0]; for (Size j=0; j