7city CFA

Forum Navigation:


FORUMS > Programming and Software Forum < refresh >
Topic Title: Free code for Heston
Created On Wed Sep 24, 03 01:10 AM
Topic View:

Pages: [ 1 2 >> Next ]
View thread in raw text format


asd
Senior Member

Posts: 662
Joined: Aug 2002

Wed Sep 24, 03 01:10 AM
User is offline


Is there any free VBA/C++ code available for Heston model implementation, on the net?


Thanks,
asd

-------------------------
Life is a journey and not a destination
quant code directory
 
Reply
   
Quote
   
Top
   
Bottom
     



robertral
Senior Member

Posts: 655
Joined: Mar 2003

Wed Sep 24, 03 05:55 PM
User is offline

Please PM when you find it

-------------------------
"WAAH EXACCLY I PACE!!!!!!!!!!!!!!!!!!!!!?"??..."WAAH EXACCLY I PACE!!!!!!!!!!!!!!!!!!!!!?"??
 
Reply
   
Quote
   
Top
   
Bottom
     



AVt
Senior Member

Posts: 965
Joined: Dec 2001

Wed Sep 24, 03 07:14 PM
User is offline View users profile

Aaron once pointed to Ling Wu, JavaScript is easy to translate.
 
Reply
   
Quote
   
Top
   
Bottom
     



AVt
Senior Member

Posts: 965
Joined: Dec 2001

Sun Sep 28, 03 10:11 PM
User is offline View users profile

Well, here is a partial translation from the linked source to Excel. You will
need the Analysis toolpack atpvba.xla to be loaded/linked: it is used for
the complex arithmetics needed (very slow, for better performance write
your own) in VBA. The integrands are oscillating, but if the strike is not
to far from spot, they are 'reasonable' enough for a Gauss-Laguerre approx
and using 18 points (pre-computed, they are not in Abramowitz-Stegun) seems
to gives a good mixture of exactness ( ~ 5 digits) and speed within Excel.
One might compare to 9 pts or mid-point-summation by looking in the code.
Remembering the above bottle neck one should look also at pb273's solution.
Feel free to post comments: improvements/errors/hints ...

Edited: Sun Sep 28, 03 at 10:12 PM by AVt

Heston93_atpvba.zip Heston93_atpvba.zip  (21 KB)

 
Reply
   
Quote
   
Top
   
Bottom
     



pb273
Senior Member

Posts: 663
Joined: Jul 2002

Mon Sep 29, 03 04:46 AM
User is offline

hi,
would be grateful for improvements/errors/suggestions etc in my code too ...
thanks
 
Reply
   
Quote
   
Top
   
Bottom
     



asd
Senior Member

Posts: 662
Joined: Aug 2002

Mon Sep 29, 03 07:14 AM
User is offline

AVt & Pb273,

Thank you very much!!


Regards,
asd

-------------------------
Life is a journey and not a destination
quant code directory
 
Reply
   
Quote
   
Top
   
Bottom
     



quantman
Member

Posts: 160
Joined: Feb 2002

Tue Sep 30, 03 09:56 AM
User is offline View users profile

Here's attached the implementation of Heston Model (using Liptons Eigenfunction Expansion Method) for pricing double barriers, digitals, double-no-touchs, power and vanilla options.

Hope that's what you're looking for !

HestonBarrierAnalytic.zip HestonBarrierAnalytic.zip  (35 KB)

 
Reply
   
Quote
   
Top
   
Bottom
     



AVt
Senior Member

Posts: 965
Joined: Dec 2001

Sat Nov 01, 03 08:10 PM
User is offline View users profile

My above translation is not very satisfactory: slow and unprecise.

I took it as an exercise: what can be done in Excel? Find the answer uploaded.

For this the function evaluation has to be speed up first. After massaging
the code the result seems nice, there is a routine tstRunTimeHestonIntegrands
to check it. Next is integration. And Gauss-Laguerre is not the right one.
One thing would be FFT (but Excel has not a reasonable one and it would be
a mess with exactness). So i thought about adaptive quadrature but after some
hack around i decided to equal spaced slicing in subintervalls and to apply a
fast and simple Gauss-Legendre method. Due to Excel's restricted excactness of
~ 15 digits a choose of degree=16 works (the higher degree above is nonsense,
it introduces error). Playing with exacter tools a cut off for integration at
128 gives good results.

That is the default used in the sheet. To change it set the global variables gLength
and gLengthSubInterval (module mHestonIntegrals) appropriate by opening
the VBA project. For the concurrent values use tstRunTimeHestonValue to check
speed: if one uses 64 and 16 it is much faster with 4 - 5 digits of exactness
for realistic parameters (for very short times one has to obey the oscillation,
i did that in a rude way, dont be careless - anyway Heston is not for short
experies ...).

The sheet contains some reference values to check excatness (esp if one wants
to integrate coarser). The values have been computed with the NAG library using
Maple for cutoff=128 and cutoff=180. It is a very reliable lib (it even checks
round off errors), and limited to machine precision it gives ~ 14 - 15 digits.

Which is enough to cross check Excel :-) For the default parameters against the
reference values the exactness of the implemented Excel solution is ~ 10^(-9)
(with a speed of 45 option values per second [on a 900 Mhz PC], increasing
almost linear to 180 values per sec for the coarser grid - the speed to
evaluate the real parts of the 2 integrands is 11500 pairs per second).

Additional there are 2 simple grafics to get a feeling what happens with the
integrands, they display on data entered in the sheet 'HestonPrice'.

Of course one could do it properly with C (i dont do), but it was fun with
Excel and i learned a lot through it. Enjoy.

Edited: Sun Nov 02, 03 at 01:42 PM by AVt

Heston93_opt.zip Heston93_opt.zip  (53 KB)

 
Reply
   
Quote
   
Top
   
Bottom
     



anniew
Junior Member

Posts: 5
Joined: Aug 2002

Fri Nov 28, 03 03:52 PM
User is offline View users profile

nice spreadshhet, AVt!

one small question:
...
'partialWidth = Application.WorksheetFunction.Min(partialWidth, 512)
' scheiss funktion!
If 512 < partialWidth Then
Else
partialWidth = 512
End If
....

The "if" construction reimplements the Max function, not the Min. Is it the one you need? Do you really need at least 512 steps?

I am a bit curious about one bit of your programming style. (I am not trying to criticize you, it is just pure curiosity!) You seem to use a lot of empty if - then blocks to insert code in else blocks - I never saw this kind of style before. Do you think that it is more readable?

 
Reply
   
Quote
   
Top
   
Bottom
     



AVt
Senior Member

Posts: 965
Joined: Dec 2001

Fri Nov 28, 03 10:53 PM
User is offline View users profile

anniew,

Thx. The code is ok, but i lost patients - especially for cleaning :-)
It cuts at 512 and then brings partialWidth down to ~ gLengthSubInterval
if one can expect heavy periodics.

'If ...': i prefer readable vs speed, esp i hate unneccessary 'not's and
all 'greater's ... may be since i once had to program in mainframe projects,
where a lot is maintaining systems. But for the fct evaluation you see
optimized code (it is the complex arithmetics for the Heston 'kernel').
No lecturer will accept that as a solution without proper & good reasons.
Hmm ... and any quality assurance will react similar.

Edited: Fri Nov 28, 03 at 11:03 PM by AVt
 
Reply
   
Quote
   
Top
   
Bottom
     



ScottCaveny
Senior Member

Posts: 340
Joined: Oct 2003

Fri Nov 28, 03 11:03 PM
User is offline



Edited: Tue Dec 23, 03 at 06:10 AM by ScottCaveny
 
Reply
   
Quote
   
Top
   
Bottom
     



mib
Member

Posts: 70
Joined: Jan 2002

Mon Dec 01, 03 04:57 PM
User is offline View users profile

AVt,

integration seems to be pretty stable for t = .1 .. 2. But for positive interest rates, yor results seem to deviate from what Mathematica gives me and become arbitrageable. E.g. S=K=100, time=5, r=15%, V1=V2=0.07, lambda=1, eta= - rho=50%. Call price is 117.407>S. Are you cutting off too early?
 
Reply
   
Quote
   
Top
   
Bottom
     



AVt
Senior Member

Posts: 965
Joined: Dec 2001

Mon Dec 01, 03 06:45 PM
User is offline View users profile

Michael,

Thx for input - 'mathematical' it is ok. Looking at the graphs one might
guess to decrease the length of the subintervalls & choosing them to be 4
you will get 117,406932780419 in the sheet while 117.40693278041 is the
value i get using the NAG library for integration without cutting the
upper integration bound. On a PC this is usually with 14 digits. So what
says MMA?

I would say the parameters are somewhat artifical :-)
 
Reply
   
Quote
   
Top
   
Bottom
     



mib
Member

Posts: 70
Joined: Jan 2002

Mon Dec 01, 03 07:41 PM
User is offline View users profile

AVt, first of all I would like to thank you for sharing this piece of code with us. I am sure it will be useful for many of us, include those fortunate who have Heston code of their own

The problem is, under any model a call option can not be worth more than the share. You see, a call option can be exchanged for the share upon a non-negative payement in some scenarios, can be discarded under some other scenarious. Simply having the share right now is strictly better than having the call with any strike; thus C<S for all strikes under any static arbitrage-free model. So C=117.4 > S=100 is wrong.

Either I am missing something crucial here or you are integrating wrong functions. Are you sure you are discounting correctly? 117.4 is about what the undiscounted price (i.e. in t=tau=5 dollars) should be.

I am sorry I can not give you Mathematica numbers right now - I am not in the office at the moment.
 
Reply
   
Quote
   
Top
   
Bottom
     



AVt
Senior Member

Posts: 965
Joined: Dec 2001

Mon Dec 01, 03 10:33 PM
User is offline View users profile

Eeeeeee ... Ummmpfff ... Ok. I am an idiot, of course you are right.

It seems i forgot to switch from fwd to spot & discounting is missing.
Let me know your value - being not at work i have a Maple solution for
a quite different way. Thx for testing and your sympathy :-)

This is ugly, since i used it without discounting for the RND in another
thread, grrr.

-------
Edited: yes, discounting is missing, rates are only taken to get the
(theoretical) future. To check it with Gatheral's notes (which i used):
"Further, suppose that we consider only the future value to expiration C
of the European option price rather than its value today ..." (see p.14).
So multiply by exp( - rate*time) to get the concurrent option price. Sorry.

Edited: Wed Dec 03, 03 at 12:50 PM by AVt

Heston93-Check.zip Heston93-Check.zip  (9 KB)

 
Reply
   
Quote
   
Top
   
Bottom
     



mib
Member

Posts: 70
Joined: Jan 2002

Thu Dec 04, 03 12:50 PM
User is offline View users profile

Yes, it is the discounting. I can even guess the source of the confusion. It is the two lines at page 14 of the popular Gatheral Lecture notes: "...we consider only the future value [...] of the European option price..."

By the way, the Heston JavaScript code on the web has the same mistake.

With proper discounting (discount factor 2.1170), your code gives 55.46 vs 53.39 I get in Mathematica. I am not sure which integration is better.
 
Reply
   
Quote
   
Top
   
Bottom
     



AVt
Senior Member

Posts: 965
Joined: Dec 2001

Thu Dec 04, 03 09:22 PM
User is offline View users profile

Thx for confirming my sloppiness :-)

If you want MMA not to loose just increase precision to compare with the
cross-check done in Maple using the characteristic function approach. The
formula can be found in Artur Sepp's paper 'FT ... An Overview' at
http://www.hot.ee/seppar/ Another way would be to use the code in Alan
Lewis' book.
 
Reply
   
Quote
   
Top
   
Bottom
     



AVt
Senior Member

Posts: 965
Joined: Dec 2001

Sat Jul 10, 04 09:37 PM
User is offline View users profile

There is a fine solution at http://www.nielses.dk and part of a
master's thesis "Simulation based pricing of American options in
a model with stochastic volatility and jumps" by Niels Sønderby.

The paper is in Danish with English summary, the code is C++ (with
english (inline) comments). It's emphasis is implementation and
requires a good understanding of used results.

This provides european pricing due to Heston's and Bates' model by
integration (using the class 'complex' and Gauss Kronrod method)
while for american options least-squares MC methods are developed
(and that is the actual goal of the paper) for that models - the
summary shows the way how to achieve that.

The code may be seen as part of Quantlib (but as far as i can see
it might be isolated to give 'stand alone' solutions).

Additionally the author provides an interface for Mathematica.
 
Reply
   
Quote
   
Top
   
Bottom
     



FiveEights
Junior Member

Posts: 8
Joined: Apr 2004

Mon Jul 26, 04 05:25 PM
User is offline

Hi,

I've got some problems with the the closed form solution of Heston93. I've just implemented all the equations from the original paper without any special features or modifications. The numerical solution of the integral is done by Gauss-Laguerreand I'm using the complex arithmetic of the C++ STL.
This might be not the most sophisticated way to implement Heston, but this is only meant as a simple beginning to play around with.

I am comparing my results to the Excel sheets posted here and to some online calculators. For some settings of the parameters I get exactly the same results as in the other calculations while for other settings of the parameters there are big differences.
For example just changing the mean reversion from 2.0 to 1.0 leads to wrong results....
P_j seems to be evaluated correctly for j=1, but not for j=2 and I have no idea what could be the reason. Rounding errors, complex logarithm ???

In many of the example programs mentioned above, there is a reference to a lecture of Jim Gatheral. By looking at the programs based on this lecture, it seems to me that he uses a different way to implement the model. Unfortunately I can't find it on the web. It would be very helpful, if someone could post this paper.

I would appreciate, if you could give me any hints ! Thank you very much...
 
Reply
   
Quote
   
Top
   
Bottom
     



redcrab
Member

Posts: 43
Joined: Jun 2004

Mon Jul 26, 04 07:14 PM
User is offline


Gatheral's lectures can be found here
 
Reply
   
Quote
   
Top
   
Bottom
     

Pages: [ 1 2 >> Next ]
View thread in raw text format
FORUMS > Programming and Software Forum < refresh >

Forum Navigation:

© All material, including contents and design, copyright Wilmott Electronic Media Limited - FuseTalk 4.01 © 1999-2012 FuseTalk Inc. Terms & Conditions