1

I am trying to fit two data sets by two model functions. I tried to do so using symfit. Here the code:

from symfit import variables, parameters, Fit, cos, sin, pi, sqrt, asin
import numpy as np
n0 = 1.5

data = np.genfromtxt('some data')
data = 1000 * data

pos=[]
for j in range( len(data) ): 
    pos.append( np.arcsin( np.sin( np.deg2rad( data[j,0]/1000 ) )/1.5 ) )

m1=[]
for j in range( len(data) ): 
    m1.append( data[j,1] ) 

p1=[]
for j in range( len(data) ): 
    p1.append(data[j,3]) 

zero=[]
for j in range( len(data) ): 
    zero.append(data[j,5]) 

y0, lam, d0, deltan, per = parameters('y0, lam, d0, deltan, per')
theta, rM1, rP1 = variables('theta, rM1, rP1')

model_dict={
    rM1: y0+((pi*deltan*d0)/(lam*cos(theta)))**2.*sin(sqrt(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(-asin(lam/(2*per*n0))-theta))/per)**2.))**2./(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(-asin(lam/(2*per*n0))-theta))/per)**2.),
    rP1: y0+((pi*deltan*d0)/(lam*cos(theta)))**2.*sin(sqrt(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(asin(lam/(2*per*n0))-theta))/per)**2.))**2./(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(asin(lam/(2*per*n0))-theta))/per)**2.)
 }


fit = Fit(model_dict, theta=pos, rM1=m1, rP1=p1)
fit_result = fit.execute()
print(fit_result)

But, I get the following error:

AttributeError: 'Variable' object has no attribute 'cos'

If I remove the cos function, then I will get other similar error concerning sqrt and sin etc. I cannot figure out what is wrong with the code!

PS: After using symfit.cos etc., I get the following results:

/anaconda2/lib/python2.7/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in arcsin

as well as

/anaconda2/lib/python2.7/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in sqrt

and the output is:

Parameter Value        Standard Deviation
d0        1.727015e+00 nan
deltan    1.880867e-02 3.534201e-03
lam       3.890715e-01 nan
per       6.123022e-01 nan
y0        -1.686541e-03 4.810316e-03
Fitting status message: Desired error not necessarily achieved due to precision loss.
Number of iterations:   105
Regression Coefficient: 0.100163679536

And after adding standard deviations, I got:

Parameter Value        Standard Deviation
d0        nan nan
deltan    nan nan
lam       nan nan
per       nan nan
y0        nan nan
Fitting status message: Desired error not necessarily achieved due to precision loss.
Number of iterations:   112
Regression Coefficient: nan 
Simd
  • 19,447
  • 42
  • 136
  • 271
Rose
  • 21
  • 1
  • 4
  • try "np.cos", "np.sqrt", "np.sin", etc. – James Phillips Sep 03 '18 at 11:03
  • 3
    @JamesPhillips ...not sure if this is the right way to go. As this uses sympy it probably needs symbolic derivatives. – mikuszefski Sep 03 '18 at 13:00
  • 1
    ...please correct the indents (for readability). The complete error message looks like (which line is responsible)? Is it possible to make a minimum example without a model three screens wide? – mikuszefski Sep 03 '18 at 13:03
  • As a test, try "import sympy" and directly use "sympy.sin", "sympy.cos", etc. – James Phillips Sep 03 '18 at 15:16
  • Perhaps you can start from 'model_dict={rM1: cos(theta), rP1: cos(theta)}', and add complexity from there until you get failure, because indeed this model is hard to read in its current form. Then when you find out where the problem is you might already be able to fix it, or you can post that as a minimal example. – tBuLi Sep 03 '18 at 16:16
  • @tBuLi I replaced my `model_dict` by the model function you have written. I get the same error! – Rose Sep 03 '18 at 17:51
  • 1
    @JamesPhillips I tried your suggestion. It worked! – Rose Sep 03 '18 at 18:01
  • The new problem: I get the following error: `/anaconda2/lib/python2.7/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in arcsin`. And also for `sqrt` function. – Rose Sep 03 '18 at 18:02
  • @tBuLi How can I add two sigmas? Does `fit = Fit(model_dict, theta=pos, rM1=m1, rP1=p1, sigmarM1, sigmarP1)` work? – Rose Sep 03 '18 at 18:10
  • For the invalid value, I suggest making a test where the arcsin and sqrt are on separate lines, which allows you to fix the problems one at a time. For troubleshooting, you can print the values passed to these functions and then determine why those values are invalid. – James Phillips Sep 03 '18 at 18:45
  • Glad the original issue is now solved. Please note that the new one is not an error, it is a warning. Probably you took the square root of a negative number, so the warning is justified ;). – tBuLi Sep 03 '18 at 20:13
  • In order to add standard deviations, use keyword names 'sigma_rP1=sigmarP1' when calling Fit. – tBuLi Sep 03 '18 at 20:15
  • Can you update your code accordingly? The model is still the complicated one and indentation of `for` loops is still wrong. Concerning the warning and as mentioned by the others: If you have a function like `asin` or `sqrt` that can only take a certain region of values while the fitting parameter is free to take every real value, you can run into trouble. There are several ways to avoid this, by constrains e.g or if you know that in `sqrt( a * x**2 + b )` the `a` and `b` must be positive, fit `sqrt( a**2 * x**2 + b**2 )` instead, with according error propagation afterwards. – mikuszefski Sep 04 '18 at 06:44
  • @tBuLi I have added the standard deviations. The result is even worse! I will add it in the edited version of the OP. – Rose Sep 04 '18 at 08:58
  • @mikuszefski Nice hint! I will try to test it. Concerning the indentation, I have written all the code after 4 spaces. Don't know why it looks like that! – Rose Sep 04 '18 at 09:00
  • @Rose, at this point I think you should be critical of your data and/or models. The code is now working, so this is no longer a coding problem but a data analysis problem :). Try plotting your data and model to do a visual inspection of what could be wrong. These 'nan' are probably caused by evaluating a function such as `asin` or `sqrt` with an illegal argument. Read from [here](https://symfit.readthedocs.io/en/latest/tutorial.html#evaluating-the-model) to the end of that page on how to evaluate and plot `symfit` models. – tBuLi Sep 04 '18 at 09:57

0 Answers0