/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2006 Mark Joshi Copyright (C) 2006 StatPro Italia srl 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 #include #include #include #include #include #include #include #include namespace QuantLib { namespace { class DecoratedHedge : public CallSpecifiedMultiProduct { public: DecoratedHedge(const CallSpecifiedMultiProduct& product) : CallSpecifiedMultiProduct(product) { savedStates_.reserve(product.evolution().numberOfSteps()); Size N = product.numberOfProducts(); numberCashFlowsThisStep_.resize(N); cashFlowsGenerated_.resize(N); for (Size i=0; i& numberCashFlowsThisStep, std::vector >& cashFlowsGenerated) { if (recording_) savedStates_.push_back(currentState); return CallSpecifiedMultiProduct::nextTimeStep( currentState, numberCashFlowsThisStep, cashFlowsGenerated); } std::auto_ptr clone() const { return std::auto_ptr( new DecoratedHedge(*this)); } void save() { lastSavedStep_ = savedStates_.size(); } void clear() { lastSavedStep_ = 0; savedStates_.clear(); recording_ = true; } void startRecording() { recording_ = true; } void stopRecording() { recording_ = false; } private: std::vector > savedStates_; Size lastSavedStep_; bool recording_; std::vector numberCashFlowsThisStep_; std::vector > cashFlowsGenerated_; }; } UpperBoundEngine::UpperBoundEngine( const boost::shared_ptr& evolver, const std::vector >& innerEvolvers, const MarketModelMultiProduct& underlying, const MarketModelExerciseValue& rebate, const MarketModelMultiProduct& hedge, const MarketModelExerciseValue& hedgeRebate, const ExerciseStrategy& hedgeStrategy, Real initialNumeraireValue) : evolver_(evolver), innerEvolvers_(innerEvolvers), composite_(MultiProductComposite()), initialNumeraireValue_(initialNumeraireValue) { composite_.add(underlying); composite_.add(ExerciseAdapter(rebate)); composite_.add(hedge); composite_.add(ExerciseAdapter(hedgeRebate)); composite_.add(DecoratedHedge(CallSpecifiedMultiProduct( hedge,hedgeStrategy,ExerciseAdapter(hedgeRebate)))); composite_.finalize(); underlyingOffset_ = 0; underlyingSize_ = underlying.numberOfProducts(); rebateOffset_ = underlyingSize_; rebateSize_ = 1; hedgeOffset_ = underlyingSize_+rebateSize_; hedgeSize_ = hedge.numberOfProducts(); hedgeRebateOffset_ = underlyingSize_+rebateSize_+hedgeSize_; hedgeRebateSize_ = 1; numberOfProducts_ = composite_.numberOfProducts(); const std::vector