Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpy to tensor #86

Open
divagora opened this issue Mar 9, 2023 · 1 comment
Open

rpy to tensor #86

divagora opened this issue Mar 9, 2023 · 1 comment

Comments

@divagora
Copy link

divagora commented Mar 9, 2023

I'm beginner. Im running a MC integration to compute the CDF of a vinecopula model (involving 6 different variables). However, I have a lack of speed and efficency in the process. Tackling it, I'm trying to perform my model using VEGASFLOW, but I'm not sure how can I convert a rpy object to tensor, and also how can I set the limits of integration .

Here is my code using VEGAS:

`@vegas.batchintegrand
def integrate_fun(k): # Calculate VineCopula pdf
pdf_vine = np.array(VineCopula.RVinePDF(k, θ))
return pdf_vine

def vegas_6D(θ,u,v,w,x,y,z):
integ = vegas.Integrator([[0, u], [0, v], [0, w], [0, x], [0, y], [0, z]])
result = integ(integrate_fun, nitn = 10, neval = 10000)
return result.mean`

where u,v,w,x,y,z are: np. linspace between 0 and 1

Thank you in advance for your help

@scarlehoff
Copy link
Member

I don't think for this case in particular you will see a gain of performance by using VegasFlow. VegasFlow is most useful when you whole problem can be cast as a set of tensorflow primitives (or more generally, tensors). In your case I see you are using an R package so I believe you won't see any advantages with respect to vegas (unless you aim to use the parallelization over function returns see docs)

But here you have some answers to your questions:

In order to change any object to a tensor, you can follow the same strategy you are following but instead of using np.array you can use tf.constant. Another possibility is to enable eager mode so that you can integrate an arbitrary numpy function. See docs for that: https://vegasflow.readthedocs.io/en/latest/examples.html#integrating-a-numpy-function

In order to change the integration limits (see docs https://vegasflow.readthedocs.io/en/latest/how_to.html#changing-the-integration-limits) you will need to use the xmin and xmax limits, for instance if I want to integrate between (0,1) the first dimension and between (-4, 10) the second one I would do:

from vegasflow import VegasFlow

dimensions = 2
vegas_instance = VegasFlow(dimensions, n_calls, xmin=[0, -4], xmax=[1, 10])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants