通过Rcpp从R调用QuantLib [英] Calling QuantLib from R through Rcpp

查看:201
本文介绍了通过Rcpp从R调用QuantLib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预备步骤



QuantLib 升级 一起安装并根据Microsoft Visual C ++ 2010中的 这些 指示创建;测试代码没有问题。



使用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

的单独C ++文件,以下示例

  #include< Rcpp.h> 
使用命名空间Rcpp;

//下面是一个将C ++函数导出为R的简单例子。
//使用Rcpp :: sourceCpp
//将这个函数导入R会话函数(或通过编辑器工具栏上的Source按钮)

//有关使用Rcpp的更多信息,请单击编辑器工具栏上的帮助按钮

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

已成功完成 R

 > timesTwo(7)
[1] 14



我想一切都很好。 p>

我的问题



如果我的设置正确,我的问题是: code> QuantLib-vc100-mt-gd.lib 目标文件库在 C:\DevTools\QuantLib-1.3 \lib ,如果从 R ?<$ p

<$ p>调用,我应该怎么做, $ p> #include< ql / quantlib.hpp>
#include< Rcpp.h>
使用命名空间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;
}


解决方案

请参阅Rcpp常见问题一般的问题我可以使用R和Rcpp与Visual Studio(tl; dr:不,你不能)。



但在Rcpp之前有已经RQuantLib,它仍然存在。下载其来源,从extras网站下载quantlib-1.4.zip在牛津,只需重新构建RQuantLib。它使用Rcpp。



然后你可以扩展RQuantLib到你的心的内容。



最新的RQuantLib也有一个类似于RcppArmadillo和RcppEigen的插件,所以你可以建立快速的小测试文件,像你发布。

我给了一个去。使用当前的RQuantLib(0.3.12)和Rcpp(0.11.1,今天发布,但0.11.0应该工作)和您的文件保存在 /tmp/lisaann.cpp 这just works:

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

如果在Windows上失败,请确保




  • 安装的Rtools

  • 预先构建的QuantLib供R使用(参见我最近的博文

  • 设置了环境变量 src / Makevars.win 预期



否则只需使用Ubuntu,Debian或任何其他

c $ c> [[Rcpp :: depends()]] 属性添加到您的代码。下面是我使用的文件:

  #include< ql / quantlib.hpp> 
#include< Rcpp.h>
使用命名空间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;
}

这里。


Preliminary steps

QuantLib was installed along with Boost and built following these instructions in Microsoft Visual C++ 2010; test code went on with no issues.

Using R with the following sample code gave the expected results:

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

#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

> 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?

#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).

But before there was Rcpp there was already RQuantLib, and it still exists. Download its sources, download the quantlib-1.4.zip from the 'extras' site in Oxford and just rebuild RQuantLib with it. Which uses Rcpp.

You can then extend RQuantLib to your heart's content.

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. 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":

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

  • Rtools installed
  • the pre-build QuantLib for use by R (see my recent blog post)
  • have the environment variables set which src/Makevars.win expects

Else, just use Ubuntu, Debian, or any other sane OS in a virtual machine.

Edit 2: One important part, though, is that the [[ Rcpp::depends() ]] attribute is added to your code. 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.

这篇关于通过Rcpp从R调用QuantLib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆