1

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?

Horace
  • 62
  • 4

1 Answers1

1

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}})
tBuLi
  • 2,295
  • 2
  • 16
  • 16
  • It worked like a charm! However, I really like to use SLSQP minimizer, but when I use it, the minimization does not return good fitted parameters. For example, when I fit only with objective=Likelihood, I get mu=0.008 and sigma=1.00 (which is nice since the data comes from np.random.normal(0, 1, 1000)). But when I useobjective=Likelihood and minimizer=SLSQP, I got mu=-3.596625e+01 and sigma=-2.204207e+01. Do you know why this happens? Cheers – Horace Nov 27 '20 at 14:39
  • 1
    So I tried to add limits on the Parameters and the fitted parameters are looking good, I will leave the last comment so if any one faces the same problem can look at my silly mistake and possibly correct their own – Horace Nov 27 '20 at 15:06