Login  Register

QuantLib R package from SWIG code

Posted by Dirk Eddelbuettel on Jun 04, 2013; 1:06am
URL: http://quantlib.414.s1.nabble.com/QuantLib-R-package-from-SWIG-code-tp14318.html


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