QuantLib R package from SWIG code

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

QuantLib R package from SWIG code

Dirk Eddelbuettel

Hi all,

A few days ago I finally made an attempt at turning the few files usually
contained in the QL-SWIG directory for R (eg makeRData.R, QuantLib.cpp,
QuantLib.R) into a proper R package (in the sense of the ~ 4500 CRAN
packages).

It is straightforward.  A working version is committed in the SVN of my
earlier (and more limited) explicit RQuantLib wrapping at R-Forge [1].  One
only needs this minimal layout:

   edd@max:~/svn/rquantlib/pkg/QuantLib$ tree
   .
   ├── cleanup
   ├── demo                    # mostly files by Joseph Wang
   │   ├── 00Index             # index of demos
   │   ├── bates.R             # a file by Klaus Spanderen
   │   ├── bonds.R             # something I added, see below
   │   ├── europeanOption.R    
   │   ├── fdOption.R
   │   ├── graph.R
   │   ├── scatter.R
   │   ├── swap.R              # incomplete, see below
   │   └── wireframe.R
   ├── DESCRIPTION
   ├── NAMESPACE
   ├── R
   │   ├── makeRData.R         # not needed
   │   └── QuantLib.R
   └── src
       ├── Makevars
       └── QuantLib.cpp

   3 directories, 16 files
   edd@max:~/svn/rquantlib/pkg/QuantLib$

where
    a) demo/ is an optional directory with examples or demos,
    b) makeRData.R is all commented-out (!!) and
    c) the files R/QuantLib.R and src/QuantLib.cpp are _unaltered_ copies
       from the current QL-SWIG files.
DESCRIPTION and NAMESPACE are needed per R standards for packages.  The rest
is gravy: src/Makevars is two lines calling quantlib-config. We can easily
autoconf this.

The main advantage: it now behaves like a standard R packages, and users can
do   library(QuantLib)   as they would with any other package. [ It will
never pass QA tests for R as every exported function would need a manual page
etc pp. The code is also fragile, it is pretty easy to end up with segfaults. ]

Now, I don't want to suggest that we shoehorn this into 1.3 if QL 1.3 is coming
soon, but maybe the one after would fit if anybody else is interested in R
integration?  I'd also be happy to keep it outside of QL if that is prefered.

Also, I spent some time on the weekend translating bonds.py into bonds.R.
That worked nicely, see below [2] I also tried to translate swap.py -- but
just realized that the enum type for the Payer vs Receiver is not in the
QuantLib.R file [3].  Any idea?   I may translate some more of the existing
examples from Python or other directories.

Feedback welcome.

Dirk


[1] You can browse the SVN here:
    https://r-forge.r-project.org/scm/viewvc.php/pkg/QuantLib/?root=rquantlib

[2] A quick transcript

edd@max:~/svn/rquantlib/pkg/QuantLib$ R --slave -e 'source("demo/bonds.R")'
Today :[1] "2008-09-15"
Settlement Date: [1] "2008-09-18"

Results:
                zeroCoupon fixedRate floatingRate
NPV                100.922 107.66829  102.3593146
Clean Price        100.922 106.12753  101.7972017
Dirty Price        100.922 107.66829  102.3593146
Accrued Amount       0.000   1.54076    0.5621129
Previous Coupon         NA   0.04500    0.0288625
Next Coupon             NA   0.04500    0.0342984

Sample indirect computations (for the floating rate bond):
Yield to Clean Price: [1] 101.797
Clean Price to Yield: [1] 0.0220096
edd@max:~/svn/rquantlib/pkg/QuantLib$


[3] No Payer/Receiver enum for Vanilla Swap, other enums are defined:

edd@max:~/svn/rquantlib/pkg/QuantLib$ grep Payer R/QuantLib.R
edd@max:~/svn/rquantlib/pkg/QuantLib$ grep Put R/QuantLib.R
                        'Put' = -1,
edd@max:~/svn/rquantlib/pkg/QuantLib$

--
Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com

------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib R package from SWIG code

Dirk Eddelbuettel

On 3 June 2013 at 20:06, Dirk Eddelbuettel wrote:
| edd@max:~/svn/rquantlib/pkg/QuantLib$ R --slave -e 'source("demo/bonds.R")'
| Today :[1] "2008-09-15"
| Settlement Date: [1] "2008-09-18"
|
| Results:
|                 zeroCoupon fixedRate floatingRate
| NPV                100.922 107.66829  102.3593146
| Clean Price        100.922 106.12753  101.7972017
| Dirty Price        100.922 107.66829  102.3593146
| Accrued Amount       0.000   1.54076    0.5621129
| Previous Coupon         NA   0.04500    0.0288625
| Next Coupon             NA   0.04500    0.0342984
|
| Sample indirect computations (for the floating rate bond):
| Yield to Clean Price: [1] 101.797
| Clean Price to Yield: [1] 0.0220096
| edd@max:~/svn/rquantlib/pkg/QuantLib$

Here is the same from slightly better version (and now source'd from R)

R> source("bonds.R")
Today :[1] "2008-09-15"
Settlement Date: [1] "2008-09-18"

Results:
                zeroCoupon fixedRate floatingRate
NPV               100.9222  107.6683    102.35931
Clean Price       100.9222  106.1275    101.79720
Dirty Price       100.9222  107.6683    102.35931
Accrued Amount      0.0000    1.5408      0.56211
Previous Coupon         NA    4.5000      2.88625
Next Coupon             NA    4.5000      3.42984
Yield               3.0001    3.6476      2.20096

Sample indirect computations (for the floating rate bond):
Yield to Clean Price: [1] 101.797
Clean Price to Yield: [1] 0.0220096


The file is in the SVN repo for R. It starts with library(QuantLib), the
three lines in Joe's README can also be used to load the shared library and
pre-created data types.  I'd be happy to check it into QuantLib too.

Dirk


--
Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com

------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib R package from SWIG code

Luigi Ballabio
In reply to this post by Dirk Eddelbuettel
Hi Dirk,
    yes, let's merge it after release. We'll autoconf it so that the
cpp is generated in the correct place and the other files are created,
too.

Re the swap enumeration: it might be due to the shenanigans we're
doing to hide the shared pointers from the interface. Option::Put,
that you see, is exported directly (see options.i:40).
VanillaSwap::Payer is first hidden, then re-exported as a static const
data member (see swap.i, line 56 and onwards). Does SWIG/R manage
this?

Luigi



On Tue, Jun 4, 2013 at 3:06 AM, Dirk Eddelbuettel <[hidden email]> wrote:

>
> Hi all,
>
> A few days ago I finally made an attempt at turning the few files usually
> contained in the QL-SWIG directory for R (eg makeRData.R, QuantLib.cpp,
> QuantLib.R) into a proper R package (in the sense of the ~ 4500 CRAN
> packages).
>
> It is straightforward.  A working version is committed in the SVN of my
> earlier (and more limited) explicit RQuantLib wrapping at R-Forge [1].  One
> only needs this minimal layout:
>
>    edd@max:~/svn/rquantlib/pkg/QuantLib$ tree
>    .
>    ├── cleanup
>    ├── demo                    # mostly files by Joseph Wang
>    │   ├── 00Index             # index of demos
>    │   ├── bates.R             # a file by Klaus Spanderen
>    │   ├── bonds.R             # something I added, see below
>    │   ├── europeanOption.R
>    │   ├── fdOption.R
>    │   ├── graph.R
>    │   ├── scatter.R
>    │   ├── swap.R              # incomplete, see below
>    │   └── wireframe.R
>    ├── DESCRIPTION
>    ├── NAMESPACE
>    ├── R
>    │   ├── makeRData.R         # not needed
>    │   └── QuantLib.R
>    └── src
>        ├── Makevars
>        └── QuantLib.cpp
>
>    3 directories, 16 files
>    edd@max:~/svn/rquantlib/pkg/QuantLib$
>
> where
>     a) demo/ is an optional directory with examples or demos,
>     b) makeRData.R is all commented-out (!!) and
>     c) the files R/QuantLib.R and src/QuantLib.cpp are _unaltered_ copies
>        from the current QL-SWIG files.
> DESCRIPTION and NAMESPACE are needed per R standards for packages.  The rest
> is gravy: src/Makevars is two lines calling quantlib-config. We can easily
> autoconf this.
>
> The main advantage: it now behaves like a standard R packages, and users can
> do   library(QuantLib)   as they would with any other package. [ It will
> never pass QA tests for R as every exported function would need a manual page
> etc pp. The code is also fragile, it is pretty easy to end up with segfaults. ]
>
> Now, I don't want to suggest that we shoehorn this into 1.3 if QL 1.3 is coming
> soon, but maybe the one after would fit if anybody else is interested in R
> integration?  I'd also be happy to keep it outside of QL if that is prefered.
>
> Also, I spent some time on the weekend translating bonds.py into bonds.R.
> That worked nicely, see below [2] I also tried to translate swap.py -- but
> just realized that the enum type for the Payer vs Receiver is not in the
> QuantLib.R file [3].  Any idea?   I may translate some more of the existing
> examples from Python or other directories.
>
> Feedback welcome.
>
> Dirk
>
>
> [1] You can browse the SVN here:
>     https://r-forge.r-project.org/scm/viewvc.php/pkg/QuantLib/?root=rquantlib
>
> [2] A quick transcript
>
> edd@max:~/svn/rquantlib/pkg/QuantLib$ R --slave -e 'source("demo/bonds.R")'
> Today :[1] "2008-09-15"
> Settlement Date: [1] "2008-09-18"
>
> Results:
>                 zeroCoupon fixedRate floatingRate
> NPV                100.922 107.66829  102.3593146
> Clean Price        100.922 106.12753  101.7972017
> Dirty Price        100.922 107.66829  102.3593146
> Accrued Amount       0.000   1.54076    0.5621129
> Previous Coupon         NA   0.04500    0.0288625
> Next Coupon             NA   0.04500    0.0342984
>
> Sample indirect computations (for the floating rate bond):
> Yield to Clean Price: [1] 101.797
> Clean Price to Yield: [1] 0.0220096
> edd@max:~/svn/rquantlib/pkg/QuantLib$
>
>
> [3] No Payer/Receiver enum for Vanilla Swap, other enums are defined:
>
> edd@max:~/svn/rquantlib/pkg/QuantLib$ grep Payer R/QuantLib.R
> edd@max:~/svn/rquantlib/pkg/QuantLib$ grep Put R/QuantLib.R
>                         'Put' = -1,
> edd@max:~/svn/rquantlib/pkg/QuantLib$
>
> --
> Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com
>
> ------------------------------------------------------------------------------
> How ServiceNow helps IT people transform IT departments:
> 1. A cloud service to automate IT design, transition and operations
> 2. Dashboards that offer high-level views of enterprise services
> 3. A single system of record for all IT processes
> http://p.sf.net/sfu/servicenow-d2d-j
> _______________________________________________
> QuantLib-dev mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-dev

------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib R package from SWIG code

Dirk Eddelbuettel

Hi Luigi,

On 4 June 2013 at 12:19, Luigi Ballabio wrote:
| Hi Dirk,
|     yes, let's merge it after release. We'll autoconf it so that the
| cpp is generated in the correct place and the other files are created,
| too.

Sounds good. We'll tackle this off-line.
 
| Re the swap enumeration: it might be due to the shenanigans we're
| doing to hide the shared pointers from the interface. Option::Put,
| that you see, is exported directly (see options.i:40).
| VanillaSwap::Payer is first hidden, then re-exported as a static const
| data member (see swap.i, line 56 and onwards). Does SWIG/R manage
| this?

I will attest to complete ignorance as to what SWIG/R does. That was AFAIK
all Joseph Wang--and singlehandedly.

Dirk
 
| Luigi
|
|
|
| On Tue, Jun 4, 2013 at 3:06 AM, Dirk Eddelbuettel <[hidden email]> wrote:
| >
| > Hi all,
| >
| > A few days ago I finally made an attempt at turning the few files usually
| > contained in the QL-SWIG directory for R (eg makeRData.R, QuantLib.cpp,
| > QuantLib.R) into a proper R package (in the sense of the ~ 4500 CRAN
| > packages).
| >
| > It is straightforward.  A working version is committed in the SVN of my
| > earlier (and more limited) explicit RQuantLib wrapping at R-Forge [1].  One
| > only needs this minimal layout:
| >
| >    edd@max:~/svn/rquantlib/pkg/QuantLib$ tree
| >    .
| >    ├── cleanup
| >    ├── demo                    # mostly files by Joseph Wang
| >    │   ├── 00Index             # index of demos
| >    │   ├── bates.R             # a file by Klaus Spanderen
| >    │   ├── bonds.R             # something I added, see below
| >    │   ├── europeanOption.R
| >    │   ├── fdOption.R
| >    │   ├── graph.R
| >    │   ├── scatter.R
| >    │   ├── swap.R              # incomplete, see below
| >    │   └── wireframe.R
| >    ├── DESCRIPTION
| >    ├── NAMESPACE
| >    ├── R
| >    │   ├── makeRData.R         # not needed
| >    │   └── QuantLib.R
| >    └── src
| >        ├── Makevars
| >        └── QuantLib.cpp
| >
| >    3 directories, 16 files
| >    edd@max:~/svn/rquantlib/pkg/QuantLib$
| >
| > where
| >     a) demo/ is an optional directory with examples or demos,
| >     b) makeRData.R is all commented-out (!!) and
| >     c) the files R/QuantLib.R and src/QuantLib.cpp are _unaltered_ copies
| >        from the current QL-SWIG files.
| > DESCRIPTION and NAMESPACE are needed per R standards for packages.  The rest
| > is gravy: src/Makevars is two lines calling quantlib-config. We can easily
| > autoconf this.
| >
| > The main advantage: it now behaves like a standard R packages, and users can
| > do   library(QuantLib)   as they would with any other package. [ It will
| > never pass QA tests for R as every exported function would need a manual page
| > etc pp. The code is also fragile, it is pretty easy to end up with segfaults. ]
| >
| > Now, I don't want to suggest that we shoehorn this into 1.3 if QL 1.3 is coming
| > soon, but maybe the one after would fit if anybody else is interested in R
| > integration?  I'd also be happy to keep it outside of QL if that is prefered.
| >
| > Also, I spent some time on the weekend translating bonds.py into bonds.R.
| > That worked nicely, see below [2] I also tried to translate swap.py -- but
| > just realized that the enum type for the Payer vs Receiver is not in the
| > QuantLib.R file [3].  Any idea?   I may translate some more of the existing
| > examples from Python or other directories.
| >
| > Feedback welcome.
| >
| > Dirk
| >
| >
| > [1] You can browse the SVN here:
| >     https://r-forge.r-project.org/scm/viewvc.php/pkg/QuantLib/?root=rquantlib
| >
| > [2] A quick transcript
| >
| > edd@max:~/svn/rquantlib/pkg/QuantLib$ R --slave -e 'source("demo/bonds.R")'
| > Today :[1] "2008-09-15"
| > Settlement Date: [1] "2008-09-18"
| >
| > Results:
| >                 zeroCoupon fixedRate floatingRate
| > NPV                100.922 107.66829  102.3593146
| > Clean Price        100.922 106.12753  101.7972017
| > Dirty Price        100.922 107.66829  102.3593146
| > Accrued Amount       0.000   1.54076    0.5621129
| > Previous Coupon         NA   0.04500    0.0288625
| > Next Coupon             NA   0.04500    0.0342984
| >
| > Sample indirect computations (for the floating rate bond):
| > Yield to Clean Price: [1] 101.797
| > Clean Price to Yield: [1] 0.0220096
| > edd@max:~/svn/rquantlib/pkg/QuantLib$
| >
| >
| > [3] No Payer/Receiver enum for Vanilla Swap, other enums are defined:
| >
| > edd@max:~/svn/rquantlib/pkg/QuantLib$ grep Payer R/QuantLib.R
| > edd@max:~/svn/rquantlib/pkg/QuantLib$ grep Put R/QuantLib.R
| >                         'Put' = -1,
| > edd@max:~/svn/rquantlib/pkg/QuantLib$
| >
| > --
| > Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com
| >
| > ------------------------------------------------------------------------------
| > How ServiceNow helps IT people transform IT departments:
| > 1. A cloud service to automate IT design, transition and operations
| > 2. Dashboards that offer high-level views of enterprise services
| > 3. A single system of record for all IT processes
| > http://p.sf.net/sfu/servicenow-d2d-j
| > _______________________________________________
| > QuantLib-dev mailing list
| > [hidden email]
| > https://lists.sourceforge.net/lists/listinfo/quantlib-dev

--
Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com

------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev