Skip to content
All library documents

Using QuantLib’s Piecewise Yield Curve Template in C++

Article Quant Q&A · Author: Sozmo

Summary

The document explains why QuantLib-Swig exposes separate names such as PiecewiseLogCubicDiscount, PiecewiseLinearZero, and PiecewiseCubicZero while QuantLib 1.17 C++ does not. In C++, these curve variants are generated from the PiecewiseYieldCurve class template by selecting a yield representation, interpolator, and bootstrap method. Python bindings expose separate classes because Python does not use C++ templates in the same way.

A sample implementation wraps the template in a function and constructs three variants: log-cubic discount, cubic zero yield, and linear zero yield curves. This provides a practical pattern for translating code that uses the Swig-specific class names. The example demonstrates construction with rate helpers and a day counter, but it is not a general guide to curve calibration, interpolation behavior, or validation. Users still need to choose template parameters and market conventions suitable for their application.

Key ideas

  • QuantLib C++ represents several named piecewise curve types through one class template.
  • The yield measure, interpolator, and bootstrap method determine the instantiated curve variant.
  • Separate curve names in QuantLib-Swig accommodate language bindings that do not expose C++ templates directly.
  • A templated helper function can construct the corresponding C++ curve variants.

Tags

Full text
# QuantLib 1.17 C++ does not contain some YieldTermStructure classes while QuantLib-Swig contains


# QuantLib 1.17 C++ does not contain some YieldTermStructure classes while QuantLib-Swig contains












I want to port some QuantLib-Swig dependent code written in Python to C++ with QuanLib-1.17. However some YieldTermStructure classes (PiecewiseLogCubicDiscount, PiecewiseLinearZero and PiecewiseCubicZero) are not present in QuantLib-1.17.

I found some yield curve examples that seems to cover those classes' functionalities. However, I want to know can't I find the equivalent of these classes in QuantLib-1.17 C++?

## Answer by Sozmo (score 1, accepted)

https://quant.stackexchange.com/a/53751

I found the reason why there are no specific versions of those classes by discussing with the developer of QuantLib. He stated that

> those classes are instances of a single PiecewiseYieldCurve class template in C++, but need to be exported as separate classes to Python where there are no templates.

I implemented a function template `calculateCurve` and also implemented a main function to show calls to `calculateCurve`:

```
template<class T, class I, template<class C> class B>
ext::shared_ptr<YieldTermStructure>
calculateCurve(Date &settlementDate,
               std::vector<ext::shared_ptr<RateHelper>> rateHelpers,
               DayCounter dayCounter,
               const I &interpolator = I())
{
    auto termStructure = ext::shared_ptr<YieldTermStructure>(new PiecewiseYieldCurve<T, I, B>(settlementDate,
                                                                                              rateHelpers,
                                                                                              dayCounter,
                                                                                              interpolator));
    return termStructure;
}

int main()
{
    ext::shared_ptr<Quote> rate(new SimpleQuote(0.0019121));
    ext::shared_ptr<RateHelper> rateHelper(new DepositRateHelper(
            Handle<Quote>(rate),
            Period(0, Days),
            2,
            TARGET(),
            Following,
            true,
            ActualActual()
    ));
    std::vector<ext::shared_ptr<RateHelper>> rateHelpers{rateHelper};

    Date settlementDate = Date(01, January, 2017);
    DayCounter dayCounter = Actual360();

    // PiecewiseLogCubicDiscount
    auto result = calculateCurve<Discount, LogCubic, IterativeBootstrap>(
            settlementDate,
            rateHelpers,
            dayCounter,
            LogCubic(CubicInterpolation::Spline));

    // PiecewiseCubicZero
    result = calculateCurve<ZeroYield, Cubic, IterativeBootstrap>(
            settlementDate,
            rateHelpers,
            dayCounter,
            Cubic(CubicInterpolation::Spline));

    // PiecewiseLinearZero
    result = calculateCurve<ZeroYield, Linear, IterativeBootstrap>(
            settlementDate,
            rateHelpers,
            dayCounter);

    return 0;
}
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.