QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmarithmeticaveragecondition.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 Ralph Schreyer
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
24#include <ql/math/interpolations/cubicinterpolation.hpp>
25#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
26#include <ql/methods/finitedifferences/stepconditions/fdmarithmeticaveragecondition.hpp>
27#include <utility>
28
29namespace QuantLib {
30
32 std::vector<Time> averageTimes,
33 Real,
34 Size pastFixings,
35 const ext::shared_ptr<FdmMesher>& mesher,
36 Size equityDirection)
37 : x_(mesher->layout()->dim()[equityDirection]),
38 a_(mesher->layout()->dim()[equityDirection == 0 ? 1 : 0]),
39 averageTimes_(std::move(averageTimes)), pastFixings_(pastFixings), mesher_(mesher),
40 equityDirection_(equityDirection) {
41
42 QL_REQUIRE(mesher->layout()->dim().size()==2, "2D allowed only");
43 QL_REQUIRE(equityDirection == 0 || equityDirection == 1,
44 "equityDirection has to be 0 or 1");
45
46 const Size xSpacing = mesher_->layout()->spacing()[equityDirection];
47 Array tmp = mesher_->locations(equityDirection);
48 for (Size i = 0; i < x_.size(); ++i) {
49 x_[i] = std::exp(tmp[i*xSpacing]);
50 }
51 const Size averageDirection = equityDirection == 0 ? 1 : 0;
52 const Size aSpacing = mesher_->layout()->spacing()[averageDirection];
53 tmp = mesher_->locations(averageDirection);
54 for (Size i = 0; i < a_.size(); ++i) {
55 a_[i] = std::exp(tmp[i*aSpacing]);
56 }
57 }
58
60 QL_REQUIRE(mesher_->layout()->size() == a.size(),
61 "inconsistent array dimensions");
62
63 const std::vector<Time>::const_iterator iter
64 = std::find(averageTimes_.begin(), averageTimes_.end(), t);
65 const Size nTimes
66 = std::count(averageTimes_.begin(), averageTimes_.end(), t);
67 if (nTimes > 0) {
68 Array aCopy(a);
69 const Size iT = iter - averageTimes_.begin() + 1 + pastFixings_;
70 const Size averageDirection = equityDirection_ == 0 ? 1 : 0;
71 const Size xSpacing = mesher_->layout()->spacing()[equityDirection_];
72 const Size aSpacing = mesher_->layout()->spacing()[averageDirection];
73 Array tmp(a_.size());
74
75 for (Size i=0; i<x_.size(); ++i) {
76 for (Size j=0; j<a_.size(); ++j) {
77 Size index = i*xSpacing + j*aSpacing;
78 tmp[j] = aCopy[index];
79 }
81 tmp.begin());
82 for (Size j=0; j<a_.size(); ++j) {
83 Size index = i*xSpacing + j*aSpacing;
84 a[index] = interp((iT-nTimes)/(double)(iT)*a_[j] +
85 nTimes/(double)(iT)*x_[i], true);
86 }
87 }
88 }
89 }
90}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
FdmArithmeticAverageCondition(std::vector< Time > averageTimes, Real, Size pastFixings, const ext::shared_ptr< FdmMesher > &mesher, Size equityDirection)
void applyTo(Array &a, Time t) const override
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.