QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
dynprogvppintrinsicvalueengine.cpp
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
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
33namespace 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 : heatRate_(heatRate),
41 fuelPrices_(fuelPrices),
42 powerPrices_(powerPrices) {}
43
44 Real innerValue(const FdmLinearOpIterator&, Time t) override {
45 Size i = (Size) t;
46 QL_REQUIRE(i < powerPrices_.size(), "invalid time");
47 return powerPrices_[i] - heatRate_*fuelPrices_[i];
48 }
49 Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override {
50 return innerValue(iter, t);
51 }
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 : fuelPrices_(fuelPrices) {}
64
65 Real innerValue(const FdmLinearOpIterator&, Time t) override {
66 Size i = (Size) t;
67 QL_REQUIRE(i < fuelPrices_.size(), "invalid time");
68 return fuelPrices_[(Size) t];
69 }
70 Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override {
71 return innerValue(iter, t);
72 }
73
74 private:
75 const std::vector<Real>& fuelPrices_;
76 };
77 }
78
80 std::vector<Real> fuelPrices,
81 std::vector<Real> powerPrices,
82 Real fuelCostAddon,
83 ext::shared_ptr<YieldTermStructure> rTS)
84 : fuelPrices_(std::move(fuelPrices)), powerPrices_(std::move(powerPrices)),
85 fuelCostAddon_(fuelCostAddon), rTS_(std::move(rTS)) {}
86
88 const ext::shared_ptr<FdmInnerValueCalculator> fuelPrice(
89 new FuelPrice(fuelPrices_));
90 const ext::shared_ptr<FdmInnerValueCalculator> sparkSpreadPrice(
91 new SparkSpreadPrice(arguments_.heatRate,fuelPrices_,powerPrices_));
92
93 const FdmVPPStepConditionFactory stepConditionFactory(arguments_);
94
95 const ext::shared_ptr<FdmMesher> mesher(
96 new FdmMesherComposite(stepConditionFactory.stateMesher()));
97
98 const FdmVPPStepConditionMesher mesh = { 0, mesher };
99
100 const ext::shared_ptr<FdmVPPStepCondition> stepCondition(
101 stepConditionFactory.build(mesh, fuelCostAddon_,
102 fuelPrice, sparkSpreadPrice));
103
104 Array state(mesher->layout()->dim()[0], 0.0);
105 for (Size j=powerPrices_.size(); j > 0; --j) {
106 stepCondition->applyTo(state, (Time) j-1);
107 }
108
109 results_.value = stepCondition->maxValue(state);
110 }
111}
112
1-D array used in linear algebra.
Definition: array.hpp:52
DynProgVPPIntrinsicValueEngine(std::vector< Real > fuelPrices, std::vector< Real > powerPrices, Real fuelCostAddon, ext::shared_ptr< YieldTermStructure > rTS)
ext::shared_ptr< Fdm1dMesher > stateMesher() const
ext::shared_ptr< FdmVPPStepCondition > build(const FdmVPPStepConditionMesher &mesh, Real fuelCostAddon, const ext::shared_ptr< FdmInnerValueCalculator > &fuel, const ext::shared_ptr< FdmInnerValueCalculator > &spark) const
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.