0

I would like to write a Python wrapper around the R library Rdtq (https://github.com/cran/Rdtq). That library (or rather, the class instance) takes as main inputs two functions: the drift f(x) and diffusion g(x). For instance,

my_drift = function(x) { -x }
my_diff  = function(x) { rep(1,length(x)) }

Since I am writing a wrapper around the Rdtq class, I would like to pass the drift and diffusion function directly from Python, ideally via lambda function

my_python_drift(x) = lambda x: -x
my_python_diff(x)  = lambda x: np.ones(len(x))

and so on. So more generally, my question is: Can I pass a Python lambda (or global) function as parameter to R, via rpy2?

2
  • This is an XY problem. You are telling us only your Y solution but not explaining the X problem. Please give the true, complete scenario with specificity or reproducible example. Though you may think so, you might need lambda. Commented Aug 16, 2017 at 2:33
  • Fair enough, I have adjusted the question. Commented Aug 16, 2017 at 3:41

1 Answer 1

1

Consider using rpy2's SignatureTranslatedAnonymousPackage (STAP) to import arbitrary R code as available package in Python environment. To demonstrate, below translates the Rdtq github written in R to Python using rpy2:

R

# Loading required package: Rdtq
require(Rdtq)

# Assigning drift and diff functions
mydrift = function(x) { -x }
mydiff = function(x) { rep(1,length(x)) }

# Running rdtq()
test = rdtq(h=0.1, k=0.01, bigm=250, init=0, fT=1,
            drift=mydrift, diffusion=mydiff, method="sparse")

# Plotting output
plot(test$xvec, test$pdf, type='l')

Python

from rpy2 import robjects 
from rpy2.robjects.packages import STAP
from rpy2.robjects.packages import importr

# Loading required package: Rdtq
Rdtq = importr('Rdtq')

fct_string = """
my_drift <- function(x) { -x }
my_diff  <- function(x) { rep(1,length(x)) }
"""

# Creating package with above drift and diff methods
my_fcts = STAP(fct_string, "my_fcts")

# Running rdtq() --notice per Python's model: all methods are period qualified
test = Rdtq.rdtq(h=0.1, k=0.01, bigm=250, init=0, fT=1,
                 drift=my_fcts.my_drift(), diffusion=my_fcts.my_diff(), method="sparse")

# Load plot function
plot = robjects.r.plot

# Plotting by name index
plot(test[test.names.index('xvec')], test[test.names.index('pdf')], type='l')
Sign up to request clarification or add additional context in comments.

3 Comments

While this is indeed a nice workaround, it is not applicable to my problem. The functions 'my_drift' and 'my_diff' are generated at run-time in a non-trivial manner and I cannot provide them explicitly.
Not sure what that means or what you really are asking. Good luck!
I mean that I cannot specify the functions explicitly as string. When compiling the code, I do not know the shape of the function, it is determined automatically at runtime. Specifically, I have some random data as input, that data is then fitted and integrated. This gives the drift function I am interested in. This explains why I need to pass the function as an object, and I cannot write the function explicitly as a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.