Search code examples
pythonpython-3.xoptimizationscipycurve-fitting

A mutual constraint for multiple curve fit sessions


I have N sequences of 2D data (x,y) I'm trying to fit into a model of a logistic function y(x)=L/(1+a exp(b*(x-c))). However I would like to put constraints on a,b,c. Normally if I want to restrain their values I use the parameters from lmfit and it does the job. This time I would like that the ratio of a/b would be constant (with an error of 0.01). Is there any way to fit all N sequences so the difference between the ratios a_i/b_i would be minimized?


Solution

  • It is not clear whether you mean that a_i/b_i should be the same constant for all N datasets -- let's assume that it should be. In such a case, it may be sufficient to define parameters like this:

    import numpy as np
    from lmfit import Parameters, Model
    
    def logistic(x, amp, a, b, c):
        return amp / (1 + a*np.exp(b*(x-c)))
     
    params = Parameters()
    params.add('b2a_scale' value=1, vary=True) # ?
    N = 5
    model = Model(logistic, prefix='p1_')
    
    for i in range(1, N+1):
        params.add('p%d_amp' % i, value=1, min=0)
        params.add('p%d_c' % i, value=1)
        params.add('p%d_b' % i, value=5)
        params.add('p%d_a' % i, expr='p%d_b * b2a_scale' % i)
        if model > 1:
            model += Model(logistic, prefix='p%d_' % i)