File: svddfwdratepc.cpp

package info (click to toggle)
quantlib 1.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 30,760 kB
  • sloc: cpp: 232,809; ansic: 21,483; sh: 11,108; makefile: 4,717; lisp: 86
file content (204 lines) | stat: -rw-r--r-- 7,736 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2008 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
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/license.shtml>.

 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 <ql/models/marketmodels/evolvers/svddfwdratepc.hpp>
#include <ql/models/marketmodels/marketmodel.hpp>
#include <ql/models/marketmodels/evolutiondescription.hpp>
#include <ql/models/marketmodels/browniangenerator.hpp>
#include <ql/models/marketmodels/driftcomputation/lmmdriftcalculator.hpp>
#include <ql/models/marketmodels/evolvers/marketmodelvolprocess.hpp>

namespace QuantLib {

    SVDDFwdRatePc::SVDDFwdRatePc(const boost::shared_ptr<MarketModel>& marketModel,
                           const BrownianGeneratorFactory& factory,
                           const boost::shared_ptr<MarketModelVolProcess>& volProcess,
                           Size firstVolatilityFactor, 
                           Size volatilityFactorStep,
                           const std::vector<Size>& numeraires,
                           Size initialStep )
    : marketModel_(marketModel),
      volProcess_(volProcess),
      firstVolatilityFactor_(firstVolatilityFactor),
      volatilityFactorStep_(volatilityFactorStep),
      volFactorsPerStep_(volProcess->variatesPerStep()),
      numeraires_(numeraires),
      initialStep_(initialStep),
      isVolVariate_(false,volProcess->variatesPerStep()+marketModel_->numberOfFactors()),
      numberOfRates_(marketModel->numberOfRates()),
      numberOfFactors_(marketModel_->numberOfFactors()),
      curveState_(marketModel->evolution().rateTimes()),
      forwards_(marketModel->initialRates()),
      displacements_(marketModel->displacements()),
      logForwards_(numberOfRates_), initialLogForwards_(numberOfRates_),
      drifts1_(numberOfRates_), drifts2_(numberOfRates_),
      initialDrifts_(numberOfRates_), allBrownians_(volProcess->variatesPerStep()+marketModel_->numberOfFactors()), 
      brownians_(numberOfFactors_),
      volBrownians_(volProcess->variatesPerStep()), 
      correlatedBrownians_(numberOfRates_),
      alive_(marketModel->evolution().firstAliveRate())
    {
        QL_REQUIRE(initialStep ==0, "initial step zero only supported currently. ");
        checkCompatibility(marketModel->evolution(), numeraires);

        Size steps = marketModel->evolution().numberOfSteps();

        generator_ = factory.create(numberOfFactors_+volFactorsPerStep_, steps-initialStep_);

        currentStep_ = initialStep_;

        calculators_.reserve(steps);
        fixedDrifts_.reserve(steps);
        for (Size j=0; j<steps; ++j) 
        {
            const Matrix& A = marketModel_->pseudoRoot(j);
            calculators_.push_back(
                LMMDriftCalculator(A,
                                   displacements_,
                                   marketModel->evolution().rateTaus(),
                                   numeraires[j],
                                   alive_[j]));
            std::vector<Real> fixed(numberOfRates_);
            for (Size k=0; k<numberOfRates_; ++k) 
            {
                Real variance =
                    std::inner_product(A.row_begin(k), A.row_end(k),
                                       A.row_begin(k), 0.0);
                fixed[k] = -0.5*variance;
            }
            fixedDrifts_.push_back(fixed);
        }

        setForwards(marketModel_->initialRates());

        Size variatesPerStep = numberOfFactors_+volFactorsPerStep_;

        firstVolatilityFactor_ = std::min(firstVolatilityFactor_,variatesPerStep - volFactorsPerStep_);

        Size volIncrement = (variatesPerStep - firstVolatilityFactor_)/volFactorsPerStep_;
        
        for (Size i=0; i < volFactorsPerStep_; ++i)
            isVolVariate_[firstVolatilityFactor_+i*volIncrement] = true;
    }

    const std::vector<Size>& SVDDFwdRatePc::numeraires() const {
        return numeraires_;
    }

    void SVDDFwdRatePc::setForwards(const std::vector<Real>& forwards)
    {
        QL_REQUIRE(forwards.size()==numberOfRates_,
                   "mismatch between forwards and rateTimes");
        for (Size i=0; i<numberOfRates_; ++i)
             initialLogForwards_[i] = std::log(forwards[i] +
                                               displacements_[i]);
        calculators_[initialStep_].compute(forwards, initialDrifts_);
    }

    void SVDDFwdRatePc::setInitialState(const CurveState& cs) 
    {
        setForwards(cs.forwardRates());
    }

    Real SVDDFwdRatePc::startNewPath() 
    {
        currentStep_ = initialStep_;
        std::copy(initialLogForwards_.begin(), initialLogForwards_.end(),
                  logForwards_.begin());
        volProcess_->nextPath();
        return  generator_->nextPath();
    }

    Real SVDDFwdRatePc::advanceStep()
    {
        // we're going from T1 to T2

        // a) compute drifts D1 at T1;
        if (currentStep_ > initialStep_) 
        {
            calculators_[currentStep_].compute(forwards_, drifts1_);
        } 
        else 
        {
            std::copy(initialDrifts_.begin(), initialDrifts_.end(),
                      drifts1_.begin());
        }

        // b) evolve forwards up to T2 using D1;
        Real weight = generator_->nextStep(allBrownians_);

        // divide Brownians between vol process and forward process

        for (Size i=0, j=0, k=0; i < allBrownians_.size(); ++i)
            if ( isVolVariate_[i])
            {
                volBrownians_[j] = allBrownians_[i];
                ++j;
            }
            else
            {
                brownians_[k] = allBrownians_[i];
                ++k;
            }


        // get sd for step

        Real weight2 = volProcess_->nextstep(volBrownians_);
        Real sdMultiplier = volProcess_->stepSd();
        Real varianceMultiplier = sdMultiplier*sdMultiplier;

        const Matrix& A = marketModel_->pseudoRoot(currentStep_);
        const std::vector<Real>& fixedDrift = fixedDrifts_[currentStep_];

        Size alive = alive_[currentStep_];
        for (Size i=alive; i<numberOfRates_; ++i) {
            logForwards_[i] += varianceMultiplier*(drifts1_[i] + fixedDrift[i]);
            logForwards_[i] += sdMultiplier*
                std::inner_product(A.row_begin(i), A.row_end(i),
                                   brownians_.begin(), 0.0);
            forwards_[i] = std::exp(logForwards_[i]) - displacements_[i];
        }

        // c) recompute drifts D2 using the predicted forwards;
        calculators_[currentStep_].compute(forwards_, drifts2_);

        // d) correct forwards using both drifts
        for (Size i=alive; i<numberOfRates_; ++i) {
            logForwards_[i] += varianceMultiplier*(drifts2_[i]-drifts1_[i])/2.0;
            forwards_[i] = std::exp(logForwards_[i]) - displacements_[i];
        }

        // e) update curve state
        curveState_.setOnForwardRates(forwards_);

        ++currentStep_;

        return weight*weight2;
    }

    Size SVDDFwdRatePc::currentStep() const {
        return currentStep_;
    }

    const CurveState& SVDDFwdRatePc::currentState() const {
        return curveState_;
    }

}