Friday, 16 June 2023

Integrate over multiple limits at once using Quadpy

My goal is to integrate over multiple limits at once using quadpy. The documentation says the following:

quadpy is fully vectorized, so if you like to compute the integral of a function on many domains at once, you can provide them all in one integrate() call, e.g.,

However, its still unclear to me which quadpy-function I should use for this objective. My script looks as follows:

import quadpy
import numpy as np


class Kelly:
    def __init__(self):
        self.odds = 1.952
        self.kelly = 0.08961344537815132
        self.i = 0.001
        self.f = np.arange(0, 1 + self.i, self.i).flatten()
        self.c1 = 1
        self.c2 = 2
        self.k = 1.5

    def loss_function(self, p):
        p = p[:, 0]
        loss_function = np.where(p[:, None] < (self.f * self.odds - 1) + 1 / self.odds,
                                 (self.c1 + self.c2) * abs(self.f - self.kelly) ** self.k, 0)
        return loss_function

    def integrate(self):
        xmin = np.zeros(len(self.f))
        xmax = np.array([self.f * (self.odds - 1) + 1 / self.odds]).flatten()

        # vals, errors = quadpy.quad(self.loss_function, xmin, xmax)
        
        return vals, errors


kelly = Kelly()
vals, errors = kelly.integrate()
print(vals, errors)

This results in the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Please advice



from Integrate over multiple limits at once using Quadpy

No comments:

Post a Comment