Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/experimental/finitedifferences/dynprogvppintrinsicvalueengine.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) 2012 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
 <http://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 dynprogvppintrinsicvalueengine.cpp
21
*/
22
23
#include <ql/experimental/finitedifferences/dynprogvppintrinsicvalueengine.hpp>
24
#include <ql/experimental/finitedifferences/fdmvppstepconditionfactory.hpp>
25
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
26
#include <ql/methods/finitedifferences/meshers/uniform1dmesher.hpp>
27
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
28
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
29
#include <ql/termstructures/yieldtermstructure.hpp>
30
#include <utility>
31
32
33
namespace QuantLib {
34
    namespace {
35
        class SparkSpreadPrice : public FdmInnerValueCalculator {
36
          public:
37
            SparkSpreadPrice(Real heatRate,
38
                             const std::vector<Real>& fuelPrices,
39
                             const std::vector<Real>& powerPrices)
40
0
            : heatRate_(heatRate),
41
0
              fuelPrices_(fuelPrices),
42
0
              powerPrices_(powerPrices) {}
43
44
0
            Real innerValue(const FdmLinearOpIterator&, Time t) override {
45
0
                Size i = (Size) t;
46
0
                QL_REQUIRE(i < powerPrices_.size(), "invalid time");
47
0
                return powerPrices_[i] - heatRate_*fuelPrices_[i];
48
0
            }
49
0
            Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override {
50
0
                return innerValue(iter, t);
51
0
            }
52
53
          private:
54
            const Real heatRate_;
55
            const std::vector<Real>& fuelPrices_;
56
            const std::vector<Real>& powerPrices_;
57
        };
58
59
60
        class FuelPrice : public FdmInnerValueCalculator {
61
          public:
62
            explicit FuelPrice(const std::vector<Real>& fuelPrices)
63
0
            : fuelPrices_(fuelPrices) {}
64
65
0
            Real innerValue(const FdmLinearOpIterator&, Time t) override {
66
0
                Size i = (Size) t;
67
0
                QL_REQUIRE(i < fuelPrices_.size(), "invalid time");
68
0
                return fuelPrices_[(Size) t];
69
0
            }
70
0
            Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override {
71
0
                return innerValue(iter, t);
72
0
            }
73
74
          private:
75
            const std::vector<Real>& fuelPrices_;
76
        };
77
    }
78
79
    DynProgVPPIntrinsicValueEngine::DynProgVPPIntrinsicValueEngine(
80
        std::vector<Real> fuelPrices,
81
        std::vector<Real> powerPrices,
82
        Real fuelCostAddon,
83
        ext::shared_ptr<YieldTermStructure> rTS)
84
0
    : fuelPrices_(std::move(fuelPrices)), powerPrices_(std::move(powerPrices)),
85
0
      fuelCostAddon_(fuelCostAddon), rTS_(std::move(rTS)) {}
86
87
0
    void DynProgVPPIntrinsicValueEngine::calculate() const {
88
0
        const ext::shared_ptr<FdmInnerValueCalculator> fuelPrice(
89
0
            new FuelPrice(fuelPrices_));
90
0
        const ext::shared_ptr<FdmInnerValueCalculator> sparkSpreadPrice(
91
0
            new SparkSpreadPrice(arguments_.heatRate,fuelPrices_,powerPrices_));
92
93
0
        const FdmVPPStepConditionFactory stepConditionFactory(arguments_);
94
95
0
        const ext::shared_ptr<FdmMesher> mesher(
96
0
            new FdmMesherComposite(stepConditionFactory.stateMesher()));
97
98
0
        const FdmVPPStepConditionMesher mesh = { 0, mesher };
99
100
0
        const ext::shared_ptr<FdmVPPStepCondition> stepCondition(
101
0
            stepConditionFactory.build(mesh, fuelCostAddon_,
102
0
                                       fuelPrice, sparkSpreadPrice));
103
104
0
        Array state(mesher->layout()->dim()[0], 0.0);
105
0
        for (Size j=powerPrices_.size(); j > 0; --j) {
106
0
            stepCondition->applyTo(state, (Time) j-1);
107
0
        }
108
109
0
        results_.value = stepCondition->maxValue(state);
110
0
    }
111
}
112