I want to use a custom model to fit some data. The syntax I used is fit()
. The mathematical model I used has the form of this:
a*exp(-x*b) c*exp(-y*d) e*exp(-z*f)
where a,b,c,d,e,f
are the parametres I will estimate and x,y,z
are independent variables. (The actual mathematical formula is more complicated but something nonlinear like this.)
How can I add the constraint of a c e=1
(and a,c,e
must be positive or 0) when fitting the curve? I know how to set the lower and upper bound but don't know how to add this normalization coefficient constraint to the fitting. Is it possible to do this when using the fit()
method?
CodePudding user response:
I think I already posted this somewhere, but can't find it right now.
As it is a non-linear fit, there is no big deal in transforming the parameters. say we chose the continuously differentiable and monotonic function:
a = f(s) = 1/2 ( 1 s / sqrt( 1 s^2 ) )
so for s
in (-inf, inf)
on gets a
in (0,1)
. Actually, with some simple shifting and scaling we could get any a
in ( u, v )
.
Now we can do the same for b
, but with the additional restriction a b c = 1
we know that at most c = 0
and b
must definitively be smaller than 1 - f(s) = 1/2 ( 1 - s / sqrt( 1 s^2 ) )
. Now, hence, is the time for scaling and we can set:
b = g(t, s) = 1/2 ( 1 - s / sqrt( 1 s^2 ) ) 1/2 ( 1 t / sqrt( 1 t^2 ) )
again with t
in (-inf, inf)
. The first part is the scaling due to the already set value for a
, the second part repeats the procedure from above.
Finally, c
is simply 1- f(s) - g(t, s)
Eventually, the fit function with parameters s
and t
looks like:
0.50 * ( 1 s / sqrt( 1 s^2 ) ) * exp( -x * b )
0.25 * ( 1 - s / sqrt( 1 s^2 ) ) * ( 1 t / sqrt( 1 t^2 ) ) * exp( -y * d )
(
1.00
-0.50 * ( 1 s / sqrt( 1 s^2 )
-0.25 * ( 1 - s / sqrt( 1 s^2 ) ) * ( 1 t / sqrt( 1 t^2 ) )
) * exp( -z * f )
Getting results for s
and t
provides a
, b
and c
via error propagation