1

I have defined a custom continuous distribution, with implemented _pdf and _cdf methods. For these I need to compute expensive constants (given the parameters), namely the normalization constant for the PDF and the integration constant for the CDF. These constants are computed every time any of the functions of the (frozen) random variable are evaluated, which takes a lot of time.

I was wondering if there is a way to precompute or cache/memoize these expensive constant for frozen random variables in SciPy?

Here is some minimal code of an example distribution with parameters a and b defined by non-normalized PDF f(x, a, b) and its antiderivative F(x, a, b). The expensive functions are the norm N(a,b) and the integration constant C(a,b):

from scipy.stats import rv_continuous


class Example_gen(rv_continuous):

    def _norm(self, a, b):
        """Expensive function"""
        return N(a, b)

    def _C(self, a, b):
        """Expensive function"""
        return C(a, b)

    def _pdf(self, x, a, b):
        return f(x, a, b) / self._norm(a, b)

    def _cdf(self, x, a, b):
        return (F(x, a, b) + self._C(a, b)) / self._norm(a, b)


Example = Example_gen()
1
  • 1
    I'm too dull but I can't help if you don't show at least some code. Commented Dec 22, 2023 at 8:35

2 Answers 2

1

How about caching them locally? You could have a dictionary, with key being tuple of values.

Along the lines (untested)

class Example_gen(rv_continuous):

    _n_cache = dict()
    _C_cache = dict()

    def _norm(self, a, b):
        """Expensive function"""
        key = (round(a,5), round(b,5))
        v = _n_cache.get(key) 
        if v is None:
            v = N(a, b)
            _n_cache[key] = v              
        
        return v

    def _C(self, a, b):
        """Expensive function"""
        key = (round(a,5), round(b,5))
        v = _C_cache.get(key) 
        if v is None:
            v = C(a, b)
            _C_cache[key] = v              
        
        return v

You could initialize cache dictionaries by, say, reading some JSON files or pickles.

Sign up to request clarification or add additional context in comments.

Comments

0

There isn't. Of course, if the specific distribution you think about defines some internal functions, you came monkey-patch them to return precomputed values. But this is certainly not supported by the framework, and you're on your own if you do that.

Comments

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.