Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2009, 2011 Klaus Spanderen
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
#include <algorithm>
21
#include <ql/stochasticprocess.hpp>
22
#include <ql/math/distributions/normaldistribution.hpp>
23
#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp>
24
25
namespace QuantLib {
26
27
    FdmSimpleProcess1dMesher::FdmSimpleProcess1dMesher(
28
        Size size,
29
        const ext::shared_ptr<StochasticProcess1D>& process,
30
        Time maturity, Size tAvgSteps, Real eps, Real mandatoryPoint)
31
0
        : Fdm1dMesher(size) {
32
            
33
0
        std::fill(locations_.begin(), locations_.end(), 0.0);    
34
0
        for (Size l=1; l<=tAvgSteps; ++l) {
35
0
            const Real t = (maturity*l)/tAvgSteps;
36
            
37
0
            const Real mp = (mandatoryPoint != Null<Real>()) ? mandatoryPoint
38
0
                                                             : process->x0();
39
40
0
            const Real qMin = std::min({
41
0
                    mp,
42
0
                    process->x0(),
43
0
                    process->evolve(0, process->x0(), t, 
44
0
                                    InverseCumulativeNormal()(eps))
45
0
                });
46
0
            const Real qMax = std::max({
47
0
                    mp,
48
0
                    process->x0(),
49
0
                    process->evolve(0, process->x0(), t,
50
0
                                    InverseCumulativeNormal()(1-eps))
51
0
                });
52
            
53
0
            const Real dp = (1-2*eps)/(size-1);
54
0
            Real p = eps;
55
0
            locations_[0] += qMin;
56
            
57
0
            for (Size i=1; i < size-1; ++i) {
58
0
                p += dp;
59
0
                locations_[i] += process->evolve(0, process->x0(), t, 
60
0
                                                 InverseCumulativeNormal()(p));
61
0
            }
62
0
            locations_.back() += qMax;
63
0
        }
64
0
        std::transform(locations_.begin(), locations_.end(), locations_.begin(),
65
0
                       [=](Real x) -> Real { return x / tAvgSteps; });
66
0
        for (Size i=0; i < size-1; ++i) {
67
0
            dminus_[i+1] = dplus_[i] = locations_[i+1] - locations_[i];
68
0
        }
69
0
        dplus_.back() = dminus_.front() = Null<Real>();        
70
0
    }
71
}