forked from lballabio/QuantLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhimalayaoption.cpp
134 lines (106 loc) · 5.28 KB
/
himalayaoption.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2008 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/experimental/exoticoptions/himalayaoption.hpp>
#include <ql/experimental/exoticoptions/mchimalayaengine.hpp>
#include <ql/math/randomnumbers/rngtraits.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/quotes/simplequote.hpp>
using namespace QuantLib;
using namespace boost::unit_test_framework;
BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(HimalayaOptionTests)
BOOST_AUTO_TEST_CASE(testCached) {
BOOST_TEST_MESSAGE("Testing Himalaya option against cached values...");
Date today = Settings::instance().evaluationDate();
DayCounter dc = Actual360();
std::vector<Date> fixingDates;
for (Size i=0; i<5; ++i)
fixingDates.push_back(today+i*90);
Real strike = 101.0;
HimalayaOption option(fixingDates, strike);
Handle<YieldTermStructure> riskFreeRate(flatRate(today, 0.05, dc));
std::vector<ext::shared_ptr<StochasticProcess1D> > processes(4);
processes[0] = ext::shared_ptr<StochasticProcess1D>(
new BlackScholesMertonProcess(
Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(100.0))),
Handle<YieldTermStructure>(flatRate(today, 0.01, dc)),
riskFreeRate,
Handle<BlackVolTermStructure>(flatVol(today, 0.30, dc))));
processes[1] = ext::shared_ptr<StochasticProcess1D>(
new BlackScholesMertonProcess(
Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(110.0))),
Handle<YieldTermStructure>(flatRate(today, 0.05, dc)),
riskFreeRate,
Handle<BlackVolTermStructure>(flatVol(today, 0.35, dc))));
processes[2] = ext::shared_ptr<StochasticProcess1D>(
new BlackScholesMertonProcess(
Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(90.0))),
Handle<YieldTermStructure>(flatRate(today, 0.04, dc)),
riskFreeRate,
Handle<BlackVolTermStructure>(flatVol(today, 0.25, dc))));
processes[3] = ext::shared_ptr<StochasticProcess1D>(
new BlackScholesMertonProcess(
Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(105.0))),
Handle<YieldTermStructure>(flatRate(today, 0.03, dc)),
riskFreeRate,
Handle<BlackVolTermStructure>(flatVol(today, 0.20, dc))));
Matrix correlation(4,4);
correlation[0][0] = 1.00;
correlation[0][1] = 0.50;
correlation[0][2] = 0.30;
correlation[0][3] = 0.10;
correlation[1][0] = 0.50;
correlation[1][1] = 1.00;
correlation[1][2] = 0.20;
correlation[1][3] = 0.40;
correlation[2][0] = 0.30;
correlation[2][1] = 0.20;
correlation[2][2] = 1.00;
correlation[2][3] = 0.60;
correlation[3][0] = 0.10;
correlation[3][1] = 0.40;
correlation[3][2] = 0.60;
correlation[3][3] = 1.00;
BigNatural seed = 86421;
Size fixedSamples = 1023;
ext::shared_ptr<StochasticProcessArray> process(
new StochasticProcessArray(processes, correlation));
option.setPricingEngine(MakeMCHimalayaEngine<PseudoRandom>(process)
.withSamples(fixedSamples)
.withSeed(seed));
Real value = option.NPV();
Real storedValue = 5.93632056;
Real tolerance = 1.0e-8;
if (std::fabs(value-storedValue) > tolerance)
BOOST_FAIL(std::setprecision(10)
<< " calculated value: " << value << "\n"
<< " expected: " << storedValue);
Real minimumTol = 1.0e-2;
tolerance = option.errorEstimate();
tolerance = std::min<Real>(tolerance/2.0, minimumTol*value);
option.setPricingEngine(MakeMCHimalayaEngine<PseudoRandom>(process)
.withAbsoluteTolerance(tolerance)
.withSeed(seed));
option.NPV();
Real accuracy = option.errorEstimate();
if (accuracy > tolerance)
BOOST_FAIL(std::setprecision(10)
<< " reached accuracy: " << accuracy << "\n"
<< " expected: " << tolerance);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()