/usr/include/ql/experimental/math/isotropicrandomwalk.hpp is in libquantlib0-dev 1.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Andres Hernandez
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 isotropicrandomwalk.hpp
\brief Isotropic random walk
*/
#ifndef quantlib_isotropic_random_walk_hpp
#define quantlib_isotropic_random_walk_hpp
#include <ql/mathconstants.hpp>
#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
#include <ql/math/array.hpp>
#include <boost/random/variate_generator.hpp>
namespace QuantLib {
//! Isotropic random walk
/*! A variate is used to draw from a random element of a
probability distribution. The draw corresponds to the
radius of a d-dimensional sphere. The position on the
surface of the d-dimensional sphere is randomly chosen
with all points on the surface having the same probability,
i.e. all directions are isotropic and the step is randomly
drawn from the given variate.
*/
template <class Distribution, class Engine>
class IsotropicRandomWalk {
public:
typedef boost::variate_generator<Engine, Distribution> VariateGenerator;
IsotropicRandomWalk(Engine eng, Distribution dist, Size dim,
const Array& weights = Array(),
unsigned long seed = 0) :
variate_(eng, dist), rng_(seed),
weights_(weights), dim_(dim) {
if (weights_.empty())
weights_ = Array(dim, 1.0);
else
QL_REQUIRE(dim_ == weights_.size(), "Invalid weights");
}
template <class InputIterator>
inline void nextReal(InputIterator first) const {
Real radius = variate_();
Array::const_iterator weight = weights_.begin();
if (dim_ > 1) {
//Isotropic random direction
Real phi = M_PI*rng_.nextReal();
for (Size i = 0; i < dim_ - 2; i++) {
*first++ = radius*cos(phi)*(*weight++);
radius *= sin(phi);
phi = M_PI*rng_.nextReal();
}
*first++ = radius*cos(2.0*phi)*(*weight++);
*first = radius*sin(2.0*phi)*(*weight);
}
else {
if (rng_.nextReal() < 0.5)
*first = -radius*(*weight);
else
*first = radius*(*weight);
}
}
inline void setDimension(Size dim) {
dim_ = dim;
weights_ = Array(dim, 1.0);
}
inline void setDimension(Size dim, const Array& weights) {
QL_REQUIRE(dim == weights.size(), "Invalid weights");
dim_ = dim;
weights_ = weights;
}
/*!
The isotropic random walk will not adjust its draw to be within the lower and upper bounds,
but if the limits are provided, they are used to rescale the sphere so as to make it to an
ellipsoid, with different radius in different dimensions.
*/
inline void setDimension(Size dim,
const Array& lowerBound, const Array& upperBound) {
QL_REQUIRE(dim == lowerBound.size(),
"Incompatible dimension and lower bound");
QL_REQUIRE(dim == upperBound.size(),
"Incompatible dimension and upper bound");
//Find largest bound
Array bounds = upperBound - lowerBound;
Real maxBound = bounds[0];
for (Size j = 1; j < dim; j++) {
if (bounds[j] > maxBound) maxBound = bounds[j];
}
//weights by dimension is the size of the bound
//divided by the largest bound
maxBound = 1.0 / maxBound;
bounds *= maxBound;
setDimension(dim, bounds);
}
protected:
mutable VariateGenerator variate_;
MersenneTwisterUniformRng rng_;
Array weights_;
Size dim_;
};
}
#endif
|