| 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 | |
| 20 | #include <ql/math/sampledcurve.hpp> |
| 21 | |
| 22 | namespace QuantLib { |
| 23 | |
| 24 | Real SampledCurve::valueAtCenter() const { |
| 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 | |
| 33 | Real SampledCurve::firstDerivativeAtCenter() const { |
| 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 | |
| 46 | Real SampledCurve::secondDerivativeAtCenter() const { |
| 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(), |
| 70 | CubicInterpolation::Spline, false, |
| 71 | CubicInterpolation::SecondDerivative, 0.0, |
| 72 | CubicInterpolation::SecondDerivative, 0.0); |
| 73 | priceSpline.update(); |
| 74 | Array newValues(new_grid.size()); |
| 75 | Array::iterator val; |
| 76 | Array::const_iterator grid; |
| 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(from&: newValues); |
| 83 | grid_ = new_grid; |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
| 88 | |