Search code examples
pythonscipysymfit

Scipy distributions on symfit?


I think the title is self explaining.

I really want to use several pdfs already implemented on scipy.stats as models for a symfit model, e.g., CrystalBall or Johnson functions. I have tried with a gaussian distribution with the following code:

x = Variable('x') 
mu = Parameter('mu') 
sigma = Parameter('sigma') 
model_sci = stats.norm.pdf(y, mean, sigma)

But I get the following TypeError

TypeError: cannot determine truth value of Relational

I believe it is because scipy distribution expects numbers (or iterables with numbers) instead of the symbol produced by sympy. Is there a possible hack to uses this distributions and not implement them by hand?


Solution

  • It is possible to do this using a CallableNumericalModel:

    x = Variable('x') 
    y = Variable('y') 
    mu = Parameter('mu') 
    sigma = Parameter('sigma') 
    
    model_sci = lambda x, mu, sigma: stats.norm.pdf(x, mu, sigma)
    
    model = CallableNumericalModel({y: model_sci}, connectivity_mapping={y: {x, mu, sigma}})