Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/math/isotropicrandomwalk.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2015 Andres Hernandez
5
6
 This file is part of QuantLib, a free-software/open-source library
7
 for financial quantitative analysts and developers - http://quantlib.org/
8
9
 QuantLib is free software: you can redistribute it and/or modify it
10
 under the terms of the QuantLib license.  You should have received a
11
 copy 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
 <https://www.quantlib.org/license.shtml>.
14
15
 This program is distributed in the hope that it will be useful, but WITHOUT
16
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19
20
/*! \file isotropicrandomwalk.hpp
21
    \brief Isotropic random walk
22
*/
23
24
#ifndef quantlib_isotropic_random_walk_hpp
25
#define quantlib_isotropic_random_walk_hpp
26
27
#include <ql/math/array.hpp>
28
#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
29
#include <ql/mathconstants.hpp>
30
#include <utility>
31
32
namespace QuantLib {
33
34
    //! Isotropic random walk
35
    /*! A variate is used to draw from a random element of a 
36
        probability distribution. The draw corresponds to the 
37
        radius of a d-dimensional sphere. The position on the
38
        surface of the d-dimensional sphere is randomly chosen
39
        with all points on the surface having the same probability,
40
        i.e. all directions are isotropic and the step is randomly
41
        drawn from the given variate.
42
    */
43
    template <class Distribution, class Engine>
44
    class IsotropicRandomWalk {
45
      public:
46
        IsotropicRandomWalk(Engine eng,
47
                            Distribution dist,
48
                            Size dim,
49
                            Array weights = Array(),
50
                            unsigned long seed = 0)
51
        : engine_(std::move(eng)), distribution_(std::move(dist)), rng_(seed), weights_(std::move(weights)), dim_(dim) {
52
            if (weights_.empty())
53
                weights_ = Array(dim, 1.0);
54
            else
55
                QL_REQUIRE(dim_ == weights_.size(), "Invalid weights");
56
        }
57
        template <class InputIterator>
58
0
        void nextReal(InputIterator first) {
59
0
            Real radius = distribution_(engine_);
60
0
            Array::const_iterator weight = weights_.begin();
61
0
            if (dim_ > 1) {
62
0
                //Isotropic random direction
63
0
                Real phi = M_PI*rng_.nextReal();
64
0
                for (Size i = 0; i < dim_ - 2; i++) {
65
0
                    *first++ = radius*cos(phi)*(*weight++);
66
0
                    radius *= sin(phi);
67
0
                    phi = M_PI*rng_.nextReal();
68
0
                }
69
0
                *first++ = radius*cos(2.0*phi)*(*weight++);
70
0
                *first = radius*sin(2.0*phi)*(*weight);
71
0
            }
72
0
            else {
73
0
                if (rng_.nextReal() < 0.5)
74
0
                    *first = -radius*(*weight);
75
0
                else
76
0
                    *first = radius*(*weight);
77
0
            }
78
0
        }
Unexecuted instantiation: void QuantLib::IsotropicRandomWalk<std::__1::normal_distribution<double>, std::__1::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >::nextReal<double*>(double*)
Unexecuted instantiation: void QuantLib::IsotropicRandomWalk<QuantLib::LevyFlightDistribution, std::__1::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >::nextReal<double*>(double*)
79
        void setDimension(Size dim) { 
80
            dim_ = dim;
81
            weights_ = Array(dim, 1.0);
82
        }
83
0
        void setDimension(Size dim, const Array& weights) {
84
0
            QL_REQUIRE(dim == weights.size(), "Invalid weights");
85
0
            dim_ = dim;
86
0
            weights_ = weights;
87
0
        }
Unexecuted instantiation: QuantLib::IsotropicRandomWalk<std::__1::normal_distribution<double>, std::__1::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >::setDimension(unsigned long, QuantLib::Array const&)
Unexecuted instantiation: QuantLib::IsotropicRandomWalk<QuantLib::LevyFlightDistribution, std::__1::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >::setDimension(unsigned long, QuantLib::Array const&)
88
        /*!
89
        The isotropic random walk will not adjust its draw to be within the lower and upper bounds,
90
        but if the limits are provided, they are used to rescale the sphere so as to make it to an
91
        ellipsoid, with different radius in different dimensions.
92
        */
93
        void setDimension(Size dim,
94
0
            const Array& lowerBound, const Array& upperBound) {
95
0
            QL_REQUIRE(dim == lowerBound.size(),
96
0
                "Incompatible dimension and lower bound");
97
0
            QL_REQUIRE(dim == upperBound.size(),
98
0
                "Incompatible dimension and upper bound");
99
0
            //Find largest bound
100
0
            Array bounds = upperBound - lowerBound;
101
0
            Real maxBound = bounds[0];
102
0
            for (Size j = 1; j < dim; j++) {
103
0
                if (bounds[j] > maxBound) maxBound = bounds[j];
104
0
            }
105
0
            //weights by dimension is the size of the bound
106
0
            //divided by the largest bound
107
0
            maxBound = 1.0 / maxBound;
108
0
            bounds *= maxBound;
109
0
            setDimension(dim, bounds);
110
0
        }
Unexecuted instantiation: QuantLib::IsotropicRandomWalk<std::__1::normal_distribution<double>, std::__1::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >::setDimension(unsigned long, QuantLib::Array const&, QuantLib::Array const&)
Unexecuted instantiation: QuantLib::IsotropicRandomWalk<QuantLib::LevyFlightDistribution, std::__1::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >::setDimension(unsigned long, QuantLib::Array const&, QuantLib::Array const&)
111
      protected:
112
        Engine engine_;
113
        Distribution distribution_;
114
        MersenneTwisterUniformRng rng_;
115
        Array weights_;
116
        Size dim_;
117
    };
118
}
119
#endif