QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
brownianbridge.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003 Ferdinando Ametrano
5 Copyright (C) 2006 StatPro Italia srl
6 Copyright (C) 2009 Bojan Nikolic
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
26// ===========================================================================
27// NOTE: The following copyright notice applies to the original code,
28//
29// Copyright (C) 2002 Peter J�ckel "Monte Carlo Methods in Finance".
30// All rights reserved.
31//
32// Permission to use, copy, modify, and distribute this software is freely
33// granted, provided that this notice is preserved.
34// ===========================================================================
35
36#ifndef quantlib_brownian_bridge_hpp
37#define quantlib_brownian_bridge_hpp
38
39#include <ql/methods/montecarlo/path.hpp>
40#include <ql/methods/montecarlo/sample.hpp>
41
42namespace QuantLib {
43
45
54 public:
60 BrownianBridge(Size steps);
70 BrownianBridge(const std::vector<Time>& times);
76 BrownianBridge(const TimeGrid& timeGrid);
78
79 Size size() const { return size_; }
80 const std::vector<Time>& times() const { return t_; }
81 const std::vector<Size>& bridgeIndex() const { return bridgeIndex_; }
82 const std::vector<Size>& leftIndex() const { return leftIndex_; }
83 const std::vector<Size>& rightIndex() const { return rightIndex_; }
84 const std::vector<Real>& leftWeight() const { return leftWeight_; }
85 const std::vector<Real>& rightWeight() const { return rightWeight_; }
86 const std::vector<Real>& stdDeviation() const { return stdDev_; }
88
90
105 template <class RandomAccessIterator1,
106 class RandomAccessIterator2>
107 void transform(RandomAccessIterator1 begin,
108 RandomAccessIterator1 end,
109 RandomAccessIterator2 output) const {
110 QL_REQUIRE(end >= begin, "invalid sequence");
111 QL_REQUIRE(Size(end-begin) == size_,
112 "incompatible sequence size");
113 // We use output to store the path...
114 output[size_-1] = stdDev_[0] * begin[0];
115 for (Size i=1; i<size_; ++i) {
116 Size j = leftIndex_[i];
117 Size k = rightIndex_[i];
118 Size l = bridgeIndex_[i];
119 if (j != 0) {
120 output[l] =
121 leftWeight_[i] * output[j-1] +
122 rightWeight_[i] * output[k] +
123 stdDev_[i] * begin[i];
124 } else {
125 output[l] =
126 rightWeight_[i] * output[k] +
127 stdDev_[i] * begin[i];
128 }
129 }
130 // ...after which, we calculate the variations and
131 // normalize to unit times
132 for (Size i=size_-1; i>=1; --i) {
133 output[i] -= output[i-1];
134 output[i] /= sqrtdt_[i];
135 }
136 output[0] /= sqrtdt_[0];
137 }
138 private:
139 void initialize();
141 std::vector<Time> t_;
142 std::vector<Real> sqrtdt_;
144 std::vector<Real> leftWeight_, rightWeight_, stdDev_;
145 };
146
147}
148
149#endif
Builds Wiener process paths using Gaussian variates.
const std::vector< Size > & rightIndex() const
std::vector< Size > leftIndex_
const std::vector< Real > & leftWeight() const
const std::vector< Size > & bridgeIndex() const
const std::vector< Size > & leftIndex() const
std::vector< Real > sqrtdt_
void transform(RandomAccessIterator1 begin, RandomAccessIterator1 end, RandomAccessIterator2 output) const
Brownian-bridge generator function.
std::vector< Size > rightIndex_
std::vector< Time > t_
const std::vector< Real > & stdDeviation() const
std::vector< Real > stdDev_
std::vector< Real > rightWeight_
const std::vector< Time > & times() const
std::vector< Size > bridgeIndex_
std::vector< Real > leftWeight_
const std::vector< Real > & rightWeight() const
time grid class
Definition: timegrid.hpp:43
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35