Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/methods/finitedifferences/bsmoperator.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5
 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
6
7
 This file is part of QuantLib, a free-software/open-source library
8
 for financial quantitative analysts and developers - http://quantlib.org/
9
10
 QuantLib is free software: you can redistribute it and/or modify it
11
 under the terms of the QuantLib license.  You should have received a
12
 copy of the license along with this program; if not, please email
13
 <quantlib-dev@lists.sf.net>. The license is also available online at
14
 <http://quantlib.org/license.shtml>.
15
16
 This program is distributed in the hope that it will be useful, but WITHOUT
17
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 FOR A PARTICULAR PURPOSE.  See the license for more details.
19
*/
20
21
#include <ql/methods/finitedifferences/bsmoperator.hpp>
22
#include <ql/math/transformedgrid.hpp>
23
#include <ql/methods/finitedifferences/pdebsm.hpp>
24
25
namespace QuantLib {
26
27
    QL_DEPRECATED_DISABLE_WARNING
28
29
    BSMOperator::BSMOperator(Size size, Real dx, Rate r,
30
                             Rate q, Volatility sigma)
31
0
    : TridiagonalOperator(size) {
32
0
        Real sigma2 = sigma*sigma;
33
0
        Real nu = r-q-sigma2/2;
34
0
        Real pd = -(sigma2/dx-nu)/(2*dx);
35
0
        Real pu = -(sigma2/dx+nu)/(2*dx);
36
0
        Real pm = sigma2/(dx*dx)+r;
37
0
        setMidRows(pd,pm,pu);
38
0
    }
39
40
    BSMOperator::BSMOperator(const Array& grid,
41
                             Rate r, Rate q, Volatility sigma)
42
0
    : TridiagonalOperator(grid.size()) {
43
0
        PdeBSM::grid_type logGrid(grid);
44
0
        Real sigma2 = sigma*sigma;
45
0
        Real nu = r-q-sigma2/2;
46
0
        for (Size i=1; i<logGrid.size()-1; ++i) {
47
0
            Real pd = -(sigma2/logGrid.dxm(i)-nu)/logGrid.dx(i);
48
0
            Real pu = -(sigma2/logGrid.dxp(i)+nu)/logGrid.dx(i);
49
0
            Real pm = sigma2/(logGrid.dxm(i)*logGrid.dxp(i)) + r;
50
0
            setMidRow(i,pd,pm,pu);
51
0
        }
52
0
    }
53
54
    QL_DEPRECATED_ENABLE_WARNING
55
56
}