PayoffInterpreter: specify new pay-offs and price them without code recompilation

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

PayoffInterpreter: specify new pay-offs and price them without code recompilation

Klaus Spanderen-2
Hi

the idea to specify new pay-offs and price them without code recompilation by
using a script language is floating around for some time.

To achieve this goal I used an interpreter for a C / Fortron90 like language
two colleagues and I have written some time ago. As an example I've connected
this interpreter with the MonteCarlo pricing engine.

The interpreter itself is running in a "Sandbox" with a C++ interface.
Therefore one has full control over the interpreter during the runtime (e.g.
reading/modifiying the variable stack, run self defined functions etc.).

If you are interest in this little project please feel free to download it
from

www.spanderen.de/PayoffInterpreter.zip
or
www.spanderen.de/PayoffInterpreter.tar.gz

Makefiles for Linux/Unix and VC++ are included. My colleagues have given
permission to put the interpreter under the Quantlib licence.

Feedback whether this project is off any use and should be developed further
on are welcome.

cheers

content of the tar ball:
 langauge/   : the interpreter incl. Bison/Flex files
 math/       : multi dimensional array and index array
 util/       : utils
 docu/       : html documentation of the language
 testsuite/  : small C++ testsuite using the Sandbox
 examples/   : QuantLib MonteCarlo pricer using the PayoffInterpreter
 pilib/      : PayoffInterpreter example programs and tests
 console/    : simple console for the PayoffInterpreter
--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
Email: [hidden email] (remove NOSPAM from the address)

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Ferdinando M. Ametrano-3
Hi Klaus

> If you are interest in this little project please feel free to download it

I don't have time enough to take a look at it, but it would surely an
interesting addition. I know others have been working on similar
projects and I wonder if anybody knowledgeable about payoff scripting
could summarize the current consensus on this issue

I would be interested in knowing what are the approaches used on the
street by commercial or proprietary parsers, any ad-hoc open source
library which could help, design decisions, etc

ciao -- nando

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Klaus Spanderen-2
Hi Nando,

the language itself is quite comprehensive C derivative with some MatLab/
Fortran90 extensions, e.g. vector/matrix as build-in types and the
interpreter has a C++ interface. The "problem" is that the project consists
of +20K lines of code and I don't want to blow up QL until I know that a
PayoffInterpreter is of any use for other users;-). The parser itself is
written using GNU Flex/Bison.


On Tuesday 18 September 2007 10:45 am, Ferdinando Ametrano wrote:
> I would be interested in knowing what are the approaches used on the
> street by commercial or proprietary parsers, any ad-hoc open source
> library which could help, design decisions, etc

At least on the equity side stucturer are using script/interpreter languages
together with MonteCarlo scenorio engines.(e.g. Reech).

cheers
--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
E-Mail: [hidden email] (remove NOSPAM from the address)


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Luigi Ballabio

On Sep 19, 2007, at 9:39 PM, Klaus Spanderen wrote:
> the language itself is quite comprehensive C derivative with some
> MatLab/
> Fortran90 extensions, e.g. vector/matrix as build-in types and the
> interpreter has a C++ interface. The "problem" is that the project
> consists
> of +20K lines of code and I don't want to blow up QL until I know that
> a
> PayoffInterpreter is of any use for other users;-).

Well, I wouldn't put in into the core library anyway; it would be great
to have it available as an additional module.  The point is that I
wouldn't commit to that one as "the" payoff interpreter. People using,
say, the Python or Ruby bindings might want to define such an
interpreter in their native language---it could be done easily enough
using SWIG.

Luigi


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

newbie73
Apologies if this seems to be off topic, though this is somewhat related to a pipe dream I had regarding QuantLib.  If it is possible to allow some external scripting language to define a new payoff structure, why not go ahead and allow the interpreter to extend QuantLib in general?

Perhaps an embedded version of Python could work or some other method of allowing "add-in" style hooks/call backs. :-)




-------------------------------------------
Well, I wouldn't put in into the core library anyway; it would be great
to have it available as an additional module.  The point is that I
wouldn't commit to that one as "the" payoff interpreter. People using,
say, the Python or Ruby bindings might want to define such an
interpreter in their native language---it could be done easily enough
using SWIG.

Luigi


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
QuantLib-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-dev


Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

HFQuant
In reply to this post by Klaus Spanderen-2
Hi,

i tried to you your tool and running the example EquityOption.cpp. It compiles but crashes at run time.
Did you have such a feedback before ? (0xC0000005: Access violation reading location 0x2183120a.)

thanks

Klaus Spanderen-2 wrote
Hi

the idea to specify new pay-offs and price them without code recompilation by
using a script language is floating around for some time.

To achieve this goal I used an interpreter for a C / Fortron90 like language
two colleagues and I have written some time ago. As an example I've connected
this interpreter with the MonteCarlo pricing engine.

The interpreter itself is running in a "Sandbox" with a C++ interface.
Therefore one has full control over the interpreter during the runtime (e.g.
reading/modifiying the variable stack, run self defined functions etc.).

If you are interest in this little project please feel free to download it
from

www.spanderen.de/PayoffInterpreter.zip
or
www.spanderen.de/PayoffInterpreter.tar.gz

Makefiles for Linux/Unix and VC++ are included. My colleagues have given
permission to put the interpreter under the Quantlib licence.

Feedback whether this project is off any use and should be developed further
on are welcome.

cheers

content of the tar ball:
 langauge/   : the interpreter incl. Bison/Flex files
 math/       : multi dimensional array and index array
 util/       : utils
 docu/       : html documentation of the language
 testsuite/  : small C++ testsuite using the Sandbox
 examples/   : QuantLib MonteCarlo pricer using the PayoffInterpreter
 pilib/      : PayoffInterpreter example programs and tests
 console/    : simple console for the PayoffInterpreter
--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
Email: klaus@NOSPAMspanderen.de (remove NOSPAM from the address)

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
QuantLib-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Klaus Spanderen-2
In reply to this post by Luigi Ballabio
Hi Luigi,

> Well, I wouldn't put in into the core library anyway; it would be great
> to have it available as an additional module.  The point is that I
> wouldn't commit to that one as "the" payoff interpreter.

The intention is/was to have "a" payoff interpreter at hand to be able to do
some prototyping when playing around with new payoffs.

> People using,
> say, the Python or Ruby bindings might want to define such an
> interpreter in their native language---it could be done easily enough
> using SWIG.

BTW: I know that one can call QL functions from Python etc. via. SWIG. Is it
also possible to call a Python-Script (which might define a payoff) from C++?

regards

--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
E-Mail: [hidden email] (remove NOSPAM from the address)


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Klaus Spanderen-2
In reply to this post by HFQuant
On Thursday 20 September 2007 5:21 pm, HFQuant wrote:
> Hi,
>
> i tried to you your tool and running the example EquityOption.cpp. It
> compiles but crashes at run time.
> Did you have such a feedback before ? (0xC0000005: Access violation reading
> location 0x2183120a.)
>
> thanks
>

arrg. No hadn't had this feedback before;-(. What operating system are you
using? On windows you might want to include the  payoff interpreter project
into your quantlib project to avoid any parameter clashes.

I checked the source code on Linux using g++-4.1 and on Windows with Visual
Studio Express. On Linux the memory checker valgrind didn't report a memery
leak or any access violation. I'm compiling against a recent QL version from
the SVN head. I

cheers

--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
E-Mail: [hidden email] (remove NOSPAM from the address)


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Luigi Ballabio
In reply to this post by Klaus Spanderen-2

Hi Klaus,

On Sep 21, 2007, at 7:51 PM, Klaus Spanderen wrote:
>> Well, I wouldn't put in into the core library anyway; it would be
>> great
>> to have it available as an additional module.  The point is that I
>> wouldn't commit to that one as "the" payoff interpreter.
>
> The intention is/was to have "a" payoff interpreter at hand to be able
> to do
> some prototyping when playing around with new payoffs.

Don't worry, I'm aware of the good intention.

> BTW: I know that one can call QL functions from Python etc. via. SWIG.
> Is it
> also possible to call a Python-Script (which might define a payoff)
> from C++?

Yes, with some code in the SWIG interface files. I'll try and send an
example when I get some time.

Luigi


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

HFQuant
In reply to this post by Klaus Spanderen-2
Hi,

I am using VS 2005 professional edition and the last Quantlib version 0.8.1.

So i changed  your include statements in the EquityOption.cpp file, basically including QuantLib.hpp and deleting the others.


thks

Klaus Spanderen-2 wrote
On Thursday 20 September 2007 5:21 pm, HFQuant wrote:
> Hi,
>
> i tried to you your tool and running the example EquityOption.cpp. It
> compiles but crashes at run time.
> Did you have such a feedback before ? (0xC0000005: Access violation reading
> location 0x2183120a.)
>
> thanks
>

arrg. No hadn't had this feedback before;-(. What operating system are you
using? On windows you might want to include the  payoff interpreter project
into your quantlib project to avoid any parameter clashes.

I checked the source code on Linux using g++-4.1 and on Windows with Visual
Studio Express. On Linux the memory checker valgrind didn't report a memery
leak or any access violation. I'm compiling against a recent QL version from
the SVN head. I

cheers

--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
E-Mail: klaus@NOSPAMspanderen.de (remove NOSPAM from the address)


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
QuantLib-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Klaus Spanderen-2
Hi

strange. Do you compile it (the whole project) as part of the quantlib? If you
want I'll send you me VS project.

On Monday 24 September 2007 11:34 am, HFQuant wrote:

> Hi,
>
> I am using VS 2005 professional edition and the last Quantlib version
> 0.8.1.
>
> So i changed  your include statements in the EquityOption.cpp file,
> basically including QuantLib.hpp and deleting the others.
>
>
> thks
>
> Klaus Spanderen-2 wrote:
> > On Thursday 20 September 2007 5:21 pm, HFQuant wrote:
> >> Hi,
> >>
> >> i tried to you your tool and running the example EquityOption.cpp. It
> >> compiles but crashes at run time.
> >> Did you have such a feedback before ? (0xC0000005: Access violation
> >> reading
> >> location 0x2183120a.)
> >>
> >> thanks
> >
> > arrg. No hadn't had this feedback before;-(. What operating system are
> > you using? On windows you might want to include the  payoff interpreter
> > project
> > into your quantlib project to avoid any parameter clashes.
> >
> > I checked the source code on Linux using g++-4.1 and on Windows with
> > Visual
> > Studio Express. On Linux the memory checker valgrind didn't report a
> > memery
> > leak or any access violation. I'm compiling against a recent QL version
> > from
> > the SVN head. I
> >
> > cheers
> >
> > --
> > Klaus Spanderen
> > Ludwig Erhard Str. 12
> > 48734 Reken (Germany)
> > E-Mail: [hidden email] (remove NOSPAM from the address)
> >
> >
> > -------------------------------------------------------------------------
> > This SF.net email is sponsored by: Microsoft
> > Defy all challenges. Microsoft(R) Visual Studio 2005.
> > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> > _______________________________________________
> > QuantLib-dev mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/quantlib-dev

--
Klaus Spanderen
Ludwig Erhard Str. 12
48734 Reken (Germany)
E-Mail: [hidden email] (remove NOSPAM from the address)


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Joseph Wang-2
In reply to this post by Klaus Spanderen-2
What would be nice is if there were some standard C++ interfaces which people
could using to "hook in" function evaluation systems from any language.  
Something very simple would be a "function adapter" which defined a function
that took a double and returned a double,

The trend I've seen in production code is to move away from special built
payoff engines and toward general purpose scripting languages, because
special built payoff engines tend in the long run to be unmaintainable.  The
other trend I've seen is to try to be interpreter agnostic since a typical
situation is one in which you have two or three custom payoff interpreters
which are used in various places, and there is an effort to try to start
merging the code base.

Also as far as payoffs, one thing that needs to be defined is the ability to
specify a payoff schedule.

One final thing, what would be useful is to create an object in python,
matlab, R, java or what have you, and this would generate a C++ object which
would look and act like any other C++ object out there.  This would be useful
not only in payoffs, but also generally useful both inside and outside of
quantlib.




-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: PayoffInterpreter: specify new pay-offs and price them without code recompilation

Luigi Ballabio
On Tue, 2007-09-25 at 00:00 -0400, Joseph Wang wrote:
> What would be nice is if there were some standard C++ interfaces which people
> could using to "hook in" function evaluation systems from any language.  

This can be done already with a combination of polymorphism and the
scripting language API. In Python, for instance, it would be enough to
define a C++ class like the following one and export it to Python via
SWIG:

class PyPayoff : public Payoff {
    PyObject payoff_;     // The payoff defined in Python. It can be
                          // a function or function object.
  public:
    PyPayoff(PyObject* payoff)
    : payoff_(payoff) {         // Store it...
        Py_XINCREF(function_);  // ...and make sure Python doesn't
    }                           // garbage-collect it.
    ~PyPayoff() {
        Py_XDECREF(function_);  // Release it when we're done.
    }
    Real operator()(Real price) const {
        // Call the Python function using the API...
        PyObject* pyResult = PyObject_CallFunction(payoff_,"d",price);
        // ...convert the result from Python...
        Real result = PyFloat_AsDouble(pyResult);
        // ...clean up...
        Py_XDECREF(pyResult);
        // ...and return.
        return result;
    }
    ...
};


Once the above is exported (which can be done in a few lines of SWIG
interface) the payoff can be defined from Python and used, as in:

def my_payoff1(S):   # a regular function
    return sqrt(S)

class my_payoff2:    # a function object
    def __init__(self,K):
        self.K = K
    def __call__(self,S):
        return (S-K)**2

option1 = SomeOption(process, PyPayoff(my_payoff1),
                     exercise, engine)
option2 = SomeOption(process, PyPayoff(my_payoff2(42.0)),
                     exercise, engine)

I'm sure the same can be done in other languages.

Later,
        Luigi


--

Poets have been mysteriously silent on the subject of cheese.
-- Gilbert K. Chesterton



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev