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
|
/* -*- 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/volprocesses/squarerootandersen.hpp>
#include <ql/errors.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
namespace
QuantLib
{
SquareRootAndersen::SquareRootAndersen(Real meanLevel,
Real reversionSpeed,
Real volVar,
Real v0,
const std::vector<Real>& evolutionTimes,
Size numberSubSteps,
Real w1,
Real w2,
Real cutPoint )
:
theta_(meanLevel),
k_(reversionSpeed),
epsilon_(volVar),
v0_(v0),
numberSubSteps_(numberSubSteps),
dt_(evolutionTimes.size()*numberSubSteps),
eMinuskDt_(evolutionTimes.size()*numberSubSteps),
w1_(w1),
w2_(w2),
PsiC_(cutPoint),
vPath_(evolutionTimes.size()*numberSubSteps+1),
state_(1)
{
Size j=0;
for (; j < numberSubSteps_; ++j)
dt_[j] = evolutionTimes[0]/numberSubSteps_;
for (Size i=1; i < evolutionTimes.size(); ++i)
{
Real dt = (evolutionTimes[i] - evolutionTimes[i-1])/numberSubSteps_;
Real ekdt = std::exp(-k_*dt);
QL_REQUIRE(dt >0.0, "Steps must be of positive size.");
for (Size k=0; k < numberSubSteps_; ++k)
{
dt_[j] = dt;
eMinuskDt_[j] = ekdt;
++j;
}
}
vPath_[0] = v0_;
}
Size SquareRootAndersen::variatesPerStep()
{
return numberSubSteps_;
}
Size SquareRootAndersen::numberSteps()
{
return dt_.size()*numberSubSteps_;
}
void SquareRootAndersen::nextPath()
{
v_=v0_;
currentStep_=0;
subStep_=0;
}
void SquareRootAndersen::DoOneSubStep(Real& vt, Real z, Size j)
{
Real eminuskT = eMinuskDt_[j];
Real m = theta_+(vt-theta_)*eminuskT;
Real s2= vt*epsilon_*epsilon_*eminuskT*(1-eminuskT)/k_
+ theta_*epsilon_*epsilon_*(1- eminuskT)*(1- eminuskT)/(2*k_);
Real s = std::sqrt(s2);
Real psi = s*s/(m*m);
if (psi<= PsiC_)
{
Real psiinv = 1.0/psi;
Real b2 = 2.0*psiinv -1+std::sqrt(2*psiinv*(2*psiinv-1.0));
Real b = std::sqrt(b2);
Real a= m/(1+b2);
vt= a*(b+z)*(b+z);
}
else
{
Real p = (psi-1.0)/(psi+1.0);
Real beta = (1.0-p)/m;
Real u = CumulativeNormalDistribution()(z);
if (u < p)
{
vt=0;
return;
}
vt = std::log((1.0-p)/(1.0-u))/beta;
}
}
Real SquareRootAndersen::nextstep(const std::vector<Real>& variates)
{
for (Size j=0; j < numberSubSteps_; ++j)
{
DoOneSubStep(v_, variates[j], subStep_);
++subStep_;
vPath_[subStep_] = v_;
}
++currentStep_;
return 1.0; // no importance sampling here
}
Real SquareRootAndersen::stepSd() const
{
QL_REQUIRE(currentStep_>0, "nextStep must be called before stepSd");
Real stepVariance =0.0;
Size lastStepStart = (currentStep_-1)*numberSubSteps_;
for (Size k=0; k < numberSubSteps_; ++k)
stepVariance += w1_*vPath_[k+lastStepStart]+w2_*vPath_[k+lastStepStart+1];
stepVariance /= numberSubSteps_;
return std::sqrt(stepVariance);
}
const std::vector<Real>& SquareRootAndersen::stateVariables() const
{
state_[0] = v_;
return state_;
}
Size SquareRootAndersen::numberStateVariables() const
{
return 1;
}
}
|