Exponential Curve Fit Equation

I am trying to fit my data in figure 1
plot_22
to be like figure 2 (exponential fit)
plot_222

but my code does not do that

using LsqFit
#days = [1,2,3,4,5,6,7,8,9 ,...........]
#y = some code to update y value 
@. model(days, p) =  p[1] * days.^(-p[2]) 
fit = curve_fit(model, days, y, [0.5,0.5]) #
@show fit.param 
plot(days, model(days, fit.param))

Fitting exponential data is a bit tricky.
If you want a quick fit try fitting the logarithm of the data. To get a correct uncertainty estimation, however, one has to use nonuniform weights.

This fit you can then use to get a better initial value.

Also your data does not look like to follow an exponential function. Maybe one could fit a sigmoid function or restrict the data range.

The function you use is also not an exponential.I would have expected something like this

@. model(days,p) = p[1] + exp(days*p[2])

For restricting the data range you can introduce a mask like this.

mask=y.<=50
curve_fit(model,days[mask],y[mask],[0.5,0.5])

In addition, I noticed that in your example there is a missing ] for the initial value array.

One thing I wanted to do for some time was figuring out how to use GLM.jl for such applications. I think by selecting the correct link function this might work.

I hope my comments help somewhat.

3 Likes

This seems much easier to do by using blackbox optimization (via Optim) with the actual functional form you want that provides a closure over your data. It’s true that you can make this into a LsqFit problem, but the more general approach of blackbox optimization is easier once you’re used to it.

1 Like

Here is a function that might work.


julia> using UnicodePlots

julia> scaled_sigmoid(x,low,high,a,b) = low+(high-low)*(1/(1+exp(a+b*x)))
scaled_sigmoid (generic function with 1 method)

julia> lineplot((x)->scaled_sigmoid(x,0,2,3,-1))
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      
        2 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠒⠋⠉⠉⠉⠉⠉│ #9(x)
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⠀⠀⠀⠀⠀⠀⠀⒀⑔⠁⠀⠀⠀⠀⠀⠀⠀⠀│      
          β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β‘‡β €β €β €β €β €β €β €β’€β Žβ €β €β €β €β €β €β €β €β €β €β”‚      
          β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β‘‡β €β €β €β €β €β €β €β‘œβ €β €β €β €β €β €β €β €β €β €β €β”‚      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⠀⠀⠀⠀⠀⑸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⠀⠀⠀⠀⒀⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β‘‡β €β €β €β €β €β‘œβ €β €β €β €β €β €β €β €β €β €β €β €β €β”‚      
   f(x)   │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⠀⠀⠀⒠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β‘‡β €β €β €β €β‘Žβ €β €β €β €β €β €β €β €β €β €β €β €β €β €β”‚      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⠀⠀⒰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⠀⠀⑇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⠀⑸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⠀⑰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑇⑔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
        0 │⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⠀⠖⑏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│      
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      
          β €-10β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €10β €      
          

At the high and low ends this can also be approximated by an exponential function. (See Fermi-Dirac statistics)

1 Like

How to use it with vectors like
x=[1,2,3,4,5,6,7,…]
y=[2,3,3,4,6,7,8,…]

using LsqFit
using Plots
x=[1,2,3,4,5,6,7]
y=[2,3,3,4,6,7,8]

@. scaled_sigmoid(x,low,high,a,b) = low+(high-low)*(1/(1+exp(a+b*x)))
scaled_sigmoid(x,p) = scaled_sigmoid(x,p...)

p0 = [0,100,0.1,-1]
fit = curve_fit(scaled_sigmoid,x,y,p0)

scatter(x,y,label="data",xlabel="x",ylabel="y")
plot!((x)->scaled_sigmoid(x,p0),label="initial")
plot!((x)->scaled_sigmoid(x,coef(fit)),label="fit")

image

1 Like

Easy, like this

using UnicodePlots
x = [1, 2, 3, 4, 5, 6, 7]
y = [2, 3, 3, 4, 6, 7, 8]
lineplot(x, y)

# result will be
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 
   8 β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β’€β ”β ‰β”‚ 
     β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β’€β‘ β Šβ β €β €β”‚ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠔⠁⠀⠀⠀⠀⠀│ 
     β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β’€β €β Šβ €β €β €β €β €β €β €β €β”‚ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀①⠒⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β’ β Šβ €β €β €β €β €β €β €β €β €β €β €β €β €β”‚ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β’€β œβ €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β”‚ 
     β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β’ β Šβ €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β”‚ 
     │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⑰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β’€β ”β Šβ β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β”‚ 
     β”‚β €β €β €β €β €β €β €β €β €β €β €β €β €β €β’€β‘ β Šβ β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β”‚ 
     │⠀⠀⠀⠀⠀⒀①⠀⠀⠀⠀⠀⠀⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
     │⠀⠀⠀①⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
   2 β”‚β£€β ”β Šβ €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β”‚ 
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 
     β €1β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €β €7β € 

I recommend you check the docs, they are nice:

Or have a look at methods(lineplot).

Good luck with Julia)

2 Likes