does anyone use java+swig bindings?

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

does anyone use java+swig bindings?

Tito Ingargiola

Hi,

I've been playing with QuantLib for the last month or so and have more
or less fallen in love.  In c++, everything I've tried works pretty
much as expected.

Unfortunately, I have an extensive gui written in java and when I try
to bridge into QuantLib via the SWIG interfaces I've run into endless
difficulties.  Given the lack of traffic on this list for java users
except the odd "I tried but it didn't work..." post, I wonder if
anyone is actually using the SWIG interfaces for java integration?  If
so, could anyone kindly point me to a non trivial example of java
accessing QuantLib functionality?  The sample provided with the swig
extension is laughably simple and doesn't demonstrate any kind of
parameter passing.

I'm frustrated at this point as things which are simple to do in c++
just don't work via the swig interfaces and I'm getting close to
dumping the swig interfaces and writing my own JNI interfaces for the
relatively limited areas of functionality I'm interested in, but I'd
prefer not to do this if a reasonable alternative exists.  

In order to illustrate the kinds of difficulties I'm running into, I
enclose below a simple sample program based on
Quantlib/Examples/EquityOption which uses the SWIG interfaces and
compiles fine but will crash in several different places when run.
Even such a simple thing as Dates don't seem to work.  That is:


       Date goodDate = Date.todaysDate(); // ok
       goodDate.add(17); // ok
// boom when date is referenced
       Date badDate = new Date( 1, Month.October, 2007);

I have similar experiences with simple objects like quotes or
handles... which is why I question that anyone is actually using these
things.

Any insights, pointers or suggestions will be very much appreciated!
Thanks and regards,

    Tito.

--- the java program I mentioned follows ---




package test;

import org.quantlib.Actual365Fixed;
import org.quantlib.AmericanExercise;
import org.quantlib.BaroneAdesiWhaleyEngine;
import org.quantlib.BlackConstantVol;
import org.quantlib.BlackScholesMertonProcess;
import org.quantlib.BlackVolTermStructureHandle;
import org.quantlib.Date;
import org.quantlib.DayCounter;
import org.quantlib.FlatForward;
import org.quantlib.Month;
import org.quantlib.Option;
import org.quantlib.Payoff;
import org.quantlib.PlainVanillaPayoff;
import org.quantlib.QuoteHandle;
import org.quantlib.SimpleQuote;
import org.quantlib.StochasticProcess;
import org.quantlib.VanillaOption;
import org.quantlib.YieldTermStructureHandle;

/**
 * Test app - simplified version of QuantLib/Examples/EquityOption to test
 * use of Quantlib through supplied SWIG interface...
 *
 * @author Tito Ingargiola
 */
public class EquityOption {
    static {
        /* You need to run this thing with something like:
         * -Djava.library.path=/usr/local/lib
         */
        System.loadLibrary("QuantLib-0.8.1");
        System.loadLibrary("QuantLibJNI");
    }
    
    public static void main(String[] args) throws Exception {
        System.out.println("starting...");
        //QuantLib ql = new QuantLib();
        //Thread.sleep(500);

        Option.Type type = Option.Type.Put;
        double strike = 14.7;
        double underlying = 14.76;
        double riskFreeRate = 0.055;
        double divYield = riskFreeRate; // for futures
        double volatility = .22;
        System.out.println("creating dates...");
        Date settle = Date.todaysDate();        // Explicitly setting date
        Date expiry = Date.todaysDate().add(13);//  causes crash...on use(?)
        Date expiry2 = new Date( 20, Month.September, 2007 ); // e.g.
        System.out.println(settle+" -> "+expiry);
        
        AmericanExercise exercise = new AmericanExercise(settle, expiry,true);
        // using the below will cause crash
       // AmericanExercise exercise2 = new AmericanExercise(settle, expiry2,true);
        
        // this is fine ... unless you use it...
        DayCounter dayc = new Actual365Fixed();
        // this is fine ... unless you use it...
        SimpleQuote under = new SimpleQuote(underlying);
        // this instead blows up on construction...
        QuoteHandle underh = new QuoteHandle(under);
        
        /// the rest is pie-in-the-sky stuff...
        
        YieldTermStructureHandle  flatTS = new YieldTermStructureHandle
            (new FlatForward(settle,riskFreeRate, dayc));
        
        YieldTermStructureHandle flatDivTS = new YieldTermStructureHandle
            (new FlatForward(settle,divYield, dayc));

        BlackVolTermStructureHandle flatVolTS = new BlackVolTermStructureHandle
            (new BlackConstantVol(settle,volatility,dayc));
        
        
        StochasticProcess stochp = new BlackScholesMertonProcess
            (underh,flatDivTS,flatTS,flatVolTS);
        Payoff payoff = new PlainVanillaPayoff(type, strike) ;

        VanillaOption option = new VanillaOption
            (stochp, payoff,exercise);
        
        option.setPricingEngine(new BaroneAdesiWhaleyEngine());
        
        System.out.println("Option:   \t"+option);
        System.out.println("NPV:      \t"+option.NPV());
        
        System.out.println("\ndone.");
        
    }
}



    


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: does anyone use java+swig bindings?

Tito Ingargiola

My apologies - I realize I've sent this to the wrong list!  I'll send to the users list - sorry for the disturbance...

Tito Ingargiola <[hidden email]> wrote:

Hi,

I've been playing with QuantLib for the last month or so and have more
or less fallen in love.  In c++, everything I've tried works pretty
much as expected.

Unfortunately, I have an extensive gui written in java and when I try
to bridge into QuantLib via the SWIG interfaces I've run into endless
difficulties.  Given the lack of traffic on this list for java users
except the odd "I tried but it didn't work..." post, I wonder if
anyone is actually using the SWIG interfaces for java integration?  If
so, could anyone kindly point me to a non trivial example of java
accessing QuantLib functionality?  The sample provided with the swig
extension is laughably simple and doesn't demonstrate any kind of
parameter passing.

I'm frustrated at this point as things which are simple to do in c++
just don't work via the swig interfaces and I'm getting close to
dumping the swig interfaces and writing my own JNI interfaces for the
relatively limited areas of functionality I'm interested in, but I'd
prefer not to do this if a reasonable alternative exists.  

In order to illustrate the kinds of difficulties I'm running into, I
enclose below a simple sample program based on
Quantlib/Examples/EquityOption which uses the SWIG interfaces and
compiles fine but will crash in several different places when run.
Even such a simple thing as Dates don't seem to work.  That is:


       Date goodDate = Date.todaysDate(); // ok
       goodDate.add(17); // ok
// boom when date is referenced
       Date badDate = new Date( 1, Month.October, 2007);

I have similar experiences with simple objects like quotes or
handles... which is why I question that anyone is actually using these
things.

Any insights, pointers or suggestions will be very much appreciated!
Thanks and regards,

    Tito.

--- the java program I mentioned follows ---




package test;

import org.quantlib.Actual365Fixed;
import org.quantlib.AmericanExercise;
import org.quantlib.BaroneAdesiWhaleyEngine;
import org.quantlib.BlackConstantVol;
import org.quantlib.BlackScholesMertonProcess;
import org.quantlib.BlackVolTermStructureHandle;
import org.quantlib.Date;
import org.quantlib.DayCounter;
import org.quantlib.FlatForward;
import org.quantlib.Month;
import org.quantlib.Option;
import org.quantlib.Payoff;
import org.quantlib.PlainVanillaPayoff;
import org.quantlib.QuoteHandle;
import org.quantlib.SimpleQuote;
import org.quantlib.StochasticProcess;
import org.quantlib.VanillaOption;
import org.quantlib.YieldTermStructureHandle;

/**
 * Test app - simplified version of QuantLib/Examples/EquityOption to test
 * use of Quantlib through supplied SWIG interface...
 *
 * @author Tito Ingargiola
 */
public class EquityOption {
    static {
        /* You need to run this thing with something like:
         * -Djava.library.path=/usr/local/lib
         */
        System.loadLibrary("QuantLib-0.8.1");
        System.loadLibrary("QuantLibJNI");
    }
    
    public static void main(String[] args) throws Exception {
        System.out.println("starting...");
        //QuantLib ql = new QuantLib();
        //Thread.sleep(500);

        Option.Type type = Option.Type.Put;
        double strike = 14.7;
        double underlying = 14.76;
        double riskFreeRate = 0.055;
        double divYield = riskFreeRate; // for futures
        double volatility = .22;
        System.out.println("creating dates...");
        Date settle = Date.todaysDate();        // Explicitly setting date
        Date expiry = Date.todaysDate().add(13);//  causes crash...on use(?)
        Date expiry2 = new Date( 20, Month.September, 2007 ); // e.g.
        System.out.println(settle+" -> "+expiry);
        
        AmericanExercise exercise = new AmericanExercise(settle, expiry,true);
        // using the below will cause crash
       // AmericanExercise exercise2 = new AmericanExercise(settle, expiry2,true);
        
        // this is fine ... unless you use it...
        DayCounter dayc = new Actual365Fixed();
        // this is fine ... unless you use it...
        SimpleQuote under = new SimpleQuote(underlying);
        // this instead blows up on construction...
        QuoteHandle underh = new QuoteHandle(under);
        
        /// the rest is pie-in-the-sky stuff...
        
        YieldTermStructureHandle  flatTS = new YieldTermStructureHandle
            (new FlatForward(settle,riskFreeRate, dayc));
        
        YieldTermStructureHandle flatDivTS = new YieldTermStructureHandle
            (new FlatForward(settle,divYield, dayc));

        BlackVolTermStructureHandle flatVolTS = new BlackVolTermStructureHandle
            (new BlackConstantVol(settle,volatility,dayc));
        
        
        StochasticProcess stochp = new BlackScholesMertonProcess
            (underh,flatDivTS,flatTS,flatVolTS);
        Payoff payoff = new PlainVanillaPayoff(type, strike) ;

        VanillaOption option = new VanillaOption
            (stochp, payoff,exercise);
        
        option.setPricingEngine(new BaroneAdesiWhaleyEngine());
        
        System.out.println("Option:   \t"+option);
        System.out.println("NPV:      \t"+option.NPV());
        
        System.out.println("\ndone.");
        
    }
}



    

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: does anyone use java+swig bindings?

Richard Gomes
In reply to this post by Tito Ingargiola
Tito Ingargiola wrote:

>
> Hi,
>
> I've been playing with QuantLib for the last month or so and have more
> or less fallen in love.  In c++, everything I've tried works pretty
> much as expected.
>
> Unfortunately, I have an extensive gui written in java and when I try
> to bridge into QuantLib via the SWIG interfaces I've run into endless
> difficulties.  Given the lack of traffic on this list for java users
> except the odd "I tried but it didn't work..." post, I wonder if
> anyone is actually using the SWIG interfaces for java integration?  If
> so, could anyone kindly point me to a non trivial example of java
> accessing QuantLib functionality?  The sample provided with the swig
> extension is laughably simple and doesn't demonstrate any kind of
> parameter passing.
>
> I'm frustrated at this point as things which are simple to do in c++
> just don't work via the swig interfaces and I'm getting close to
> dumping the swig interfaces and writing my own JNI interfaces for the
> relatively limited areas of functionality I'm interested in, but I'd
> prefer not to do this if a reasonable alternative exists.  
>
> In order to illustrate the kinds of difficulties I'm running into, I
> enclose below a simple sample program based on
> Quantlib/Examples/EquityOption which uses the SWIG interfaces and
> compiles fine but will crash in several different places when run.
> Even such a simple thing as Dates don't seem to work.  That is:
>
>
>        Date goodDate = Date.todaysDate(); // ok
>        goodDate.add(17); // ok
> // boom when date is referenced
>        Date badDate = new Date( 1, Month.October, 2007);
>
> I have similar experiences with simple objects like quotes or
> handles... which is why I question that anyone is actually using these
> things.
>
> Any insights, pointers or suggestions will be very much appreciated!
> Thanks and regards,
>
>     Tito.
>
> --- the java program I mentioned follows ---
>
>
>
>
> package test;
>
> import org.quantlib.Actual365Fixed;
> import org.quantlib.AmericanExercise;
> import org.quantlib.BaroneAdesiWhaleyEngine;
> import org.quantlib.BlackConstantVol;
> import org.quantlib.BlackScholesMertonProcess;
> import org.quantlib.BlackVolTermStructureHandle;
> import org.quantlib.Date;
> import org.quantlib.DayCounter;
> import org.quantlib.FlatForward;
> import org.quantlib.Month;
> import org.quantlib.Option;
> import org.quantlib.Payoff;
> import org.quantlib.PlainVanillaPayoff;
> import org.quantlib.QuoteHandle;
> import org.quantlib.SimpleQuote;
> import org.quantlib.StochasticProcess;
> import org.quantlib.VanillaOption;
> import org.quantlib.YieldTermStructureHandle;
>
> /**
>  * Test app - simplified version of QuantLib/Examples/EquityOption to test
>  * use of Quantlib through supplied SWIG interface...
>  *
>  * @author Tito Ingargiola
>  */
> public class EquityOption {
>     static {
>         /* You need to run this thing with something like:
>          * -Djava.library.path=/usr/local/lib
>          */
>         System.loadLibrary("QuantLib-0.8.1");
>         System.loadLibrary("QuantLibJNI");
>     }
>    
>     public static void main(String[] args) throws Exception {
>         System.out.println("starting...");
>         //QuantLib ql = new QuantLib();
>         //Thread.sleep(500);
>
>         Option.Type type = Option.Type.Put;
>         double strike = 14.7;
>         double underlying = 14.76;
>         double riskFreeRate = 0.055;
>         double divYield = riskFreeRate; // for futures
>         double volatility = .22;
>         System.out.println("creating dates...");
>         Date settle = Date.todaysDate();        // Explicitly setting date
>         Date expiry = Date.todaysDate().add(13);//  causes crash...on
> use(?)
>         Date expiry2 = new Date( 20, Month.September, 2007 ); // e.g.
>         System.out.println(settle+" -> "+expiry);
>        
>         AmericanExercise exercise = new AmericanExercise(settle,
> expiry,true);
>         // using the below will cause crash
>        // AmericanExercise exercise2 = new AmericanExercise(settle,
> expiry2,true);
>        
>         // this is fine ... unless you use it...
>         DayCounter dayc = new Actual365Fixed();
>         // this is fine ... unless you use it...
>         SimpleQuote under = new SimpleQuote(underlying);
>         // this instead blows up on construction...
>         QuoteHandle underh = new QuoteHandle(under);
>        
>         /// the rest is pie-in-the-sky stuff...
>        
>         YieldTermStructureHandle  flatTS = new YieldTermStructureHandle
>             (new FlatForward(settle,riskFreeRate, dayc));
>        
>         YieldTermStructureHandle flatDivTS = new YieldTermStructureHandle
>             (new FlatForward(settle,divYield, dayc));
>
>         BlackVolTermStructureHandle flatVolTS = new
> BlackVolTermStructureHandle
>             (new BlackConstantVol(settle,volatility,dayc));
>        
>        
>         StochasticProcess stochp = new BlackScholesMertonProcess
>             (underh,flatDivTS,flatTS,flatVolTS);
>         Payoff payoff = new PlainVanillaPayoff(type, strike) ;
>
>         VanillaOption option = new VanillaOption
>             (stochp, payoff,exercise);
>        
>         option.setPricingEngine(new BaroneAdesiWhaleyEngine());
>        
>         System.out.println("Option:   \t"+option);
>         System.out.println("NPV:      \t"+option.NPV());
>        
>         System.out.println("\ndone.");
>        
>     }
> }
>
>
>
>    
>
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems?  Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >>  http://get.splunk.com/
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> QuantLib-dev mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-dev

Hi Tito,

I have a lot of code in Java as well and I'd like to plug QuantLib in.

It's incredible how you and me are facing the same problems, following
the same path and started translating the same example! :D  When I
looked at you Java code it was quite similar to mine (of course... you
also translated the example in C++).

Oh well...

I dont think that writing your own JNI is a good idea.
It's a huge, boring and error prone task. I'd rely on SWIG instead.
It'd be much easier to learn how to hack the intrincacies of SWIG.

It would be great if we were able to convert comments from C++ code to
JavaDOC as well, as Eclipse would be able to show documentation as you type.

Another thing to be considered is performance.
I tried to use Matrix and it does not make sense to initialize every
cell at a time. The performance will be poor. It means that we should
create additional methods intended to be called by another languages due
to poor performance of separated calls.

I've posted one thread quite similiar to yours and Luigi gave me some
directions. In case, I've use Matrix as an example of issues I've found.

I'm planning to work on SWIG this weekend. Maybe we could work together
on this kind of thing.

Kind Regards

-- Richard Gomes


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev