92 {
93
94 BOOST_TEST_MESSAGE("Testing CrossAssetModel parametrizations (base classes)...");
95
96
97
99
100 if (p1.parameterTimes(42) != Array()) {
101 BOOST_ERROR("empty parametrization should have empty times "
102 "array, it has size "
103 << p1.parameterTimes(42).size() << " though");
104 }
105
106 if (p1.parameterValues(42) != Array()) {
107 BOOST_ERROR("empty parametrization should have empty values "
108 "array, it has size "
109 << p1.parameterValues(42).size() << " though");
110 }
111
112 if (p1.parameter(42)->params().size() != 0) {
113 BOOST_ERROR("empty parametrization should have empty parameter "
114 "array, it has size "
115 << p1.parameter(42)->params().size() << " though");
116 }
117
118
119
120
121
122
123 Array noTimes;
125 helper11.p()->setParam(0, std::sqrt(3.0));
126 check(
"helper11.y", 0.0, helper11.y(0.0), 3.0);
127 check(
"helper11.y", 1.0, helper11.y(1.0), 3.0);
128 check(
"helper11.y", 3.0, helper11.y(3.0), 3.0);
129 check(
"helper11.int_y_sqr", 0.0, helper11.int_y_sqr(0.0), 0.0);
130 check(
"helper11.int_y_sqr", 1.0, helper11.int_y_sqr(1.0), 9.0);
131 check(
"helper11.int_y_sqr", 3.0, helper11.int_y_sqr(3.0), 27.0);
132
134 helper21.p()->setParam(0, 3.0);
135 check(
"helper21.y", 0.0, helper21.y(0.0), 3.0);
136 check(
"helper21.y", 1.0, helper21.y(1.0), 3.0);
137 check(
"helper21.y", 3.0, helper21.y(3.0), 3.0);
138 check(
"helper21.exp_m_int_y", 0.0, helper21.exp_m_int_y(0.0), 1.0);
139 check(
"helper21.exp_m_int_y", 1.0, helper21.exp_m_int_y(1.0), std::exp(-3.0));
140 check(
"helper21.exp_m_int_y", 3.0, helper21.exp_m_int_y(3.0), std::exp(-9.0));
141 check(
"helper21.int_exp_m_int_y", 0.0, helper21.int_exp_m_int_y(0.0), 0.0);
142 check(
"helper21.int_exp_m_int_y", 1.0, helper21.int_exp_m_int_y(1.0), (1.0 - std::exp(-3.0)) / 3.0);
143 check(
"helper21.int_exp_m_int_y", 3.0, helper21.int_exp_m_int_y(3.0), (1.0 - std::exp(-9.0)) / 3.0);
144
145
146
147
149 helper31.p1()->setParam(0, std::sqrt(3.0));
150 helper31.p2()->setParam(0, 2.0);
151
152 helper31.update();
153 check(
"helper31.y1", 0.0, helper31.y1(0.0), 3.0);
154 check(
"helper31.y1", 1.0, helper31.y1(1.0), 3.0);
155 check(
"helper31.y1", 3.0, helper31.y1(3.0), 3.0);
156 check(
"helper31.y2", 0.0, helper31.y2(0.0), 2.0);
157 check(
"helper31.y2", 1.0, helper31.y2(1.0), 2.0);
158 check(
"helper31.y2", 3.0, helper31.y2(3.0), 2.0);
159 check(
"helper31.int_y1_sqr_int_exp_2_int_y2", 0.0, helper31.int_y1_sqr_exp_2_int_y2(0.0), 0.0);
160 check(
"helper31.int_y1_sqr_int_exp_2_int_y2", 1.0, helper31.int_y1_sqr_exp_2_int_y2(1.0),
161 9.0 / 4.0 * (std::exp(2.0 * 2.0 * 1.0) - 1.0));
162 check(
"helper31.int_y1_sqr_int_exp_2_int_y2", 3.0, helper31.int_y1_sqr_exp_2_int_y2(3.0),
163 9.0 / 4.0 * (std::exp(2.0 * 2.0 * 3.0) - 1.0));
164
165
166 Array times1(2);
167 Array times2(3);
168 times1[0] = 0.1;
169 times1[1] = 0.5;
170 times2[0] = 0.2;
171 times2[1] = 0.5;
172 times2[2] = 1.0;
174 helper32.p1()->setParam(0, 0.0);
175 helper32.p1()->setParam(1, 0.0);
176 helper32.p1()->setParam(2, 0.0);
177 helper32.p2()->setParam(0, 0.0);
178 helper32.p2()->setParam(1, 0.0);
179 helper32.p2()->setParam(2, 0.0);
180 helper32.p2()->setParam(3, 0.0);
181 helper32.update();
182 Array exTu(4);
183 exTu[0] = 0.1;
184 exTu[1] = 0.2;
185 exTu[2] = 0.5;
186 exTu[3] = 1.0;
187 if (helper32.tUnion() != exTu)
188 BOOST_ERROR("helper32 expected tUnion array " << exTu << ", but is " << helper32.tUnion());
189
191 helper22.p()->setParam(0, 0.0);
192 check(
"helper22.y", 0.0, helper22.y(0.0), 0.0);
193 check(
"helper22.y", 1.0, helper22.y(1.0), 0.0);
194 check(
"helper22.y", 3.0, helper22.y(3.0), 0.0);
195 check(
"helper22.exp_m_int_y", 0.0, helper22.exp_m_int_y(0.0), 1.0);
196 check(
"helper22.exp_m_int_y", 1.0, helper22.exp_m_int_y(1.0), 1.0);
197 check(
"helper22.exp_m_int_y", 3.0, helper22.exp_m_int_y(3.0), 1.0);
198 check(
"helper22.int_exp_m_int_y", 0.0, helper22.int_exp_m_int_y(0.0), 0.0);
199 check(
"helper22.int_exp_m_int_y", 1.0, helper22.int_exp_m_int_y(1.0), 1.0);
200 check(
"helper22.int_exp_m_int_y", 3.0, helper22.int_exp_m_int_y(3.0), 3.0);
201
202 Array times(3), values(4), sqrt_values(4);
203 times[0] = 1.0;
204 times[1] = 2.0;
205 times[2] = 3.0;
206 values[0] = 1.0;
207 values[1] = 2.0;
208 values[2] = 0.0;
209 values[3] = 3.0;
210 sqrt_values[0] = std::sqrt(1.0);
211 sqrt_values[1] = std::sqrt(2.0);
212 sqrt_values[2] = std::sqrt(0.0);
213 sqrt_values[3] = std::sqrt(3.0);
215 helper12.p()->setParam(0, sqrt_values[0]);
216 helper12.p()->setParam(1, sqrt_values[1]);
217 helper12.p()->setParam(2, sqrt_values[2]);
218 helper12.p()->setParam(3, sqrt_values[3]);
219 helper12.update();
220 check(
"helper12.y", 0.0, helper12.y(0.0), 1.0);
221 check(
"helper12.y", 0.5, helper12.y(0.5), 1.0);
222 check(
"helper12.y", 1.0, helper12.y(1.0), 2.0);
223 check(
"helper12.y", 2.2, helper12.y(2.2), 0.0);
224 check(
"helper12.y", 3.0 - 1.0E-8, helper12.y(3.0 - 1.0E-8), 0.0);
225 check(
"helper12.y", 3.0, helper12.y(3.0), 3.0);
226 check(
"helper12.y", 5.0, helper12.y(5.0), 3.0);
227 check(
"helper12.int_y_sqr", 0.0, helper12.int_y_sqr(0.0), 0.0);
228 check(
"helper12.int_y_sqr", 0.5, helper12.int_y_sqr(0.5), 0.5);
229 check(
"helper12.int_y_sqr", 1.0, helper12.int_y_sqr(1.0), 1.0);
230 check(
"helper12.int_y_sqr", 1.2, helper12.int_y_sqr(1.2), 1.0 + 4.0 * 0.2);
231 check(
"helper12.int_y_sqr", 2.0, helper12.int_y_sqr(2.0), 1.0 + 4.0);
232 check(
"helper12.int_y_sqr", 2.1, helper12.int_y_sqr(2.1), 1.0 + 4.0);
233 check(
"helper12.int_y_sqr", 2.5, helper12.int_y_sqr(2.5), 1.0 + 4.0);
234 check(
"helper12.int_y_sqr", 2.9, helper12.int_y_sqr(2.9), 1.0 + 4.0);
235 check(
"helper12.int_y_sqr", 3.0, helper12.int_y_sqr(3.0), 1.0 + 4.0);
236 check(
"helper12.int_y_sqr", 5.0, helper12.int_y_sqr(5.0), 1.0 + 4.0 + 9.0 * 2.0);
237
239 helper23.p()->setParam(0, values[0]);
240 helper23.p()->setParam(1, values[1]);
241 helper23.p()->setParam(2, values[2]);
242 helper23.p()->setParam(3, values[3]);
243 helper23.update();
244 check(
"helper23.y", 0.0, helper23.y(0.0), 1.0);
245 check(
"helper23.y", 0.5, helper23.y(0.5), 1.0);
246 check(
"helper23.y", 1.0, helper23.y(1.0), 2.0);
247 check(
"helper23.y", 2.2, helper23.y(2.2), 0.0);
248 check(
"helper23.y", 3.0 - 1.0E-8, helper23.y(3.0 - 1.0E-8), 0.0);
249 check(
"helper23.y", 3.0, helper23.y(3.0), 3.0);
250 check(
"helper23.y", 5.0, helper23.y(5.0), 3.0);
251 check(
"helper23.exp_m_int_y", 0.0, helper23.exp_m_int_y(0.0), 1.0);
252 check(
"helper23.exp_m_int_y", 0.5, helper23.exp_m_int_y(0.5), std::exp(-0.5));
253 check(
"helper23.exp_m_int_y", 1.0, helper23.exp_m_int_y(1.0), std::exp(-1.0));
254 check(
"helper23.exp_m_int_y", 1.5, helper23.exp_m_int_y(1.5), std::exp(-2.0));
255 check(
"helper23.exp_m_int_y", 2.0, helper23.exp_m_int_y(2.0), std::exp(-3.0));
256 check(
"helper23.exp_m_int_y", 2.1, helper23.exp_m_int_y(2.1), std::exp(-3.0));
257 check(
"helper23.exp_m_int_y", 2.5, helper23.exp_m_int_y(2.5), std::exp(-3.0));
258 check(
"helper23.exp_m_int_y", 2.9, helper23.exp_m_int_y(2.9), std::exp(-3.0));
259 check(
"helper23.exp_m_int_y", 3.0, helper23.exp_m_int_y(3.0), std::exp(-3.0));
260 check(
"helper23.exp_m_int_y", 5.0, helper23.exp_m_int_y(5.0), std::exp(-3.0 - 6.0));
261
262 check(
"helper23.int_exp_m_int_y", 0.0, helper23.int_exp_m_int_y(0.0), 0.0);
263 Real h = 1.0E-5, x0 = 0.0,
sum = 0.0;
264 while (x0 < 5.0) {
265 sum += h * helper23.exp_m_int_y(x0 + h / 2.0);
266 x0 += h;
267 check2(
"helper23.int_exp_m_int_y2", x0, helper23.int_exp_m_int_y(x0),
sum, 1.0E-10);
268 }
269
270
271
272 helper12.p()->setParam(0, std::sqrt(0.5));
273 helper12.p()->setParam(1, std::sqrt(1.0));
274 helper23.p()->setParam(0, 0.5);
275 helper23.p()->setParam(1, 1.0);
276 helper12.update();
277 helper23.update();
278 check(
"update helper12.y", 1.0, helper12.y(1.0), 1.0);
279 check(
"update helper12.int_y_sqr", 2.0, helper12.int_y_sqr(2.0), 0.5 * 0.5 + 1.0 * 1.0);
280 check(
"update helper23.y", 1.0, helper23.y(1.0), 1.0);
281 check(
"update helper23.exp_m_int_y", 2.0, helper23.exp_m_int_y(2.0), std::exp(-0.5 - 1.0));
282
283
284
285 Handle<YieldTermStructure> yts(QuantLib::ext::make_shared<FlatForward>(0, NullCalendar(), 0.0, Actual365Fixed()));
286 std::vector<Date> dates;
287 dates.push_back(yts->referenceDate() + 100);
288 dates.push_back(yts->referenceDate() + 200);
289 dates.push_back(yts->referenceDate() + 250);
290 dates.push_back(yts->referenceDate() + 2385);
292
293 check(
"time from date helper1x", 0.0, helper1x.t()[0], yts->timeFromReference(dates[0]));
294 check(
"time from date helper1x", 0.0, helper1x.t()[1], yts->timeFromReference(dates[1]));
295 check(
"time from date helper1x", 0.0, helper1x.t()[2], yts->timeFromReference(dates[2]));
296 check(
"time from date helper1x", 0.0, helper1x.t()[3], yts->timeFromReference(dates[3]));
297}
Piecewise Constant Helper 1.
Piecewise Constant Helper2.
Piecewise Constant Helper 3.
Real sum(const Cash &c, const Cash &d)