Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions | Variables
commodityschwartzmodel.cpp File Reference
#include "utilities.hpp"
#include "toplevelfixture.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/make_shared.hpp>
#include <boost/test/data/test_case.hpp>
#include <ql/currencies/america.hpp>
#include <ql/math/interpolations/linearinterpolation.hpp>
#include <ql/math/optimization/levenbergmarquardt.hpp>
#include <ql/math/randomnumbers/rngtraits.hpp>
#include <ql/math/statistics/incrementalstatistics.hpp>
#include <ql/methods/montecarlo/multipathgenerator.hpp>
#include <ql/methods/montecarlo/pathgenerator.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <qle/methods/multipathgeneratorbase.hpp>
#include <qle/models/commodityschwartzmodel.hpp>
#include <qle/models/commodityschwartzparametrization.hpp>
#include <qle/processes/commodityschwartzstateprocess.hpp>
#include <qle/termstructures/pricecurve.hpp>
#include <fstream>
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/covariance.hpp>
#include <boost/accumulators/statistics/density.hpp>
#include <boost/accumulators/statistics/error_of_mean.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/variates/covariate.hpp>

Go to the source code of this file.

Functions

 BOOST_DATA_TEST_CASE (testMartingaleProperty, bdata::make(driftFreeState) *bdata::make(steps), driftFreeState, steps)
 

Variables

std::vector< booldriftFreeState { true, false }
 
std::vector< Size > steps { 1, 52 }
 

Function Documentation

◆ BOOST_DATA_TEST_CASE()

BOOST_DATA_TEST_CASE ( testMartingaleProperty  ,
bdata::make(driftFreeState) *bdata::make(steps ,
driftFreeState  ,
steps   
)

Definition at line 111 of file commodityschwartzmodel.cpp.

113 {
114
115 //BOOST_AUTO_TEST_CASE(testMartingaleProperty) {
116
117 BOOST_TEST_MESSAGE("Testing martingale property in the COM Schwartz model ...");
118
119 CommoditySchwartzModelTestData data(driftFreeState);
120 QuantLib::ext::shared_ptr<StochasticProcess> process = data.model->stateProcess();
121 QL_REQUIRE(process != NULL, "process has null pointer!");
122
123 Size n = 100000; // number of paths
124 Size seed = 42; // rng seed
125 Time t = 10.0; // simulation horizon
126 Time T = 20.0; // forward price maturity
127 //Size steps = 1;
128
129 TimeGrid grid(t, steps);
130 LowDiscrepancy::rsg_type sg = LowDiscrepancy::make_sequence_generator(steps, seed);
131 MultiPathGenerator<LowDiscrepancy::rsg_type> pg(process, grid, sg, false);
132 //MultiPathGeneratorMersenneTwister pg(process, grid, seed, true);
133
134 accumulator_set<double, stats<tag::mean, tag::variance, tag::error_of<tag::mean>>> acc_price, acc_state;
135
136 Array state(1);
137 for (Size j = 0; j < n; ++j) {
138 Sample<MultiPath> path = pg.next();
139 Size l = path.value[0].length() - 1;
140 state[0] = path.value[0][l];
141 Real price = data.model->forwardPrice(t, T, state);
142 acc_price(price);
143 acc_state(state[0]);
144 }
145
146 BOOST_TEST_MESSAGE("sigma = " << data.model->parametrization()->sigmaParameter());
147 BOOST_TEST_MESSAGE("kappa = " << data.model->parametrization()->kappaParameter());
148 BOOST_TEST_MESSAGE("samples = " << n);
149 BOOST_TEST_MESSAGE("steps = " << steps);
150 BOOST_TEST_MESSAGE("t = " << t);
151 BOOST_TEST_MESSAGE("T = " << T);
152
153 // Martingale test for F(t,T)
154 {
155 Real found = mean(acc_price);
156 Real expected = data.parametrization->priceCurve()->price(T);
157 Real error = error_of<tag::mean>(acc_price);
158
159 BOOST_TEST_MESSAGE("Check that E[F(t,T)] = F(0,T)");
160 BOOST_TEST_MESSAGE("Avg = " << found
161 << " +- " << error
162 << " vs expected " << expected);
163 BOOST_TEST(fabs(found - expected) < error, "Martingale test failed for F(t,T) evolution, found " << found << ", expected " << expected);
164 }
165
166 // Martingale test for the state variable
167 {
168 Real found = mean(acc_state);
169 Real expected = 0;
170 Real error = error_of<tag::mean>(acc_state);
171
172 BOOST_TEST_MESSAGE("Check that the mean of the state variable is zero");
173 BOOST_TEST_MESSAGE("Avg = " << found
174 << " +- " << error_of<tag::mean>(acc_state)
175 << " vs expected " << expected);
176 BOOST_TEST(fabs(found - expected) < error, "Martingale test failed for the state variable, found " << found << ", expected " << expected);
177 }
178
179 // Variance test for the state variable, implicit in the first test above
180 {
181 Real found = variance(acc_state);
182 Real expected = data.parametrization->variance(t);
183 // FIXME: What's the MC error here? I assume that the mean error is smaller than the error of variance estimate, so re-using it.
184 Real error = error_of<tag::mean>(acc_state);
185
186 QuantLib::ext::shared_ptr<CommoditySchwartzStateProcess> stateProcess = QuantLib::ext::dynamic_pointer_cast<CommoditySchwartzStateProcess>(process);
187 QL_REQUIRE(stateProcess, "state process is null");
188 Real expected2 = stateProcess->variance(0.0, 0.0, t);
189
190 // consistency check, should be identical
191 BOOST_TEST(fabs(expected - expected2) < 1e-10, "Inconsistent state variable variance " << expected << " vs " << expected2);
192
193 BOOST_TEST_MESSAGE("Check that the variance of the state variable matches expectation");
194 BOOST_TEST_MESSAGE("Var = " << found
195 << " +- " << error
196 << " vs expected " << expected);
197 BOOST_TEST(fabs(found - expected2) < error,
198 "Simulated variance of the state variable does match expectation, found " << found << ", expected " << expected);
199
200 }
201
202} // testMartingaleProperty
RandomVariable variance(const RandomVariable &r)
std::vector< bool > driftFreeState
std::vector< Size > steps
+ Here is the call graph for this function:

Variable Documentation

◆ driftFreeState

std::vector<bool> driftFreeState { true, false }

Definition at line 108 of file commodityschwartzmodel.cpp.

◆ steps

std::vector<Size> steps { 1, 52 }

Definition at line 109 of file commodityschwartzmodel.cpp.