简体   繁体   English

从R到Rcpp调用QuantLib

[英]Calling QuantLib from R through Rcpp

Preliminary steps 初步步骤

QuantLib was installed along with Boost and built following these instructions in Microsoft Visual C++ 2010; QuantLibBoost一起安装,并按照Microsoft Visual C ++ 2010中的这些说明构建; test code went on with no issues. 测试代码继续没有问题。

Using R with the following sample code gave the expected results: 使用R和以下示例代码给出了预期的结果:

install.packages("Rcpp")
library(Rcpp)

cppFunction('
  int add(int x, int y, int z) {
    int sum = x + y + z;
    return sum;
  }'
)

add(1, 2, 3)
# > add(1, 2, 3)
# [1] 6

As of the use of separate C++ files, the example below 截至使用单独的C ++文件,下面的示例

#include <Rcpp.h>
using namespace Rcpp;

// Below is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp 
// function (or via the Source button on the editor toolbar)

// For more on using Rcpp click the Help button on the editor toolbar

// [[Rcpp::export]]
int timesTwo(int x) {
   return x * 2;
}

succeded for the result in R was 成功结果R

> timesTwo(7)
[1] 14

I guess that everything is fine. 我想一切都很好。

My question 我的问题

If my setup is correct, my question is: assuming my QuantLib-vc100-mt-gd.lib object file library is in C:\\DevTools\\QuantLib-1.3\\lib , what should I do to make something like the code below to work properly if called from R ? 如果我的设置是正确的,我的问题是:假设我的QuantLib-vc100-mt-gd.lib对象文件库在C:\\DevTools\\QuantLib-1.3\\lib ,我应该怎么做才能使下面的代码工作从R调用时是否正确?

#include <ql/quantlib.hpp>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double timesTwo(double x) {
  QuantLib::Calendar myCal = QuantLib::UnitedKingdom();
  QuantLib::Date newYearsEve(31, QuantLib::Dec, 2008);
  QuantLib::Rate zc3mQuote = x;
  return zc3mQuote * 2;
}

Please see the Rcpp FAQ for the general question of 'can I use R and Rcpp with Visual Studio' (tl;dr: No, you can't). 请参阅Rcpp常见问题解答“我可以在Visual Studio中使用R和Rcpp”的一般问题(tl;博士:不,你不能)。

But before there was Rcpp there was already RQuantLib, and it still exists. 但是在Rcpp之前已经存在RQuantLib,它仍然存在。 Download its sources, download the quantlib-1.4.zip from the 'extras' site in Oxford and just rebuild RQuantLib with it. 下载其源代码,从牛津大学的'extras'站点下载quantlib-1.4.zip,然后用它重建RQuantLib。 Which uses Rcpp. 其中使用Rcpp。

You can then extend RQuantLib to your heart's content. 然后,您可以将RQuantLib扩展到您的内容。

The newest RQuantLib also has a plugin similar to what RcppArmadillo and RcppEigen have, so you can build the quick little test files like the one you posted. 最新的RQuantLib也有一个类似于RcppArmadillo和RcppEigen的插件,所以你可以像你发布的那样构建快速的小测试文件。 I will try to follow-up on the weekend with an existence proof example. 我会尝试在周末跟进一个存在证明的例子。

Edit : As promised, I gave that a go. 编辑按照承诺,我放弃了。 With the current RQuantLib (0.3.12) and Rcpp (0.11.1, released today but 0.11.0 should work) and your file save in /tmp/lisaann.cpp this "just works": 使用当前的RQuantLib(0.3.12)和Rcpp(0.11.1,今天发布,但0.11.0应该可以工作)和你的文件保存在/tmp/lisaann.cpp这个“正常工作”:

R> library(Rcpp)
R> sourceCpp("/tmp/lisaann.cpp")
R> timesTwo(1.23)
[1] 2.46
R> 

If it fails for you on Windows, make sure have 如果它在Windows上失败了,请确保有

  • Rtools installed 安装了Rtools
  • the pre-build QuantLib for use by R (see my recent blog post ) 由R使用的预构建QuantLib(参见我最近的博客文章
  • have the environment variables set which src/Makevars.win expects 设置src/Makevars.win期望的环境变量

Else, just use Ubuntu, Debian, or any other sane OS in a virtual machine. 另外,只需在虚拟机中使用Ubuntu,Debian或任何其他理智的操作系统。

Edit 2: One important part, though, is that the [[ Rcpp::depends() ]] attribute is added to your code. 编辑2:但重要的一点是, [[ Rcpp::depends() ]]属性被添加到您的代码中。 With that, here is the file I used: 有了它,这是我使用的文件:

#include <ql/quantlib.hpp>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::depends(RQuantLib)]]

// [[Rcpp::export]]
double timesTwo(double x) {
  QuantLib::Calendar myCal = QuantLib::UnitedKingdom();
  QuantLib::Date newYearsEve(31, QuantLib::Dec, 2008);
  QuantLib::Rate zc3mQuote = x;
  return zc3mQuote * 2;
}

which differs from yours only in the (important!) reference to the plugin used here. 这与您在此处使用的插件的(重要!)引用不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM