-
Notifications
You must be signed in to change notification settings - Fork 19
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
symfit stdev are NaN #340
Comments
There could be many causes. Did you do a visual inspection of the resulting fit? Does the resulting fit have a covariance matrix ( |
Indeed, there was a problem with my model, I found a mistake there, but after corrections the error remains. Here is the current code: from symfit import parameters, variables, Fit, Model, exp
import numpy as np
xdata = np.array([0.25, 0.5, 0.8, 1, 1.5, 2.5, 4, 6])
ydata = np.array([0.01389280763166483, 0.010951313911009092, 0.008881284120217327, 0.008367037366939192, 0.006991931576970159, 0.005906215242754372, 0.004687694177322324, 0.003921013582101654])
ydata1 = np.array([0.013222900783408978, 0.009442058970360209, 0.007346568319997657, 0.006988578383538889, 0.005517204596638713, 0.004189095685860909, 0.003153282844562755, 0.002626529796610057])
Rbounda, Rboundb, Ka, Kb = parameters('Rbounda, Rboundb, Ka, Kb')
L0, Robsa, Robsb = variables('L0, Robsa, Robsb')
s = 1.3
P0 = 0.01
Rfreea = 0.0008064818334553477
Rfreeb = 0.0005987646455598504
a = s*Kb + 2*Ka + (Ka**2/(s*Kb))
b = -(L0*s*Kb + P0*s*Kb + Ka*P0 + Ka*L0 + Ka*Kb + Ka*Kb*s)
c = P0*L0*s*Kb
delta = b**2 - 4*a*c
sdelta = delta ** (1/2)
ca = (-b - sdelta)/(2*a)
La = s*((L0-ca*(1+(Ka/(s*Kb))))/(1+s))
Lb = La/s
cb = (Ka*ca)/(Kb*s)
model1 = Model({
Robsa: Rfreea * La/(La+ca) + Rbounda * ca/(La+ca),
Robsb: Rfreeb * Lb/(Lb+cb) + Rboundb * cb/(Lb+cb),
})
fit = Fit(model1, L0=xdata, Robsa=ydata, Robsb=ydata1)
fit_result = fit.execute()
print(fit_result)
print('covariance_matrix', fit_result.covariance_matrix)
hess = fit_result.objective.eval_hessian(**fit_result.params)
print('hess', hess)
print('invert_hess', np.linalg.inv(hess)) And the current result, together with covariance matrix, hessian and inversion of the hessian, is below:
I can see that some diagonal elements of the covariance matrix are negative, this is strange, right? |
In my previous post there was some problem with code formatting, now it should be OK: from symfit import parameters, variables, Fit, Model, exp
import numpy as np
xdata = np.array([0.25, 0.5, 0.8, 1, 1.5, 2.5, 4, 6])
ydata = np.array([0.01389280763166483, 0.010951313911009092, 0.008881284120217327, 0.008367037366939192, 0.006991931576970159, 0.005906215242754372, 0.004687694177322324, 0.003921013582101654])
ydata1 = np.array([0.013222900783408978, 0.009442058970360209, 0.007346568319997657, 0.006988578383538889, 0.005517204596638713, 0.004189095685860909, 0.003153282844562755, 0.002626529796610057])
Rbounda, Rboundb, Ka, Kb = parameters('Rbounda, Rboundb, Ka, Kb')
L0, Robsa, Robsb = variables('L0, Robsa, Robsb')
s = 1.3
P0 = 0.01
Rfreea = 0.0008064818334553477
Rfreeb = 0.0005987646455598504
a = s*Kb + 2*Ka + (Ka**2/(s*Kb))
b = -(L0*s*Kb + P0*s*Kb + Ka*P0 + Ka*L0 + Ka*Kb + Ka*Kb*s)
c = P0*L0*s*Kb
delta = b**2 - 4*a*c
sdelta = delta ** (1/2)
ca = (-b - sdelta)/(2*a)
La = s*((L0-ca*(1+(Ka/(s*Kb))))/(1+s))
Lb = La/s
cb = (Ka*ca)/(Kb*s)
model1 = Model({
Robsa: Rfreea * La/(La+ca) + Rbounda * ca/(La+ca),
Robsb: Rfreeb * Lb/(Lb+cb) + Rboundb * cb/(Lb+cb),
})
fit = Fit(model1, L0=xdata, Robsa=ydata, Robsb=ydata1)
fit_result = fit.execute()
print(fit_result)
print('covariance_matrix', fit_result.covariance_matrix)
hess = fit_result.objective.eval_hessian(**fit_result.params)
print('hess', hess)
print('invert_hess', np.linalg.inv(hess)) |
Well, I don't know why inserting the code does not work for me now, I hope you will understand it anyway. |
Did you do a visual inspection of this fit? :) ALWAYS look at fits, don't be complacent and settle for e.g. R^2 [1]. Given that you are surprised by k having such different values, do you have an ballpark idea for the value they should have? If so, set those as initial values ( See https://stackoverflow.com/questions/28702631/scipy-curve-fit-returns-negative-variance for more details. [1] https://en.wikipedia.org/wiki/Anscombe%27s_quartet PS. code should be in so-called backticks (`), next to the number 1 on your keyboard. |
Here are the plots of the experimental points together with the fits: The orange fit looks pretty fine, but the blue one deviates from data for larger 'x'. Still Kb is 10^5 larger than Ka. |
Your initial guesses are not good (enough):
Note that the errors I get for Kb and Rboundb are both very large, which should prompt you to take a critical look at your model. |
Hi,
I have a problem with global fitting of two data sets (with four fitting parameters) with symfit Fit function, which gives me the fitting results, but all the standard deviations are nan.
Here is my code:
(i know that my model is a bit complicated...)
And here is the results that I get:
RuntimeWarning: invalid value encountered in sqrt
return np.sqrt(self.variance(param))
Parameter Value Standard Deviation
Ka 8.842217e-01 nan
Kb 7.191712e-01 nan
Ra 2.699504e+00 nan
Rb 2.496971e+00 nan
Status message Optimization terminated successfully.
Number of iterations 41
Objective <symfit.core.objectives.LeastSquares object at 0x7f926fc8aa90>
Minimizer <symfit.core.minimizers.BFGS object at 0x7f926fc3c8d0>
Goodness of fit qualifiers:
chi_squared 6.016628549760758e-06
objective_value 3.008314274880379e-06
r_squared 0.9634526939817522
I will be grateful for any help!
The text was updated successfully, but these errors were encountered: