Skip to content
All library documents

Customizing Stochastic Process Discretization in QuantLib

Article Quant Q&A · Author: NewWrtier

Summary

The document addresses whether QuantLib supports exact numerical simulation for stochastic processes such as geometric Brownian motion. Its answer explains that the library’s design allows a process to use a supplied discretization scheme, even though the implementation available to the questioner provided Euler discretization only. That limitation is attributed to the set of contributions implemented at the time, rather than to a fundamental restriction in the library’s design.

A user seeking exact simulation can implement a discretization class by inheriting from the process discretization interface and pass an instance of that class when creating the process. The post offers this as the route to using exact numerics within the framework. It does not compare the accuracy, stability, or computational cost of exact and approximate schemes, nor does it establish that exact discretization exists for every process; the method depends on having a suitable scheme for the model in question.

Key ideas

  • QuantLib’s process design can accept different discretization schemes.
  • The implementation discussed provided Euler discretization, while exact schemes could be added by contributors.
  • A custom scheme can inherit from the stochastic process discretization interface.
  • The custom discretization is supplied when the process is instantiated.

Tags

Full text
# Bootstrapping spot rates based on swap rates using QuantLib


# Bootstrapping spot rates based on swap rates using QuantLib












I am bootstrapping the shibor curve and fr007 curve using swap rates in China. I created my own index like following:

```
Shibor3M=IborIndex("Shibor3M",Period(3,Months),0,CNYCurrency(),
                    China(China.IB),Following,False,Actual360(),
                    discounting_curve)
```

and using to bootstrapping the spot rates. And code is:

```
today=Date(17,6,2019)
Settings.instance().evaluationDate = today
discounting_curve=RelinkableYieldTermStructureHandle()
forecasting_curve=RelinkableYieldTermStructureHandle()
quotes = [SimpleQuote(2.80763/100)]
#Shibor index
Shibor3M=IborIndex("Shibor-3M",Period(3,Months),0,
                   CNYCurrency(),China(China.IB), 
                   Following,False,Actual360(),
                   forecasting_curve)
depo_helper = [DepositRateHelper(
                   QuoteHandle(quotes[0]),Period(3,Months),0,China(China.IB),
                   Following,False,Actual360())]

swap_helpers = [ SwapRateHelper(QuoteHandle(SimpleQuote(rate/100.0)), Period(*tenor), China(China.IB),Quarterly,Following,Actual365Fixed(),Shibor3M())
        for tenor, rate in [((6,Months),2.985),((9,Months),3.0163),((1,Years),3.0475),((2,Years), 3.1638),
        ((3,Years), 3.2788),
        ((4,Years), 3.3813),
        ((5,Years), 3.4688),
        ((7,Years), 3.6175),((10,Years),3.765)] ]
rate_helpers=depo_helper+swap_helpers
curve1=PiecewiseCubicZero(today,rate_helpers,Actual360)
spots = []
tenors = []
for d in curve1.dates():
    yrs=Actual360.yearFraction(today,d)
    compounding=Simple
    freq=Quarterly
    zero_rate=curve1.zeroRate(yrs,compounding,freq)
    tenors.append(yrs)
eq_rate=zero_rate.equivalentRate(Actual360,compounding,freq,today,d).rate()
spots.append(100*eq_rate)
datatable={'Dates':curve1.dates(),'Tenors':tenors,'spots':spots}
df=pd.DataFrame.from_dict((datatable))
```

But I can not get the result. Can anyone give some help to me? Thanks.

## Answer by David Duarte (score 1)

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

There are a few errors in your code:

- You created an index object and pointed to it with a variable, therefore you should use it as `Shibor3M` and not `Shibor3M()`, ie, after created the index is not callable

- Everytime you use the daycounter class Actual360 you have to call it, i.e. `Actual360()`

Try this:

```
today=Date(17,6,2019)
Settings.instance().evaluationDate = today
discounting_curve=RelinkableYieldTermStructureHandle()
forecasting_curve=RelinkableYieldTermStructureHandle()
quotes = [SimpleQuote(2.80763/100)]
#Shibor index
Shibor3M=IborIndex("Shibor-3M",Period(3,Months),0,
                   CNYCurrency(),China(China.IB), 
                   Following,False,Actual360(),
                   forecasting_curve)
depo_helper = [DepositRateHelper(
                   QuoteHandle(quotes[0]),Period(3,Months),0,China(China.IB),
                   Following,False,Actual360())]

swap_helpers = [ SwapRateHelper(QuoteHandle(SimpleQuote(rate/100.0)), Period(*tenor), China(China.IB),Quarterly,Following,Actual365Fixed(),Shibor3M)
        for tenor, rate in [((6,Months),2.985),((9,Months),3.0163),((1,Years),3.0475),((2,Years), 3.1638),
        ((3,Years), 3.2788),
        ((4,Years), 3.3813),
        ((5,Years), 3.4688),
        ((7,Years), 3.6175),((10,Years),3.765)] ]
rate_helpers=depo_helper+swap_helpers
curve1=PiecewiseCubicZero(today,rate_helpers,Actual360())
spots = []
tenors = []
for d in curve1.dates():
    yrs=Actual360().yearFraction(today,d)
    compounding=Simple
    freq=Quarterly
    zero_rate=curve1.zeroRate(yrs,compounding,freq)
    tenors.append(yrs)
    eq_rate=zero_rate.equivalentRate(Actual360(),compounding,freq,today,d).rate()
    spots.append(100*eq_rate)
datatable={'Dates':curve1.dates(),'Tenors':tenors,'spots':spots}
df=pd.DataFrame.from_dict((datatable))
```

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.