QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
dynprogvppintrinsicvalueengine.cpp
Go to the documentation of this file.
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
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:
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
const DefaultType & t
const Real heatRate_
const std::vector< Real > & fuelPrices_
const std::vector< Real > & powerPrices_
intrinsic value engine using dynamic programming
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
const ext::shared_ptr< YieldTermStructure > rTS_
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
FdmMesher which is a composite of Fdm1dMesher.
factory for VPP step conditions for FD models
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.
One-dimensional simple uniform grid mesher.
Interest-rate term structure.