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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007 Klaus Spanderen
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.
*/
/*! \file jointstochasticprocess.cpp
\brief multi model process for hybrid products
*/
#include <ql/math/matrixutilities/svd.hpp>
#include <ql/math/matrixutilities/pseudosqrt.hpp>
#include <ql/processes/jointstochasticprocess.hpp>
namespace QuantLib {
JointStochasticProcess::JointStochasticProcess(
const std::vector<boost::shared_ptr<StochasticProcess> > & l,
Size factors)
: l_ (l),
size_ (0),
factors_(factors),
modelFactors_(0) {
for (const_iterator iter=l_.begin(); iter != l_.end(); ++iter) {
registerWith(*iter);
}
vsize_.reserve (l_.size()+1);
vfactors_.reserve(l_.size()+1);
for (const_iterator iter = l_.begin(); iter != l_.end(); ++iter) {
vsize_.push_back(size_);
size_ += (*iter)->size();
vfactors_.push_back(modelFactors_);
modelFactors_ += (*iter)->factors();
}
vsize_.push_back(size_);
vfactors_.push_back(modelFactors_);
if (factors_ == Null<Size>()) {
factors_ = modelFactors_;
} else {
QL_REQUIRE(factors_ <= size_, "too many factors given");
}
}
Size JointStochasticProcess::size() const {
return size_;
}
Size JointStochasticProcess::factors() const {
return factors_;
}
Disposable<Array> JointStochasticProcess::slice(const Array& x,
Size i) const {
// cut out the ith process' variables
Size n = vsize_[i+1]-vsize_[i];
Array y(n);
std::copy(x.begin()+vsize_[i], x.begin()+vsize_[i+1], y.begin());
return y;
}
Disposable<Array> JointStochasticProcess::initialValues() const {
Array retVal(size());
for (const_iterator iter = l_.begin(); iter != l_.end(); ++iter) {
const Array& pInitValues = (*iter)->initialValues();
std::copy(pInitValues.begin(), pInitValues.end(),
retVal.begin()+vsize_[iter - l_.begin()]);
}
return retVal;
}
Disposable<Array> JointStochasticProcess::drift(Time t,
const Array& x) const {
Array retVal(size());
for (Size i=0; i < l_.size(); ++i) {
const Array& pDrift = l_[i]->drift(t, slice(x,i));
std::copy(pDrift.begin(), pDrift.end(),
retVal.begin()+vsize_[i]);
}
return retVal;
}
Disposable<Array> JointStochasticProcess::expectation(Time t0,
const Array& x0,
Time dt) const {
Array retVal(size());
for (Size i=0; i < l_.size(); ++i) {
const Array& pExpectation = l_[i]->expectation(t0, slice(x0,i), dt);
std::copy(pExpectation.begin(), pExpectation.end(),
retVal.begin()+ vsize_[i]);
}
return retVal;
}
Disposable<Matrix> JointStochasticProcess::diffusion(
Time t, const Array& x) const {
// might need some improvement in the future
const Time dt = 0.001;
return pseudoSqrt(covariance(t, x, dt)/dt);
}
Disposable<Matrix> JointStochasticProcess::covariance(Time t0,
const Array& x0,
Time dt) const {
// get the model intrinsic covariance matrix
Matrix retVal(size(), size(), 0.0);
for (Size j=0; j < l_.size(); ++j) {
const Size vs = vsize_[j];
const Matrix& pCov = l_[j]->covariance(t0, slice(x0,j), dt);
for (Size i=0; i < pCov.rows(); ++i) {
std::copy(pCov.row_begin(i), pCov.row_end(i),
retVal.row_begin(vs+i) + vs);
}
}
// add the cross model covariance matrix
const Array& volatility = Sqrt(retVal.diagonal());
Matrix crossModelCovar = this->crossModelCorrelation(t0, x0);
for (Size i=0; i < size(); ++i) {
for (Size j=0; j < size(); ++j) {
crossModelCovar[i][j] *= volatility[i]*volatility[j];
}
}
retVal += crossModelCovar;
return retVal;
}
Disposable<Matrix> JointStochasticProcess::stdDeviation(Time t0,
const Array& x0,
Time dt) const {
return pseudoSqrt(covariance(t0, x0, dt));
}
Disposable<Array> JointStochasticProcess::apply(const Array& x0,
const Array& dx) const {
Array retVal(size());
for (Size i=0; i < l_.size(); ++i) {
const Array& pApply = l_[i]->apply(slice(x0,i), slice(dx,i));
std::copy(pApply.begin(), pApply.end(),
retVal.begin()+vsize_[i]);
}
return retVal;
}
Disposable<Array> JointStochasticProcess::evolve(
Time t0, const Array& x0, Time dt, const Array& dw) const {
Array dv(modelFactors_);
if ( correlationIsStateDependent()
|| correlationCache_.count(CachingKey(t0, dt)) == 0) {
Matrix cov = covariance(t0, x0, dt);
const Array& sqrtDiag = Sqrt(cov.diagonal());
for (Size i=0; i < cov.rows(); ++i) {
for (Size j=i; j < cov.columns(); ++j) {
const Real div = sqrtDiag[i]*sqrtDiag[j];
cov[i][j] = cov[j][i] = ( div > 0) ? cov[i][j]/div : 0.0;
}
}
Matrix diff(size(), modelFactors_, 0.0);
for (Size j = 0; j < l_.size(); ++j) {
const Size vs = vsize_ [j];
const Size vf = vfactors_[j];
Matrix stdDev = l_[j]->stdDeviation(t0, slice(x0,j), dt);
for (Size i=0; i < stdDev.rows(); ++i) {
const Volatility vol = std::sqrt(
std::inner_product(stdDev.row_begin(i),
stdDev.row_end(i),
stdDev.row_begin(i), 0.0));
if (vol > 0.0) {
std::transform(stdDev.row_begin(i), stdDev.row_end(i),
stdDev.row_begin(i),
std::bind2nd(std::divides<Real>(),
vol));
}
else {
// keep the svd happy
std::fill(stdDev.row_begin(i), stdDev.row_end(i),
100*i*QL_EPSILON);
}
}
SVD svd(stdDev);
const Array& s = svd.singularValues();
Matrix w(s.size(), s.size(), 0.0);
for (Size i=0; i < s.size(); ++i) {
if (std::fabs(s[i]) > std::sqrt(QL_EPSILON)) {
w[i][i] = 1.0/s[i];
}
}
const Matrix inv = svd.U() * w * transpose(svd.V());
for (Size i=0; i < stdDev.rows(); ++i) {
std::copy(inv.row_begin(i), inv.row_end(i),
diff.row_begin(i+vs)+vf);
}
}
Matrix rs = rankReducedSqrt(cov, factors_, 1.0,
SalvagingAlgorithm::Spectral);
if (rs.columns() < factors_) {
// less eigenvalues than expected factors.
// fill the rest with zero's.
Matrix tmp = Matrix(cov.rows(), factors_, 0.0);
for (Size i=0; i < cov.rows(); ++i) {
std::copy(rs.row_begin(i), rs.row_end(i),
tmp.row_begin(i));
}
rs = tmp;
}
const Matrix m = transpose(diff) * rs;
if (!correlationIsStateDependent()) {
correlationCache_[CachingKey(t0,dt)] = m;
}
dv = m*dw;
}
else {
if (!correlationIsStateDependent()) {
dv = correlationCache_[CachingKey(t0,dt)] * dw;
}
}
this->preEvolve(t0, x0, dt, dv);
Array retVal(size());
for (const_iterator iter = l_.begin(); iter != l_.end(); ++iter) {
const Size i = iter - l_.begin();
Array dz((*iter)->factors());
std::copy(dv.begin()+vfactors_[i],
dv.begin()+vfactors_[i] + (*iter)->factors(),
dz.begin());
Array x((*iter)->size());
std::copy(x0.begin()+vsize_[i],
x0.begin()+vsize_[i] + (*iter)->size(),
x.begin());
const Array r = (*iter)->evolve(t0, x, dt, dz);
std::copy(r.begin(), r.end(), retVal.begin()+vsize_[i]);
}
return this->postEvolve(t0, x0, dt, dv, retVal);
}
const std::vector<boost::shared_ptr<StochasticProcess> > &
JointStochasticProcess::constituents() const {
return l_;
}
Time JointStochasticProcess::time(const Date& date) const {
QL_REQUIRE(l_.size() > 0, "process list is empty");
return l_[0]->time(date);
}
void JointStochasticProcess::update() {
// clear all caches
correlationCache_.clear();
this->StochasticProcess::update();
}
}
|