Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up adding fixings #2085

Merged
merged 10 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions ql/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ namespace QuantLib {
Real fixing,
bool forceOverwrite) {
checkNativeFixingsAllowed();
addFixings(&fixingDate, (&fixingDate)+1,
&fixing,
forceOverwrite);
addFixings(&fixingDate, (&fixingDate) + 1, &fixing, forceOverwrite);
}

void Index::addFixings(const TimeSeries<Real>& t,
Expand Down
42 changes: 8 additions & 34 deletions ql/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,42 +100,16 @@ namespace QuantLib {
ValueIterator vBegin,
bool forceOverwrite = false) {
checkNativeFixingsAllowed();
std::string tag = name();
TimeSeries<Real> h = IndexManager::instance().getHistory(tag);
bool noInvalidFixing = true, noDuplicatedFixing = true;
Date invalidDate, duplicatedDate;
Real nullValue = Null<Real>();
Real invalidValue = Null<Real>();
Real duplicatedValue = Null<Real>();
DateIterator dBegin2 = dBegin, dEnd2 = dEnd;
ValueIterator vBegin2 = vBegin;
while (dBegin != dEnd) {
bool validFixing = isValidFixingDate(*dBegin);
Real currentValue = h[*dBegin];
bool missingFixing = forceOverwrite || currentValue == nullValue;
if (validFixing) {
if (missingFixing)
h[*(dBegin++)] = *(vBegin++);
else if (close(currentValue, *(vBegin))) {
++dBegin;
++vBegin;
} else {
noDuplicatedFixing = false;
duplicatedDate = *(dBegin++);
duplicatedValue = *(vBegin++);
}
} else {
noInvalidFixing = false;
invalidDate = *(dBegin++);
invalidValue = *(vBegin++);
}
QL_REQUIRE(isValidFixingDate(*dBegin),
"At least on invalid fixing provided: " << dBegin->weekday() << *dBegin
<< ", " << *vBegin);
++dBegin;
++vBegin;
}
IndexManager::instance().setHistory(tag, h);
QL_REQUIRE(noInvalidFixing, "At least one invalid fixing provided: "
<< invalidDate.weekday() << " " << invalidDate << ", "
<< invalidValue);
QL_REQUIRE(noDuplicatedFixing, "At least one duplicated fixing provided: "
<< duplicatedDate << ", " << duplicatedValue
<< " while " << h[duplicatedDate]
<< " value is already present");
IndexManager::instance().addFixings(name(), dBegin2, dEnd2, vBegin2, forceOverwrite);
}
//! clears all stored historical fixings
void clearFixings();
Expand Down
18 changes: 15 additions & 3 deletions ql/indexes/indexmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,27 @@ namespace QuantLib {
}

const TimeSeries<Real>& IndexManager::getHistory(const std::string& name) const {
return data_[name].value();
return data_[name];
}

void IndexManager::setHistory(const std::string& name, TimeSeries<Real> history) {
data_[name] = std::move(history);
pcaspers marked this conversation as resolved.
Show resolved Hide resolved
}

void IndexManager::addFixing(const std::string& name,
const Date& fixingDate,
Real fixing,
bool forceOverwrite) {
addFixings(name, &fixingDate, (&fixingDate) + 1, &fixing, forceOverwrite);
}

ext::shared_ptr<Observable> IndexManager::notifier(const std::string& name) const {
return data_[name];
auto n = notifiers_.find(name);
if(n != notifiers_.end())
return n->second;
auto o = ext::make_shared<Observable>();
notifiers_[name] = o;
return o;
}

std::vector<std::string> IndexManager::histories() const {
Expand All @@ -52,7 +64,7 @@ namespace QuantLib {
bool IndexManager::hasHistoricalFixing(const std::string& name, const Date& fixingDate) const {
auto const& indexIter = data_.find(name);
return (indexIter != data_.end()) &&
((*indexIter).second.value()[fixingDate] != Null<Real>());
((*indexIter).second[fixingDate] != Null<Real>());
}

}
44 changes: 42 additions & 2 deletions ql/indexes/indexmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <ql/patterns/singleton.hpp>
#include <ql/timeseries.hpp>
#include <ql/math/comparison.hpp>
#include <ql/utilities/observablevalue.hpp>
#include <algorithm>
#include <cctype>
Expand All @@ -47,7 +48,45 @@
const TimeSeries<Real>& getHistory(const std::string& name) const;
//! stores the historical fixings of the index
void setHistory(const std::string& name, TimeSeries<Real> history);
//! observer notifying of changes in the index fixings
//! add a fixing
void addFixing(const std::string& name,
const Date& fixingDate,
Real fixing,
bool forceOverwrite = false);
//! add fixings
template <class DateIterator, class ValueIterator>
void addFixings(const std::string& name,
DateIterator dBegin,
DateIterator dEnd,
ValueIterator vBegin,
bool forceOverwrite = false) {
auto& h = data_[name];
bool noDuplicatedFixing = true;
Date duplicatedDate;
Real nullValue = Null<Real>();
Real invalidValue = Null<Real>();
Real duplicatedValue = Null<Real>();
while (dBegin != dEnd) {
Real currentValue = h[*dBegin];
bool missingFixing = forceOverwrite || currentValue == nullValue;
if (missingFixing)
h[*(dBegin++)] = *(vBegin++);
else if (close(currentValue, *(vBegin))) {
++dBegin;
++vBegin;
} else {
noDuplicatedFixing = false;
duplicatedDate = *(dBegin++);
duplicatedValue = *(vBegin++);
}
}
notifier(name)->notifyObservers();
QL_REQUIRE(noDuplicatedFixing, "At least one duplicated fixing provided: "
<< duplicatedDate << ", " << duplicatedValue
<< " while " << h[duplicatedDate]
<< " value is already present");
}
//! observer notifying of changes in the index fixings_
ext::shared_ptr<Observable> notifier(const std::string& name) const;
//! returns all names of the indexes for which fixings were stored
std::vector<std::string> histories() const;
Expand All @@ -67,7 +106,8 @@
}
};

mutable std::map<std::string, ObservableValue<TimeSeries<Real>>, CaseInsensitiveCompare> data_;
mutable std::map<std::string, TimeSeries<Real>, CaseInsensitiveCompare> data_;
mutable std::map<std::string, ext::shared_ptr<Observable>> notifiers_;

Check notice on line 110 in ql/indexes/indexmanager.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ql/indexes/indexmanager.hpp#L110

class member 'IndexManager::notifiers_' is never used.
};

}
Expand Down
1 change: 0 additions & 1 deletion ql/utilities/observablevalue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ namespace QuantLib {
const T& ObservableValue<T>::value() const {
return value_;
}

}

#endif