| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 J. Erik Radmall |
| 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/experimental/commodities/commoditycurve.hpp> |
| 21 | #include <ql/experimental/commodities/commoditypricinghelpers.hpp> |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | CommodityCurve::CommodityCurve(std::string name, |
| 27 | CommodityType commodityType, |
| 28 | Currency currency, |
| 29 | UnitOfMeasure unitOfMeasure, |
| 30 | const Calendar& calendar, |
| 31 | const std::vector<Date>& dates, |
| 32 | std::vector<Real> prices, |
| 33 | const DayCounter& dayCounter) |
| 34 | : TermStructure(dates[0], calendar, dayCounter), name_(std::move(name)), |
| 35 | commodityType_(std::move(commodityType)), unitOfMeasure_(std::move(unitOfMeasure)), |
| 36 | currency_(std::move(currency)), dates_(dates), data_(std::move(prices)), |
| 37 | interpolator_(ForwardFlat()), basisOfCurveUomConversionFactor_(1) { |
| 38 | |
| 39 | QL_REQUIRE(dates_.size()>1, "too few dates" ); |
| 40 | QL_REQUIRE(data_.size()==dates_.size(), "dates/prices count mismatch" ); |
| 41 | |
| 42 | times_.resize(new_size: dates_.size()); |
| 43 | times_[0]=0.0; |
| 44 | for (Size i = 1; i < dates_.size(); i++) { |
| 45 | QL_REQUIRE(dates_[i] > dates_[i-1], |
| 46 | "invalid date (" << dates_[i] << ", vs " |
| 47 | << dates_[i-1] << ")" ); |
| 48 | times_[i] = dayCounter.yearFraction(d1: dates_[0], d2: dates_[i]); |
| 49 | } |
| 50 | |
| 51 | interpolation_ = |
| 52 | interpolator_.interpolate(xBegin: times_.begin(), xEnd: times_.end(), |
| 53 | yBegin: data_.begin()); |
| 54 | interpolation_.update(); |
| 55 | } |
| 56 | |
| 57 | CommodityCurve::CommodityCurve(std::string name, |
| 58 | CommodityType commodityType, |
| 59 | Currency currency, |
| 60 | UnitOfMeasure unitOfMeasure, |
| 61 | const Calendar& calendar, |
| 62 | const DayCounter& dayCounter) |
| 63 | : TermStructure(0, calendar, dayCounter), name_(std::move(name)), |
| 64 | commodityType_(std::move(commodityType)), unitOfMeasure_(std::move(unitOfMeasure)), |
| 65 | currency_(std::move(currency)), interpolator_(ForwardFlat()), |
| 66 | basisOfCurveUomConversionFactor_(1) {} |
| 67 | |
| 68 | void CommodityCurve::setPrices(std::map<Date, Real>& prices) { |
| 69 | QL_REQUIRE(prices.size()>1, "too few prices" ); |
| 70 | |
| 71 | dates_.clear(); |
| 72 | data_.clear(); |
| 73 | for (std::map<Date, Real>::const_iterator i = prices.begin(); i != prices.end(); ++i) { |
| 74 | dates_.push_back(x: i->first); |
| 75 | data_.push_back(x: i->second); |
| 76 | } |
| 77 | |
| 78 | times_.resize(new_size: dates_.size()); |
| 79 | times_[0]=0.0; |
| 80 | for (Size i = 1; i < dates_.size(); i++) |
| 81 | times_[i] = dayCounter().yearFraction(d1: dates_[0], d2: dates_[i]); |
| 82 | |
| 83 | interpolation_ = |
| 84 | interpolator_.interpolate(xBegin: times_.begin(), xEnd: times_.end(), |
| 85 | yBegin: data_.begin()); |
| 86 | interpolation_.update(); |
| 87 | } |
| 88 | |
| 89 | void CommodityCurve::setBasisOfCurve( |
| 90 | const ext::shared_ptr<CommodityCurve>& basisOfCurve) { |
| 91 | basisOfCurve_ = basisOfCurve; |
| 92 | basisOfCurveUomConversionFactor_ = |
| 93 | CommodityPricingHelper::calculateUomConversionFactor( |
| 94 | commodityType: commodityType_, |
| 95 | fromUnitOfMeasure: basisOfCurve_->unitOfMeasure_, |
| 96 | toUnitOfMeasure: unitOfMeasure_); |
| 97 | } |
| 98 | |
| 99 | std::ostream& operator<<(std::ostream& out, const CommodityCurve& curve) { |
| 100 | out << "[" << curve.name_ << "] (" << curve.currency_.code() |
| 101 | << "/" << curve.unitOfMeasure_.code() << ")" ; |
| 102 | if (curve.basisOfCurve_ != nullptr) |
| 103 | out << "; basis to (" << (*curve.basisOfCurve_) << ")" ; |
| 104 | return out; |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |