I am very new to Julia and maths is quite old for me now, so I am struggling with my code.
The thing is that I would like to find the best linear combination of six parameters that fits the seventh parameter.
Let me explain:
I have seven 1-column matrices of 300 data for each (say C1,C2…C7)
I would like to find the best vector (a,b,c,d,e,f) (all positive) that minimizes C7 - aC1-bC2-cC3-dC4-eC5-fC6
I have tried the optimization package using NLopt.LD_LBFGS() and NLopt.GN_DIRECT() but it failed to converge.
Here is the code for the last one:
function optim(a,b,c,d,e,f)
deviation = (C7 - a*C1 - b*C2 - c*C3 - d*C4 - e*C5 - f*C6)
somme = 0
for i in deviation
somme = somme + i^2
end
return somme
end
optfun = OptimizationFunction(optim, Optimization.AutoForwardDiff())
begin
init = zeros(6)
prob = OptimizationProblem(optfun, init, lb = [0, 0, 0, 0, 0, 0], ub = [1.0, 1.0, 1.0, 1.0, 1.0,1.0])
end
sol = solve(prob, NLopt.GN_DIRECT(), maxtime = 10.0)
And it gives me “NLopt failed to converge: FORCED_STOP”
Because you stated that your math is rusty, I will mention that the product of a matrix and a vector is just the linear combination of the columns of the matrix, with the vector elements as the linear coefficients. Therefore, your problem looks like a nonnegative least squares problem. It can be solved very simply using the NonNegLeastSquares package. Here is a solution using random values for the vectors c1 through c7:
Thanks both of you, it helped me a lot!
Both methods are now working perfectly.
I had to struggle a little bit with the matrix multiplications but in the end, it worked, thanks.