QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
montecarlomodel.hpp
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) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2007 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/*! \file montecarlomodel.hpp
22 \brief General-purpose Monte Carlo model
23*/
24
25#ifndef quantlib_montecarlo_model_hpp
26#define quantlib_montecarlo_model_hpp
27
30#include <ql/shared_ptr.hpp>
31#include <utility>
32
33namespace QuantLib {
34
35 //! General-purpose Monte Carlo model for path samples
36 /*! The template arguments of this class correspond to available
37 policies for the particular model to be instantiated---i.e.,
38 whether it is single- or multi-asset, or whether it should use
39 pseudo-random or low-discrepancy numbers for path
40 generation. Such decisions are grouped in trait classes so as
41 to be orthogonal---see mctraits.hpp for examples.
42
43 The constructor accepts two safe references, i.e. two smart
44 pointers, one to a path generator and the other to a path
45 pricer. In case of control variate technique the user should
46 provide the additional control option, namely the option path
47 pricer and the option value.
48
49 \ingroup mcarlo
50 */
51 template <template <class> class MC, class RNG, class S = Statistics>
53 public:
54 typedef MC<RNG> mc_traits;
55 typedef RNG rng_traits;
56 typedef typename MC<RNG>::path_generator_type path_generator_type;
57 typedef typename MC<RNG>::path_pricer_type path_pricer_type;
58 typedef typename path_generator_type::sample_type sample_type;
59 typedef typename path_pricer_type::result_type result_type;
60 typedef S stats_type;
61 // constructor
63 ext::shared_ptr<path_generator_type> pathGenerator,
64 ext::shared_ptr<path_pricer_type> pathPricer,
66 bool antitheticVariate,
67 ext::shared_ptr<path_pricer_type> cvPathPricer = ext::shared_ptr<path_pricer_type>(),
68 result_type cvOptionValue = result_type(),
69 ext::shared_ptr<path_generator_type> cvPathGenerator =
70 ext::shared_ptr<path_generator_type>())
71 : pathGenerator_(std::move(pathGenerator)), pathPricer_(std::move(pathPricer)),
73 cvPathPricer_(std::move(cvPathPricer)), cvOptionValue_(cvOptionValue),
74 cvPathGenerator_(std::move(cvPathGenerator)) {
75 isControlVariate_ = static_cast<bool>(cvPathPricer_);
76 }
77 void addSamples(Size samples);
78 const stats_type& sampleAccumulator() const;
79 private:
80 ext::shared_ptr<path_generator_type> pathGenerator_;
81 ext::shared_ptr<path_pricer_type> pathPricer_;
84 ext::shared_ptr<path_pricer_type> cvPathPricer_;
87 ext::shared_ptr<path_generator_type> cvPathGenerator_;
88 };
89
90 // inline definitions
91 template <template <class> class MC, class RNG, class S>
93 for(Size j = 1; j <= samples; j++) {
94
95 const sample_type& path = pathGenerator_->next();
96 result_type price = (*pathPricer_)(path.value);
97
98 if (isControlVariate_) {
99 if (!cvPathGenerator_) {
100 price += cvOptionValue_-(*cvPathPricer_)(path.value);
101 }
102 else {
103 const sample_type& cvPath = cvPathGenerator_->next();
104 price += cvOptionValue_-(*cvPathPricer_)(cvPath.value);
105 }
106 }
107
108 if (isAntitheticVariate_) {
109 const sample_type& atPath = pathGenerator_->antithetic();
110 result_type price2 = (*pathPricer_)(atPath.value);
111 if (isControlVariate_) {
112 if (!cvPathGenerator_)
113 price2 += cvOptionValue_-(*cvPathPricer_)(atPath.value);
114 else {
115 const sample_type& cvPath = cvPathGenerator_->antithetic();
116 price2 += cvOptionValue_-(*cvPathPricer_)(cvPath.value);
117 }
118 }
119
120 sampleAccumulator_.add((price+price2)/2.0, path.weight);
121 } else {
122 sampleAccumulator_.add(price, path.weight);
123 }
124 }
125 }
126
127 template <template <class> class MC, class RNG, class S>
128 inline const typename MonteCarloModel<MC,RNG,S>::stats_type&
130 return sampleAccumulator_;
131 }
132
133}
134
135
136#endif
General-purpose Monte Carlo model for path samples.
ext::shared_ptr< path_generator_type > pathGenerator_
path_generator_type::sample_type sample_type
MC< RNG >::path_pricer_type path_pricer_type
const stats_type & sampleAccumulator() const
ext::shared_ptr< path_pricer_type > pathPricer_
void addSamples(Size samples)
MonteCarloModel(ext::shared_ptr< path_generator_type > pathGenerator, ext::shared_ptr< path_pricer_type > pathPricer, stats_type sampleAccumulator, bool antitheticVariate, ext::shared_ptr< path_pricer_type > cvPathPricer=ext::shared_ptr< path_pricer_type >(), result_type cvOptionValue=result_type(), ext::shared_ptr< path_generator_type > cvPathGenerator=ext::shared_ptr< path_generator_type >())
MC< RNG >::path_generator_type path_generator_type
path_pricer_type::result_type result_type
ext::shared_ptr< path_generator_type > cvPathGenerator_
ext::shared_ptr< path_pricer_type > cvPathPricer_
std::size_t Size
size of a container
Definition: types.hpp:58
Monte Carlo policies.
Definition: any.hpp:35
RiskStatistics Statistics
default statistics tool
Definition: statistics.hpp:35
STL namespace.
Maps shared_ptr to either the boost or std implementation.
statistics tool with risk measures