QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
sampledcurve.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005 Joseph Wang
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
21
22namespace QuantLib {
23
25 QL_REQUIRE(!empty(), "empty sampled curve");
26 Size jmid = size()/2;
27 if (size() % 2 == 1)
28 return values_[jmid];
29 else
30 return (values_[jmid]+values_[jmid-1])/2.0;
31 }
32
34 QL_REQUIRE(size()>=3,
35 "the size of the curve must be at least 3");
36 Size jmid = size()/2;
37 if (size() % 2 == 1) {
38 return (values_[jmid+1]-values_[jmid-1])/
39 (grid_[jmid+1]-grid_[jmid-1]);
40 } else {
41 return (values_[jmid]-values_[jmid-1])/
42 (grid_[jmid]-grid_[jmid-1]);
43 }
44 }
45
47 QL_REQUIRE(size()>=4,
48 "the size of the curve must be at least 4");
49 Size jmid = size()/2;
50 if (size() % 2 == 1) {
51 Real deltaPlus = (values_[jmid+1]-values_[jmid])/
52 (grid_[jmid+1]-grid_[jmid]);
53 Real deltaMinus = (values_[jmid]-values_[jmid-1])/
54 (grid_[jmid]-grid_[jmid-1]);
55 Real dS = (grid_[jmid+1]-grid_[jmid-1])/2.0;
56 return (deltaPlus-deltaMinus)/dS;
57 } else {
58 Real deltaPlus = (values_[jmid+1]-values_[jmid-1])/
59 (grid_[jmid+1]-grid_[jmid-1]);
60 Real deltaMinus = (values_[jmid]-values_[jmid-2])/
61 (grid_[jmid]-grid_[jmid-2]);
62 return (deltaPlus-deltaMinus)/
63 (grid_[jmid]-grid_[jmid-1]);
64 }
65 }
66
67 void SampledCurve::regrid(const Array &new_grid) {
68 CubicInterpolation priceSpline(grid_.begin(), grid_.end(),
69 values_.begin(),
73 priceSpline.update();
74 Array newValues(new_grid.size());
77 for (val = newValues.begin(), grid = new_grid.begin() ;
78 grid != new_grid.end();
79 ++val, ++grid) {
80 *val = priceSpline(*grid, true);
81 }
82 values_.swap(newValues);
83 grid_ = new_grid;
84 }
85
86}
87
1-D array used in linear algebra.
Definition: array.hpp:52
const Real * const_iterator
Definition: array.hpp:124
Real * iterator
Definition: array.hpp:123
void swap(Array &) noexcept
Definition: array.hpp:546
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
Cubic interpolation between discrete points.
@ SecondDerivative
Match value of second derivative at end.
Real firstDerivativeAtCenter() const
const Array & grid() const
Real valueAtCenter() const
void regrid(const Array &new_grid)
Real secondDerivativeAtCenter() const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
a class that contains a sampled curve