QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
squarerootandersen.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4Copyright (C) 2008 Mark Joshi
5
6This file is part of QuantLib, a free-software/open-source library
7for financial quantitative analysts and developers - http://quantlib.org/
8
9QuantLib is free software: you can redistribute it and/or modify it
10under the terms of the QuantLib license. You should have received a
11copy 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
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20
21#include <ql/models/marketmodels/evolvers/volprocesses/squarerootandersen.hpp>
22#include <ql/errors.hpp>
23#include <ql/math/distributions/normaldistribution.hpp>
24namespace
26{
27 SquareRootAndersen::SquareRootAndersen(Real meanLevel,
28 Real reversionSpeed,
29 Real volVar,
30 Real v0,
31 const std::vector<Real>& evolutionTimes,
32 Size numberSubSteps,
33 Real w1,
34 Real w2,
35 Real cutPoint )
36 :
37 theta_(meanLevel),
38 k_(reversionSpeed),
39 epsilon_(volVar),
40 v0_(v0),
41 numberSubSteps_(numberSubSteps),
42 dt_(evolutionTimes.size()*numberSubSteps),
43 eMinuskDt_(evolutionTimes.size()*numberSubSteps),
44 w1_(w1),
45 w2_(w2),
46 PsiC_(cutPoint),
47 vPath_(evolutionTimes.size()*numberSubSteps+1),
48 state_(1)
49 {
50 Size j=0;
51 for (; j < numberSubSteps_; ++j)
52 dt_[j] = evolutionTimes[0]/numberSubSteps_;
53
54 for (Size i=1; i < evolutionTimes.size(); ++i)
55 {
56 Real dt = (evolutionTimes[i] - evolutionTimes[i-1])/numberSubSteps_;
57
58 Real ekdt = std::exp(-k_*dt);
59 QL_REQUIRE(dt >0.0, "Steps must be of positive size.");
60
61 for (Size k=0; k < numberSubSteps_; ++k)
62 {
63 dt_[j] = dt;
64 eMinuskDt_[j] = ekdt;
65
66 ++j;
67 }
68 }
69 vPath_[0] = v0_;
70 }
71
72
74 {
75 return numberSubSteps_;
76 }
77
79 {
80 return dt_.size()*numberSubSteps_;
81 }
82
84 {
85 v_=v0_;
87 subStep_=0;
88
89 }
90
92 {
93
94 Real eminuskT = eMinuskDt_[j];
95 Real m = theta_+(vt-theta_)*eminuskT;
96 Real s2= vt*epsilon_*epsilon_*eminuskT*(1-eminuskT)/k_
97 + theta_*epsilon_*epsilon_*(1- eminuskT)*(1- eminuskT)/(2*k_);
98 Real s = std::sqrt(s2);
99 Real psi = s*s/(m*m);
100 if (psi<= PsiC_)
101 {
102 Real psiinv = 1.0/psi;
103 Real b2 = 2.0*psiinv -1+std::sqrt(2*psiinv*(2*psiinv-1.0));
104 Real b = std::sqrt(b2);
105 Real a= m/(1+b2);
106 vt= a*(b+z)*(b+z);
107 }
108 else
109 {
110 Real p = (psi-1.0)/(psi+1.0);
111 Real beta = (1.0-p)/m;
113
114 if (u < p)
115 {
116 vt=0;
117 return;
118 }
119
120 vt = std::log((1.0-p)/(1.0-u))/beta;
121 }
122
123 }
124
125
126 Real SquareRootAndersen::nextstep(const std::vector<Real>& variates)
127 {
128 for (Size j=0; j < numberSubSteps_; ++j)
129 {
130 DoOneSubStep(v_, variates[j], subStep_);
131 ++subStep_;
132 vPath_[subStep_] = v_;
133 }
134
135 ++currentStep_;
136
137 return 1.0; // no importance sampling here
138 }
139
141 {
142 QL_REQUIRE(currentStep_>0, "nextStep must be called before stepSd");
143 Real stepVariance =0.0;
144 Size lastStepStart = (currentStep_-1)*numberSubSteps_;
145 for (Size k=0; k < numberSubSteps_; ++k)
146 stepVariance += w1_*vPath_[k+lastStepStart]+w2_*vPath_[k+lastStepStart+1];
147
148 stepVariance /= numberSubSteps_;
149
150 return std::sqrt(stepVariance);
151 }
152
153 const std::vector<Real>& SquareRootAndersen::stateVariables() const
154 {
155 state_[0] = v_;
156 return state_;
157 }
158
160 {
161 return 1;
162 }
163
164}
Cumulative normal distribution function.
Size numberStateVariables() const override
void DoOneSubStep(Real &v, Real variate, Size j)
const std::vector< Real > & stateVariables() const override
Real nextstep(const std::vector< Real > &variates) override
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35