RQuantlib and Mac OS X 10.8.2

2019-08-06 07:47发布

问题:

I'm a total newbie in Mac OS X, R and C++. Sound like a good mix, doesn't it?

I have the need to use RQuantLib, because I want to use some pricing functions part of the QuantLib package inside R, all on a Mac OS X-powered environment.

I've correctly installed QuantLib. I've already asked to the official QuantLib mailing list, and together we seem to have reached the conclusion that the problems I'm encountering are not related to my QuantLib installation, which seems ok and correctly configured.

So, I turned to R to try and solve the problem. Whenever I try to run ZeroCouponBond from within R, copying and pasting the first example provided with the official documentation, I get the following error:

"Error in DiscountCurve.default(discountCurve.param, list(flat = 0.05)) : 
cannot find function errorOccured"

Now, I would rule any syntax.related problem out, since I'm copying the very same example present in the official help.

I don't know what I did wrong, but I know I need to find a solution at all costs. I've installed Rcpp, and the configuration seems really ok. Just one question I was not able to find an answert to: in my understanding, RQuantLib basically acts as a link between QuantLib and R. If that's correct, how can I tell RQuantLib where to look to find libQuantLib.a, that is, the compiled library resulting from the "make && sudo make install" commands performed while installing QuantLib itself?

回答1:

Right, so, I've finally managed to get it to work.

First of all, I would like to say that things would have been much easier if a thorough, step-by-step installation procedure had been provided. I acknowledge I'm a total newbie, but I think other people approaching to R for the first time might encounter difficulties similar to those I had to overcome.

Anyway, this is what I did:

  • I've downloaded the .tar.gz source packages for both Rcpp and RQuantLib from cran.r-project.org
  • I've compiled them installing them from within the R environment. This is where I was making a mistake. Indeed, I was trying to compile them by invoking the configure installation script from the terminal; however, as Dirk said, the config script looks for QuantLib's quantlib-config script, and I didn't know the correct syntax to tell the configure script the correct path to QuantLib. Executing the procedure from R (by just installing the package) sorts out any problem, as all the dependencies are correctly located and loaded

So, that's pretty much it: just install the .tar.gz source package as you would do with the binary version, and everything should work ok.

Of course, I'm still curious to understand:

  • If it is possible to compile Rcpp and RQuantLib from the terminal; and
  • Why the binary version for Mac OS X will not work on my system, ie: why do I have to compile starting from the source code?

Thank you so much to anyone willing to answer my (probably naive and silly) questions. I'm eager to understand a bit more!

Thanks!



回答2:

The RQuantLib package uses a tool called configure which determines the patch at package build-time. It looks for the script quantlib-config from which it learns about the location of libQuantLib.a.



回答3:

First, install boost (brew install boost) and, secondly, Quantlib (currently at 1.7.1) by following instructions at http://quantlib.org/install/macosx.shtml:

cd QuantLib-1.x.y
./configure --enable-static --with-boost-include=/opt/local/include/ \
            --with-boost-lib=/opt/local/lib/ --prefix=/opt/local/
make && sudo make install

It takes some time (~1 hour) for make.

Then in R or Rstudio, install packages Rcpp and RQuantlib. The later requires type="source" since only source package is available.

At this point, you should be able to use RQuantlib. The American Option value (SPY as of 4/1/2016, maturity 7/15/2016) can be calculated in R as

AmericanOption("put", strike=206, volatility=0.1525, underlying = 206.92, 0.021, 0.003, 73/252, engine="CrankNicolson")