QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
multisteptarn.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2010 Mark Joshi
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
22#include <cmath>
23#include <utility>
24
25namespace QuantLib {
26
27 MultiStepTarn::MultiStepTarn(const std::vector<Time>& rateTimes,
28 const std::vector<Real>& accruals,
29 const std::vector<Real>& accrualsFloating,
30 const std::vector<Time>& paymentTimes,
31 const std::vector<Time>& paymentTimesFloating,
32 Real totalCoupon,
33 const std::vector<Real>& strikes,
34 std::vector<Real> multipliers,
35 const std::vector<Real>& floatingSpreads)
36 : MultiProductMultiStep(rateTimes), accruals_(accruals), accrualsFloating_(accrualsFloating),
37 paymentTimes_(paymentTimes), paymentTimesFloating_(paymentTimesFloating),
38 allPaymentTimes_(paymentTimes), totalCoupon_(totalCoupon), strikes_(strikes),
39 multipliers_(std::move(multipliers)), floatingSpreads_(floatingSpreads) {
40 QL_REQUIRE(accruals_.size()+1 == rateTimes.size(), "missized accruals in MultiStepTARN");
41 QL_REQUIRE(accrualsFloating.size()+1 == rateTimes.size(), "missized accrualsFloating in MultiStepTARN");
42 QL_REQUIRE(paymentTimes.size()+1 == rateTimes.size(), "missized paymentTimes in MultiStepTARN");
43 QL_REQUIRE(paymentTimesFloating.size()+1 == rateTimes.size(), "missized paymentTimesFloating in MultiStepTARN");
44 QL_REQUIRE(strikes.size()+1 == rateTimes.size(), "missized strikes in MultiStepTARN");
45 QL_REQUIRE(floatingSpreads.size()+1 == rateTimes.size(), "missized floatingSpreads in MultiStepTARN");
46
47 lastIndex_ = accruals.size();
48
49 for (Size i=0; i < paymentTimesFloating_.size(); ++i)
50 allPaymentTimes_.push_back(paymentTimes[i]);
51 }
52
54 const CurveState& currentState,
55 std::vector<Size>& numberCashFlowsThisStep,
56 std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
57 genCashFlows)
58 {
59 Rate liborRate = currentState.forwardRate(currentIndex_);
60
61 numberCashFlowsThisStep[0] =2;
62
63 genCashFlows[0][0].amount = (liborRate+floatingSpreads_[currentIndex_])*accrualsFloating_[currentIndex_];
64 genCashFlows[0][0].timeIndex = lastIndex_ + currentIndex_;
65
66 genCashFlows[0][1].timeIndex = currentIndex_;
67
68 Real obviousCoupon = std::max(strikes_[currentIndex_] - multipliers_[currentIndex_]*liborRate,0.0)*accruals_[currentIndex_];
69
70 couponPaid_+= obviousCoupon;
71
73
75 {
76 genCashFlows[0][1].amount = - obviousCoupon;
77 return false;
78 }
79
80 Real coupon = obviousCoupon +(totalCoupon_ -couponPaid_);
81 genCashFlows[0][1].amount = - coupon;
82
83 return true;
84 }
85
86 std::unique_ptr<MarketModelMultiProduct>
88 return std::unique_ptr<MarketModelMultiProduct>(new MultiStepTarn(*this));
89 }
90
91
92
93 std::vector<Time>
95 return allPaymentTimes_;
96 }
97
99 return 1;
100 }
101
102 Size
104 return 2;
105 }
106
108 {
110 couponPaid_ = 0.0;
111 }
112
113}
114
Curve state for market-model simulations
Definition: curvestate.hpp:41
virtual Rate forwardRate(Size i) const =0
Multiple-step market-model product.
std::vector< Time > allPaymentTimes_
std::unique_ptr< MarketModelMultiProduct > clone() const override
returns a newly-allocated copy of itself
std::vector< Real > floatingSpreads_
std::vector< Real > strikes_
bool nextTimeStep(const CurveState &currentState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
std::vector< Time > paymentTimesFloating_
std::vector< Time > possibleCashFlowTimes() const override
std::vector< Real > multipliers_
Size maxNumberOfCashFlowsPerProductPerStep() const override
Size numberOfProducts() const override
MultiStepTarn(const std::vector< Time > &rateTimes, const std::vector< Real > &accruals, const std::vector< Real > &accrualsFloating, const std::vector< Time > &paymentTimes, const std::vector< Time > &paymentTimesFloating, Real totalCoupon, const std::vector< Real > &strikes, std::vector< Real > multipliers, const std::vector< Real > &floatingSpreads)
std::vector< Real > accruals_
void reset() override
during simulation put product at start of path
std::vector< Real > accrualsFloating_
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.