QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fwdperiodadapter.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006 Ferdinando Ametrano
5 Copyright (C) 2006 Mark Joshi
6 Copyright (C) 2007 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/models/marketmodels/curvestates/lmmcurvestate.hpp>
23#include <ql/models/marketmodels/evolutiondescription.hpp>
24#include <ql/models/marketmodels/forwardforwardmappings.hpp>
25#include <ql/models/marketmodels/models/fwdperiodadapter.hpp>
26#include <ql/utilities/dataformatters.hpp>
27#include <set>
28#include <utility>
29
30namespace QuantLib {
31
32 FwdPeriodAdapter::FwdPeriodAdapter(const ext::shared_ptr<MarketModel>& largeModel,
33 Size period,
34 Size offset,
35 std::vector<Spread> newDisplacements)
36 : numberOfFactors_(largeModel->numberOfFactors()),
37 numberOfRates_((largeModel->numberOfRates() - offset) / (period > 0 ? period : 1)),
38 numberOfSteps_(largeModel->numberOfSteps()),
39 pseudoRoots_(numberOfSteps_, Matrix(numberOfRates_, numberOfFactors_)),
40 displacements_(std::move(newDisplacements)) {
41 QL_REQUIRE( period >0, "period must be greater than zero in fwdperiodadapter");
42 QL_REQUIRE(period > offset, "period must be greater than offset in fwdperiodadapter");
43
44 const std::vector<Spread>& largeDisplacements_ =
45 largeModel->displacements();
46
47 if (displacements_.size() == 1)
48 {
49 Real dis = displacements_[0];
51 std::fill(displacements_.begin(), displacements_.end(), dis);
52 }
53
54 if (displacements_.empty()) // if not specified use average across rate
55 {
57 Size m=0;
58 Real sum=0.0;
59 for (Size k=0; k < numberOfRates_; ++k)
60 {
61 for (Size l=0; l < period; ++l, ++m)
62 sum+= largeDisplacements_[m];
63
64 displacements_.push_back(sum/period);
65 }
66 }
67 QL_REQUIRE( displacements_.size() == numberOfRates_,"newDisplacements should be empty,1, or number of new rates in fwdperiodadapter");
68
69 LMMCurveState largeCS(largeModel->evolution().rateTimes());
70 largeCS.setOnForwardRates(largeModel->initialRates());
71
72 LMMCurveState smallCS(
74 period, offset
75 ));
76
77 initialRates_ =smallCS.forwardRates();
78
79 Real finalReset = smallCS.rateTimes()[smallCS.numberOfRates()-1];
80 std::vector<Time> oldEvolutionTimes(largeModel->evolution().evolutionTimes());
81 std::vector<Time> newEvolutionTimes;
82 for (Size i =0; i < oldEvolutionTimes.size() && oldEvolutionTimes[i]<= finalReset; ++i)
83 newEvolutionTimes.push_back(oldEvolutionTimes[i]);
84
86 newEvolutionTimes);
87
88 numberOfSteps_ = newEvolutionTimes.size();
89
90
91 const std::vector<Time>& rateTimes =
92 smallCS.rateTimes();
93 // we must ensure we step through all rateTimes
94 const std::vector<Time>& evolutionTimes =
96
97 std::set<Time> setTimes(evolutionTimes.begin(),evolutionTimes.end());
98
99 for (Size i=0; i < rateTimes.size()-1; ++i)
100 QL_REQUIRE(setTimes.find(rateTimes[i]) != setTimes.end(),
101 "every new rate time except last must be an evolution time in fwdperiod adapter");
102
103
104 Matrix YMatrix =
106 largeDisplacements_,
108 period,
109 offset
110 );
111
112 const std::vector<Size>& alive =
114
115 for (Size k = 0; k<numberOfSteps_; ++k) {
116 pseudoRoots_[k]=YMatrix*largeModel->pseudoRoot(k);
117 for (Size i=0; i<alive[k]; ++i)
118 std::fill(pseudoRoots_[k].row_begin(i),
119 pseudoRoots_[k].row_end(i),
120 0.0);
121 }
122 }
123}
124
125
Size numberOfRates() const
Definition: curvestate.hpp:58
const std::vector< Time > & rateTimes() const
Definition: curvestate.hpp:60
Market-model evolution description.
const std::vector< Time > & evolutionTimes() const
const std::vector< Size > & firstAliveRate() const
FwdPeriodAdapter(const ext::shared_ptr< MarketModel > &largeModel, Size period, Size offset, std::vector< Spread > newDisplacements_)
std::vector< Spread > displacements_
std::vector< Matrix > pseudoRoots_
std::vector< Rate > initialRates_
EvolutionDescription evolution_
Curve state for Libor market models
void setOnForwardRates(const std::vector< Rate > &fwdRates, Size firstValidIndex=0)
const std::vector< Rate > & forwardRates() const override
Matrix used in linear algebra.
Definition: matrix.hpp:41
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
LMMCurveState RestrictCurveState(const CurveState &cs, Size multiplier, Size offSet)
Matrix YMatrix(const CurveState &cs, const std::vector< Spread > &shortDisplacements, const std::vector< Spread > &longDisplacements, Size Multiplier, Size offset)
Definition: any.hpp:35
STL namespace.