1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2002, 2003 Sadruddin Rejeb
5 Copyright (C) 2004 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#include <ql/pricingengines/vanilla/discretizedvanillaoption.hpp>
22#include <vector>
23
24namespace QuantLib {
25
26 DiscretizedVanillaOption::DiscretizedVanillaOption(
27 const VanillaOption::arguments& args,
28 const StochasticProcess& process,
29 const TimeGrid& grid)
30 : arguments_(args) {
31 stoppingTimes_.resize(new_size: args.exercise->dates().size());
32 for (Size i=0; i<stoppingTimes_.size(); ++i) {
33 stoppingTimes_[i] =
34 process.time(args.exercise->date(index: i));
35 if (!grid.empty()) {
36 // adjust to the given grid
37 stoppingTimes_[i] = grid.closestTime(t: stoppingTimes_[i]);
38 }
39 }
40 }
41
42 void DiscretizedVanillaOption::reset(Size size) {
43 values_ = Array(size, 0.0);
44 adjustValues();
45 }
46
47 void DiscretizedVanillaOption::postAdjustValuesImpl() {
48
49 Time now = time();
50 switch (arguments_.exercise->type()) {
51 case Exercise::American:
52 if (now <= stoppingTimes_[1] &&
53 now >= stoppingTimes_[0])
54 applySpecificCondition();
55 break;
56 case Exercise::European:
57 if (isOnTime(t: stoppingTimes_[0]))
58 applySpecificCondition();
59 break;
60 case Exercise::Bermudan:
61 for (Real stoppingTime : stoppingTimes_) {
62 if (isOnTime(t: stoppingTime))
63 applySpecificCondition();
64 }
65 break;
66 default:
67 QL_FAIL("invalid option type");
68 }
69 }
70
71 void DiscretizedVanillaOption::applySpecificCondition() {
72 Array grid = method()->grid(time());
73 for (Size j=0; j<values_.size(); j++) {
74 values_[j] = std::max(a: values_[j],
75 b: (*arguments_.payoff)(grid[j]));
76 }
77 }
78
79}
80
81

source code of quantlib/ql/pricingengines/vanilla/discretizedvanillaoption.cpp