Skip to content
All library documents

QuantLib Rate Curves: Scalar Quotes and Zero Curve Inputs

Article Quant Q&A · Author: Tiburce Baubeau

Summary

This exchange clarifies how QuantLib curve constructors interpret rate inputs in a Hull–White short rate simulation setup. A FlatForward curve is built from a single forward rate quote, so passing an array of rates to its scalar quote object causes a type error. To change that single rate, retain the quote object and update its value; this still represents one rate, not a time varying curve.

For market inputs consisting of zero coupon rates at different maturities, the answer recommends constructing a zero curve from corresponding dates and rate values, then supplying that term structure through a YieldTermStructureHandle. The examples illustrate the distinction between a flat forward curve and a curve defined by multiple zero rates. The exchange does not explain how to derive a future forward curve from a zero curve, calibrate the Hull–White model, or validate simulated paths, so those modeling questions remain outside its scope.

Key ideas

  • A FlatForward curve uses a single forward rate quote and does not accept an array of rates as that quote.
  • Retaining the SimpleQuote object allows its scalar rate to be updated.
  • A ZeroCurve can represent zero rates observed at multiple dates or maturities.
  • A zero curve can be passed to a YieldTermStructureHandle for use in the short rate process.

Tags

Full text
# Implementation of the Hull and White short rate model


# Implementation of the Hull and White short rate model












This is the first time I'm using quantlib, and I wanted to compare the velocity of quantlib with my own Python code.

I found a tutorial about Hull and White to generate the short rate paths with quantlib: (Tutorial about Hull and White)

The author seems to say that it is very simple to change the future instantaneous rate, which is a constant in the example... But when I replace it with an array of 361 values corresponding to the values of the future rate for the differents dates defined by the time step, I get the following error: TypeError: in method 'new_SimpleQuote', argument 1 of type 'Real'

I tried to investigate and search in the quantlib library how to fix this, but I havn't learned to code in C++ yet so I may need a little help

Thank you, and have a good day

PS: My input data is the zero-coupon curve at a certain time, so if it possible to use it directly instead of converting it to a future rate curve first, I'm interesed to know how to do it

Edit: This is my code

```
import QuantLib as ql
import matplotlib.pyplot as plt
import numpy as np

sigma = 0.1
a = 0.1
timestep = 360
length = 30 # in years
#forward_rate = 0.05 I replaced this line by an random array and it doesn't work
forward_rate=np.random.randn(361)
day_count = ql.Thirty360()
todays_date = ql.Date(15, 1, 2015)
print(ql.QuoteHandle(ql.SimpleQuote(forward_rate)))

ql.Settings.instance().evaluationDate = todays_date
spot_curve = ql.FlatForward(todays_date, 
ql.QuoteHandle(ql.SimpleQuote(forward_rate)), day_count)
spot_curve_handle = ql.YieldTermStructureHandle(spot_curve)

hw_process = ql.HullWhiteProcess(spot_curve_handle, a, sigma)
rng = ql.GaussianRandomSequenceGenerator(
           ql.UniformRandomSequenceGenerator(timestep, ql.UniformRandomGenerator()))
seq = ql.GaussianPathGenerator(hw_process, length, timestep, rng, False)

def generate_paths(num_paths, timestep):
    arr = np.zeros((num_paths, timestep+1))
    for i in range(num_paths):
        sample_path = seq.next()
        path = sample_path.value()
        time = [path.time(j) for j in range(len(path))]
        value = [path[j] for j in range(len(path))]
        arr[i, :] = np.array(value)
    return np.array(time), arr

num_paths = 10
time, paths = generate_paths(num_paths, timestep)
for i in range(num_paths):
    plt.plot(time, paths[i, :], lw=0.8, alpha=0.6)
plt.title("Hull-White Short Rate Simulation")
plt.show()
```

## Answer by byouness (score 1, accepted)

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

When you call `ql.FlatForward` it simply means you are constructing a rate curve that will lead to flat forward rates.

The constructor of this curve takes the forward rate as an input. If you want to change the input (say, because the market moved and forward value changed), then you can change the quote value with the new value like this. First, keep a pointer on the quote:

```
forward_rate = 0.05
quote = ql.SimpleQuote(forward_rate)
spot_curve = ql.FlatForward(todays_date, ql.QuoteHandle(quote), day_count)
print(spot_curve.zeroRate(1.0, ql.Continuous))
# 5.000000 % 30/360 (Bond Basis) continuous compounding
```

Then use the `setValue()` method to update the value, like this:

```
quote.setValue(0.04)
print(spot_curve.zeroRate(1.0, ql.Continuous))
# 4.000000 % 30/360 (Bond Basis) continuous compounding
```

You cannot pass an array of value to `setValue()`, the quote consists in a single value (= the value of the forward rate in this case). In particular, I don't understand why you want to make this forward random?

Now, to answer your other question, you can instantiate a zero curve directly using a list of dates and list of zero rates in this way:

```
ql.ZeroCurve(dates, rate_values, day_counter)
```

For example:

```
zero_curve = ql.ZeroCurve([todays_date + ql.Period(p) for p in ['6M', '1Y', '5Y', '10Y']],
                          [0.04, 0.05, 0.06, 0.57], ql.Actual365Fixed())
```

Then you can pass this curve to your `ql.YieldTermStructureHandle` just as you did, with your `ql.FlatForward` curve.

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.