QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
parametricexercise.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) 2006 StatPro Italia srl
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
24namespace QuantLib {
25
26 namespace {
27
28 class ValueEstimate : public CostFunction {
29 public:
30 ValueEstimate(const std::vector<NodeData>& simulationData,
31 const ParametricExercise& exercise,
32 Size exerciseIndex);
33 Real value(const Array& parameters) const override;
34 Array values(const Array&) const override {
35 QL_FAIL("values method not implemented");
36 }
37
38 private:
39 const std::vector<NodeData>& simulationData_;
40 const ParametricExercise& exercise_;
42 mutable std::vector<Real> parameters_;
43 };
44
45 ValueEstimate::ValueEstimate(
46 const std::vector<NodeData>& simulationData,
47 const ParametricExercise& exercise,
48 Size exerciseIndex)
49 : simulationData_(simulationData), exercise_(exercise),
50 exerciseIndex_(exerciseIndex),
51 parameters_(exercise.numberOfParameters()[exerciseIndex]) {
52 for (const auto& i : simulationData_) {
53 if (i.isValid)
54 return;
55 }
56 QL_FAIL("no valid paths");
57 }
58
59 Real ValueEstimate::value(const Array& parameters) const {
60 std::copy(parameters.begin(), parameters.end(),
61 parameters_.begin());
62 Real sum = 0.0;
63 Size n = 0;
64 for (const auto& i : simulationData_) {
65 if (i.isValid) {
66 ++n;
67 if (exercise_.exercise(exerciseIndex_, parameters_, i.values))
68 sum += i.exerciseValue;
69 else
70 sum += i.cumulatedCashFlows;
71 }
72 }
73 return -sum/n;
74 }
75
76 }
77
78
79
81 std::vector<std::vector<NodeData> >& simulationData,
82 const ParametricExercise& exercise,
83 std::vector<std::vector<Real> >& parameters,
84 const EndCriteria& endCriteria,
85 OptimizationMethod& method) {
86
87 Size steps = simulationData.size();
88 parameters.resize(steps-1);
89
90 for (Size i=steps-1; i!=0; --i) {
91 const std::vector<NodeData>& exerciseData = simulationData[i];
92
93 parameters[i-1].resize(exercise.numberOfParameters()[i-1]);
94
95
96 // optimize
97 ValueEstimate f(exerciseData, exercise, i-1);
98
99 Array guess(parameters[i-1].size());
100 exercise.guess(i-1, parameters[i-1]);
101 std::copy(parameters[i-1].begin(), parameters[i-1].end(),
102 guess.begin());
103
104 NoConstraint c;
105
106 Problem p(f, c, guess);
107 method.minimize(p, endCriteria);
108
109 Array result = p.currentValue();
110 std::copy(result.begin(), result.end(),
111 parameters[i-1].begin());
112
113 std::vector<NodeData>& previousData = simulationData[i-1];
114 for (Size j=0; j<previousData.size(); ++j) {
115 if (exerciseData[j].isValid) {
116 if (exercise.exercise(i-1,
117 parameters[i-1],
118 exerciseData[j].values))
119 previousData[j].cumulatedCashFlows +=
120 exerciseData[j].exerciseValue;
121 else
122 previousData[j].cumulatedCashFlows +=
123 exerciseData[j].cumulatedCashFlows;
124 }
125 }
126 }
127
128 Real sum = 0.0;
129 const std::vector<NodeData>& initialData = simulationData.front();
130 for (const auto& i : initialData)
131 sum += i.cumulatedCashFlows;
132 return sum/initialData.size();
133 }
134
135}
136
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
const_iterator begin() const
Definition: array.hpp:503
Criteria to end optimization process:
Definition: endcriteria.hpp:40
No constraint.
Definition: constraint.hpp:79
Abstract class for constrained optimization method.
Definition: method.hpp:36
virtual EndCriteria::Type minimize(Problem &P, const EndCriteria &endCriteria)=0
minimize the optimization problem P
virtual void guess(Size exerciseNumber, std::vector< Real > &parameters) const =0
virtual std::vector< Size > numberOfParameters() const =0
virtual bool exercise(Size exerciseNumber, const std::vector< Real > &parameters, const std::vector< Real > &variables) const =0
Constrained optimization problem.
Definition: problem.hpp:42
const Array & currentValue() const
current value of the local minimum
Definition: problem.hpp:81
Abstract constraint class.
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
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
Real genericEarlyExerciseOptimization(std::vector< std::vector< NodeData > > &simulationData, const ParametricExercise &exercise, std::vector< std::vector< Real > > &parameters, const EndCriteria &endCriteria, OptimizationMethod &method)
returns the biased estimate obtained while optimizing
Size exerciseIndex_
std::vector< Real > parameters_
const ParametricExercise & exercise_
const std::vector< NodeData > & simulationData_
Abstract optimization problem class.