Discussion:
[Quantlib-users] Variance Swap test
ziegele
2017-02-07 13:42:18 UTC
Permalink
I was looking at the VarianceSwap example in test-suite.

I created a class called varSwap, and call it from the main() function:

<Loading Image...>

I notice that the testing functions testReplicatingVarianceSwap() and
testMCVarianceSwap() are both called by the other function static
boost::unit_test_framework::test_suite* suite().

<Loading Image...>

Since the return type is test_suite*, I'm wondering how to fill in the
main() function to call it, and what kind of results shall I expect?

Thanks!



--
View this message in context: http://quantlib.10058.n7.nabble.com/Variance-Swap-test-tp18059.html
Sent from the quantlib-users mailing list archive at Nabble.com.
ziegele
2017-02-07 14:52:57 UTC
Permalink
BTW, if I "directly call" the suite() function in main() this way:

<Loading Image...>

I got 2 errors while compiling:

<Loading Image...>

This is how to link boost library and quantlib for this project:

<Loading Image...>

<Loading Image...>

Could anyone please advise how to fix?





--
View this message in context: http://quantlib.10058.n7.nabble.com/Variance-Swap-test-tp18059p18060.html
Sent from the quantlib-users mailing list archive at Nabble.com.
Luigi Ballabio
2017-02-15 10:34:00 UTC
Permalink
The main() function using the suite is provided by the Boost unit-test
framework; you'll need to provide an init_unit_test_suite function, as done
in the quantlibtestsuite.cpp file.

As for the linking errors you were getting: flatRate is defined
in test-suite/utilities.cpp, and you need to link it together with your
other files.

Luigi
Post by ziegele
<http://quantlib.10058.n7.nabble.com/file/n18060/main.jpg>
<http://quantlib.10058.n7.nabble.com/file/n18060/error.jpg>
<http://quantlib.10058.n7.nabble.com/file/n18060/include.jpg>
<http://quantlib.10058.n7.nabble.com/file/n18060/library.jpg>
Could anyone please advise how to fix?
--
http://quantlib.10058.n7.nabble.com/Variance-Swap-test-tp18059p18060.html
Sent from the quantlib-users mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
https://lists.sourceforge.net/lists/listinfo/quantlib-users
ziegele
2017-03-01 03:02:00 UTC
Permalink
Thanks Luigi!

I bypassed the problem by replacing flatRate() as defined in utilities.hpp
with the FlatForward class. As shown in the screenshot below, the
calculation matches with expected result very well. I also included the cpp
codes in attachment ( flat.cpp
<http://quantlib.10058.n7.nabble.com/file/n18127/flat.cpp> ).

<Loading Image...>

Naturally, the next step is to replace flat interest rate and dividend with
a term structure. So I tried the InterpolatedZeroCurve class. In order to
make sure that I get this class setup correctly, I defined a flat interest
rate terms structure, to compare with the FlatForward results. The code is
also attached ( termStructure.cpp
<http://quantlib.10058.n7.nabble.com/file/n18127/termStructure.cpp> ).

Unfortunately, the calculation result didn't match with expectation:

<Loading Image...>

My guess is that, building flat forward curve from a flat interest rate term
structure should use some other functions. How should I correctly
interpolate the term structure, so that I can:

1) match with the constant div and r at the first step;
2) introduce non-flat interest rate and dividend term structure?

Thanks!



--
View this message in context: http://quantlib.10058.n7.nabble.com/Variance-Swap-test-tp18059p18127.html
Sent from the quantlib-users mailing list archive at Nabble.com.
Luigi Ballabio
2017-04-06 08:20:38 UTC
Permalink
The difference might be that in the flat forward case your curve starts at
today's date, while in the interpolated case it starts at February 24th and
the discount factors used in the calculation will change accordingly. You
should try using today as the first date in the vector instead.

Luigi
Post by ziegele
Thanks Luigi!
I bypassed the problem by replacing flatRate() as defined in utilities.hpp
with the FlatForward class. As shown in the screenshot below, the
calculation matches with expected result very well. I also included the cpp
codes in attachment ( flat.cpp
<http://quantlib.10058.n7.nabble.com/file/n18127/flat.cpp> ).
<http://quantlib.10058.n7.nabble.com/file/n18127/match.jpg>
Naturally, the next step is to replace flat interest rate and dividend with
a term structure. So I tried the InterpolatedZeroCurve class. In order to
make sure that I get this class setup correctly, I defined a flat interest
rate terms structure, to compare with the FlatForward results. The code is
also attached ( termStructure.cpp
<http://quantlib.10058.n7.nabble.com/file/n18127/termStructure.cpp> ).
<http://quantlib.10058.n7.nabble.com/file/n18127/mismatch.jpg>
My guess is that, building flat forward curve from a flat interest rate term
structure should use some other functions. How should I correctly
1) match with the constant div and r at the first step;
2) introduce non-flat interest rate and dividend term structure?
Thanks!
--
http://quantlib.10058.n7.nabble.com/Variance-Swap-test-tp18059p18127.html
Sent from the quantlib-users mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
https://lists.sourceforge.net/lists/listinfo/quantlib-users
ziegele
2017-05-12 21:19:59 UTC
Permalink
Thanks Luigi.

I tried to expand the variance swap replication example to include: non-flat
volatility surface, non-flat interest rate, and non-flat discrete dividend
term structure, based on the example I could find from Mick Hittesdorf's
tutorial at
https://mhittesdorf.wordpress.com/2013/11/17/introducing-quantlib-american-option-pricing-with-dividends/
<https://mhittesdorf.wordpress.com/2013/11/17/introducing-quantlib-american-option-pricing-with-dividends/>

As you can find in the attached file main.cpp
<http://quantlib.10058.n7.nabble.com/file/n18277/main.cpp> , I used
boost::shared_ptr<ZeroCurve> to generate the discrete dividend term
structure, and boost::shared_ptr<YieldTermStructure> with
InterpolatedZeroCurve<ForwardFlat> to return the interest rate term
structure.

There are some things that I'm not sure if I did correctly:

1. How do I include discrete dividend, rather than dividend yield, into the
calculation? Currently I translated discreteDiv to divYield by the equation:
divYield = discreteDiv / spot * yearFraction(evaluationDate,
optionExpiryDate), for each exDividend date (data read from
dividend_schedule.csv
<http://quantlib.10058.n7.nabble.com/file/n18277/dividend_schedule.csv>
file). Is it correct?

2. How do I correctly interpolate the interest rate term structure?
(1) Currently I used a flat interest rate of 5% (read from ir_schedule.csv
<http://quantlib.10058.n7.nabble.com/file/n18277/ir_schedule.csv> file,
with the 1st dividend date to be the evaluationDate, otherwise PV would be
wrong). Is there a way of inputting discount factor directly? If so, how do
I change InterpolatedZeroCurve() to?
(2) Do I really need the <ForwardFlat> selection to interpolate the interest
rate? If not, what are other choices?

3. What is the difference between <ZeroCurve> used in dividend, and
<YieldTermStructure> used in interest rate term structure? Loos to be that I
could also use one to replace the other;

4. Currently the volatility surface is kept the same as what's written in
the example (vol=0.3~0.13, linearly dependent on strike). If I want to
include a full volatility surface which can also be read from a file, can I
keep the codes unchanged (lines 15-45)?

Thanks,
ziegele



--
View this message in context: http://quantlib.10058.n7.nabble.com/Variance-Swap-test-tp18059p18277.html
Sent from the quantlib-users mailing list archive at Nabble.com.
Luigi Ballabio
2017-05-18 14:31:47 UTC
Permalink
Hello,

1. yes, you'll have to translate discrete dividends into dividend yields.
I'm not sure of the times you're using, though. If an option expires in 6
months and you expect a dividend in 2 months, do you divide by 6 or 2? Do
you spread next dividend starting from today or from the first dividend
date? (The two answers might be correlated.)

2. You can use InterpolateDiscountCurve and build it from discounts. There
are a number of interpolations available; look into ql/math/interpolations
for classes documented as "traits".

3. ZeroCurve is a particular kind of YieldTermStructure.

4. I'm not sure I got your question...

Luigi
Post by ziegele
Thanks Luigi.
I tried to expand the variance swap replication example to include: non-flat
volatility surface, non-flat interest rate, and non-flat discrete dividend
term structure, based on the example I could find from Mick Hittesdorf's
tutorial at
https://mhittesdorf.wordpress.com/2013/11/17/introducing-quantlib-american-option-pricing-with-dividends/
<
https://mhittesdorf.wordpress.com/2013/11/17/introducing-quantlib-american-option-pricing-with-dividends/
As you can find in the attached file main.cpp
<http://quantlib.10058.n7.nabble.com/file/n18277/main.cpp> , I used
boost::shared_ptr<ZeroCurve> to generate the discrete dividend term
structure, and boost::shared_ptr<YieldTermStructure> with
InterpolatedZeroCurve<ForwardFlat> to return the interest rate term
structure.
1. How do I include discrete dividend, rather than dividend yield, into the
divYield = discreteDiv / spot * yearFraction(evaluationDate,
optionExpiryDate), for each exDividend date (data read from
dividend_schedule.csv
<http://quantlib.10058.n7.nabble.com/file/n18277/dividend_schedule.csv>
file). Is it correct?
2. How do I correctly interpolate the interest rate term structure?
(1) Currently I used a flat interest rate of 5% (read from ir_schedule.csv
<http://quantlib.10058.n7.nabble.com/file/n18277/ir_schedule.csv> file,
with the 1st dividend date to be the evaluationDate, otherwise PV would be
wrong). Is there a way of inputting discount factor directly? If so, how do
I change InterpolatedZeroCurve() to?
(2) Do I really need the <ForwardFlat> selection to interpolate the interest
rate? If not, what are other choices?
3. What is the difference between <ZeroCurve> used in dividend, and
<YieldTermStructure> used in interest rate term structure? Loos to be that I
could also use one to replace the other;
4. Currently the volatility surface is kept the same as what's written in
the example (vol=0.3~0.13, linearly dependent on strike). If I want to
include a full volatility surface which can also be read from a file, can I
keep the codes unchanged (lines 15-45)?
Thanks,
ziegele
--
http://quantlib.10058.n7.nabble.com/Variance-Swap-test-tp18059p18277.html
Sent from the quantlib-users mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Continue reading on narkive:
Loading...