lmfit and scipy curve_fit return initial guesses as best-fitted parameters - curve-fitting

I want to fit a function to some data and I’ m facing a problem. I’ ve tried to use lmfit or curve_fit from scipy. Below I describe the problem.
Here is my data:
dataOT = pd.read_csv("KIC3239945e.csv", sep=';')
t=dataOT['time']
y=dataOT['flux']
Also, here is the model-function to be fitted to the data:
def model(t, Rp, Rs, a, orb_inclination, Rin, Rout, tau):
gps=Rp/Rs
gis=Rin/Rs
gos=Rout/Rs
Agps=A(t, gps, Rp, Rs, a, orb_inclination, Rin, Rout)
Agos=A(t, gos, Rp, Rs, a, orb_inclination, Rin, Rout)
Agis=A(t, gis, Rp, Rs, a, orb_inclination, Rin, Rout)
return (np.pi*(1-u1/3-u2/6)-Agps-(1-np.exp(-tau))*(Agos-Agis))/(np.pi*(1-u1/3-u2/6))
where u1, u2 are known numbers and the parameters to be fitted are: Rp, Rs, a, orb_inclination, Rin, Rout, tau and they are contained in the quantities Agps, Agos, Agis.
Here is the definition of function A:
def A(t, gamma, Rp, Rs, a, orb_inclination, Rin, Rout):
Xp,Zp= planetary_position(t, a, orb_inclination)
return np.where(rho(Xp,Zp,Rs)<1-gamma,
np.pi*gamma**2*(1-u1-u2*(2-rho(Xp,Zp,Rs)**2-gamma**2/2)+(u1+2*u2)*W11(Xp,Zp,gamma,Rs) ) ,
np.where(np.logical_and( (1-gamma<=rho(Xp,Zp,Rs)), (rho(Xp,Zp,Rs)<=1+gamma) ),
(1-u1-3*u2/2)*(gamma**2*np.arccos(zeta(Xp,Zp,gamma,Rs)/gamma)+np.arcsin(zo(Xp,Zp,gamma,Rs))-rho(Xp,Zp,Rs)*zo(Xp,Zp,gamma,Rs))+(u2/2)*rho(Xp,Zp,Rs)*((rho(Xp,Zp,Rs)+2*zeta(Xp,Zp,gamma,Rs))*gamma**2*np.arccos(zeta(Xp,Zp,gamma,Rs)/gamma)-zo(Xp,Zp,gamma,Rs)*(rho(Xp,Zp,Rs)*zeta(Xp,Zp,gamma,Rs)+2*gamma**2)) +(u1+2*u2)*W3(Xp,Zp,gamma,Rs) , 0))
1st attempt: curve_fit
from scipy.optimize import curve_fit
p0=[4.5*10**9, 4.3*10**10, 1.4*10**13, 1.2, 4.5*10**9, 13.5*10**9, 1]
popt, pcov = curve_fit(model, t, y, p0, bounds=((0, 0, 0, 0, 0, 0 ,0 ),(np.inf, np.inf, np.inf,np.inf, np.inf, np.inf ,np.inf )), maxfev=6000)
print(popt)
2nd attempt: lmfit
from lmfit import Parameters, Minimizer, report_fit, Model
gmodel=Model(model)
def residual(p,t, y):
Rp=p['Rp']
Rs=p['Rs']
a=p['a']
orb_inclination=p['orb_inclination']
Rin=p['Rin']
Rout=p['Rout']
tau=p['tau']
tmp = model(t, Rp, Rs, a, orb_inclination, Rin, Rout, tau)-y
return tmp
p = Parameters()
p.add('Rp' , value=0.000394786, min= 0,max=1)
p.add('Rs' , value=0.003221125, min= 0,max=1)
p.add('a', value=1.86, min= 0,max= 1)
p.add('orb_inclination', value=1, min= 0,max=4)
p.add('Rin', value=0.000394786, min= 0,max=1)
p.add('Rout', value=0.000394786, min= 0,max=1)
p.add('tau', value=0, min= 0,max=2)
mini = Minimizer(residual,params=p,fcn_args=(t,y))
out = mini.minimize(method='leastsq')
print(report_fit(out))
All cases return as best-fitted parameters the initial guesses. What should I do in order to make it work properly?
Note:Assuming that the parameters are known the model has the expected behavior (Figure 1), so I suppose that the model is well-defined and the problem is not related with this.
Any help would be appreciated. Thank you in advance!

I've got two ideas, and I think the first one is probably your culprit.
Using nan_policy = 'omit' seems to me to work in very specific cases. If you get the error message "nan_policy = 'omit' will probably not work" when trying to fit, well, it probably won't work. You could do a simple check for NaN values to confirm that the function outputs NaN values for your interval.
The bounds for the variables are huge. Try raising the minimum for the intervals.

Without real data and a complete example, it is very hard to guess what might be going wrong. So, this will include some advice on how to approach the problem.
First: since you are doing curve-fitting, and have a model function, I recommend starting with your 2nd version, using lmfit.Model. But, I would suggest explicitly making a set of Parameters, as with:
from lmfit import Model
def A(t, gamma, Rp, Rs, a, orb_inclination, Rin, Rout):
Alpha = np.zeros(len(t))
Xp, Zp = planetary_position(t, a, orb_inclination)
values_rho = rho(Xp, Zp, Rs)
v_W11 = W11(Xp,Zp, gamma, Rs)
v_W11 = pd.Series(v_W11)
v_zeta = zeta(Xp,Zp, gamma,Rs)
v_zo = zo(Xp, Zp, gamma,Rs)
v_W3 = W3(Xp,Zp, gamma,Rs)
for i in range(len(values_rho)):
Alpha[i]=np.where(values_rho[i]<1-gamma, np.pi*gamma**2*(1-u1-u2*(2-values_rho[i]**2-gamma**2/2)+(u1+2*u2)*v_W11[i] ) , np.where(((1-gamma<=values_rho[i]) and (values_rho[i]<=1+gamma)), (1-u1-3*u2/2)*(gamma**2*np.arccos(v_zeta[i]/gamma)+np.arcsin(v_zo[i])-values_rho[i]*v_zo[i])+(u2/2)*values_rho[i]*((values_rho[i]+2*v_zeta[i])*gamma**2*np.arccos(v_zeta[i]/gamma) -v_zo[i]*(values_rho[i]*v_zeta[i]+2*gamma**2))+(u1+2*u2)*v_W3[i] , 0))
return Alpha
model = Model(model)
params = model.make_params(Rp=4.5*10**9, Rs=4.3*10**10,
a=1.4*10**13, orb_inclination=1.2,
Rin=4.5*10**9, Rout=13.5*10**9, tau=1)
result = gmodel.fit(y, params, t=t)
print(result.fit_report())
By itself, this won't solve the problem, but clarity counts. But, you can call that model function yourself or do
gmodel.eval(params, t=t)
and see what it actually calculates for any set of parameter values.
Second: you should be cautious about have variables in a fitting problem that span many orders of magnitude. Have the variables be more like order 1 (or, well between order 1.e-6 and 1.e6), and then multiply by factors of 1e9 or 1e12 as appropriate - or just work in units with values closer to 1. The numerics of fitting are all in double precision floating point, and the relative values of the parameters matters.
Third: your model function, yikes. Readability count. Writing an incomprehensible function does not help anyone. Including you. I guarantee that you do not know what this does. For example, you might be able to avoid the loop and just use the ufunc-ness of numpy, but it is impossible to tell. And to be clear, it is impossible to tell because you wrote it this way. Like what the heck are u1 and u2 supposed to be? Really, this function did not exist and you wrote a complete mess and then something went wrong.
So: write your model function as if you expect to read it next year, and then test what it calculates with reasonable input values. When that works, the fit should work too.

I solved the problem by reducing the number of parameters. Also, another problem was that one of the parameters was not affecting the fitting at all.

Related

Resorting and holding a vector

So this is probably a very specific problem and i am not sure if it is even solvable but here we go:
I have a vector with 6 indices which each are variables. These variables get calculated separately. What I want now is that the order of the indices changes at a specific time and stays like that. But the actual value of the indices needs to keep being calculated. Maybe explaining it in my Modelica code helps with understanding.
I have a vector with six indices, made up out of 6 variables, let's name them A to F. A to F are each calculated in a different way which is (probably) not relevant here so I'm simply writing [...] for that here. They behave independently of each other.
Real Vector[6];
Real A;
Real B;
Real C;
Real D;
Real E;
Real F;
equation
A = 3*x;
B = 5-x +7/x ...;
C = [and so on]
D = [...]
E = [...];
F = [...];
Initially, the Vector is sorted like this:
Vector = {A, B, C, D, E, F};
But I want the order of the indices to be resorted via some if-clauses every 100 seconds (starting at time=0) which i make work like that:
when sample(0,100) then
Vector = {if xyz then A,
elseif xyz then B ....}
end when;
Again, the specific way in which i resort the indices (probably) doesn't matter because it definitely works.
My problem is: While it does resort my Vector every 100 seconds and holds this new order/sequence (which is exactly what i need), it of course also holds the calculated actual values of A to F at that time. Which means i get constant values between each time step.
What i need is the new order to hold but the values of A to F need to keep being calculated.
I also tried using if instead of when like
if time <100 then Vector = {A, B, C, D, E, F}
elseif time >=100 and <200 then Vector = {if xyz then A, elseif xyz then B ....(see above)}
else ...;
end if;
Problem here: it does also resort my Vector while also calculating A to F. But it looks to resort my vector all the time, not only once every 100 seconds --> holding the order until the next 100 seconds are over (the resorting is dependent on other calculated values in the model which are constantly changing).
My model is very huge so it's tricky to share all the parts that weave into this part of my work which is the reason i had to simplify my explanations as much as possible. I hope someone can help me with this.
I'm still relatively new at this and have been mostly teaching myself for the last few months so maybe I'm simply not aware of an easy obvious solution here. Or what I need is simply not doable in Modelica.
Thank you!
Not 100% sure I got the question correctly, but would the the graph below show what you need?
...with v being the original vector and vs being the continuously computed, but sorted (ascending every 100s) version of v.
This is the respective code:
model VectorSorting "Computes 'vs' every 100s from 'v' with acending order"
Real A, B, C, D, E, F; // Some variables computed in equations below
Real v[6]; // vector for A...F
Real vs[6]; // sorted version of 'v'
Integer i[6](start=1:6, fixed=true); // indexes of vector
Real d[6](start=zeros(6), fixed=true); // dummy variable
equation
A = time+200;
B = time-150;
C = 3*time-333;
D = 0.5*time+75;
E = -250;
F = 750;
v = {A, B, C, D, E, F};
vs = v[i];
when sample(0, 100) then
(d, i) = Modelica.Math.Vectors.sort(v);
end when;
annotation (experiment(StopTime=500), uses(Modelica(version="4.0.0")));
end VectorSorting;

maxima: How to transform a symbolic matrix by specific variables

In maxima I have calculated a complicated 2x2 matrix, let's call it "HUGE" and it has 8 variables in it:
a, b, c, d, tr11, tr12, tr21, tr22
This is not correct syntax, but just to illustrate:
Given
HUGE(a,b,c,d,tr11,tr12,tr21,tr22)=matrix([0,0],[0,0])
I want to factorize/transform it into
HUGE_NEW(a,b,c,d)=matrix([tr11,tr21],[tr12,tr22])
By writing things like HUGE_NEW(a,b,c,d) I mean a new matrix that contains variables a,b,c,d only.
In other words I want to say something like
solve (HUGE - matrix([tr11,tr21],[tr12,tr22]) = matrix([0,0],[0,0]), [a, b, c, d], [tr11, tr12, tr21, tr22])
How can I go about it? My matrix is really complicated, so doing it with pen and paper is not an option.
In case someone asks for the actual value of my HUGE-matrix to try it out:
HUGE: matrix([−(sqrt(d^2−2*a*d+4*b*c+a^2)*((b*d+a*b)*tr12*tr22+(−d^2−a*d)*tr12*tr21+(−b*d−a*b)*tr11*tr12+(d^2+a*d)*tr11^2)+(b*d^2+2*b^2*c+a^2*b)*tr12*tr22+((−2*b*c−a^2)*d−d^3)*tr12*tr21+(b*d^2+2*b^2*c+a^2*b)*tr11*tr12+((−2*b*c−a^2)*d−d^3)*tr11^2)/(2*a*d−2*b*c),−(sqrt(d^2−2*a*d+4*b*c+a^2)*((b*d+a*b)*tr22^2+(−d^2−a*d)*tr21*tr22+((−b*d−a*b)*tr12+(d^2+a*d)*tr11)*tr21)+(b*d^2+2*b^2*c+a^2*b)*tr22^2+((−2*b*c−a^2)*d−d^3)*tr21*tr22+((b*d^2+2*b^2*c+a^2*b)*tr12+((−2*b*c−a^2)*d−d^3)*tr11)*tr21)/(2*a*d−2*b*c)],[(sqrt(d^2−2*a*d+4*b*c+a^2)*((a*d+a^2)*tr12*tr22+(−c*d−a*c)*tr12*tr21+(−a*d−a^2)*tr11*tr12+(c*d+a*c)*tr11^2)+(a*d^2+2*a*b*c+a^3)*tr12*tr22+(−c*d^2−2*b*c^2−a^2*c)*tr12*tr21+(a*d^2+2*a*b*c+a^3)*tr11*tr12+(−c*d^2−2*b*c^2−a^2*c)*tr11^2)/(2*a*d−2*b*c),(sqrt(d^2−2*a*d+4*b*c+a^2)*((a*d+a^2)*tr22^2+(−c*d−a*c)*tr21*tr22+((−a*d−a^2)*tr12+(c*d+a*c)*tr11)*tr21)+(a*d^2+2*a*b*c+a^3)*tr22^2+(−c*d^2−2*b*c^2−a^2*c)*tr21*tr22+((a*d^2+2*a*b*c+a^3)*tr12+(−c*d^2−2*b*c^2−a^2*c)*tr11)*tr21)/(2*a*d−2*b*c)])
Edit:
When I do this:
solve(HUGE = matrix([0,0],[0,0]),[tr11,tr12,tr21,tr11]);
maxima returns:
[]
I also tried:
solve([
−(sqrt(d^2−2*a*d+4*b*c+a^2)*((b*d+a*b)*tr12*tr22+(−d^2−a*d)*tr12*tr21+(−b*d−a*b)*tr11*tr12+(d^2+a*d)*tr11^2)+(b*d^2+2*b^2*c+a^2*b)*tr12*tr22+((−2*b*c−a^2)*d−d^3)*tr12*tr21+(b*d^2+2*b^2*c+a^2*b)*tr11*tr12+((−2*b*c−a^2)*d−d^3)*tr11^2+2*a^2*d−2*a*b*c)/(2*a*d−2*b*c)=0,
−(sqrt(d^2−2*a*d+4*b*c+a^2)*((b*d+a*b)*tr22^2+(−d^2−a*d)*tr21*tr22+((−b*d−a*b)*tr12+(d^2+a*d)*tr11)*tr21)+(b*d^2+2*b^2*c+a^2*b)*tr22^2+((−2*b*c−a^2)*d−d^3)*tr21*tr22+((b*d^2+2*b^2*c+a^2*b)*tr12+((−2*b*c−a^2)*d−d^3)*tr11)*tr21+2*a*c*d−2*b*c^2)/(2*a*d−2*b*c)=0,
(sqrt(d^2−2*a*d+4*b*c+a^2)*((a*d+a^2)*tr12*tr22+(−c*d−a*c)*tr12*tr21+(−a*d−a^2)*tr11*tr12+(c*d+a*c)*tr11^2)+(a*d^2+2*a*b*c+a^3)*tr12*tr22+(−c*d^2−2*b*c^2−a^2*c)*tr12*tr21+(a*d^2+2*a*b*c+a^3)*tr11*tr12+(−c*d^2−2*b*c^2−a^2*c)*tr11^2−2*a*b*d+2*b^2*c)/(2*a*d−2*b*c)=0,
(sqrt(d^2−2*a*d+4*b*c+a^2)*((a*d+a^2)*tr22^2+(−c*d−a*c)*tr21*tr22+((−a*d−a^2)*tr12+(c*d+a*c)*tr11)*tr21)+(a*d^2+2*a*b*c+a^3)*tr22^2+(−c*d^2−2*b*c^2−a^2*c)*tr21*tr22+((a*d^2+2*a*b*c+a^3)*tr12+(−c*d^2−2*b*c^2−a^2*c)*tr11)*tr21−2*a*d^2+2*b*c*d)/(2*a*d−2*b*c)=0
],[tr11,tr12,tr21,tr22]);
which results in an error:
Polynomial quotient is not exact
-- an error. To debug this try: debugmode(true);
I see your equations have sqrt in them. Try the to_poly_solve package for that. i.e. load(to_poly_solve); and then to_poly_solve([HUGE[1,1] = 0, HUGE[1,2] = 0, HUGE[2,1] = 0, HUGE[2,2] = 0], [tr11, tr12, tr21, tr22]);
When I try that, I get several messy expressions as a result. I didn't check to see if they are correct solutions. Good luck, have fun, let us know how it goes.

Why does this tensorflow loop require so much memory?

I have a contrived version of a complicated network:
import tensorflow as tf
a = tf.ones([1000])
b = tf.ones([1000])
for i in range(int(1e6)):
a = a * b
My intuition is that this should require very little memory. Just the space for the initial array allocation and a string of commands that utilizes the nodes and overwrites the memory stored in tensor 'a' at each step. But memory usage grows quite rapidly.
What is going on here, and how can I decrease memory usage when I compute a tensor and overwrite it a bunch of times?
Edit:
Thanks to Yaroslav's suggestions the solution turned out to be using a while_loop to minimize the number of nodes on the graph. This works great and is much faster, requires far less memory, and is all contained in-graph.
import tensorflow as tf
a = tf.ones([1000])
b = tf.ones([1000])
cond = lambda _i, _1, _2: tf.less(_i, int(1e6))
body = lambda _i, _a, _b: [tf.add(_i, 1), _a * _b, _b]
i = tf.constant(0)
output = tf.while_loop(cond, body, [i, a, b])
with tf.Session() as sess:
result = sess.run(output)
print(result)
Your a*b command translates to tf.mul(a, b), which is equivalent to tf.mul(a, b, g=tf.get_default_graph()). This command adds a Mul node to the current Graph object, so you are trying to add 1 million Mul nodes to the current graph. That's also problematic since you can't serialize Graph object larger than 2GB, there are some checks that may fail once you are dealing with such a large graph.
I'd recommend reading Programming Models for Deep Learning by MXNet folks. TensorFlow is "symbolic" programming in their terminology, and you are treating it as imperative.
To get what you want using Python loop you could construct multiplication op once, and run it repeatedly, using feed_dict to feed updates
mul_op = a*b
result = sess.run(a)
for i in range(int(1e6)):
result = sess.run(mul_op, feed_dict={a: result})
For more efficiency you could use tf.Variable objects and var.assign to avoid Python<->TensorFlow data transfers

finding the best/ scale/shift between two vectors

I have two vectors that represents a function f(x), and another vector f(ax+b) i.e. a scaled and shifted version of f(x). I would like to find the best scale and shift factors.
*best - by means of least squares error , maximum likelihood, etc.
any ideas?
for example:
f1 = [0;0.450541598502498;0.0838213779969326;0.228976968716819;0.91333736150167;0.152378018969223;0.825816977489547;0.538342435260057;0.996134716626885;0.0781755287531837;0.442678269775446;0];
f2 = [-0.029171964726699;-0.0278570165494982;0.0331454732535324;0.187656956432487;0.358856370923984;0.449974662483267;0.391341738643094;0.244800719791534;0.111797007617227;0.0721767235173722;0.0854437239807415;0.143888234591602;0.251750993723227;0.478953530572365;0.748209818420035;0.908044924557262;0.811960826711455;0.512568916956487;0.22669198638799;0.168136111568694;0.365578085161896;0.644996661336714;0.823562159983554;0.792812945867018;0.656803251999341;0.545799498053254;0.587013303815021;0.777464637372241;0.962722388208354;0.980537136457874;0.734416947254272;0.375435649393553;0.106489547770962;0.0892376361668696;0.242467741982851;0.40610516900965;0.427497319032133;0.301874099075184;0.128396341665384;0.00246347624097456;-0.0322120242872125]
*note that f(x) may be irreversible...
Thanks,
Ohad
For each f(x), take the absolute value of f(x) and normalize it such that it can be considered a probability mass function over its support. Calculate the expected value E[x] and variance of Var[x]. Then, we have that
E[a x + b] = a E[x] + b
Var[a x + b] = a^2 Var[x]
Use the above equations and the known values of E[x] and Var[x] to calculate a and b. Taking your values of f1 and f2 from your example, the following Octave script performs this procedure:
% Octave script
% f1, f2 are defined as given in your example
f1 = [zeros(length(f2) - length(f1), 1); f1];
save_f1 = f1; save_f2 = f2;
f1 = abs( f1 ); f2 = abs( f2 );
f1 = f1 ./ sum( f1 ); f2 = f2 ./ sum( f2 );
mean = #(x)sum(((1:length(x))' .* x));
var = #(x)sum((((1:length(x))'-mean(x)).^2) .* x);
m1 = mean(f1); m2 = mean(f2);
v1 = var(f1); v2 = var(f2)
a = sqrt( v2 / v1 ); b = m2 - a * m1;
plot( a .* (1:length( save_f1 )) + b, save_f1, ...
1:length( save_f2 ), save_f2 );
axis([0 length( save_f1 )];
And the output is
Here's a simple, effective, but perhaps somewhat naive approach.
First make sure you make a generic interpolator through both functions. That way you can evaluate both functions in between the given data points. I used a cubic-splines interpolator, since that seems general enough for the type of smooth functions you provided (and does not require additional toolboxes).
Then you evaluate the source function ("original") at a large number of points. Use this number also as a parameter in an inline function, that takes as input X, where
X = [a b]
(as in ax+b). For any input X, this inline function will compute
the function values of the target function at the same x-locations, but then scaled and offset by a and b, respectively.
The sum of the squared-differences between the resulting function values, and the ones of the source function you computed earlier.
Use this inline function in fminsearch with some initial estimate (one that you have obtained visually or by via automatic means). For the example you provided, I used a few random ones, which all converged to near-optimal fits.
All of the above in code:
function s = findScaleOffset
%% initialize
f2 = [0;0.450541598502498;0.0838213779969326;0.228976968716819;0.91333736150167;0.152378018969223;0.825816977489547;0.538342435260057;0.996134716626885;0.0781755287531837;0.442678269775446;0];
f1 = [-0.029171964726699;-0.0278570165494982;0.0331454732535324;0.187656956432487;0.358856370923984;0.449974662483267;0.391341738643094;0.244800719791534;0.111797007617227;0.0721767235173722;0.0854437239807415;0.143888234591602;0.251750993723227;0.478953530572365;0.748209818420035;0.908044924557262;0.811960826711455;0.512568916956487;0.22669198638799;0.168136111568694;0.365578085161896;0.644996661336714;0.823562159983554;0.792812945867018;0.656803251999341;0.545799498053254;0.587013303815021;0.777464637372241;0.962722388208354;0.980537136457874;0.734416947254272;0.375435649393553;0.106489547770962;0.0892376361668696;0.242467741982851;0.40610516900965;0.427497319032133;0.301874099075184;0.128396341665384;0.00246347624097456;-0.0322120242872125];
figure(1), clf, hold on
h(1) = subplot(2,1,1); hold on
plot(f1);
legend('Original')
h(2) = subplot(2,1,2); hold on
plot(f2);
linkaxes(h)
axis([0 max(length(f1),length(f2)), min(min(f1),min(f2)),max(max(f1),max(f2))])
%% make cubic interpolators and test points
pp1 = spline(1:numel(f1), f1);
pp2 = spline(1:numel(f2), f2);
maxX = max(numel(f1), numel(f2));
N = 100 * maxX;
x2 = linspace(1, maxX, N);
y1 = ppval(pp1, x2);
%% search for parameters
s = fminsearch(#(X) sum( (y1 - ppval(pp2,X(1)*x2+X(2))).^2 ), [0 0])
%% plot results
y2 = ppval( pp2, s(1)*x2+s(2));
figure(1), hold on
subplot(2,1,2), hold on
plot(x2,y2, 'r')
legend('before', 'after')
end
Results:
s =
2.886234493867320e-001 3.734482822175923e-001
Note that this computes the opposite transformation from the one you generated the data with. Reversing the numbers:
>> 1/s(1)
ans =
3.464721948700991e+000 % seems pretty decent
>> -s(2)
ans =
-3.734482822175923e-001 % hmmm...rather different from 7/11!
(I'm not sure about the 7/11 value you provided; using the exact values you gave to make a plot results in a less accurate approximation to the source function...Are you sure about the 7/11?)
Accuracy can be improved by either
using a different optimizer (fmincon, fminunc, etc.)
demanding a higher accuracy from fminsearch through optimset
having more sample points in both f1 and f2 to improve the quality of the interpolations
Using a better initial estimate
Anyway, this approach is pretty general and gives nice results. It also requires no toolboxes.
It has one major drawback though -- the solution found may not be the global optimizer, e.g., the quality of the outcomes of this method could be quite sensitive to the initial estimate you provide. So, always make a (difference) plot to make sure the final solution is accurate, or if you have a large number of such things to do, compute some sort of quality factor upon which you decide to re-start the optimization with a different initial estimate.
It is of course very possible to use the results of the Fourier+Mellin transforms (as suggested by chaohuang below) as an initial estimate to this method. That might be overkill for the simple example you provide, but I can easily imagine situations where this could indeed be very useful.
For the scale factor a, you can estimate it by computing the ratio of the amplitude spectra of the two signals since the Fourier transform is invariant to shift.
Similarly, you can estimate the shift factor b by using the Mellin transform, which is scale invariant.
Here's a super simple approach to estimate the scale a that works on your example data:
a = length(f2) / length(f1)
This gives 3.4167 which is close to your stated value of 3.4. If that estimate is good enough, you can use correlation to estimate the shift.
I realize that this is not exactly what you asked, but it may be an acceptable alternative depending on the data.
Both Rody Oldenhuis and jstarr's answers are correct. I'm adding my own answer just to sum things up, and connect between them.
I've messed up Rody's code a little bit and ended up with the following:
function findScaleShift
load f1f2
x0 = [length(f1)/length(f2) 0]; %initial guess, can do better
n=length(f1);
costFunc = #(z) sum((eval_f1(z,f2,n)-f1).^2);
opt.TolFun = eps;
xopt=fminsearch(costFunc,x0,opt);
f1r=eval_f1(xopt,f2,n);
subplot(211);
plot(1:n,f1,1:n,f1r,'--','linewidth',5)
title(xopt);
subplot(212);
plot(1:n,(f1-f1r).^2);
title('squared error')
end
function y = eval_f1(x,f2,n)
t = maketform('affine',[x(1) 0 x(2); 0 1 0 ; 0 0 1]');
y=imtransform(f2',t,'cubic','xdata',[1 n ],'ydata',[1 1])';
end
This gives zero results:
This method is accurate but exhaustive and may take some time. Another disadvantage is that it finds only a local minima, and may give false results if initial guess (x0) is far.
On the other hand, jstarr method gave the following results:
xopt = [ 3.49655562549115 -0.676062367063033]
which is 10% deviation from the correct answer. Pretty fast solution, but not as accurate as I requested, but still should be noted.
I think in order to get the best results jstarr method should be used as an initial guess for the method purposed by Rody, giving an accurate solution.
Ohad

Performance of swapping two elements in MATLAB

Purely as an experiment, I'm writing sort functions in MATLAB then running these through the MATLAB profiler. The aspect I find most perplexing is to do with swapping elements.
I've found that the "official" way of swapping two elements in a matrix
self.Data([i1, i2]) = self.Data([i2, i1])
runs much slower than doing it in four lines of code:
e1 = self.Data(i1);
e2 = self.Data(i2);
self.Data(i1) = e2;
self.Data(i2) = e1;
The total length of time taken up by the second example is 12 times less than the single line of code in the first example.
Would somebody have an explanation as to why?
Based on suggestions posted, I've run some more tests.
It appears the performance hit comes when the same matrix is referenced in both the LHS and RHS of the assignment.
My theory is that MATLAB uses an internal reference-counting / copy-on-write mechanism, and this is causing the entire matrix to be copied internally when it's referenced on both sides. (This is a guess because I don't know the MATLAB internals).
Here are the results from calling the function 885548 times. (The difference here is times four, not times twelve as I originally posted. Each of the functions have the additional function-wrapping overhead, while in my initial post I just summed up the individual lines).
swap1: 12.547 s
swap2: 14.301 s
swap3: 51.739 s
Here's the code:
methods (Access = public)
function swap(self, i1, i2)
swap1(self, i1, i2);
swap2(self, i1, i2);
swap3(self, i1, i2);
self.SwapCount = self.SwapCount + 1;
end
end
methods (Access = private)
%
% swap1: stores values in temporary doubles
% This has the best performance
%
function swap1(self, i1, i2)
e1 = self.Data(i1);
e2 = self.Data(i2);
self.Data(i1) = e2;
self.Data(i2) = e1;
end
%
% swap2: stores values in a temporary matrix
% Marginally slower than swap1
%
function swap2(self, i1, i2)
m = self.Data([i1, i2]);
self.Data([i2, i1]) = m;
end
%
% swap3: does not use variables for storage.
% This has the worst performance
%
function swap3(self, i1, i2)
self.Data([i1, i2]) = self.Data([i2, i1]);
end
end
In the first (slow) approach, the RHS value is a matrix, so I think MATLAB incurs a performance penalty in creating a new matrix to store the two elements. The second (fast) approach avoids this by working directly with the elements.
Check out the "Techniques for Improving Performance" article on MathWorks for ways to improve your MATLAB code.
you could also do:
tmp = self.Data(i1);
self.Data(i1) = self.Data(i2);
self.Data(i2) = tmp;
Zach is potentially right in that a temporary copy of the matrix may be made to perform the first operation, although I would hazard a guess that there is some internal optimization within MATLAB that attempts to avoid this. It may be a function of the version of MATLAB you are using. I tried both of your cases in version 7.1.0.246 (a couple years old) and only saw a speed difference of about 2-2.5.
It's possible that this may be an example of speed improvement by what's called "loop unrolling". When doing vector operations, at some level within the internal code there is likely a FOR loop which loops over the indices you are swapping. By performing the scalar operations in the second example, you are avoiding any overhead from loops. Note these two (somewhat silly) examples:
vec = [1 2 3 4];
%Example 1:
for i = 1:4,
vec(i) = vec(i)+1;
end;
%Example 2:
vec(1) = vec(1)+1;
vec(2) = vec(2)+1;
vec(3) = vec(3)+1;
vec(4) = vec(4)+1;
Admittedly, it would be much easier to simply use vector operations like:
vec = vec+1;
but the examples above are for the purpose of illustration. When I repeat each example multiple times over and time them, Example 2 is actually somewhat faster than Example 1. For a small loop with a known number (in the example, just 4), it can actually be more efficient to forgo the loop. Of course, in this particular example, the vector operation given above is actually the fastest.
I usually follow this rule: Try a few different things, and pick the fastest for your specific problem.
This post deserves an update, since the JIT compiler is now a thing (since R2015b) and so is timeit (since R2013b) for more reliable function timing.
Below is a short benchmarking function for element swapping within a large array.
I have used the terms "directly swapping" and "using a temporary variable" to describe the two methods in the question respectively.
The results are pretty staggering, the performance of directly swapping 2 elements using is increasingly poor by comparison to using a temporary variable.
function benchie()
% Variables for plotting, loop to increase size of the arrays
M = 15; D = zeros(1,M); W = zeros(1,M);
for n = 1:M;
N = 2^n;
% Create some random array of length N, and random indices to swap
v = rand(N,1);
x = randi([1, N], N, 1);
y = randi([1, N], N, 1);
% Time the functions
D(n) = timeit(#()direct);
W(n) = timeit(#()withtemp);
end
% Plotting
plot(2.^(1:M), D, 2.^(1:M), W);
legend('direct', 'with temp')
xlabel('number of elements'); ylabel('time (s)')
function direct()
% Direct swapping of two elements
for k = 1:N
v([x(k) y(k)]) = v([y(k) x(k)]);
end
end
function withtemp()
% Using an intermediate temporary variable
for k = 1:N
tmp = v(y(k));
v(y(k)) = v(x(k));
v(x(k)) = tmp;
end
end
end

Resources